/**
 * @Class: PromoRotator
 * @Description: Changes out an image and a link with new values you set.
 * @Author: Paul McLanahan <paul dot mclanahan at digital insight>
 * @Copyright: 2006 Digital Insight Corporation - All Rights Reserved
 * @Usage: Link this file to your page then add the following to the bottom of the page.
 *		<script type="text/javascript">
 *			var mypr = new PromoRotator('imgID',delay,'varName');
 *			mypr.addPromo('path/to/image.jpg','link/text.html','Alt text for image');
 *			mypr.start();
 *		</script>
 *		For 'new PromoRotator()':
 *			imgID is the id='imgID' from the image tag.  THIS MUST BE UNIQUE!
 *			delay is the ammount of time you wish to pass before the next image is displayed in seconds.
 *				Set delay to 0 to make the promo a "random on refresh" promo instead of a timed rotator.
 *			varName must be the same as the variable name you gave to the new PromoRotator object ('mypr' in this example).  This is due to JS not being a real OO language.
 *		For 'mypr.addPromo()':
 *			path/to/image.jpg is pretty self-explainatory
 *			link/text.html is also pretty obvious.  The can be a relative, absolute, or JS link.
 *			"Alt text for image" is just that.  I added it because IE shows that annoying yellow popup of the alt text, and for compliance.  If you don't add anything, it'll use "Promotion" for the alt text.
 *		The script will automatically add the image and link coded into the HTML as the first Promo, you only need to call mypr.addPromo() for any other images you want.
 */
function PromoRotator(imgID,delay,varName){
	if(!document.getElementById)return;
	if(typeof jQuery == 'undefined'){
		alert('This script requires that jquery.js be loaded first.');
		return;
	}
	// properties
	this.index = 0;
	this.varName = varName;
	this.promos = new Array();
	this.imgObj = document.getElementById(imgID);
	this.lnkObj = $(this.imgObj).parent('a')[0];
	delay = parseFloat(delay);
	this.delay = isNaN(delay)?8000:delay*1000;
	this.debug = false;
	this.isOver = false;
		
	// methods
	this.addPromo = function(imgURI,lnkHREF,altText){
		tmp = new Promo(imgURI,lnkHREF,altText);
		this.promos.push(tmp);
		if(this.debug)alert(tmp.toString());
	};
	this.swap = function(){
		this.index++;
		if(this.timerId)window.clearTimeout(this.timerId);
		if(this.index >= this.promos.length)this.index=0;
		if(this.index < 0)this.index = this.promos.length -1;
		this.imgObj.src = this.promos[this.index].imgObj.src;
		this.imgObj.alt = this.promos[this.index].altText;
		this.lnkObj.href = this.promos[this.index].lnkHREF;
		this.nextSwap();
	};
	this.nextSwap = function(){
		if(this.delay > 0 && this.promos.length > 1 && !this.isOver){
			this.timerId = window.setTimeout(this.varName+".swap()",this.delay);
			return true;
		}
		else{
			return false;
		}
	};
	this.start = function(){
        	this.index = Math.floor(Math.random()*this.promos.length);
        	this.swap();
	};
	this.skipForward = function(){
		//this.index++;
		this.swap();
		return false;
	};
	this.skipBack = function(){
		this.index -= 2;
		this.swap();
		return false;
	};
	this.show = function(i){
		this.index = i;
		this.isOver = true;
		this.swap();
	};
	this.go = function(){
		document.location.href = this.promos[this.index].lnkHREF;
		return false;
	};
	this.out = function(){
		this.isOver = false;
		this.nextSwap();
	};
	this.over = function(){
		this.isOver = true;
		if(this.timerId)window.clearTimeout(this.timerId);
	};
	// add the pre-coded value of the image to the array
	// this.addPromo(this.imgObj.src,this.lnkObj.href,this.imgObj.alt);
}

function Promo(imgURI,lnkHREF,altText){
    // set the object's values
	
	this.imgURI = fiImages + imgURI;
	/*this.lnkHREF = (/\.(pdf|doc|txt|xls|ppt)$/i.test(lnkHREF)?fiDocs + '/':contextPath)+lnkHREF;*/
	if(/\.(pdf|doc|txt|xls|ppt)$/i.test(lnkHREF)){
		this.lnkHREF = fiDocs + '/' + lnkHREF;
	}
	else if(/^(http|javascript|mailto)/i.test(lnkHREF)){
		this.lnkHREF = lnkHREF;
	}
	else
	{
		this.lnkHREF = contextPath + '/' + lnkHREF;
	}
	
	this.altText = altText==''||altText==null?'Promotion':altText;
	
    // preload the images
    this.imgObj = new Image();
    this.imgObj.src = this.imgURI;
	
	// debugging function
	this.toString = function(){
		return "Image = "+this.imgURI+"\nLink = "+this.lnkHREF+"\nAlt = "+this.altText;
	}
	
}
