var FormulaController = {

	CLASS_NAME_INVISIBLE    : 'invisible',
	CLASS_NAME_SELECTED     : 'selected',
	
	AUTO_ANIMATION_INTERVAL : 10000,

	oElementFrameWrapper : null,
	oElementArrowPrev    : null,
	oElementArrowNext    : null,
	oElementFormula      : null,
	oElementHider        : null,
	oElementFormulaPopup : null,
	aElementsFormula     : null,
	oElementRoof         : null,
	aElementsRoofSelect  : null,
	iFrameCurrent        : 0,
	iFramesCount         : 0,
	iRoofCurrent         : 0,
	oAnimationController : null,
	iAutoAnimationTimer  : null,
	bShowedFormula       : false,

	init : function() {			
	
		this.oAnimationController = new AnimationController(30);
		Animator.oDefaultAnimationController = this.oAnimationController;
	
		this.oElementFrameWrapper = document.getElementById('frame-wrapper');
		this.iFramesCount = Common.Dom.getElementsByClassName(this.oElementFrameWrapper, 'frame', 'div').length;
		this.oElementArrowPrev = document.getElementById('arrow-prev');
		this.oElementArrowNext = document.getElementById('arrow-next');
		this.oElementFormula = document.getElementById('formula').getElementsByTagName('span')[0];
		this.oElementHider = document.getElementById('hider');
		this.oElementFormulaPopup = document.getElementById('formula-popup');
		this.aElementsFormula = Common.Dom.getElementsByClassName(this.oElementFormulaPopup, 'this', 'span');
		this.oElementRoof = Common.Dom.getElementsByClassName(document.getElementById('frame-6'), 'object-1', 'div')[0];
		this.aElementsRoofSelect = Common.Dom.getElementsByClassName(document.getElementById('frame-6'), 'this', 'span');
		this.iFrameCurrent = this.getInitialFrameCurrent();
		
		var oThis = this;
		
		Common.Event.add(
			this.oElementArrowPrev,
			'click',
			function(oEvent) {
						
				Common.Event.cancel(oEvent);
						
				oThis.stopAutoAnimation();	
				oThis.setFrameCurrent(oThis.getFrameCurrent() - 1);
			
			}
			);					
			
		Common.Event.add(
			this.oElementArrowNext,
			'click',
			function(oEvent) {
			
				Common.Event.cancel(oEvent);
			
				oThis.stopAutoAnimation();
				oThis.setFrameCurrent(oThis.getFrameCurrent() + 1);
			
			}
			);
		
		Common.Event.add(
			this.oElementFormula,
			'click',
			function(oEvent) {
			
				Common.Event.cancel(oEvent);
				oThis.displayFormula(true);
			
			}
			);
			
		Common.Event.add(
			document,
			'click',
			function() {
			
				oThis.displayFormula(false);
			
			}
			);		
		
		Common.Event.add(	
			this.oElementFormulaPopup,
			'click',
			function(oEvent) {
			
				Common.Event.cancel(oEvent);
			
			}
			);
			
		for(var i = 0; i < this.aElementsFormula.length; i++) {
			Common.Event.add(
			 	this.aElementsFormula[i],
			 	'click',
			 	function(i) {
			 	
			 		return function() {
			 		
			 			oThis.displayFormula(false);
			 			oThis.stopAutoAnimation();
						oThis.setFrameCurrent(i);
			 		
			 		}
			 	
			 	}(i)
				);
		}
		
		for(var i = 0; i < this.aElementsRoofSelect.length; i++) {
			Common.Event.add(
			 	this.aElementsRoofSelect[i],
			 	'click',
			 	function(i) {
			 	
			 		return function() {
			 					 			
						oThis.setRoofCurrent(i);
			 		
			 		}
			 	
			 	}(i)
				);
		}
		
		this.iAutoAnimationTimer = setInterval(
			function() {
				
				if(oThis.getFrameCurrent() + 1 > oThis.iFramesCount - 1) {
				
					oThis.stopAutoAnimation();
					return;
					
				}
			
				oThis.setFrameCurrent(oThis.getFrameCurrent() + 1);
			
			},
			this.AUTO_ANIMATION_INTERVAL
			);						
	
	},
	
	getInitialFrameCurrent : function() {
	
		return parseInt(this.oElementFrameWrapper.parentNode.className.match(/frame-([0-9]+)/)[1]);
	
	},
	
	stopAutoAnimation : function() {
	
		clearInterval(this.iAutoAnimationTimer);
	
	},
	
	getFrameCurrent : function() {
	
		return this.iFrameCurrent;
	
	},
	
	setFrameCurrent : function(iFrameCurrent) {
	
		if(this.getFrameCurrent() == iFrameCurrent || (iFrameCurrent < 0 || iFrameCurrent > this.iFramesCount - 1)) {
			return;
		}
	
		this.hideArrows();				
	
		var oThis = this;
		
		iFramCurrentInitial = this.iFrameCurrent;
		
		this.oAnimationController.addAnimation(
			new AnimationEquation(
				this.oElementFrameWrapper,
				{
					aEquations   : [AnimationEquation.Back.easeInOut],
					aProperties  : ['left'],
					aUnits       : ['%'],
					aValuesStart : [-this.getFrameCurrent() * 100],
					aValuesEnd   : [-iFrameCurrent * 100],
					iFramesCount : 50,
					fStopCallbackFunction : function() {					
						
						oThis.showArrows();
						
						Common.Class.remove(oThis.oElementFrameWrapper.parentNode, 'frame-' + iFramCurrentInitial);
						Common.Class.add(oThis.oElementFrameWrapper.parentNode, 'frame-' + iFrameCurrent);
					
					}					
				}
				)
			);								
		
		Common.Class.remove(this.aElementsFormula[this.getFrameCurrent()], this.CLASS_NAME_SELECTED);
		Common.Class.add(this.aElementsFormula[iFrameCurrent], this.CLASS_NAME_SELECTED);
			
		this.iFrameCurrent = iFrameCurrent;		
			
		this.oAnimationController.start();
	
	},
	
	hideArrows : function() {
	
		Common.Class.add(
			this.oElementArrowPrev,
			this.CLASS_NAME_INVISIBLE
			);
			
		Common.Class.add(
			this.oElementArrowNext,
			this.CLASS_NAME_INVISIBLE
			);
	
	},
	
	showArrows : function() {
	
		if(this.getFrameCurrent() > 0) {
			Common.Class.remove(
				this.oElementArrowPrev,
				this.CLASS_NAME_INVISIBLE
				);
		}
		
		if(this.getFrameCurrent() < this.iFramesCount - 1) {	
			Common.Class.remove(
				this.oElementArrowNext,
				this.CLASS_NAME_INVISIBLE
				);
		}
	
	},
	
	displayFormula : function(bShow) {
		
		if(bShow == this.bShowedFormula) {
			return;
		}
		
		this.bShowedFormula = bShow;
		
		if(bShow) {		
			Common.Class.remove(
				this.oElementHider,
				this.CLASS_NAME_INVISIBLE
				);
		}
		else {
			Animator.fadeOut(this.oElementFormulaPopup, { bShared : true });
		}	
	
		var oThis = this;
	
		this.oAnimationController.addAnimation(
			new AnimationEquation(
				this.oElementHider,
				{
					aEquations   : [AnimationEquation.Quartic.easeIn],
					aProperties  : null,
					aValuesStart : [bShow? 0 : 0.4],
					aValuesEnd   : [bShow? 0.4 : 0],
					iFramesCount : 5,
					fCallbackFunction : function(oAnimation) {
					
						Common.Dom.setOpacity(
							oAnimation.getElement(),
							oAnimation.getValuesCurrent()[0]
							);
					
					},
					fStopCallbackFunction : function(oAnimation) {
					
						if(bShow) {
							Animator.fadeIn(oThis.oElementFormulaPopup, { bShared : true });
						}
						else {
							Common.Class.add(
								oAnimation.getElement(),
								FormulaController.CLASS_NAME_INVISIBLE
								);
						}
					
					}
				}
				)
			);	
			
		this.oAnimationController.start();		
	
	},
	
	setRoofCurrent : function(iRoofCurrent) {
	
		if(this.iRoofCurrent == iRoofCurrent) {
			return;
		}
		
		this.oElementRoof.style.backgroundPosition = -iRoofCurrent * this.oElementRoof.offsetWidth + 'px 0';
		
		Common.Class.remove(this.aElementsRoofSelect[this.iRoofCurrent], this.CLASS_NAME_SELECTED);
		Common.Class.add(this.aElementsRoofSelect[iRoofCurrent], this.CLASS_NAME_SELECTED);
		
		this.iRoofCurrent = iRoofCurrent;
	
	}

};

Common.Event.add(
	document,
	'DOMContentLoaded',
	function() {

		FormulaController.init();

	}
	);
