/**
 * @author Tim Hooijmans
 */
var ST = function(){

	this.setTimer = function(){

		var self = this;
		this.timer = window.setTimeout(function(){self.NS()},8000);
		
	}
	
	this.clearTimer = function(){window.clearTimeout(this.timer);}
	this.SLUGS = new Array();
	this.feed = function(html,flag){
		
		 if(!html)return false;
		 var s = new this.SLUG(html,flag);
		 this.SLUGS.push(s);
	
	}
	this.CI = 0;
	this.NI = 0;
	this.active = false;
	this.updateIND = function(i){this.indication.innerHTML = "("+parseInt(i+1)+"/"+this.SLUGS.length+")";}
	this.LM = function(elementID){
		
		if(!elementID||this.SLUGS.length<1)return false;
		this.container = (document.getElementById(elementID)) ? document.getElementById(elementID) : false;
		if(this.containeer==false)return false;
		
		this.container.className = "STWINDOW";
		
		var indWrapper = document.createElement("span");
		indWrapper.innerHTML = "0/4";
		indWrapper.className = "INDWRAPPER";
		
		this.indication = indWrapper;
		this.container.appendChild(indWrapper);
		
		var STMAG = document.createElement("div");
		STMAG.className = "STMAG";
		this.container.appendChild(STMAG);
		this.MAG = STMAG;
		
				
		if(this.SLUGS.length==1){
			this.MAG.appendChild(this.SLUGS[0]);
			this.updateIND(this.CI);
			return;
		}
		
		var indexWrapper = document.createElement("div");
		indexWrapper.className = "STINDEXWRAPPER";
		
		var NB = document.createElement("input");
		NB.setAttribute("type","button");
		NB.className = "STNEXT";
		
		var PB = document.createElement("input");
		PB.setAttribute("type","button");
		PB.className = "STPREVIOUS";
		
		var self = this;
		
		NB.onmouseup = function(){self.NS()};
		PB.onmouseup = function(){self.PS()};
		
		indexWrapper.appendChild(NB);
		indexWrapper.appendChild(PB);
		this.container.appendChild(indexWrapper);
		
		this.MAG.appendChild(this.SLUGS[0]);
		this.updateIND(this.CI);
		this.setTimer();
		
	}
	this.NS = function(){
		
		if(this.active==true)return;
		this.clearTimer();
		this.active = true;
		this.NI = (this.NI == parseInt(this.SLUGS.length-1) ) ? 0 : this.NI + 1 ;
		this.MAG.appendChild(this.SLUGS[this.NI]);
		this.updateIND(this.NI);
		this.SLIDE(this.NI,this.CI);
		
	}
	this.PS = function(){
	
		if(this.active==true)return;
		this.clearTimer();
		this.active = true;
		this.NI = ( this.NI == 0 ) ? this.SLUGS.length - 1 : this.NI - 1 ;
		this.MAG.appendChild(this.SLUGS[this.NI]);
		this.updateIND(this.NI);
		this.SLIDE(this.NI,this.CI);
		
	}
	this.SLIDE = function(newIndex,oldIndex){

		var pace = 5;
		var i = setTimeout(a,pace);
		
		var NEWSLUG = this.SLUGS[newIndex];
		var OLDSLUG = this.SLUGS[oldIndex];
		var self = this;
		var newLeft = 0;
	
		function a(){

			newLeft += (580 - newLeft)/4;
			self.MAG.style.left = (0 - newLeft) + "px";
			
			if (newLeft > 578) {
				
				self.MAG.removeChild(OLDSLUG);
				self.MAG.style.left = "0px";
				self.CI = newIndex;
				self.active = false;
				self.setTimer();
				
			}else{	
			
				var i = setTimeout(a,pace);

			}
			
		}

	}
	this.SLUG = function(data,flag){
		
		if(!data)var data = document.createElement("div");
		var div = document.createElement("div");
		div.className = "STSLUG";
		div.appendChild(data);
		return div;
		
	} 
	
}

