/**
 * Slideshow support scrolling marque type slidshow.
 * Run the SlideShow.init(id) method to start the slideshow.
 * 
 * @author Martin Reurings
 */
var SlideShow = {
	/**
	 * Initialise the slideshow. Provide the id of the container div.
	 * Expects the first ul to contain the ads and to be a container
	 * that has only 1 line of ads expanding beyond the right bounding
	 * box.
	 * 
	 * @param {String} The id of the slideshow container
	 */
	init:function(id) {
		//Get the ul
		var slideContainer = document.getElementById(id);
		var ss = slideContainer.getElementsByTagName("ul")[0];
		//Get the ads
		var lis = ss.getElementsByTagName("li");
		var lc = null;
		var length = lis.length;
		var count = 0;
		//remove whitespace nodes
		while (count < ss.childNodes.length) {
			var c = ss.childNodes[count];
			if (c.nodeType != 1) {
				ss.removeChild(c);
			} else count++;
		}
		//Duplicate the ads (needed to create a 'virtual' loop)
		for (var i = 0; i < length; i++) {
			lc = lis[i];
			ss.appendChild(lc.cloneNode(true));
		}
		
		//Calculate the reset point
		lc = lc.nextSibling; //Get the very last child
		var end = lc.offsetLeft;
		var start = ss.childNodes[0].offsetLeft;
		//Setup the location objects
		ss.scrollMax = {x:end,y:0};
		ss.scrollPos = {x:0,y:0};
		ss.scrollStart = {x:start, y:0}; //Somehow this made sense when I built this, and it works too :)
		//Start the timer
		ss.timer = false;
		SlideShow.start(id);
		//Stop the show on mouseover
		/*ss.ssMOver = Events.attach(ss, "mouseover", function() {
			SlideShow.stop(id);
		});*/
		//Restart the show on mouseout
		/*ss.ssMOut = Events.attach(ss, "mouseout", function() {
			SlideShow.start(id);
		});*/
		//Cleanup when the page is unloaded
		/*ss.ssCleanup = Events.attach(window, "unload", function() {
			Events.detach(ss.ssMOver);
			Events.detach(ss.ssMOut);
			SlideShow.stop(id);
			Events.detach(ss.ssCleanup);
		});*/
	},
	/**
	 * This is the code-snippet use to scroll the slideshow.
	 * All data is stored on the ul-element so that it only needs an
	 * id to get access.
	 * 
	 * @param {String} The id of the slideshow container 
	 */
	scroll:function(id) {
		var ss = document.getElementById(id).getElementsByTagName("ul")[0];
		var scrollPos = ss.scrollPos;
		var scrollMax = ss.scrollMax;
		var scrollStart = ss.scrollStart;
		if (scrollPos.x==scrollMax.x) {
			scrollPos = {x:scrollStart.x,y:scrollStart.y}; //Reset the scroll-position to the start of the list
		} else {
			scrollPos.x += 1;
		}
		ss.scrollLeft = scrollPos.x;
		ss.scrollPos = scrollPos;
	},
	/**
	 * This code stops the slideshow's animation.
	 * 
	 * @param {String} The id of the slideshow container 
	 */
	stop:function(id) {
		var ss = document.getElementById(id).getElementsByTagName("ul")[0];
		if (ss.timer != false) {
			window.clearInterval(ss.timer);
		}
		ss.timer = false;
	},
	/**
	 * This code restarts the slideshow's animation.
	 * 
	 * @param {String} The id of the slideshow container 
	 */
	start:function(id) {
		var ss = document.getElementById(id).getElementsByTagName("ul")[0];
		if (ss.timer != false) {
			SlideShow.stop(id);
		}
		ss.timer = window.setInterval( function() {
			SlideShow.scroll(id); //Need closure, IE fucks up if I try to do without.
		}, 50 );
	}
}

//Init the slideshow when the page is done loading.
var ssInit = Events.attach(window,"load", function() {
	SlideShow.init("sponsorSlideshow");
	Events.detach(ssInit);
});
