function rotator (promos) {this.init(promos);}

rotator.prototype = {

	init : function (promos) {
		
		// image display period
		this.imagePause = 3000;
		// blank display period
		this.blankPause = 200;
		// fade out speed
		this.fadeOutSpeed = 500;
		// fade in speed
		this.fadeInSpeed = 500;
		// image width
		this.imageWidth = 756;
		// image height
		this.imageHeight = 318;
		
		// promo container
		this.promoElm = common.$("major-promo");
		
		// ref to current promo index
		this.currentPromo = 0;
		
		// add preloaded tag
		for(var i=0; i<promos.length; i++){
			promos[i]["preloaded"] = false;
		}
		
		// globalise promos
		this.promos = promos;
		
		// create promo link element
		this.anchorElm = document.createElement("a");
		
		// create promo image element
		this.imageElm = document.createElement("img");
		
		this.imageElm.width = this.imageWidth;
		this.imageElm.height = this.imageHeight;
		
		// add image to anchor
		this.anchorElm.appendChild(this.imageElm);
		// add anchor to container
		this.promoElm.appendChild(this.anchorElm);
		
		// preload first image
		this.preloadImage();
	},
	
	fade : function (direction) {
		if (direction == "out") {
			common.fade(rotator.imageElm, "out", rotator.fadeOutSpeed, "rotator.callback('out')");
		} else if (direction == "in") {
			common.fade(rotator.imageElm, "in", rotator.fadeInSpeed, "rotator.callback('in')");
		}
	},
	
	preloadImage : function () {
		// trial loading blank image
		this.imageElm.src = "/f/t.gif";
		// preload image if necessary
		if (!this.promos[this.getCurrentPromo()]["preloaded"]) {
			this.imageObj = new Image;
			this.imageObj.src = this.promos[this.getCurrentPromo()]["src"];
			this.imageObj.onload = this.onloadImage;
		} else {
			this.onloadImage();
		}
	},
	
	onloadImage : function () {
		// update preloaded tag
		rotator.promos[rotator.getCurrentPromo()]["preloaded"] = true;
		// show image
		rotator.showImage();
	},
	
	getCurrentPromo : function () {
		// return to start if last image is passed
		if (this.currentPromo >= this.promos.length) this.currentPromo = 0;
		return this.currentPromo;
	},
	
	showImage : function () {
		// update anchor
		rotator.anchorElm.href = rotator.promos[this.getCurrentPromo()]["link"];
		// update image
		rotator.imageElm.src = rotator.promos[this.getCurrentPromo()]["src"];
		rotator.imageElm.alt = rotator.promos[this.getCurrentPromo()]["alt"];
		// increment count
		rotator.currentPromo++;
		// fade image in
		rotator.fade("in");
	},
	
	callback : function (direction) {
		if (direction == "out") {
			// blank pause
			this.timeout = setTimeout("rotator.preloadImage()", this.blankPause);
			//rotator.preloadImage();
		} else if (direction == "in") {
			// image pause
			this.timeout = setTimeout("rotator.fade('out')", this.imagePause);
		}		
	}
}