/** Copyright (C) 2009  Simon ANDRE
 *
 *   This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


/** Ce framework est fait par moi même afin de bien comprendre les mecanisme 
 * 	des autres framework javascript. Si vous avez des amelioration, des idées, des recommandations n'hesitez
 * 	à me les faire parvenir à elsilent.hunter[at]gmail.com
 * merçi
 */


var stateManager = function()
{
	this.coordAnimBase = coordAnimBase;
	//Les elements dynamique
	this.overlay = $('overlay');
	this.fondEffect;
	this.riesling = $('riesling');
	this.sylvaner= $('sylvaner');
	this.muscat= $('muscat');
	//La div qui va subir les animations
	this.divEnMvt; 				//contiendra le pointeur vers le dom
	this.id;				//contiendra l'id en string
	//L'etat de l'animation(charge ou decharge)
	this.state = true;
	//le tableau multidimensionnelle qui contiendra les effects
	this.effect = new Array();
	
	this.startGetDiv = function(id)
	{	
		this.state = true;
		//desactive les differents div
		removeEvent(this.riesling,"onclick",getRiesling);
		removeEvent(this.sylvaner,"onclick",getSylvaner);
		removeEvent(this.muscat,"onclick",getMuscat);
		//met en place la div
		this.id = id;
		this.divEnMvt = $(id);
		//recuperation du millieu du navigateur du client
		//coordAnimFinish.giveMiddle();
		this.effect.push(new Array(new animDiv(this.divEnMvt,parseInt(coordAnimInter[this.id].top),parseInt(coordAnimInter[this.id].left),20,1.005)));
		this.effect.push(new Array(new animDiv(this.divEnMvt,parseInt(coordAnimFinish.middle.top),parseInt(coordAnimFinish.middle.left),50,1.005),new resizeDiv(this.divEnMvt,parseInt(coordAnimFinish.middle.width),parseInt(coordAnimFinish.middle.height),50,1.005)));

		switch(this.id){
		case 'sylvaner':
			addEvent(this.overlay,"onclick",removeSylvaner);
			addEvent(this.divEnMvt,"onclick",goSylvaner);
			break;
		case 'riesling':
			addEvent(this.overlay,"onclick",removeRiesling);
			addEvent(this.divEnMvt,"onclick",goRiesling);
			break;
		case 'muscat':
			addEvent(this.overlay,"onclick",removeMuscat);
			addEvent(this.divEnMvt,"onclick",goMuscat);
			break;
		}
                this.divEnMvt.style.zIndex = parseInt(coordAnimFinish.middle.zIndex);
		setOpacity(this.overlay,0);
		this.fond = new opacObj(this.overlay,0,80,10,1);
		this.overlay.style.visibility= "visible";
		this.overlay.style.zIndex = "15";
	};
	
	this.startRemoveDiv = function(id)
	{
		this.state = false;
		this.effect.push(new Array(new animDiv(this.divEnMvt,parseInt(coordAnimInter[this.id].top),parseInt(coordAnimInter[this.id].left),25,1.005),new resizeDiv(this.divEnMvt,parseInt(coordAnimInter[this.id].width),parseInt(coordAnimInter[this.id].height),50,1.005)));
		this.effect.push(new Array(new animDiv(this.divEnMvt,parseInt(coordAnimBase[this.id].top),parseInt(coordAnimBase[this.id].left),50,1.005)));
		switch(this.id){
		case 'sylvaner':
			removeEvent(this.overlay,"onclick",removeSylvaner);
			removeEvent(this.divEnMvt,"onclick",goSylvaner);
			break;
		case 'riesling':
			removeEvent(this.overlay,"onclick",removeRiesling);
			removeEvent(this.divEnMvt,"onclick",goRiesling);
			break;
		case 'muscat':
			removeEvent(this.overlay,"onclick",removeMuscat);
			removeEvent(this.divEnMvt,"onclick",goMuscat);
			break;
		}
	};
	
	this.after = function(){
		var loader=0;
		if(this.effect.length>=1)
		{
			for(var i=0;i< this.effect[0].length;i++)
			{
				var nextB = this.effect[0][i].action();
				if(!nextB)loader++;
			}

		if(loader == this.effect[0].length)
		{
			this.effect.shift();
			if(this.effect.length>=1)
			{
				for(var i=0;i< this.effect[0].length;i++)
				{
					this.effect[0][i].init();
				}
			}else
			{
				this.clean();
			}
		}
		}
		if(this.fond){
			this.fond.opac();
		}
	};
	this.clean = function(){
		if(this.state == false){
			this.overlay.style.visibility= "hidden";
                this.divEnMvt.style.zIndex = parseInt(coordAnimBase[this.id].zIndex);
			this.init();
			for(var i=0;i< this.effect.length;i++)
			{
				this.effect[i] = null;
			}

		}

	};
	this.init = function(){
		this.riesling = $("riesling");
		this.sylvaner = $("sylvaner");
		this.muscat = $("muscat");
		addEvent(this.riesling,"onclick",getRiesling);
		addEvent(this.sylvaner,"onclick",getSylvaner);
		addEvent(this.muscat,"onclick",getMuscat);
		addEvent(this.riesling,"onmouseover",changeBorder);
		addEvent(this.sylvaner,"onmouseover",changeBorder);
		addEvent(this.muscat,"onmouseover",changeBorder);
		addEvent(this.riesling,"onmouseout",reChangeBorder);
		addEvent(this.sylvaner,"onmouseout",reChangeBorder);
		addEvent(this.muscat,"onmouseout",reChangeBorder);
	};
};

