/**
 * @Class: PromoRotator
 * @Description: Changes out an image and a link with new values you set.
 * @Author: Paul McLanahan <paul dot mclanahan at digital insight>
 * @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,headTextID,textID){
	if(!document.getElementById)return;
	
	// properties
	this.index = 0;
	this.varName = varName;
	this.promos = new Array();
	this.headerArray = new Array();
	this.textArray = new Array();
	this.imgObj = document.getElementById(imgID);
	this.headObj = document.getElementById(headTextID);
	this.textObj = document.getElementById(textID);
	this.onStObj = document.getElementById('promo1');
	
	this.lnkObj = libDI.getParent(this.imgObj,'a');
	this.delay = parseInt(delay)*1000;
	this.debug = false;
	this.isOver = false;
		
	// methods
	this.addPromo = function(imgURI,lnkHREF,altText,headerText,bodyText){
		tmp = new Promo(imgURI,lnkHREF,altText);
		this.promos.push(tmp);
		this.headerArray.push(headerText);
		this.textArray.push(bodyText);
		if(this.debug)alert(tmp.toString());
	};
	this.swap = function(){
		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;
		
		//Change the header text and body text
		this.headObj.innerHTML = this.headerArray[this.index];
		this.textObj.innerHTML = this.textArray[this.index];
		
		//Set the number to the on state
		this.setOnState(this.index);
		
        if(this.delay > 0 && this.promos.length > 1 && !this.isOver){
            this.timerId = window.setTimeout(this.varName+".swap()",this.delay);
			this.index++;
		}
		
		
	};
	this.setOnState = function(promoNum){
	    promoNum++;
	    var j;
	    var promonumbers = document.getElementsByName('promoNums');
        for (var i = 0; i < promonumbers.length; i++)
        {
            j = i+1;
            promonumbers[i].src = 'fiFiles/static/images/nav_promoSm'+j+'_off.gif';       
        }    
	    
	    onPromo = "promo"+promoNum;
	    onStObj = document.getElementById(onPromo);
	    onStObj.src = 'fiFiles/static/images/nav_promoSm'+promoNum+'_on.gif';
	};
	this.start = function(){
        this.index = 0;
        this.swap();
	};
	this.skipForward = function(){
		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.go_link = function(){
		document.location.href = this.promos[this.index -1].lnkHREF;
		return false;
	};
	this.out = function(){
		this.isOver = false;
		this.timerId = window.setTimeout(this.varName+".swap()",0);
	};
	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){
    // preload the images
    this.imgObj = new Image();
    this.imgObj.src = imgURI;
    
    // set the object's values
	this.imgURI = imgURI;
	/* this.lnkHREF = (/^http:/i.test(lnkHREF)||/^javascript:/i.test(lnkHREF)?'':'')+lnkHREF; */
	if(/\.(pdf|doc|txt|xls|ppt)$/i.test(lnkHREF)){
		this.lnkHREF = fiDocs + '/' + lnkHREF;
	}
	else if(/^(http|javascript|mailto|\/bridge|\/onlineserv)/i.test(lnkHREF)){
		this.lnkHREF = lnkHREF;
	}
	else
	{
		this.lnkHREF = contextPath + '/' + lnkHREF;
	}
	
	this.altText = altText==''||altText==null?'Promotion':altText;
	this.toString = function(){
		return "Image = "+this.imgURI+"\nLink = "+this.lnkHREF+"\nAlt = "+this.altText;
	}
}
