/**************************************************
 * CPushSlide, version 0.20
 * Copyright 2007 Ivengi BV. Benelux
 **************************************************/
/*
CONSTRUCTOR
new CPushSlide(ContainerID, aSlides, Intervalms, SlideAnimationLength, SliderType, Direction, bDetailedArray);
ContainerID							-> ID of the DIV that contains the slides
aSlides									-> Array with ID's of the different slides
Intervalms	  					-> Change to tune the smoothness (default: 4)
SlideAnimationLength		-> Time in Ms that it takes to do a full slide
SliderType							-> Type of slide ('smooth', 'static', 'easeout' or 'random')
Direction								-> Direction form which the slides appear: ('top','left','bottom','right','fade','noloop' or 'random')
bDetailedArray					-> Define if the array is in a 'Detailed' setup

#################################################
# FOR MORE INFO & EXAMPLES REFER TO THE README! #
#################################################

*/
function CPushSlide(ContainerID, aSlides, Intervalms, SlideAnimationLength, SliderType, Direction, bDetailedArray) 
{
	this.ContainerID	   			= ContainerID;
	this.aSlides							=	aSlides;
	
	this.Intervalms 	 				= Intervalms; //times to run the script
	this.SlideAnimationLength = SlideAnimationLength; //time to slide up or down
  this.SliderType  					= SliderType // smooth,easeout,random or static 
  this.Direction						=	Direction;
  this.bDetailedArray				= bDetailedArray;
  
  this.autoSlideSpeed = 0;
  
  this.noLoop					=	false;
  this.NextSlide 			= -1;
  this.CurrentSlide 	= 0;
  this.Container 			= null;
  this.SlideDirection	= "";
  
  this.SlideDiv				= null;
  this.LastSlideDiv		= null;
  this.TimerID 				= null;
	this.StartTime 			= 0;
	this.SlideSize 			= 0;
	this.bMoving 				= false;
	this.bFirstPassed 	= false;
	this.Spacing				= 5;
	this.randomEffect 	= false;
	this.arrayindex			= 0;
	
	this.attemptDisableScrolls = false;
	this.bShowLocation 	= false;
	this.aLocationtxt 	= new Array();
	this.LocationDiv		= "";
	this.bAddtionalFunction = false;
	
	this.AutoSlideTimer = null;
	
	this.aDirections		= new Array('left','right','bottom','top');
	this.aEffects				= new Array('smooth','static','easeout');
	
	this.noLoopDirection = "up";
	
	if (this.Direction == "noloop") {
		this.noLoop = true;
		this.Direction = 'right';
	}
	
	if(this.SliderType == 'random') {
		this.randomEffect = true;
	}
}

CPushSlide.prototype.setFirstSlide = function(slideNo) {
	this.CurrentSlide = slideNo;
	this.NextSlide = slideNo;
	this.bFirstPassed = true;
}

CPushSlide.prototype.StartAutoSlide = function(MsInterval) {
	this.autoSlideSpeed = MsInterval;
	this.ShowNextSlide();
	var ThisObject = this;
  this.AutoSlideTimer = setInterval(function(){ThisObject.ShowNextSlide()},MsInterval);
}

CPushSlide.prototype.StopAutoSlide = function() {
	clearInterval(this.AutoSlideTimer);
}

CPushSlide.prototype.ForceNextSlide = function(SlideNo, bAutoStart) {
	if(SlideNo != this.CurrentSlide) {
		this.StopAutoSlide();
		this.CurrentSlide = this.NextSlide;
		this.NextSlide = SlideNo;
		this.MoveSlide();
		this.CurrentSlide = this.NextSlide;
		if (bAutoStart) {
			var ThisObject=this;
  		this.AutoSlideTimer = setInterval(function(){ThisObject.ShowNextSlide()},this.autoSlideSpeed);
		}
	}
}

CPushSlide.prototype.ShowPreviousSlide = function(){	
	this.CurrentSlide = this.NextSlide;
	this.decreaseSlide();
	this.MoveSlide();
}

CPushSlide.prototype.ShowNextSlide = function(){
	if(this.bFirstPassed) {
		this.CurrentSlide = this.NextSlide;
	}
	if(!this.noLoop) {
		this.timeLineSlide();
	} else {
		this.increaseSlide();
	}
	this.MoveSlide();
}

CPushSlide.prototype.ShowLocation = function(aLocationTxt,LocationDiv,bAddtionalFunction){
	this.bShowLocation 	= true;
	this.aLocationtxt 	= aLocationTxt;
	this.LocationDiv		= LocationDiv;
	this.bAddtionalFunction = bAddtionalFunction;
}

CPushSlide.prototype.updateLocation = function(){
	document.getElementById(this.LocationDiv).innerHTML = this.aLocationtxt[this.NextSlide][0];
	if (this.bAddtionalFunction) {
		eval(this.aLocationtxt[this.NextSlide][1]);
	}
}

CPushSlide.prototype.SwitchArrayWithIndex = function(aSlideArray, bOneSlide,ArrayIndex) {
	if (this.arrayindex != ArrayIndex) {
		if ((this.aSlides != aSlideArray) && !this.bMoving) {	
			this.StopAutoSlide();
			bOneSlide = true;
			if(aSlideArray.length > 1) {
				bOneSlide = false;
			}
			var lastDiv = this.aSlides[this.CurrentSlide];
			var nextdivlocation = -1;	
			while (this.aSlides.length > 0) {
				this.aSlides.pop();
			}
			this.aSlides = this.aSlides.concat(aSlideArray);
			if (this.arrayindex > ArrayIndex) {
				this.noLoopDirection = "down";
				nextdivlocation = this.aSlides.length;			
			} else if (this.arrayindex < ArrayIndex) {
				this.noLoopDirection = "up";
				nextdivlocation = -1;
			}
			this.aSlides[nextdivlocation] = lastDiv;
			this.NextSlide 			= nextdivlocation;
			this.CurrentSlide 	= nextdivlocation;
		  if (bOneSlide) {
		  	this.ShowNextSlide();
		  } else {
		  	this.StartAutoSlide(this.autoSlideSpeed);
		  }
		  while (this.aSlides.length > 0) {
				this.aSlides.pop();
			}
			this.aSlides = this.aSlides.concat(aSlideArray);
		  document.getElementById(lastDiv).display = 'none';
		  this.arrayindex = ArrayIndex;
		}
	}
}

CPushSlide.prototype.ChangeSpacing = function(Spacing) {
	this.Spacing = Spacing;
}

CPushSlide.prototype.AttemptDisableScrolls = function() {
	this.attemptDisableScrolls = true;
}


CPushSlide.prototype.DisableScolls = function() {
	var aElements 		= document.getElementById(this.ContainerID).getElementsByTagName("div");
	for (var i = 0; i < aElements.length; i++) {
		if(aElements[i].className.match('scroll') || aElements[i].className.match('noscroll')) {
			aElements[i].className = 'noscroll';
		}
	}
}


CPushSlide.prototype.EnableScolls = function() {
		var aElements 		= document.getElementById(this.ContainerID).getElementsByTagName("div");
	for (var i = 0; i < aElements.length; i++) {
		if(aElements[i].className.match('scroll') || aElements[i].className.match('noscroll')) {
			aElements[i].className = 'scroll';
		}
	}
}

CPushSlide.prototype.SwitchArray = function(aSlideArray) {
	if (this.aSlides != aSlideArray && !this.bMoving) {	
		this.StopAutoSlide();
		var bOneSlide = true;
		if(aSlideArray.length > 1) {
				bOneSlide = false;
			}
		var lastDiv = this.aSlides[this.CurrentSlide];
		this.aSlides = aSlideArray
		this.aSlides[-1] = lastDiv;
		this.NextSlide 			= -1;
	  this.CurrentSlide 	= -1;
	  if (bOneSlide) {
	  	this.ShowNextSlide();
	  } else {
	  	this.StartAutoSlide(this.autoSlideSpeed);
	  }
	  delete(this.aSlides[-1]);
	  document.getElementById(lastDiv).display = 'none';
	 }
}

CPushSlide.prototype.MoveSlide = function() {
	if(this.bMoving) {
		this.endSlide();
	}

	if(this.bShowLocation) {
		this.updateLocation();
	}

	this.SlideDiv = document.getElementById(this.aSlides[this.NextSlide][0]);
		
	if(this.bFirstPassed) {
		this.LastSlideDiv = document.getElementById(this.aSlides[this.CurrentSlide][0]);
	}
	this.Container = document.getElementById(this.ContainerID);
	
	this.StartTime = (new Date()).getTime();
	this.bMoving = true;
	
	if(this.Direction == 'random') {
		var RandDirection = Math.floor((this.aDirections.length)*Math.random())
		this.SlideDirection = this.aDirections[RandDirection];
	} else {
		this.SlideDirection = this.Direction;
	}
	
	if (this.bDetailedArray) {
		if (this.aSlides[this.NextSlide][1] == "noloop") {
			this.noLoop = true;
		} else {
			this.noLoop = false;
			this.SlideDirection = this.aSlides[this.NextSlide][1];
			if(this.SlideDirection == 'random') {
				var RandDirection = Math.floor((this.aDirections.length)*Math.random())
				this.SlideDirection = this.aDirections[RandDirection];
			}
		}
		if(this.AutoSlideTimer != null) {
			clearInterval(this.AutoSlideTimer);
			var ThisObject=this;
  		this.AutoSlideTimer = setInterval(function(){ThisObject.ShowNextSlide()},this.aSlides[this.NextSlide][2]);
		}
	}
	

	
	if(this.noLoop) {
		if (this.CurrentSlide <= this.NextSlide) {
			this.SlideDirection = "bottom";
		} else {
			this.SlideDirection = "top";
		}
	}
	
 	switch(this.SlideDirection) {
 		case 'top':

      this.SlideSize = this.style2num(this.SlideDiv.style.height);
 		  this.SlideDiv.style.top = this.SlideSize +"px";
 		  this.SlideDiv.style.left = "0px";
 			if(this.bFirstPassed) {
	      this.LastSlideDiv.style.top = this.Spacing + "px";
		  }
 			break;
 		case 'bottom':
 		  this.SlideSize = this.style2num(this.SlideDiv.style.height);
 		  this.SlideDiv.style.top = (-this.SlideSize) +"px";
 		  this.SlideDiv.style.left = "0px";
 			if(this.bFirstPassed) {
		    this.LastSlideDiv.style.top = (this.SlideSize - this.Spacing) + "px";
		  }
 			break;
 	case 'fade':
 	    if(this.bFirstPassed) {
		  	this.LastSlideDiv.style.left = 0 + "px";
		  	this.LastSlideDiv.style.left = 0 + "px";
		  	if(this.NextSlide > this.CurrentSlide) {
		  		this.LastSlideDiv.style.zIndex = "87";
		  	} else {
		  		this.LastSlideDiv.style.zIndex = "91";
		  	}
		  }
 	    this.SlideSize = this.style2num(this.SlideDiv.style.width);
 	    this.SlideDiv.style.left = "0px";
 	    this.SlideDiv.style.filter = "alpha(opacity=0)";
 	    this.SlideDiv.style.opacity = "0"
 	    this.SlideDiv.style.zIndex = "89";
 			this.SlideDiv.style.left = "0px";
 			this.SlideDiv.style.top = "0px";
 			break;
 	  case 'right':
 	  
 	    this.SlideSize = this.style2num(this.SlideDiv.style.width);
 			this.SlideDiv.style.left = (-this.SlideSize) +"px";
 			this.SlideDiv.style.top = "0px";
 			if(this.bFirstPassed) {
		  	this.LastSlideDiv.style.left = (this.SlideSize - this.Spacing) + "px";
		  }
 			break;
 	  default : //left
 	    this.SlideSize = this.style2num(this.SlideDiv.style.width);
 			this.SlideDiv.style.left = this.SlideSize +"px";
 			this.SlideDiv.style.top = "0px";
 			  if(this.bFirstPassed) {
		  	this.LastSlideDiv.style.left = this.Spacing + "px";
		  }
 			break;
 	}
 			
 	if(this.randomEffect)
  {
  	var RandEffect = Math.floor((this.aEffects.length)*Math.random())
		this.SliderType = this.aEffects[RandEffect];
  }
 		
 	if(this.SlideDiv.style.display != "block") {	
  	this.SlideDiv.style.display = "block";
  }
  if(this.bFirstPassed) {
  	if(this.LastSlideDiv.style.display != "block") {
  		this.LastSlideDiv.style.display = "block";
  	}
  }
  if(this.attemptDisableScrolls) {
  	this.DisableScolls();
  }
  var ThisObject=this;
  this.TimerID = setInterval(function(){ThisObject.SlideTick()},this.Intervalms);
}

CPushSlide.prototype.SlideTick = function()
{
  var ElapsedTime = (new Date()).getTime() - this.StartTime; 
  if (ElapsedTime >= this.SlideAnimationLength) {
    this.endSlide()
  } else {
    var NextStep = 0;
    switch(this.SliderType)
    {
    	case 'smooth' :
	      var DistancePercent = 0;  
	      DistancePercent = this.curveincrement(ElapsedTime/this.SlideAnimationLength);
	      NextStep = Math.round(DistancePercent * this.SlideSize);
	      break;
	    case 'easeout' :
	      var DistancePercent = 0;  
	      DistancePercent = this.easeout(ElapsedTime/this.SlideAnimationLength);
	      NextStep = Math.round(DistancePercent * this.SlideSize);
	      break;   
	   default: //static
	      var d =Math.round(ElapsedTime / this.SlideAnimationLength * this.SlideSize);
	      NextStep = d;
	    	break;
	  }
	  
	  switch(this.SlideDirection) {
	 		case 'top':
	 			if(this.bFirstPassed) {
		    	this.LastSlideDiv.style.top = NextStep + this.Spacing + "px";
		    }
			  this.SlideDiv.style.top = ((-this.SlideSize) + NextStep) +"px";
	 			break;
	 		case 'bottom':
	 			if(this.bFirstPassed) {
		    	this.LastSlideDiv.style.top = (0-NextStep) - this.Spacing + "px";
		    }
			  this.SlideDiv.style.top = (this.SlideSize - NextStep) +"px";
	 			break;
	 		case 'fade':
			  this.SlideDiv.style.filter = "alpha(opacity="+(DistancePercent*100)+")";
 	    	this.SlideDiv.style.opacity = DistancePercent;
 	    	if(this.bFirstPassed) {
	 	    	this.LastSlideDiv.style.filter = "alpha(opacity="+(100-(DistancePercent*100))+")";
	 	    	this.LastSlideDiv.style.opacity = (1-DistancePercent);
	 	    }
	 			break;
	 	  case 'right':
	 	    if(this.bFirstPassed) {
		    	this.LastSlideDiv.style.left = (0-NextStep) - this.Spacing + "px";
		    }
			  this.SlideDiv.style.left = (this.SlideSize - NextStep) +"px";
	 			break;
	 	  default : //left
	 	    if(this.bFirstPassed) {
		    	this.LastSlideDiv.style.left = NextStep + this.Spacing + "px";
		    }
			  this.SlideDiv.style.left = ((-this.SlideSize) + NextStep) +"px";
	 			break;
		}
  }
}

CPushSlide.prototype.endSlide = function()
{
	if(this.attemptDisableScrolls) {
		this.EnableScolls();
	}
	this.bMoving = false;
  clearInterval(this.TimerID);
  this.SlideDiv.style.left = 0;
  this.SlideDiv.style.top	 = 0;
  if(this.bFirstPassed) {
		this.LastSlideDiv.style.display = "none";
  } else {
  	this.bFirstPassed = true;
  }
  return;
}

CPushSlide.prototype.easeout = function(percent){
	percent = 100-(percent * 100);
	percent = (100-Math.pow(1.0465,percent))+1;
	if (percent < 1) { // remove flicker
		percent = 1;
	}
	return (percent/100); //return power curve based value from a percentage input
}

CPushSlide.prototype.curveincrement=function(percent){
	return (1-Math.cos(percent*Math.PI)) / 2 //return cos curve based value from a percentage input
}

CPushSlide.prototype.increaseSlide = function() {
	if((this.NextSlide+1) >= this.aSlides.length) {
		this.NextSlide = 0;
	} else {
		this.NextSlide++;
	}
}

CPushSlide.prototype.timeLineSlide = function() {
	if(this.noLoopDirection == "up") {
		if((this.NextSlide+1) >= this.aSlides.length) {
			this.noLoopDirection = "down";
			this.NextSlide--;
		} else {
			this.NextSlide++;
		}
	} else {
		if ((this.NextSlide-1) < 0) {
			this.noLoopDirection = "up";
			this.NextSlide++;
		} else {
			this.NextSlide--;
		}
	}
}

CPushSlide.prototype.decreaseSlide = function() {
	if((this.NextSlide-1) < 0) {
		this.NextSlide = (this.aSlides.length-1);
	} else {
		this.NextSlide--;
	}
}

CPushSlide.prototype.style2num = function(value) {
	if(value)
		return (+value.substring(0,value.search(/\D/)));
	else
		return 0;
}

CPushSlide.prototype.StartSlidesAfterLoad = function(value)
{
	var _this = this;
	this.addLoadHandler(function(){ _this.StartAutoSlide(value); });
}

CPushSlide.prototype.addLoadHandler = function(handler)
{
	if(window.addEventListener)
	{
		window.addEventListener("load",handler,false);
	}
	else if(window.attachEvent)
	{
		window.attachEvent("onload",handler);
	}
	else if(window.onload)
	{
		var oldHandler = window.onload;
		window.onload = function piggyback()
		{
			oldHandler();
			handler();
		};
	}
	else
	{
		window.onload = handler;
	}
}