/**
 * @Class: PromoRotator
 * @Description: Changes out an image and a link with new values you set.
 * @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');
 *		</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.
 *			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;
	
	// properties
	this.index = 1;
	this.varName = varName;
	this.promos = new Array();
	this.imgObj = document.getElementById(imgID);
	this.lnkObj = getParent(this.imgObj,'a');
	this.delay = parseInt(delay)*1000;
	this.debug = false;
	
	// methods
	this.addPromo = function(imgURI,lnkHREF,altText,imgMap){
		tmp = new Promo(imgURI,lnkHREF,altText,imgMap);
		this.promos.push(tmp);
		//if(debug)alert(tmp.toString());
	}
	this.swap = function(){
		if(this.index == this.promos.length)this.index=0;
		this.imgObj.src = this.promos[this.index].imgURI;
		this.imgObj.alt = this.promos[this.index].altText;
		this.imgObj.useMap = this.promos[this.index].imgMap;
		this.lnkObj.href = this.promos[this.index++].lnkHREF;
	}
	// add the pre-coded value of the image to the array
	this.addPromo(this.imgObj.src,this.lnkObj.href,this.imgObj.alt,this.imgObj.useMap);
	intervalID = window.setInterval(this.varName+".swap()",this.delay);
}

function Promo(imgURI,lnkHREF,altText,imgMap){
	this.imgURI = imgURI;
	this.lnkHREF = lnkHREF;
	this.altText = altText==''||altText==null?'Promotion':altText;
	this.imgMap = imgMap==''||imgMap==null?'':imgMap;
	this.toString = function(){
		return "Image = "+this.imgURI+"\nLink = "+this.lnkHREF+"\nAlt = "+this.altText+"\nImgMap = "+this.imgMap;
	}
}

function getParent(el, pTagName){
	if (el == null) return null;
	else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())	// Gecko bug, supposed to be uppercase
		return el;
	else
		return getParent(el.parentNode, pTagName);
}
