function SetupSliders()
{
	$$('div.slider').each(function(slider) {

		slider.setAttribute('current','0');
		slider.setAttribute('currentIdx',0);
		var left=slider.down();
		slider.left=left;
		var center=left.next();
		var items=center.down();
		//I need at least three items for this to work.
		switch(items.children.length)
		{
			case 0:
				return;
			case 1:
				var rep=items.children[0].clone(true);
				var rep2=items.children[0].clone(true);
				items.appendChild(rep);
				items.appendChild(rep2);
				slider.setAttribute('total',3);
				break;
			case 2:
				var rep1=items.children[0].clone(true);
				var rep2=items.children[1].clone(true);
				items.appendChild(rep1);
				items.appendChild(rep2);
				slider.setAttribute('total',4);
				break;
		}
		slider.width=function () {
			return center.getWidth();
		}
		left.onclick=function() {
			if(left.inprogress)
				return ;
			left.inprogress=1;

			var currentPos=parseInt(slider.getAttribute('current'));
			var total=parseInt(slider.getAttribute('total'));
			if(currentPos>0)
			{
				currentPos--;
				var center=left.next();
				var child=center.down();
				new Effect.Move(child,{x:-slider.width()*currentPos, y:0, mode:'absolute', afterFinish: function() {
					var childrenlength=child.children.length;
					var clone=child.children[0].clone(true);
					child.insertBefore(clone,child.children[1]);
					currentPos++;

					new Effect.Move(child,{x:-slider.width()*currentPos, y:0, mode:'absolute',duration:0.0,
						afterFinish: function () {
							var lastchild=child.children[child.children.length-1].remove();
							child.insertBefore(lastchild,child.children[0]);
							child.children[1].remove();
						
						}});
					slider.setAttribute('current',currentPos);
					left.inprogress=0;

				}
				});
				var i=parseInt(slider.getAttribute('currentIdx'));
				i--;
				if(i<0)
				{
					i=parseInt(slider.getAttribute('total'))-1;
				}
				slider.setAttribute('currentIdx',i);
				slider.updateMarkers();

			}
		}
		left.inprogress=0;

		var right=center.next();
		right.onclick=function() {
			if(right.inprogress)
				return;
			right.inprogress=1;
			var currentPos=parseInt(slider.getAttribute('current'));
			var total=parseInt(slider.getAttribute('total'));
			if(currentPos+1<total)
			{
				currentPos++;
				var center=right.previous();
				var child=center.down();
				new Effect.Move(child,{x:-slider.width()*currentPos, y:0, mode:'absolute', afterFinish: function() {
					var childrenlength=child.children.length;
					var clone=child.children[2].clone(true);
					child.insertBefore(clone,child.children[2]);
					currentPos--;

					new Effect.Move(child,{x:-slider.width()*currentPos, y:0, mode:'absolute',duration:0.0,
						afterFinish: function () {
							var firstchild=child.children[0].remove();
							child.children[2].remove();
							child.appendChild(firstchild);

						}});
						slider.setAttribute('current',currentPos);
						right.inprogress=0;

				}	});

				var i=parseInt(slider.getAttribute('currentIdx'));
				i++;
				if(i>=parseInt(slider.getAttribute('total'))){
					i=0;
				}
				slider.setAttribute('currentIdx',i);
				slider.updateMarkers();
			}
		}
		right.inprogress=0;
		slider.right=right;
		var currentPos=parseInt(slider.getAttribute('current'));
		var total=parseInt(slider.getAttribute('total'));
		if(currentPos+1<total)
		{
			currentPos++;
			var center=right.previous();
			var child=center.down();

			var lastchild=child.children[child.children.length-1].remove();
			child.insertBefore(lastchild,child.children[0]);

			new Effect.Move(child,{x:-slider.width()*currentPos, y:0, mode:'absolute', duration:0.0 });
			slider.setAttribute('current',currentPos);

		}

		var controls=new Element('div',{ "class": "slidercontrols" });
		var markers = new Element('div',{ "class": "markers" });
		var playpause = new Element('div', {"class": "playpause" });
		var leftcontrol= new Element('img', { src: '/images/left.gif'});
		var pausecontrol = new Element('img', { src: '/images/pause.gif'});
		var rightcontrol= new Element('img', { src: '/images/right.gif'});
		leftcontrol.onclick=left.onclick;
		rightcontrol.onclick=right.onclick;
		playpause.appendChild(leftcontrol);
		playpause.appendChild(pausecontrol);
		playpause.appendChild(rightcontrol);
		controls.appendChild(markers);
		controls.appendChild(playpause);
		slider.appendChild(controls);
		slider.updateMarkers=function() {
			var currentIdx=parseInt(slider.getAttribute('currentIdx'));
			var total=parseInt(slider.getAttribute('total'));
			markers.innerHTML='';
			var i;
			for (i=0; i<total; i++) {
				if(i==currentIdx)
				{
					markers.appendChild(new Element('img',{src: '/images/dotx.gif'}));
				}
				else
				{
					markers.appendChild(new Element('img',{src: '/images/dot.gif'}));
				}
			}
		};
		slider.timeout=true;
		slider.updateMarkers();
		slider.updateControls=function() {
			if(slider.timeout)
			{
				pausecontrol.src='/images/pause.gif';
			}
			else
			{
				pausecontrol.src='/images/play.gif';
			}
		}
		slider.updateControls();
		pausecontrol.onclick=function() {
			if(slider.timeout)
			{
				slider.timeout=false;
			}
			else
			{
				slider.timeout=true;
			}
			slider.updateControls();
		}
		slider.hideControls=function () {
			leftcontrol.hide();
			rightcontrol.hide();
			pausecontrol.hide();
		}
	});
}
function HideMoveControls()
{
	$$('div.slider').each(function(slider) {
		slider.hideControls();
	});
}
function SetupMoveSliders()
{
	setTimeout('MoveSliders();',6000);
}
function MoveSliders()
{
	$$('div.slider').each(function(slider) {
		if(slider.timeout)
		{
			slider.right.onclick();			
		}
	});
	setTimeout('MoveSliders();',6000);
}


