/*
Banner rotator for stpaul.gov
Author: Glen Fingerholz
Date: 10/27/2010
Modified: 2/28/2011 - Glen Fingerholz
Modified: 3/31/2011 - Glen: Added mouse over and out events, banner rotates onmouseout
*/

var customPath = "http://www.stpaul.gov/web/bannerimages/";
var swapInterval = 5000; //in miliseconds = seconds * 1000
var clockID = null;
var parentElementID = "rotationImage2";
var nextImage = 0;
var bannerList = new Array();

function buildCollection()
{
	//total_rotation_time = bannerList.length * swapInterval 
	//IE: trt = 5 * 7.5 = 37.5 second rotation at 5 items
		

	bannerList[0] = new Banner(customPath + "Black History Month Library widget 2010.jpg", "http://www.sppl.org/black-history-month", "_blank");
	bannerList[1] = new Banner(customPath + "homeworkhelp.jpg", "http://www.sppl.org/services/homework-help", "_blank");
    	bannerList[2] = new Banner(customPath + "Join-Committee-Board-Commission-Banner.jpg", "http://www.stpaul.gov/index.aspx?nid=550", "_self");
	bannerList[3] = new Banner(customPath + "651EmailMasthead4.jpg", "http://the651.com/", "_blank");
	bannerList[4] = new Banner(customPath + "theline_logo.png", "http://www.thelinemedia.com/cities/", "_blank");
	bannerList[5] = new Banner(customPath + "8989_Promo_tile.jpg", "http://www.stpaul.gov/index.aspx?NID=3784", "_self");
}

function Banner(imgURL, httplink, target, title)
{
	this.image = imgURL;
	this.link = httplink;
	this.target = target;
	this.title = title == null ? "Click to learn more" : title;	
	this.getImage = function(){return this.image;};
	this.getLink = function(){return this.link;};
	this.getTarget = function(){return this.target;};
	this.getTitle = function(){return this.title;};
}

function rotate()
{

	var ele = document.getElementById(parentElementID);
	while(ele.childNodes.length > 0)
		ele.removeChild(ele.lastChild);
			
	var img = document.createElement("img");
	var lnk = document.createElement("a");
	var thisBanner = bannerList[nextImage];
	
	lnk.href = thisBanner.getLink();
	lnk.setAttribute("target", thisBanner.getTarget());
	img.src = thisBanner.getImage();
	img.style.border = 0;
	img.setAttribute("title", thisBanner.getTitle());
	img.onmouseover = function() { stop(); }
	img.onmouseout = function() { rotate();start(); }

	//IE does not respond to these
	//img.setAttribute("onmouseover", "stop()");
	//img.setAttribute("onmouseout", "rotate();start()");
	
	ele.appendChild(lnk);
	lnk.appendChild(img);

	nextImage = ((nextImage + 1) >(bannerList.length -1)) ? 0 : (nextImage + 1);	
}

function start()
{
	clockID = setInterval("rotate()", swapInterval);
}

function stop()
{
	clearInterval(clockID);
	clockID = null;
}
