// -------------------------------------------------------------
// Gestion des boutons
// -------------------------------------------------------------
//	Fonctionne ainsi :
//	- Sélection un par un des boutons avec leur ID
//	- Attribue un par un les actions/évènements
// -------------------------------------------------------------

var buttons = function () {
	// -------------------------------------------
	// Initialise les onglets custom
	this.customTab = document.getElementById('variablesTab');
	this.customBtTab = document.getElementById('variablesBt');
	this.customMenu = document.getElementById('phytoplanktonTab');
	this.customBtMenu = document.getElementById('phytoplanktonBt');
	this.hydroSelected = document.getElementById('nemoOpaHydro');

	// ------------------------------------------
	// Sélection des boutons, attribution d'une variable
	// ------------------------------------------
	// Help
	var z_help = document.getElementById('help');
	// ------------------------------------------
	// Bouton de la fenêtre WELCOME
	var start_simulation = document.getElementById('startSimulation');
	var bt_demo = document.getElementById('menuDemo');
	// ------------------------------------------
	// Boutons de fenetre MODELS
	var bt_defaultSim = document.getElementById('defaultSimulation');
	var bt_customSim = document.getElementById('customSimulation');
	// ------------------------------------------
	// Boutons des onglets de la fenetre CUSTOM
	var variablesBt = document.getElementById('variablesBt');
	var hydrodynamicsBt = document.getElementById('hydrodynamicsBt');
	var othersBt = document.getElementById('othersBt');
	var phytoplanktonBt = document.getElementById('phytoplanktonBt');
	var zooplanktonBt = document.getElementById('zooplanktonBt');
	var organicaMatterBt = document.getElementById('organicaMatterBt');
	var foodMatrixBt = document.getElementById('foodMatrixBt');
	// ------------------------------------------
	// Bouton CHECKALL, fenetre OUTPUT
	var bt_checkAllOutputs = document.getElementById('checkAllOutputs');
	// ------------------------------------------
	// Bouton RUN simulation
	var run_simulation = document.getElementById('runBt');
	var models_run_resume = document.getElementById('modelResumeIcon');
	var map_run_resume = document.getElementById('mapResumeIcon');
	var time_run_resume = document.getElementById('timeResumeIcon');
	var custom_run_resume = document.getElementById('customResumeIcon');
	var output_run_resume = document.getElementById('outputResumeIcon');
	// ------------------------------------------
	// Results
	var resultsNext = document.getElementById('resultsOutputsNext');
	var resultsPrev = document.getElementById('resultsOutputsPrev');
	var resultsMapBt = document.getElementById('img');
	var menuDepthPlotBt = document.getElementById('menuDepthPlot');
	var menuTimePlotBt = document.getElementById('menuTimePlot');
	var menuContourPlotBt = document.getElementById('menuContourPlot');
	var resultsExportBt = document.getElementById('resultsExport');
	var resultsExportHref = document.getElementById('resultsExportHref');
	var resultsQuitBt = document.getElementById('resultsQuit');
	var outputSelect = document.getElementById('selectOutputs');

	// ------------------------------------------
	// ------------------------------------------
	bt_defaultSim.onmouseover = function()
	{
		if (document.getElementById('customChoise').className == 'custom')
			document.getElementById('customChoise').className = 'defaultHover';
	}
	// ------------------------------------------
	bt_defaultSim.onmouseout = function()
	{
		if (document.getElementById('customChoise').className == 'defaultHover')
			document.getElementById('customChoise').className = 'custom';
	}
	// ------------------------------------------
	bt_defaultSim.onclick = function()
	{
		if (document.getElementById('customChoise').className == 'defaultHover')
		{
			toogleSimulation('default');
		}
	}
	// ------------------------------------------
	// ------------------------------------------
	bt_customSim.onmouseover = function()
	{
		if (document.getElementById('customChoise').className == 'default')
			document.getElementById('customChoise').className = 'customHover';
	}
	// ------------------------------------------
	bt_customSim.onmouseout = function()
	{
		if (document.getElementById('customChoise').className == 'customHover')
			document.getElementById('customChoise').className = 'default';
	}
	// ------------------------------------------
	bt_customSim.onclick = function()
	{
		if (document.getElementById('customChoise').className == 'customHover')
		{
			toogleSimulation('custom');
		}
	}
	// ------------------------------------------
	// ------------------------------------------
	variablesBt.onclick = function()
	{
		toogleCustomTab('variablesTab', this);
	}
	// ------------------------------------------
	othersBt.onclick = function()
	{
		toogleCustomTab('othersTab', this);
	}
	// ------------------------------------------
	hydrodynamicsBt.onclick = function()
	{
		toogleCustomTab('hydrodynamicsTab', this);
	}
	// ------------------------------------------
	phytoplanktonBt.onclick = function()
	{
		toogleCustomMenu('phytoplanktonTab', this);
	}
	// ------------------------------------------
	zooplanktonBt.onclick = function()
	{
		toogleCustomMenu('zooplanktonTab', this);
	}
	// ------------------------------------------
	organicaMatterBt.onclick = function()
	{
		toogleCustomMenu('organicaMatterTab', this);
	}
	// ------------------------------------------
	foodMatrixBt.onclick = function()
	{
		toogleCustomMenu('foodMatrixTab', this);
	}

	// ------------------------------------------
	bt_checkAllOutputs.onclick = function()
	{
		checkAll(document.form.outputInd,this, 'outputTable');
	}
	// ------------------------------------------
	run_simulation.onclick = function()
	{
		if (this.className != "runDisabled")
		{
      //alert(runParameters.toJSONString());
			alertBox(
				"<h3>Run simulation</h3><p>This action may take a while. Please check all parameters before launch.</p>",
				'',
				"xajax_runSimulationModel(runParameters.toJSONString())"
			);
		}
		else
		{
			alertBox(
				"<h3>Run simulation</h3><p>You have some errors. Please correct each one of them before launching simulation.</p>",
				'error',
				""
			);
		}
	}
	// ------------------------------------------
	models_run_resume.onclick = function() {
		if(thisPage.changePage('welcome'))
			thisPage.changePage('simulation',700,'simulation')}
	// ------------------------------------------
	map_run_resume.onclick = function() {
		if(thisPage.changePage('welcome'))
			thisPage.changePage('geographic',440,'simulation')}
	// ------------------------------------------
	time_run_resume.onclick = function() {
		if(thisPage.changePage('welcome'))
			thisPage.changePage('period',500,'simulation')}
	// ------------------------------------------
	custom_run_resume.onclick = function() {
		if(thisPage.changePage('welcome'))
			thisPage.changePage('custom',700,'simulation')}
	// ------------------------------------------
	output_run_resume.onclick = function() {
		if(thisPage.changePage('welcome'))
			thisPage.changePage('export',600,'simulation');
	}
	// ------------------------------------------
	models_run_resume.onmouseover = 
	map_run_resume.onmouseover = 
	time_run_resume.onmouseover = 
	custom_run_resume.onmouseover = 
	output_run_resume.onmouseover = function()
	{
		if (this.className != "error")
			this.className = "hover";
	}
	// ------------------------------------------
	models_run_resume.onmouseout = 
	map_run_resume.onmouseout = 
	time_run_resume.onmouseout = 
	custom_run_resume.onmouseout = 
	output_run_resume.onmouseout = function()
	{
		if (this.className != "error")
			this.className = "";
	}
	// ------------------------------------------
	resultsPrev.onclick = function()
	{
		if (document.getElementById('resultsOutputsSelect').options.selectedIndex > 0)
		{
			el = document.getElementById('resultsOutputsSelect');
			el.options.selectedIndex -=1;
			changeOutputs(el[el.selectedIndex].value);
		}
		else
			return false;
	}
	// ------------------------------------------
	resultsNext.onclick = function() {
            if (document.getElementById('resultsOutputsSelect').options.selectedIndex < document.getElementById('resultsOutputsSelect').options.length)
		{
			el = document.getElementById('resultsOutputsSelect');
			el.options.selectedIndex +=1;
			changeOutputs(el[el.selectedIndex].value);
		}
		else
			return false;
	}
	// ------------------------------------------
	document.getElementById('resultsOutputsSelect').onchange = function()
	{
		var el = document.getElementById('resultsOutputsSelect').options;
		changeOutputs(el[el.selectedIndex].value);
	}	
	// ------------------------------------------
	resultsMapBt.onmouseover = function ()
	{
		Tip (
		 	'Lat. '+document.form.latitude.value+'° - Long. '+document.form.longitude.value+'°',
		 	WIDTH, 130,
		 	FADEIN, 500,
		 	FADEOUT, 500
		 	);
	}
	// ------------------------------------------
	resultsMapBt.onmouseout = function() {
		UnTip();
	}
	// ------------------------------------------
	menuDepthPlotBt.onclick = function() {
          runDepthPlot();
	}
	// ------------------------------------------
	menuTimePlotBt.onclick = function()
	{
          runTimePlot();
	}
	// ------------------------------------------
	menuContourPlotBt.onclick = function()
	{
          runContourPlot();
/*
                    if(thisPage.changePage('results',90,'results'))
		{
			setStateRes(this);
			document.getElementById('resultsVariables').className = 'resultsVariablesContour';			
			// Actions a realiser au click sur le bouton CONTOUR PLOT
		}
*/
        }
	// ------------------------------------------
/*
	resultsExportBt.onclick = function()
	{
    alertBox("<h3>Not yet implemented</h3><p>This action is not yet available, please wait few weeks.</p><i>er.82</i>", "error","");
	}
*/
	// ------------------------------------------
	resultsQuitBt.onclick = function()
	{
		alertBox(
			"<h3>Quit results</h3><p>This action will reset your values.</p>",
			'',
			"quitResults()"
		);
	}

}

