/**
 * 
 * Slider class
 * 
 * @author:		Jochen Vandendriessche
 * @version:	0.1 alpha
 * @since		2007 08 14
 * 
 */

//	document.getElementById rewrite
	function $(id){
		if (document.getElementById(id)){
			return document.getElementById(id);
		}else{
			return null;
		}
	}
	
//	getElementsByTagName rewrite, filter by class
	function $$(t, c){
		
		var e	=	document.getElementsByTagName(t);
		if (c){
			var ne = [];
			for (var i = 0;i<e.length;i++){
				if (e[i].className == c){
					ne.push(e[i]);
				}
			}
			return ne;
		}else{
			return e;
		}
		
	}

//	the bind function, binds a method to his object when called in an other object's method (fiew, what an explanation)
	function bind(el, func){
	    return function() {
			 func.call(el); 
		}
	}

//	Function to find the absolute offset position of an element (ie takes the offset position with the parent object...)
	function findPos(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return [curleft,curtop];
	}
	
//	Slider class constructor
	function Slider(id, w, h){
		
		this.oS		=	$(id);					// the html object
		this.oW		=	parseInt(w);			// the width
		this.oH		=	parseInt(h);			// the height
		var pos		=	findPos(this.oS);
		this.oT		=	pos[1];					// offsetTop
		this.oL		=	pos[0];					// offsetLeft
		this.cN		=	$$('div', 'node');		// childnodes


		this.fP		=	0;						// the from pos
		this.tP		=	0;						// the to pos
		
		this.cnt	=	0;
		this.time	=	20;
		
		this.busy	=	false;
		
		this.nP		=	this.oW;						// the new clip pos
		
		this.nN		=	1;						// the current node number
		this.nM		=	this.cN.length - 1;			// the max node number
		
		this.clip	= [];						// clipping proporties
		this.clip['top'] 	= 	0;
		this.clip['right'] 	=	this.oW;
		this.clip['bottom']	=	this.oH;
		this.clip['left']	=	0;
		 
		if (this.oS){
			//window.alert('offset Top:' + this.oT + '\noffset Left:' + this.oL)
			this.createSlider();
		}
		
	}
	
//	Slider methods
	Slider.prototype		=		{
		
		// slider creation, css attributes
		createSlider		:		function(){
						
			this.oS.style.overflow		=		'visible';
			this.oS.style.position			=	'absolute';
			this.oS.style.width				=	this.cN.length * this.oW + 'px';
			this.oS.style.height			=	this.oH + 'px';
			for (var i = 0;i<this.cN.length;i++){
				this.cN[i].style.width 	= 	this.oW + 'px';
				this.cN[i].style.height	=	this.oH + 'px';
			}
			this.positionSlider();
//			window.alert('slider init')
		}
		
		,
		
		// clip the slider to the right coords
		positionSlider		:		function(){
			// calculate the position
				this.oS.style.left				=	(this.oL - this.nP) + 'px';
				this.oS.style.top				=	this.oT + 'px';
			// calculate the clip coords
				this.clip['top']	=	0;
				this.clip['right']	=	this.nP + this.oW;
				this.clip['bottom']	=	this.oH;
				this.clip['left']	=	this.nP;			
			// clip the div
				this.oS.style.clip				=	'rect(' + this.clip['top'] + 'px, ' + this.clip['right'] + 'px, ' + this.clip['bottom'] + 'px, ' + this.clip['left'] + 'px)';			
		}
		
		,
		
		moveToNextNode		:		function(){
			// adjusting the node number
			if (!this.busy){
				this.fP	=	this.nN * this.oW;
				this.tP =	this.oW;
				++this.nN;
				if (this.nN > this.nM){
					this.tP = 0 - ((this.nN - 1) * this.oW);
					this.nN = 0;
				}
				this.busy = true;
				this.animateSlider();
			}
		}
		
		,
	
		moveToNodeNumber	:		function(n){
			n = parseInt(n);
			if ((n >= 0) && (n <= this.nM)){			
				if (!this.busy){			
					this.fP = this.nN * this.oW;
					this.tP = (n - this.nN) * this.oW;
					this.nN = n;		
					this.busy = true;		
					this.animateSlider();
				}
			}else{
				window.alert('node number out of range or NaN')
			}
		}
	
		,
		
		moveToPreviousNode		:		function(){
			if (!this.busy){
				this.fP	=	this.nN * this.oW;
				this.tP =	0 - this.oW;
				// adjusting the node number
				--this.nN;
				if (this.nN < 0){
					this.nN = this.nM;	
					this.fP = 0;
					this.tP = this.nM * this.oW;
				}
				this.busy = true;
				this.animateSlider();
			}
		}		
		
		,
		
		animateSlider			:		function(){
			if (this.cnt < this.time){
					this.nP = Math.floor(this.outQuintic(this.cnt, parseInt(this.fP), parseInt(this.tP), this.time));
					this.positionSlider();
					this.cnt++;
					window.setTimeout(bind(this, this.animateSlider), 15);
			}else{
				this.cnt = 0;
				this.nP = this.fP + this.tP;
				this.positionSlider();
				this.nP = 0;
				this.busy = false;
// laatste elemente en eerste opvangen, zodat hij door"slide"
				if (this.nN == 0){
					this.nN = this.cN.length - 2;
					window.alert('jump to last node')
				}else{
					if (this.nN == this.cN.length -1){
						this.nN = 1;
						window.alert('jump to first node');
					}
				}
// einde opvang
			}
		}
		
		,

	    noEase		:		function(t, b, c, d){
             t/=d;
             return b+c*(t);
		    }

		,

	    outCubic			:	function(t, b, c, d){
	                                var ts = (t/=d)*t;
	                                var tc = ts*t;
	                                return b+c*(tc + -3*ts + 3*t);		
	    }

	    ,
	
	    outQuintic		:	function(t, b, c, d){
	                                var ts = (t/=d)*t;
	                                var tc = ts*t;
	                                return b+c*(tc*ts + -5*ts*ts + 10*tc + -10*ts + 5*t);		
	    }
		
	}