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

//	getElementsByTagName rewrite, filter by class
	function s$$(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;
		}
		
	}

//	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		=	getPos(this.oS);
		this.oT		=	0;					// offsetTop
		this.oL		=	175;					// offsetLeft
		this.cN		=	s$$('div', 'node');		// childnodes
    
		this.fP		=	0;						// the from pos
		this.tP		=	0;						// the to pos
		
		this.cnt	=	0;
		this.time	=	25;
		
		this.busy	=	false;
		
		//this.nP		=	this.oW;						// the new clip pos
		this.nP = 130;
    
		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['right'] 	=	130;
		this.clip['bottom']	=	this.oH;
		this.clip['left']	=	0;
		 
    this.timer = null;
     
		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.width				=	this.cN.length * 150 + '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.width 	= 130 + 'px';
				this.cN[i].style.height	=	this.oH + 'px';
			}
			this.positionSlider();
      $('iteminfo').style.visibility = 'visible';
		}
		
		,
		
		// 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['right']	=	this.nP + 150;
				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.fP	=	this.nN * 130;
        this.tP =	130;
				++this.nN;
				if (this.nN > this.nM){
					//this.tP = 0 - ((this.nN - 1) * this.oW);
          this.tP = 0 - ((this.nN - 1) * 130);
					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.fP = this.nN * 130;
					this.tP = (n - this.nN) * 130;
          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;
        this.fP	=	this.nN * 130;
				this.tP =	0 - 130;
				// adjusting the node number
				--this.nN;
				if (this.nN < 0){
					this.nN = this.nM;	
					this.fP = 0;
					//this.tP = this.nM * this.oW;
        	this.tP = this.nM * 130;
  			}
				this.busy = true;
				this.animateSlider();
			}
		}		
		
		,
		
    continueToPreviousNode : function(){
        while(!this.busy){
            this.moveToPreviousNode();
        }
        this.timer = window.setTimeout(bind(this, this.continueToPreviousNode), 15);
    }
    
    ,
    
    stopToPreviousNode : function(){
        window.clearTimeout(this.timer);
    }
    
    ,

    continueToNextNode : function(){
        while(!this.busy){
            this.moveToNextNode();
        }
        this.timer = window.setTimeout(bind(this, this.continueToNextNode), 15);
    }
    
    ,
    
    stopToNextNode : function(){
        window.clearTimeout(this.timer);
    }
    
    ,
    
		animateSlider			:		function(){
			if (this.cnt < this.time){
					this.nP = Math.floor(this.noEase(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 = 15;
				}else{
					if (this.nN == 16){
						this.nN = 1;
					}
				}
// 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);		
	    }
		
	}