// JavaScript Document
function MagicScroller(targetObj , prev , next){
	this.height = 93;
	this.width = 599;
	this.xIconSpacing = 0;
	// products is an indexed array of associative arrays.  Each position has three values - o (object), x (left) and width
	// x and width are kept here instead of using Moo's get stlye because getComputedStyle is quite slow for some reason.
	// So we cache the values here.

	this.iconScrollSpeed = 0;
	this.iconScrollMultiplier = 8;
	// How quickly setinterval fires
	this.iconUpdateSpeed = 40;
	// Careful about putting the speed adjuster below 1... there's an odd rounding issue
	this.speedAdjuster = 1;
	// The speed we're aiming for
	this.targetSpeed = 1;
	// The minimum the speed will get to (negative too)
	this.slowSpeed = 1;
	// the highest the speed will get to (negative too)
	this.fastSpeed = 8;

	var scrollInt;
	var productsMC = targetObj;
	var prevButton = prev;
	var nextButton = next;
	var products; // array

	this.init = function(){
		var host = this;

		products = new Array();

		var cNode
		var len



		for(a = 0; a < targetObj.childNodes.length; a++){
			cNode = targetObj.childNodes[a];
			// We only want nodes type 1
			if(cNode.nodeType == 1){
				products.push(new Array());
				len = products.length - 1;
				products[len]['o'] = cNode;
				products[len]['width'] = parseInt($(cNode).getStyle('width'));
			}
		}
		// Event Handlers
		if(prevButton){
			prevButton.onmouseover = function(){host.startScrolling();host.makeSpeedFast(false); return false;};
			//prevButton.onclick = function(){host.startScrolling();host.makeSpeedFast(false); return false;};
			prevButton.onmouseout = function(){host.makeSpeedSlow(false);};
			//prevButton.onmouseout = function(){host.stopScrolling();};
		}

		if(nextButton){
			nextButton.onmouseover = function(){host.startScrolling();host.makeSpeedFast(true); return false;};
			nextButton.onmouseout = function(){host.makeSpeedSlow(true); return false;};
			//nextButton.onmouseout = function(){host.stopScrolling(); return false;};
		}


		this.layoutIcons();
	}

	this.layoutIcons = function(){
		var cumX = 0;
		var cMc;

		for(var a = 0; a < products.length; a++){

			cMc = products[a]['o'];
			this._sX(a , cumX);

			cumX += this._gWidth(a) + this.xIconSpacing;
		}

	}

	this.stopScrolling = function(){
		window.clearInterval(scrollInt);
		//iconScrollSpeed - targetSpeed;
	}

	this.scrollIcons = function(){
		if(this.iconScrollSpeed > this.targetSpeed){ this.iconScrollSpeed = Math.max(this.targetSpeed , this.iconScrollSpeed - this.speedAdjuster); }
		if(this.iconScrollSpeed < this.targetSpeed){ this.iconScrollSpeed = Math.min(this.targetSpeed , this.iconScrollSpeed + this.speedAdjuster); }

		var lIcon = this.iconScrollSpeed > 0 ? products[products.length - 1]['o'] : products[0]['o'];
		var lIconNum = this.iconScrollSpeed > 0 ? products.length - 1 : 0;

		var cIcon;
		var sPoint = this.iconScrollSpeed > 0 ? 0 : products.length - 1 ;
		var ePoint = this.iconScrollSpeed > 0 ? products.length + 1  : 0;
		var increment = this.iconScrollSpeed > 0 ? 1 : -1;


		for(var a = sPoint; a != ePoint - 1; a += increment){
			cIcon = products[a]['o'];


			this._sX(a , this._gX(a) - this.iconScrollSpeed);


			if(this.iconScrollSpeed > 0){

				// Right to left
				if(this._gX(a) + (this._gWidth(a)) <= 0){
					this._sX(a ,  this._gX(lIconNum) + this._gWidth(lIconNum) + this.xIconSpacing);
				}
			}else{
				// left to right
				if(this._gX(a) >= this.width){
					this._sX(a , this._gX(lIconNum) - this._gWidth(a) - this.xIconSpacing);
				}
			}

			lIcon = cIcon;
			lIconNum = a;

		}

	}

		this._gX = function(num){
			return parseInt(products[num]['x']);
		}

		this._gWidth = function(num){
			return parseInt(products[num]['width']);
		}

		this._sX = function(num , v){
			products[num]['x'] = v;
			products[num]['o'].style.left = v + 'px';
		}

		this._sWidth = function(num , v){
			products[num]['width'] = v;
			products[num]['o'].style.width = v + 'px';
		}


	this.makeSpeedFast = function(dir){
		if(dir){
			this.targetSpeed = this.fastSpeed;
			return;
		}
		this.targetSpeed = -this.fastSpeed;
	}

	this.makeSpeedSlow = function(dir){
		if(dir){
			this.targetSpeed = this.slowSpeed;
			return;
		}
		this.targetSpeed = -this.slowSpeed;
	}

	this.startScrolling = function(){
		this.stopScrolling();
		var host = this;
		scrollInt = window.setInterval(function(){host.scrollIcons();} , this.iconUpdateSpeed);
	}
}





// JavaScript Document
var scroller;

function initAdvertScroller(){
	sc = $('logoScroller');

	if(sc.childNodes.length > 4){

		for(var a = 0; a < sc.childNodes.length; a++){

			if(sc.childNodes[a].nodeType == 1){
				c = $(sc.childNodes[a]);
				c.style.cssFloat = "none";
				c.style.position = "absolute";

			}
		}
	}

	scroller = new MagicScroller($('logoScroller') , $('logoScrollLeft') , $('logoScrollRight'));
	scroller.width = 599;
	scroller.xIconSpacing = 0;
	scroller.init();
	scroller.startScrolling();
}