// --------------------------------------------------------
// © 2008 - EUROCEAN - DIATEAM
// --------------------------------------------------------
// Fonction de gestion de la MAP
// --------------------------------------------------------
// Avec l'aide de :
// http://www.acko.net/files/mouse-positioning.html
// --------------------------------------------------------

// --------------------------------------------------------
// Constructeur
// --------------------------------------------------------
var pageSelection = function () {
	// ------------------------------------------
	// Noeuds HTML associés à l'Objet
	// ------------------------------------------
	this.nodes =
	{
		nav :
		{
			welcome : document.getElementById('navWelcome'),
			models : document.getElementById('navModels'),
			docs : document.getElementById('navDocs'),
			demo : document.getElementById('navDemo')
		},
		simulation :
		{
			bar : document.getElementById("menuBar"),
			models : document.getElementById('menuModels'),
			maps : document.getElementById('menuGeographic'),
			time : document.getElementById('menuPeriod'),
			custom : document.getElementById('menuCustom'),
			output : document.getElementById('menuExport'),
			run : document.getElementById('menuRun')
		},
		results :
		{
			bar : document.getElementById("resultsBar")
		},
		app : document.getElementById("App")
	},
	// ------------------------------------------
	// Varibles associées à l'Objet
	// ------------------------------------------
	this.current =
	{
		pages : 'help',
		navigation : '',
		menu : ''
	}
}

// --------------------------------------------------------
// Fonctions
// --------------------------------------------------------
pageSelection.prototype = {
	changePage : function(pageNew,large,state)
	{
		// --------------------------------------
		// Affiche la nouvelle page
		if (pageNew != this.current.pages)
		{
			// --------------------------------------
			// Change l'état du menu
			this.nodes.simulation.bar.style.display = "none";
			this.nodes.results.bar.style.display = "none";
			if (state)
			{
				switch (state)
				{
					case 'simulation':
						this.nodes.simulation.bar.style.display = "block";
					break;
					case 'results':
						this.nodes.results.bar.style.display = "block";
					break;
				}
			}
			var el = document.getElementById(pageNew).style.display = "block";
			var el2 = document.getElementById(this.current.pages).style.display = "none";
		    this.current.pages = pageNew;
		}
		// --------------------------------------
		// Change la largeur du cadre
		if (parseInt(large) > 100)
			this.nodes.app.style.width = large + "px";
		else if (parseInt(large) <= 100)
			this.nodes.app.style.width = large + "%";
		// --------------------------------------	
		return true;
	},

	// ------------------------------------------
	setMenuState : function(bouton)
	{
		this.current.navigation.className = "";
		bouton.className = "current";
		this.current.navigation = bouton;
	},

	// ------------------------------------------
	setState : function(bouton,state)
	{
		switch (state)
		{
			case "disabled":
				bouton.className = "disabled";
			break;
			case "warning":
				bouton.className = "warning";
			break;
			default:
				this.current.menu.className = "";
				bouton.className = "current";
				this.current.menu = bouton;
			break;
		}
	}
}

// --------------------------------------------------------
function enableCustom(el)
{
	if (thisPage.changePage("custom",700,'simulation'))
	{
		toogleSimulation("custom");
		thisPage.setState(document.getElementById("menuCustom"));
	}
}
// --------------------------------------------------------
function openResults()
{
	if(thisPage.changePage('results',90,'results'))
		initResults();
}
// --------------------------------------------------------
function quitResults()
{
	if(thisPage.changePage('simulation',700,'simulation'))
		setState(document.getElementById('menuModels'));
}
// --------------------------------------------------------
function initPage() {
  thisPage = new pageSelection();

  if(thisPage.changePage('welcome'))
    thisPage.setMenuState(thisPage.nodes.nav.welcome);

  // ------------------------------------------
  // Affectation des actions/evenements
  // ------------------------------------------
  thisPage.nodes.nav.welcome.onclick = function() {
    if(thisPage.changePage('welcome'))
      thisPage.setMenuState(this);
  }
    // ------------------------------------------
  thisPage.nodes.nav.demo.onclick = function() {
    if(thisPage.changePage('demo'))
      thisPage.setState(this);
  }
    // ------------------------------------------
    document.getElementById('startSimulation').onclick =
    thisPage.nodes.nav.models.onclick = function()
    {
            if(thisPage.changePage('simulation',700,'simulation'))
            {
                    thisPage.setMenuState(thisPage.nodes.nav.models);
                    thisPage.setState(thisPage.nodes.simulation.models);
            }
    }
    // ------------------------------------------
    thisPage.nodes.simulation.models.onclick = function()
    {
            if(thisPage.changePage('simulation',700,'simulation'))
                    thisPage.setState(this);
    }
    // ------------------------------------------
    thisPage.nodes.simulation.maps.onclick = function()
    {
            /* 440 */
            if(thisPage.changePage('geographic',700,'simulation'))
                    thisPage.setState(this);
    }
    // ------------------------------------------
    thisPage.nodes.simulation.time.onclick = function()
    {
            /* 500 */
            if(thisPage.changePage('period',700,'simulation'))
                    thisPage.setState(this);
    }
    // ------------------------------------------
    thisPage.nodes.simulation.custom.onclick = function()
    {
            if (this.className != 'disabled')
            {
                    if(thisPage.changePage('custom',700,'simulation'))
                    {
                            thisPage.setState(this);
                    }
            }
            else
            {
                    alertBox(
                            "<h3>Custom variables</h3><p>Do you want to enable custom variables ?</p>",
                            '',
                            "enableCustom()"
                    );
            }
    }
    // ------------------------------------------
    thisPage.nodes.simulation.output.onclick = function()
    {
            /* 600 */
            if(thisPage.changePage('export',700,'simulation'))
                    thisPage.setState(this);
    }
    // ------------------------------------------
    thisPage.nodes.simulation.run.onclick = function()
    {
            /* 600 */
            if(verifVars())
                    if(thisPage.changePage('run',600,'simulation')) {
                            thisPage.setState(this);
  }
    }
    // ------------------------------------------
    this.customTab = document.getElementById('variablesTab');
    this.customBtTab = document.getElementById('variablesBt');
    this.customMenu = document.getElementById('phytoplanktonTab');
    this.customBtMenu = document.getElementById('phytoplanktonBt');
}


