var arcSlider = new Array();

function getArcDimention(obj){
		if ( obj.currentStyle ) {
			return {height:obj.currentStyle.height,width:obj.currentStyle.width};
		} else if ( document.defaultView.getComputedStyle ){
				return {height:document.defaultView.getComputedStyle(obj,'').height,width:document.defaultView.getComputedStyle(obj,'').width};
		} else {
			return {height:obj.style.height,width:obj.style.width};
		}
	}

function sideArcScroller(ID,group,isCont){
	this.ID = ID;
	this.group = group;
	this.spacing = 0;
	this.scrollerObject = document.getElementById(this.ID);
	this.viewportDimensions = getArcDimention(this.scrollerObject)
	this.viewPortHeight = this.viewportDimensions.height;
	this.viewPortWidth = this.viewportDimensions.width;
	this.blocks = new Array();
	this.speed = (isCont)? 10:20;
	this.origspeed = this.speed
	this.stopFlag = new Array(false,false);
	this.timers = new Array(null,null)
	this.waiting = null;
	this.init = init;
	this.divs = this.scrollerObject.getElementsByTagName("div");
	this.theorder = new Array()
	this.stopMoving = stopMoving;
	this.resetStop = resetStop;
	this.beginMove = beginMove;
	this.simMove = simMove;
	this.origdelay = 1
	this.targetdelay = this.origdelay;
	this.maxdelay = 50;
	this.iscontinous = isCont;
	this.currentChange = 0;
	this.targetChange = parseInt(this.viewPortWidth);
	this.howmanytoshow = 0;
	this.init();



	function init(){
		this.scrollerObject.innerHTML = '<div id="wrapper" style="width:15000px; position:relative; ">'+this.scrollerObject.innerHTML+"</div>";
		var count = 0;
		for(var k=0;k<this.divs.length;k++){
			if(this.divs[k].id.indexOf(group)==0){

				this.theorder.push(count);
				this.divs[k].style.position = "absolute";
				this.divs[k].style.top = "0px";

				if(count==0){
					this.divs[k].xpos = 0;
					this.divs[k].style.left =	 "0px";
				}else{
					this.divs[k].xpos = (parseInt(this.blocks[this.blocks.length-1].style.left) +  parseInt(this.blocks[this.blocks.length-1].style.width)) +this.spacing;
					this.divs[k].style.left = this.divs[k].xpos+"px";
				}

				var tmp = getArcDimention(this.divs[k])
				if(tmp.width!=undefined){
					this.divs[k].style.width = parseInt(tmp.width)+"px";
				}else{
					this.divs[k].style.width = parseInt(this.divs[k].offsetWidth)+"px";
				}

				if(count==0)
					this.howmanytoshow = parseInt(this.targetChange/parseInt(this.divs[k].style.width));
				count++;
				this.blocks.push(this.divs[k]);

			}
			this.last = this.blocks.length-1;

		}
	}

	function resetStop(direction){
		var index = (direction > 0 )? 1:0;
		this.stopFlag[index] = false;
	}

	function stopMoving(direction){
		var index = (direction > 0 )? 1:0;
		this.stopFlag[index] = true;
	}

     function beginMove(direction){


		if(!this.iscontinous){
			if(this.currentChange > 0){
				if (this.waiting==null) this.waiting =  window.setTimeout("arcSlider['"+this.ID+"'].beginMove("+direction+")",500);
			}else{
				if(direction > 0) this.currentIndex=1
				this.waiting = null;
				this.resetStop(direction);
				this.simMove(direction)
			}
		}else{
			for(var t=0;t < this.timers.length;t++ )
			window.clearTimeout(this.timers[t]);
			this.resetStop(direction);
			this.simMove(direction)
		}
	 }


	 function simMove(direction){
		var index = (direction > 0 )? 1:0;
		if(!this.iscontinous){
			var target =  this.targetChange;
			if ((this.currentChange+this.speed) > target){
				this.speed =target - this.currentChange;
			}
		}
		var index2 = null;
		var obj = null;
		for(var i=0;i<this.theorder.length;i++){
			index2 = (direction < 0 )? i: this.theorder.length - i - 1;
			obj = this.blocks[this.theorder[index2]];
			obj.xpos = parseInt(obj.xpos)+ (direction*this.speed);
		}
		var newOrder = this.theorder.concat([])
		var tmp = null;
		for(var i=0;i<newOrder.length;i++){
			index2 = (direction < 0 )? i: newOrder.length - i - 1;
			obj = this.blocks[newOrder[index2]];
			if(direction < 0){
				if ((obj.xpos + parseInt(obj.style.width)) < 0 ) {
					obj.xpos = 	this.blocks[this.theorder[this.theorder.length-1]].xpos + parseInt(this.blocks[this.theorder[this.theorder.length-1]].style.width)
					tmp = this.theorder.shift();
					this.theorder.push(tmp)
				}

			}else{
				if (obj.xpos  > parseInt(this.viewPortWidth)) {
					obj.xpos =this.blocks[this.theorder[0]].xpos-parseInt( obj.style.width);
					tmp = this.theorder.pop();
					this.theorder.unshift(tmp)
				}

			}
			obj.style.left = obj.xpos + "px";
		}

		if(this.iscontinous){
			if(!this.stopFlag[index]||(this.stopFlag[index]&&this.targetdelay < this.maxdelay)){
				if(this.stopFlag[index]&&this.targetdelay < this.maxdelay){
					this.targetdelay =this.targetdelay*1.4;
				}
				this.timers[index] = window.setTimeout("arcSlider['"+this.ID+"'].simMove("+direction+")",this.targetdelay);
			}else{
				this.targetdelay = this.origdelay;
			}
		}else{
			if((this.currentChange+this.speed) < target ){
				this.currentChange+=this.speed;
				this.timers[index] = window.setTimeout("arcSlider['"+this.ID+"'].simMove("+direction+")",this.origdelay);
			}else{
				this.speed  = this.origspeed
				this.currentChange = 0;
			}
		}
	}

}
