
/*
The promo scroller method used on the homepage content area
*/
var promoScroller = function($) {
	var scrollerActiveIndex = 0;
	var scrollerContentArr = new Array();
	var scrollerContentWidth = 0;
	var delay = 4000; // default delay in miliseconds
	var timerID = null;

	var timerInit = function() {
		timerID = window.setTimeout("promoScroller.autoScroll()", delay);
	};

	var left = function() {
		window.clearTimeout(timerID);
		//initialize and get widths
		var activeEl = scrollerContentArr[scrollerActiveIndex];
		var newIndex = scrollerActiveIndex - 1;
		if (newIndex < 0) {
			//proceed to the last item in the array
			newIndex = scrollerContentArr.length - 1;
		}
		var newEl = scrollerContentArr[newIndex];

		//move the new el into position just off to the left (the width of the active el)
		newEl.css("left", "-" + scrollerContentWidth + "px");
		newEl.show();

		//animate the current el off to the right
		activeEl.animate({ "left":"+=" + scrollerContentWidth + "px" });

		//animate the new el in from the left
		newEl.animate({ "left":"+=" + scrollerContentWidth + "px" });

		scrollerActiveIndex = newIndex;
		timerInit();
	};

	var right = function() {
		window.clearTimeout(timerID);
		//initialize and get widths
		var activeEl = scrollerContentArr[scrollerActiveIndex];
		var newIndex = scrollerActiveIndex + 1;
		var newEl = scrollerContentArr[newIndex];
		if (newEl == null) {
			//go back to 0 and start over
			newIndex = 0;
			newEl = scrollerContentArr[newIndex];
		}

		//move the new el into position just off to the right (the width of the active el)
		newEl.css("left", scrollerContentWidth + "px");
		newEl.show();

		//animate the current el off to the left
		activeEl.animate({ "left":"-=" + scrollerContentWidth + "px" });

		//animate the new el in from the right
		newEl.animate({ "left":"-=" + scrollerContentWidth + "px" });

		scrollerActiveIndex = newIndex;
		timerInit();
	};

	return {
		autoScroll : function() {
			right();
		},

		init : function(firstId,delayInSecs) {
			//if delay is not specified, used the default delay of 15 seconds
			if (delayInSecs > 0)
				delay = delayInSecs * 1000;

			//get the content key and track the active index
			var key = firstId.slice(0,firstId.length - 1);

			//find siblings and build array
			var testEl = $("#" + firstId);
			scrollerContentWidth = testEl.width(); //set width for animations while we are here
			while ( testEl.attr("id") && testEl.attr("id").indexOf(key) > -1 ) {
				scrollerContentArr.push(testEl);
				if (testEl.next())
					testEl = testEl.next();
				else
					break;
			}

			//display and bind scrollers if necessary
			if (scrollerContentArr.length > 1) {
				$(".scroller_l").show();
				$(".scroller_r").show();
				$(".scroller_l a").click(left);
				$(".scroller_r a").click(right);
				timerInit();
			}
		}
	};
}($);



