/**
 *  @author michal.senk@siteone.cz
 *  @require yui/yahoo-dom-event.js
 *  @require yui/animation.js
 */

function GalleryMover( prevButton, nextButton, rowElem, moveSize, rowEntitClass, nextMoveRowDirection, speed ) {

	// constructor	========================
	
	
	// YUI
	try {
		var Dom = YAHOO.util.Dom;
		var Ev = YAHOO.util.Event;
	} catch (e){
		alert('Missing yahoo-dom-event.js');
	}
	
	try {
		var Anim = YAHOO.util.Anim;
	} catch (e){
		alert('Missing animation.js');
	}
	
	this.nextMoveRowDirection=nextMoveRowDirection; // left|right|up|down
	this.speed=speed; // animation speed
	this.rowElem=rowElem; // row elements 
	this.rowEntitClass=rowEntitClass; // className of entits in row
	this.moveSize=moveSize; // holder window size (animation track)
	this.prevButton=prevButton;
	this.nextButton=nextButton;
	
	var isIE6 = (navigator.userAgent.indexOf("MSIE 6")>(-1) && navigator.userAgent.indexOf("MSIE 7")<(0) && navigator.userAgent.indexOf("MSIE 8")<(0));


	//======================================

	this.isAnimating = false;
	var that = this;

	// colsize =============================

	var entits = Dom.getElementsByClassName(this.rowEntitClass,null,this.rowElem);
	this.entitsCount = entits.length

	switch(this.nextMoveRowDirection){
		case 'right':
		case 'left':
			
			var margins = parseInt(Dom.getStyle(entits[0],'margin-left').replace("px",""))+ parseInt(Dom.getStyle(entits[0],'margin-right').replace("px",""));
			var entitSize = entits[0].offsetWidth+margins;
			this.entitSize = entitSize;
			this.colSize = entitSize*entits.length+(isIE6?2:0);
			Dom.setStyle(this.rowElem,'width',this.colSize+'px');
						
		break;
		case 'up':
		case 'down':
		
			var margins = parseInt(Dom.getStyle(entits[0],'margin-top').replace("px",""))+ parseInt(Dom.getStyle(entits[0],'margin-bottom').replace("px",""));
			var entitSize = entits[0].offsetHeight+margins;
			this.entitSize = entitSize;
			this.colSize = entitSize*entits.length;
			Dom.setStyle(this.rowElem,'height',this.colSize+'px');

		break;
	
	}
	//======================================
	
	// px count if last screen 
	
	//this.lastMove = (( Math.ceil(this.colSize / this.moveSize) * this.moveSize ) - this.colSize);
	this.lastMove = ( (this.colSize - Math.floor(this.colSize / this.moveSize) * this.moveSize ) );
	this.stepPosition = 0;
	this.lastMove!=0?this.stepsCount = Math.floor(this.colSize / this.moveSize):this.stepsCount = Math.floor(this.colSize / this.moveSize)-1;
	this.doLastStep = (this.stepsCount==1);
	this.wasMadeLastStep = false;


	/**
	 *  method to get actual absolute position
	 */
	this.actualPosition = function(side){
			return parseInt(Dom.getStyle(this.rowElem,side).replace("px",""));
		}
	
	
	/**
	 * method to enable or disable button
	 */
	 this.changeButton = function(button,action){
		switch(action){
			case 'disable':
				Dom.addClass(button,'button-disable');
			break;
			case 'enable':
				Dom.removeClass(button,'button-disable');
			break;
		}
	 }
	
	// set disable prevarrow
	this.changeButton(this.prevButton,'disable');
	
	if ((this.entitSize*this.entitsCount) <= this.moveSize){
		this.changeButton(this.nextButton,'disable');
	}
	
	this.moveNext=function(e,fce){
		
		e?Ev.stopEvent(e):true;
						
		if (that.isAnimating==true || (Dom.hasClass(that.nextButton.length>0?that.nextButton[0]:that.nextButton,'button-disable')) ){
			return;
		}
		
		// lastButOne screen
		if ( ( (that.stepPosition+1)==that.stepsCount && that.lastMove!=0 && that.wasMadeLastStep==false) ){
			
			that.doLastStep = true;
			that.wasMadeLastStep = true;
		} else {
			that.doLastStep = false;
		}
		
		that.isAnimating = true;
		
		// set animation params		
		switch(that.nextMoveRowDirection){
			case 'right':
				
				var params = {left:{
								'to'      	: that.actualPosition('left')+(that.doLastStep?(-1)*that.lastMove:(-1)*that.moveSize),
								'units'   	: 'px'
							}}
							
				var controlData = {
						'absolutka'	: 'left',
						'multip'  	: (-1)
					}
				
			break;
			case 'left':
				var params = {left:{
								'to'    	: that.actualPosition('left')+that.moveSize,
								'units' 	: 'px'
							}}
							
				var controlData = {
						'absolutka' : 'left',
						'multip'  	: (1)
					}
			break;
			case 'up':
				var params = {top:{
								'to'    	: that.actualPosition('top')+(that.doLastStep?(-1)*that.lastMove:(-1)*that.moveSize),
								'units' 	: 'px'
							}}
							
				var controlData = {
						'absolutka' : 'top',
						'multip'  	: (-1)
					}
			break;
			case 'down':
				var params = {top:{
								'to'    	: that.actualPosition('top')+(that.doLastStep?that.lastMove:that.moveSize),
								'units' 	: 'px'
							}}
							
				var controlData = {
						'absolutka' : 'top',
						'multip'  	: (1)
					}
			break;
		}

		var animation = new Anim(that.rowElem,params,that.speed,YAHOO.util.Easing.easeOut);
		animation.onComplete.subscribe(
										function(){
													that.isAnimating = false;
													that.changeButton(that.prevButton,'enable');
													that.stepPosition++;
													
													if ( that.stepPosition == that.stepsCount ){
														 that.changeButton(that.nextButton,'disable');
													}
													
													if (typeof(fce)=="function"){ 
														fce();
													}
											}
										);
		animation.animate();
		
	}
	
	this.movePrevious=function(e,fce){
		e?Ev.stopEvent(e):true;

		if (that.isAnimating==true || Dom.hasClass(that.prevButton.length>0?that.prevButton[0]:that.prevButton,'button-disable')){
			return;
		}
		
		// lastButOne screen
		if ( that.stepPosition == 1 && that.wasMadeLastStep==true ){
			that.doLastStep = true;
			that.wasMadeLastStep = false;
		} else {
			that.doLastStep = false;
		}
		
		that.isAnimating = true;
		
		// set animation params		
		switch(that.nextMoveRowDirection){
			case 'right':
				var params = {left:{
								'to'    : that.actualPosition('left')+(that.doLastStep?that.lastMove:that.moveSize),
								'units' : 'px'
							}}
							
				var controlData = {
						'absolutka' : 'left',
						'multip'  	: (1)
					}
							
			break;
			case 'left':
				var params = {left:{
								'to'    : that.actualPosition('left')+((-1)*that.moveSize),
								'units' : 'px'
							}}
							
				var controlData = {
						'absolutka' : 'left',
						'multip'  	: (-1)
					}		
							
			break;
			case 'up':
				var params = {top:{
								'to'    : that.actualPosition('top')+(that.doLastStep?that.lastMove:that.moveSize),
								'units' : 'px'
							}}
							
				var controlData = {
						'absolutka' : 'top',
						'multip'  	: (1)
					}
							
			break;
			case 'down':
				var params = {top:{
								'to'    : that.actualPosition('top')+(that.doLastStep?(-1)*that.lastMove:(-1)*that.moveSize),
								'units' : 'px'
							}}
							
				var controlData = {
						'absolutka' : 'top',
						'multip'  	: (-1)
					}
							
			break;
		}

		var animation = new Anim(that.rowElem,params,that.speed,YAHOO.util.Easing.easeOut);
		animation.onComplete.subscribe(
										function(){
													that.isAnimating = false;
													that.changeButton(that.nextButton,'enable');
													that.stepPosition--;
													if ( that.actualPosition(controlData.absolutka)+((controlData.multip)*that.moveSize) - that.moveSize >= 0 ){
														that.changeButton(that.prevButton,'disable');
													}
													
													if (typeof(fce)=="function"){ 
															fce();
														}
													
											}
										);
		animation.animate();
		
	}
	
	/**
	 * Externi funkce ktara zajisti presun galerie k danemu prvku, pokud v ni je
	 * @param elem ke kteremu se ma gallery nacpat
	 * @param fce ktera se vykona po prasunu 
	 * @package blueStyle-calendar
	 * 
	 */
	this.getGalleryToElement = function(elem){

		
		var elemRegion = Dom.getRegion(elem);
		var rowRegon = Dom.getRegion(this.rowElem);
		var elemLeft = elemRegion.left-rowRegon.left;
		var step = Math.ceil((elemLeft/this.moveSize)-1);
		
		if (step>0){
			this.changeButton(this.prevButton,'enable');
		} 
		
		if (step>=this.stepsCount){
			this.changeButton(this.nextButton,'disable');
		}
		
		//Dom.setStyle(this.rowElem,'left', (step*this.moveSize*(-1))+"px");
			
		if (step!=0){
			
			// prvek neni na poslednim screenu
			if (step!=this.stepsCount){	
				Dom.setStyle(this.rowElem,'left', (step*this.moveSize*(-1))+"px");
				
			// prvek je na poslednim screenu 
			} else {
				this.changeButton(this.nextButton,'disable');
				Dom.setStyle(this.rowElem,'left', ( (((step-1)*this.moveSize)+this.lastMove)*(-1) )+"px");
				that.wasMadeLastStep=true;
			}
		
		}
		

		this.stepPosition = step;
		
		
	}
	
	
}
