// scroller.js
// Horizontal scroller
// Copyright 2005 Ben Krueger : ben@phoenixdesigns.biz

function getObj(objID)
{
    if(document.getElementById){
        return document.getElementById(objID);
    } else if (document.all){
        return document.all[objID];
    } else if (document.layers){
        return document.layers[objID];
    }
}
function DL_GetElementLeft(eElement)
{
	if (!eElement && this)                       // if argument is invalid
	{                                            // (not specified, is null or is 0)
		eElement = this;                         // and function is a method
	}                                            // identify the element as the method owner
	
	var nLeftPos = eElement.offsetLeft;          // initialize var to store calculations
	var eParElement = eElement.offsetParent;     // identify first offset parent element  
	while (eParElement != null)
	{                                            // move up through element hierarchy
		nLeftPos += eParElement.offsetLeft;      // appending left offset of each parent
		eParElement = eParElement.offsetParent;  // until no more offset parents exist
	}
	return nLeftPos;                             // return the number calculated
}
function scrollerObj(scrollerName,objName,width,height,toShow,autoScrolling,autoScrollingTime,arrContent,autoScrollingDelay){
	this.scrollerWin		= scrollerName;
	this.scrollerWidth		= width;
	this.scrollerHeight		= height;
	this.toShow				= toShow;
	this.autoScrolling		= autoScrolling;
	this.autoScrollingTime	= autoScrollingTime;
	this.objName			= objName;
	this.autoScrollingDelay	= autoScrollingDelay;
	
	if(!autoScrollingDelay){
		this.autoScrollingDelay	= 0;
	}
	else{
		this.autoScrollingDelay	= autoScrollingDelay;
	}
	
	this.autoScrollEnabled	= autoScrolling;
	this.sWin				= null;
	this.scrolling			= true;
	this.baseX				= 0;
	this.showItems			= 0;
	this.itemWidth			= 0;
	this.contentStr			= '';
	this.amScrolling		= false;
	this.arrVisItems		= new Array();
	if(!arrContent.length){
		this.contentItems	= new Array();	
	}			
	else{
		this.contentItems	= arrContent;
	}	
	
	/* Methods */				
	
	// Add an item to the content array
	this.addContentItem	= function(cStr){			
		this.contentItems[this.contentItems.length] = cStr;
	}
	
	// Write the scroller
	this.write	= function(){
		// write the div to the page
		var divStr	= "<div id=\"" + this.scrollerWin + "\" style=\"text-align=center;overflow:hidden;position:relative;width:" + this.scrollerWidth + "px;height:" + this.scrollerHeight + "px;\" onmouseover=\"" + this.objName + ".setScrolling(0)\" onmouseout=\"" + this.objName + ".setScrolling(1)\">&nbsp;</div>";
					
		document.write(divStr);
		
		this.sWin	= getObj(this.scrollerWin);						
	}
	
	// Initialize the scroller
	this.init	= function(){			
		if(!this.sWin){
			alert('The scroller div has not been written')
			return;
		}
		
		var sWin	= this.sWin;		
		
		this.baseX	= 	DL_GetElementLeft(sWin);
		setInterval(this.objName + '.setBaseX()',250);		
		
		this.showItems	= this.contentItems.length > this.toShow ? this.toShow : this.contentItems.length;
		this.itemWidth	= this.scrollerWidth / this.showItems;
		
		if(this.contentItems.length <= this.toShow){
			this.scrolling = false;
		}
		
		sWin.innerHTML = '';
		for(var i=0;i<this.contentItems.length;i++){
			sWin.innerHTML += "<div id=\"c" + this.scrollerWin + (i+1) + "\">hello</div>";
			var thisObj	= getObj('c' + this.scrollerWin + (i+1));
			
			thisObj.style.width	= this.itemWidth	+ 'px';
			thisObj.style.height	= this.scrollerHeight + 'px';
			thisObj.style.verticalAlign	= 'middle';
			thisObj.style.overflow	= 'hidden';			
			thisObj.style.textAlign	= 'center';			
			thisObj.style.position	= 'absolute';			
			thisObj.style.left	= i * this.itemWidth + 'px';			
			thisObj.style.color	= 'white';	
			thisObj.innerHTML	= this.contentItems[i];
			
			if(i < this.showItems){				
				this.arrVisItems[this.arrVisItems.length] = i+1;
				thisObj.style.display 	= '';
			}
			else{
				thisObj.style.display	= '';
			}
		}
		
		if(this.autoScrolling){				
			setTimeout(this.objName + '.launchAutoScroll()',this.autoScrollingDelay);
			//setInterval(this.objName + '.autoScroll()',this.autoScrollingTime);
		}			
					
	}
	
	this.setBaseX	= function(){
		var baseX	= DL_GetElementLeft(this.sWin);
		if(baseX != this.baseX){
			this.baseX	= DL_GetElementLeft(this.sWin);
		}		
	}
	
	this.launchAutoScroll	= function(){		
		setInterval(this.objName + '.autoScroll()',this.autoScrollingTime);
	}
	
	// turn auto scrolling on or off temporary
	this.setScrolling = function(type){			
		if(type == 0){
			this.autoScrolling	= false;
		}
		else{
			this.autoScrolling	= true;
		}
	}
	
	this.setAutoScrollEnabled	= function(type){
		if(type == 0){
			this.autoScrollEnabled	= false;
		}
		else{
			this.autoScrollEnabled	= true;
		}
	}
	
	// left and right scrolling functions
	this.scrollLeft	= function(obj,dis){		
		var cObj	= getObj(obj);			
		var realX	= DL_GetElementLeft(cObj) - this.baseX;
		var newX	= realX - dis;			
			
		cObj.style.left = newX + 'px';				
	}
	
	this.scrollRight	= function(obj,dis){		
		var cObj	= getObj(obj);
		var realX	= DL_GetElementLeft(cObj) - this.baseX;
		var newX	= realX + dis;			
		
		cObj.style.left = newX + 'px';			
	}
	
	this.shiftLeft	= function(obj,targetX,step,time){		
		var cObj	= getObj(obj);
		var cX	= DL_GetElementLeft(cObj) - this.baseX;
		var aTargetX	= targetX;		
		
		this.amScrolling	= true;
		
		this.scrollLeft(obj,step);			
		
		if((cX-step) > aTargetX){			
			cX -= step;
			if(cX - step < aTargetX){
				step = cX - aTargetX;
			}					
			
			setTimeout(this.objName + '.shiftLeft(\'' + obj + '\',' + targetX + ',' + step + ',' + time +')',time);
		}
		else{
			this.amScrolling	= false;
		}	
	}
	
	this.shiftRight	= function(obj,targetX,step,time){			
		var cObj	= getObj(obj);
		var cX	= DL_GetElementLeft(cObj) - this.baseX;
		var aTargetX	= targetX;		
		
		this.amScrolling	= true;
		
		this.scrollRight(obj,step);
		
		if((cX+step) < aTargetX){			
			cX += step;
			if(cX + step > aTargetX){
				step = aTargetX - cX;
			}				
			
			setTimeout(this.objName + '.shiftRight(\'' + obj + '\',' + targetX + ',' + step + ',' + time +')',time);
		}
		else{
			this.amScrolling	= false;	
		}				
		
	}
	
	this.indexLeft	= function(){
		// determine the content on deck to bring in
		if(this.scrolling && !this.amScrolling){
			var onDeck;
			var cEnd	= this.arrVisItems[this.arrVisItems.length-1];
			
			if(cEnd == this.contentItems.length){
				onDeck	= 1;				
			}
			else{				
				onDeck	= cEnd+1;				
			}			
			this.moveOnDeck('c' + this.scrollerWin + onDeck,2);			
			
			var tmpArr	= new Array();
			for(var i=0;i<this.arrVisItems.length;i++){
				tmpArr[i] = this.arrVisItems[i];
			}			
			tmpArr[tmpArr.length] = onDeck;			
			
			// move the existing divs to the left
			for(var i=0;i<tmpArr.length;i++){			
				this.shiftLeft('c' + this.scrollerWin + tmpArr[i],(i-1)*this.itemWidth,20,5);				
				//xSlideTo('c' + this.scrollerWin + tmpArr[i],(i-1)*this.itemWidth,0,1000);
			}
			
			this.arrVisItems.shift();
			this.arrVisItems.push(tmpArr[tmpArr.length-1]);			
					
		}
	}
	
	this.indexRight	= function(){
		// determine the content on deck to bring in
		if(this.scrolling && !this.amScrolling){
			var onDeck;			
			var cStart	= this.arrVisItems[0];
			
			if(cStart == 1){
				onDeck	= this.contentItems.length;				
			}
			else{				
				onDeck	= cStart-1;				
			}			
			this.moveOnDeck('c'+ this.scrollerWin + onDeck,1);
			
			
			var tmpArr	= new Array();
			for(var i=0;i<this.arrVisItems.length;i++){
				tmpArr[i] = this.arrVisItems[i];
			}			
			tmpArr.unshift(onDeck);				
			
			// move the existing divs to the right
			for(var i=tmpArr.length-1;i>=0;i--){				
				this.shiftRight('c'+ this.scrollerWin + tmpArr[i],i*this.itemWidth,20,5);
				//xSlideTo('c' + this.scrollerWin + tmpArr[i],i*this.itemWidth,0,1000);
			}
			this.arrVisItems.pop();
			this.arrVisItems.unshift(tmpArr[0]);				
					
		}
	}
	
	this.moveOnDeck	= function(obj,type){
		var sWin	= this.sWin;
		var cObj	= getObj(obj);
		
		if(type == 1){
			// left side
			cObj.style.left	= -this.itemWidth + 'px';			
		}
		else{
			// right side
			cObj.style.left	= (this.itemWidth * this.showItems) + 'px';			
		}
		
	}	
	
	this.autoScroll	= function(){
		if(this.autoScrollEnabled && this.autoScrolling && this.scrolling && !this.amScrolling){				
			this.indexLeft();
		}		
	}		
	
}