//object definition
function bannerimage(filename,linkhref,buttontext,imgalt,type) {
	this.linkhref = linkhref;		//href that the link should point to
	this.filename = filename;		//name of banner filename (without the .jpg)
	this.buttontext = buttontext;	//text for the button (also the title attribute of the link)
	this.imgalt = imgalt;			//alt text for the image - should match the text in the image
	this.type = type;}			
//array of bannerimage objects
var banners = [
	new bannerimage(
		'../calendar/calendar.asp',
		'../calendar/calendar.asp',
		'Calendar',
		'',
		'iframe'),
	new bannerimage(
		'/centers/caro/homecoming.asp',
		'banners/0808-homecoming.jpg',
		'Festivities Begin October 6th!',
		'Homecoming Draws Near! Tune In... Pointers Battle Reality TV! UWSP Homecoming 2008.',
		'img')];

//initialized with a value
var waittime = 5000;					//time to wait on a banner
var moveinterval = -1000;				//delay between frames during slide transition
var movepixels = 300;					//pixels to move each frame during transition
var currentbannernum = 0;				//starts with the first banner in the list
var totalwidth = 560;					//width of the banner
var oldside = 0;						//current banner's style.left or style.right is 0px
var newside = totalwidth;				//new banner is entirely out of the frame
var buttonbottomvisible = 3;			//button's style.bottom when visible
var buttonbottomhidden = -33;			//the same when hidden outside the frame
var buttonbottom = buttonbottomvisible;	//button is visible to start
var buttonmovepixels = 12;				//pixels to move button per frame
var curstate = 'play';					//banners starts in the 'play' state

//null initialization
var bannerdiv;							//the frame that contains the banners and animation
var curbanner;							//currently displayed banner
var bannerbutton;						//current banner's button
var rightbannernum;						//next banner number to the right
var rightbanner;						//next banner to the right
var leftbannernum;						//...to the the left
var leftbanner;							//...and this too
var playtimer;							//the setTimeout for the delay between switching banners
var scrolltimer;						//the setInterval for banner transition animation
var buttontimer;						//the setInterval to animate the button motion
var animating;							//which side is currently being transitioned to, if any
var pauseplaybutton;					//the pause/play button that changes when clicked

//on page load
function bannerinit() {
	bannerdiv = document.getElementById('banner');
	var leftbutton = document.getElementById('leftbutton');
		leftbutton.onmouseover = function() {leftbutton.src = 'images/bannernavlefthover.gif';}
		leftbutton.onmouseout = function() {leftbutton.src = 'images/bannernavleft.gif';}
		leftbutton.onclick = leftclick;
	pauseplaybutton = document.getElementById('pauseplaybutton');
		pauseplaybutton.onmouseover = function() {pauseplaybutton.src = 'images/bannernavpausehover.gif';}
		pauseplaybutton.onmouseout = function() {pauseplaybutton.src = 'images/bannernavpause.gif';}
		pauseplaybutton.onclick = pauseplayclick;
	var rightbutton = document.getElementById('rightbutton');
		rightbutton.onmouseover = function() {rightbutton.src = 'images/bannernavrighthover.gif';}
		rightbutton.onmouseout = function() {rightbutton.src = 'images/bannernavright.gif';}
		rightbutton.onclick = rightclick;
	preparenext();}

//button clicks
function leftclick() {
	if (animating == 'left') {
		newside = 0;}
	else if (animating == 'right') {
		curbanner.style.left = '0px';
		bannerdiv.removeChild(rightbanner);
		stopscroll();}
	else {
		clearTimeout(playtimer);
		newbanner('left');}}

function rightclick() {
	if (animating == 'right') {
		newside = 0;}
	else if (animating == 'left') {
		curbanner.style.right = '0px';
		bannerdiv.removeChild(leftbanner);
		stopscroll();}
	else {
		clearTimeout(playtimer);
		newbanner('right');}}
function pauseplayclick() {
	if (curstate == 'play') {
		clearTimeout(playtimer);
		curstate = 'pause';
		pauseplaybutton.src = 'images/bannernavplay.gif';
		pauseplaybutton.onmouseover = function() {pauseplaybutton.src = 'images/bannernavplayhover.gif';}
		pauseplaybutton.onmouseout = function() {pauseplaybutton.src = 'images/bannernavplay.gif';}}
	else {
		curstate = 'play'
		if (!animating) newbanner('right');
		pauseplaybutton.src = 'images/bannernavpause.gif';
		pauseplaybutton.onmouseover = function() {pauseplaybutton.src = 'images/bannernavpausehover.gif';}
		pauseplaybutton.onmouseout = function() {pauseplaybutton.src = 'images/bannernavpause.gif';}}}

//prepare next banners, left and right
function preparenext() {
	curbanner = document.getElementById('currentbanner');
	curbanner.style.left = '';
	curbanner.style.right = '';
	bannerbutton = curbanner.getElementsByTagName('span')[0];
	if (buttonbottom < buttonbottomvisible) buttontimer = setInterval('movebutton("up");', moveinterval);
	rightbannernum = currentbannernum + 1;
	if (rightbannernum == banners.length) rightbannernum = 0;
	rightbanner = preparebanner(rightbannernum);
	rightbanner.style.left = totalwidth + 'px';
	leftbannernum = currentbannernum - 1;
	if (leftbannernum < 0) leftbannernum = banners.length - 1;
	leftbanner = preparebanner(leftbannernum);
	leftbanner.style.right = totalwidth + 'px';
	if (curstate != 'pause') playtimer = setTimeout('newbanner("right");', waittime);}

//prepare a banner div
function preparebanner(num) {
	var newdiv = document.createElement('div');
		newdiv.setAttribute('id', 'animbanner');
		newdiv.title = banners[num].buttontext;
	var link = document.createElement('a');
		link.className = 'bannerlink';
		link.href = banners[num].linkhref;
		link.title = banners[num].buttontext;
		newdiv.appendChild(link);
	var image = document.createElement(banners[num].type);
		image.src = 'banners/' + banners[num].filename;
		image.height = '515';
		image.width = '700';
		image.alt = banners[num].imgalt;
		link.appendChild(image);
	var button = document.createElement('span');
		button.className = 'bannerbutton';
		button.innerHTML = banners[num].buttontext;
		button.style.bottom = buttonbottomhidden + 'px';
		link.appendChild(button);
	return newdiv;}

//transition to next banner (all steps)
function newbanner(side) {
	animating = side;
	clearInterval(buttontimer);
	buttontimer = setInterval('movebutton("down");', moveinterval);}

//move the button
function movebutton(direction) {
	if (direction == 'down') {
		buttonbottom = buttonbottom - buttonmovepixels;
		if (buttonbottom < buttonbottomhidden) buttonbottom = buttonbottomhidden;
		bannerbutton.style.bottom = buttonbottom + 'px';
		if (buttonbottom == buttonbottomhidden) {
			clearInterval(buttontimer);
			slidenext();}}
	else {
		buttonbottom = buttonbottom + buttonmovepixels;
		if (buttonbottom > buttonbottomvisible) buttonbottom = buttonbottomvisible;
		bannerbutton.style.bottom = buttonbottom + 'px';
		if (buttonbottom == buttonbottomvisible) clearInterval(buttontimer);}}

//slide to next banner
function slidenext() {
	if (animating == 'left') {
		bannerdiv.appendChild(leftbanner);
		scrolltimer = setInterval('scroll();', moveinterval);}
	else {
		bannerdiv.appendChild(rightbanner);
		scrolltimer = setInterval('scroll();', moveinterval);}}

//animated transition
function scroll() {
	newside = newside - movepixels;
	oldside = newside - totalwidth;
	if (newside < 0) {
		oldside = 0 - totalwidth;
		newside = 0;}
	if (animating == 'left') {
		leftbanner.style.right = newside + 'px';
		curbanner.style.right = oldside + 'px';}
	else {
		rightbanner.style.left = newside + 'px';
		curbanner.style.left = oldside + 'px';}
	if (newside == 0) {
		clearInterval(scrolltimer);
		oldside = 0;
		newside = totalwidth;
		if (animating == 'left') {
			currentbannernum = leftbannernum;
			document.getElementById('banner').replaceChild(leftbanner, curbanner);
			leftbanner.setAttribute('id', 'currentbanner');}
		else {
			currentbannernum = rightbannernum;
			document.getElementById('banner').replaceChild(rightbanner, curbanner);
			rightbanner.setAttribute('id', 'currentbanner');}
		animating = '';
		preparenext();}}

//go back to previous banner
function stopscroll() {
	clearInterval(scrolltimer);
	oldside = 0;
	newside = totalwidth;
	animating = '';
	preparenext();}

//make sure a previous window.onload is not overwritten
if (window.onload) {
	var previnit = window.onload;
	window.onload = function() {
		previnit();
		bannerinit();}}
else window.onload = bannerinit;
