scrollObjects = {};

function scrollObject(divId,iniX,iniY){
	this.id=divId;
	scrollObjects[this.id]=this;
	this.animString="scrollObjects."+this.id;
	this.load(divId,iniX,iniY);
};


scrollObject.prototype.load=function(divId,iniX,iniY){
	if(!document.getElementById)return;
	var scrollingDiv;

	this.scrollingDiv=document.getElementById(divId);
	
	this.scrollingDiv.style.top=this.y=iniY;
	this.scrollingDiv.style.left=this.x=iniX;
	
	this.ready=true;
};


scrollObject.slideDur = 500; // duration of glide

scrollObject.scrollBy = function(divId, x, y, dur, hideWhenStop) {
	if ( scrollObjects[divId] ) scrollObjects[divId].glideBy(x, y, dur, hideWhenStop);
}


scrollObject.prototype.glideBy = function(dx, dy, dur, hideWhenStop) {
  if ( !document.getElementById || this.isMoving ) return;
  this.slideDur = dur || scrollObject.slideDur;
  this.destX = this.destY = this.distX = this.distY = 0;
  this.startX = this.x; this.startY = this.y;
  
  this.distY = dy;
  this.distX = dx;
  
  this.destX = this.startX + this.distX; 
  this.destY = this.startY + this.distY;
  
  this.hideWhenStop = hideWhenStop ;
  
  this.isMoving = true;
  this.per = Math.PI/(2 * this.slideDur);
  this.slideStart = (new Date()).getTime();
  this.aniTimer = setInterval(this.animString + ".doSlide()",10);
}

scrollObject.prototype.doSlide = function() {
	var elapsed = (new Date()).getTime() - this.slideStart;
	if (elapsed < this.slideDur) {
		var x = this.startX + this.distX * Math.sin(this.per*elapsed);
		var y = this.startY + this.distY * Math.sin(this.per*elapsed);
    this.shiftTo(this.scrollingDiv, x, y); 
	} else {
    clearInterval(this.aniTimer); this.isMoving = false;
	this.shiftTo(this.scrollingDiv, this.destX, this.destY);
		if (this.hideWhenStop){
			this.scrollingDiv.style.visibility="hidden";
		}
	}
}

scrollObject.prototype.shiftTo=function(scrollingDiv,x,y){
	if(!scrollingDiv.style)return;
	scrollingDiv.style.left=(this.x=x)+"px";
	scrollingDiv.style.top=(this.y=y)+"px";
};

function initSlideshow(itemWidth) {
	if(document.getElementById){
		slideShowList = document.getElementById('slideshowList');
		slideShowItems = 0 ;
		slideShowChildNodes = slideShowList.childNodes ;
		for (i=0; i<slideShowChildNodes.length; i++){
			if (slideShowChildNodes[i].nodeName == "LI") slideShowItems++ ;
		}
		
		slideLeftButVar=document.getElementById('leftArrow');
		slideRightButVar=document.getElementById('rightArrow');
		
		if (slideShowItems > 4){
			
			slideRightButVar.style.display = "block" ;

		}

		var slideShow = new scrollObject('slideshow', 0, 0);
		
		liWidth = itemWidth;
		
	} 	
}


currentItem = 1 ;
function slider(direction){
	if(!scrollObjects['slideshow'].isMoving){
		tempItem = currentItem + direction ;
		if (tempItem > 0 && tempItem <= slideShowItems - 6) {
			if (tempItem == 1){
				slideLeftButVar.style.display = "none" ;	
			} else {
				slideLeftButVar.style.display = "block" ;
			}
			if (tempItem == slideShowItems - 6){
				slideRightButVar.style.display = "none" ;
			} else {
				slideRightButVar.style.display = "block" ;
			}
			scrollObject.scrollBy('slideshow',-1*direction*liWidth,0);
			currentItem += direction;
		}
	}
}

