var CS_currentBrand;
var CS_numBrands;
var CS_sliderAnim;
var CS_sliderAnimNone;

//10000 = 10 seconds.
var timeInterval = 3000;
var t = window.setInterval('CS_widget.moveBrands("CS_rightArrow")', timeInterval);
var CS_widget = function() {
	return {
		init : function() {
			CS_numBrands = $$('.CS_brandLogo').length;
			CS_currentBrand = Math.floor(Math.random()*CS_numBrands);
			if (CS_currentBrand == 0)
			{	CS_currentBrand = 1;	}
			
			CS_sliderAnim = new Fx.Style('CS_slider', 'left',{duration:750, wait: true, transition: Fx.Transitions.Quad.easeOut});;
			CS_sliderAnimNone = new Fx.Style('CS_slider', 'left',{duration:0, wait: true, transition: Fx.Transitions.Quad.easeOut});;

			CS_sliderAnimNone.stop();
			CS_sliderAnimNone.start(-CS_currentBrand * 228);

			$('CS_leftArrow').addEvent('click', CS_widget.arrowClick);
			$('CS_rightArrow').addEvent('click', CS_widget.arrowClick);
			CS_widget.updateArrows();
		},
		updateArrows : function() {
			/**
			if(CS_currentBrand == 0) {
				$('CS_leftArrow').setOpacity(.75);
			}
			else {
				$('CS_leftArrow').setOpacity(1);
			}
			if(CS_currentBrand == (CS_numBrands - 1)) {
				$('CS_rightArrow').setOpacity(.75);
			}
			else {
				$('CS_rightArrow').setOpacity(1);
			}
			*/
		},
		arrowClick : function() {
			if (CS_currentBrand == 0)
			{	CS_currentBrand = 1;	}
			var arrow = this.id;
			CS_widget.moveBrands(arrow);		
		},
		moveBrands : function(arrow) {
			if( arrow == "CS_leftArrow") {
				if(CS_currentBrand == 1) 
				{	//if it's the first one, move to the last one.
					CS_currentBrand = CS_numBrands - 1;			
					//use sliderAnimNone because we don't want the "sliding" look
					//alert(CS_currentBrand);
					CS_sliderAnimNone.stop();
					CS_sliderAnimNone.start(-CS_currentBrand * 228);
				}
				else
				{
					--CS_currentBrand;
					CS_sliderAnim.stop();
					//alert(CS_currentBrand);
					CS_sliderAnim.start(-CS_currentBrand * 228);

				}
			}
			else {
			  try {
				if(CS_currentBrand == (CS_numBrands - 1)) 
				{	//if it's the last one, move to the first one
					CS_currentBrand = 1;	
					CS_sliderAnimNone.stop();
					//alert(CS_currentBrand);
					CS_sliderAnimNone.start(-CS_currentBrand * 228);
				}
				else
				{
					++CS_currentBrand;
					CS_sliderAnim.stop();
					//alert(CS_currentBrand);
					CS_sliderAnim.start(-CS_currentBrand * 228);

				}
			  //an error is thrown if this function is called, before the rotator has time to initiate itself.
			  } catch(e) { }
			}
			//log(CS_currentBrand * 228);
			CS_widget.updateArrows();
			window.clearTimeout(t);
			t = window.setInterval('CS_widget.moveBrands("CS_rightArrow")', timeInterval);
		}
	}
}();