//SlideShow class
function SlideShow() {
	// fields
	this.time = 0;						// time between transitions in ms
	this.fadeDuration = 0;				// length of ie fade's duration in seconds
	this.fadeOverlap = 0;				// length of the filter's overlap in seconds
	this.id = '';						// id of the image element
	this.adNum = 0;						// current image
	this.bannerArray = [];				// array of image links
	this.linkArray = [];					// array of href links
	this.endLoopRedirectUrl = '';		// redirect at end of loop
	this.target = '';					// target window for href links

	this.opacityTimeoutId = 0;			// id to clear timeout for opacity timer
}

// compare two SlideShows
SlideShow.prototype.equals = function(that) {
	return (this.time == that.time &&
			this.id == that.id &&
			this.adNum == that.adNum &&
			this.bannerArray == that.bannerArray &&
			this.linkArray == that.linkArray &&
			this.endLoopRedirectUrl == that.endLoopRedirectUrl &&
			this.target == that.target);
};

// main loop
SlideShow.prototype.nextAd = function() {
	this.redirectIfUrlSet();
	this.incrementAdNum();
	this.setTransition();
	this.preloadNextImage();
	var me = this;
	SlideShow.setTimeout(function() { me.swapTheImage(); });
	this.setTheLink();
	this.playTransition();
};

/////////////////////////////
// nextAd methods

// redirect at the end of the animation if there is a url
SlideShow.prototype.redirectIfUrlSet = function() {
	var redirectUrl = this.endLoopRedirectUrl;
	if(redirectUrl && redirectUrl.length > 0 && this.adNum == this.bannerArray.length - 1) {
		window.location = redirectUrl;
	}
};

// increment or wrap ad index
SlideShow.prototype.incrementAdNum = function() {
	if(this.adNum < this.bannerArray.length - 1) {
		this.adNum++;
	} else {
		this.adNum = 0;
	}
};

// setup the transition for IE
SlideShow.prototype.setTransition = function() {
	if (document.all) {
        this.addFilter();
		this.getImageElement().filters[0].apply();
	} else {
		var image = this.getImageElement();
		var style = image.parentNode.style;
		style.backgroundImage = 'url(' + image.src + ')';		// then set the background
		style.backgroundRepeat = 'no-repeat';
		this.setOpacity(0);
	}
};

// load the image for the frame after this one
SlideShow.prototype.preloadNextImage = function() {
	var newImage = new Image();
	if(this.adNum <  this.bannerArray.length - 1) {
		newImage.src = this.bannerArray[this.adNum + 1];
	}
};

// set the src to the new frame
SlideShow.prototype.swapTheImage = function() {
	var imageElem = this.getImageElement();
	if(imageElem.src) {
		imageElem.src = this.bannerArray[this.adNum];
	} else {
		imageElem.background = this.bannerArray[this.adNum];
	}
};

// set the href to the new frame
SlideShow.prototype.setTheLink = function() {
	if(this.adNum < this.linkArray.length && this.linkArray[this.adNum] && this.linkArray[this.adNum].length > 0) {
	    var thisObj = this;	// 'this' doesn't carry through to the function, but a var will...
	    var clickElem = this.getImageElement();
	    if(clickElem.parentNode.nodeName == "A") {
			clickElem = clickElem.parentNode;
		} else if (clickElem.parentNode.parentNode.nodeName == "A") {
			clickElem = clickElem.parentNode.parentNode;
		}
		clickElem.onclick = function() { thisObj.jump2url(thisObj.target, thisObj.linkArray[thisObj.adNum]); return false; };
		if(clickElem.href) {
			clickElem.href = thisObj.linkArray[thisObj.adNum];
		}
    }
};

// run the fade
SlideShow.prototype.playTransition = function() {
	if (document.all) {
        this.addFilter();
		this.getImageElement().filters[0].play();
    } else {
		var me = this;
		if(this.opacityTimeoutId && this.opacityTimeoutId > 0) {
			SlideShow.clearInterval(this.opacityTimeoutId);
		}
		this.opacityTimeoutId = SlideShow.setInterval(function() { me.opacityFade(0.025); }, this.fadeDuration / 40 * 1000);
    }
};

/////////////////////////////
// helper methods

// add a filter if necessary
SlideShow.prototype.addFilter = function() {
    var imgElem = this.getImageElement();
    if (!imgElem.filters || imgElem.filters.length < 1) {
        var filter = 'progid:DXImageTransform.Microsoft.Fade(duration=' + this.fadeDuration + ',overlap=' + this.fadeOverlap + ');';
        imgElem.style.filter = filter;
    }
};

// replace the image with a div and place the image in the div
SlideShow.prototype.wrapImage = function() {
	var div = document.createElement('div');
	var img = this.getImageElement();
	var imgParent = img.parentNode;
	
	div.id = 'slidesWrapper';
	imgParent.replaceChild(div, img);
	div.appendChild(img);
};

// get the img with the specified id
SlideShow.prototype.getImageElement = function() {
	var results = document.getElementById(this.id);
	if(!results) {
		alert("Couldn't find element with id of " + this.id);
	}
	return results;
};

// redirect to the correct page based on current frame
SlideShow.prototype.jump2url = function(jumpTarget, jumpUrl) {
	if (jumpUrl && jumpUrl.length > 0){
		if (jumpTarget && jumpTarget.length > 0) {
			window.open(jumpUrl,jumpTarget);
		} else {
			window.location = jumpUrl;
		}
	}
};

SlideShow.prototype.setOpacity = function(value) {
	if(value < 0 || value > 1) {
		alert("Opacity must be between 0 and 1, was " + value);
		return;
	}
	
	var image = this.getImageElement();
	image.style.opacity = value;
	image.style.MozOpacity = value;
	image.style.KhtmlOpacity = value;
}

SlideShow.prototype.getOpacity = function() {
	var results = Number(this.getImageElement().style.opacity);
	return results
}

/////////////////////////////
// timing methods

// start the main loop running
SlideShow.prototype.start = function(){
	if(!document.all) {
		this.wrapImage();
	}
	this.setTheLink();
    var me = this;
    SlideShow.setTimeout(function() { me.timedNextAd(); }, this.time);
};

// run nextAd setup a time to run again
SlideShow.prototype.timedNextAd = function()
{
	this.nextAd();

	// run this again in <this.time> ms
	var me = this;
	SlideShow.setTimeout(function() { me.timedNextAd(); }, this.time);
};

SlideShow.prototype.opacityFade = function(amount) {
	if(this.getOpacity >= 1) {
		SlideShow.clearInterval(this.opacityTimeoutId);
	} else {
		var opacity = this.getOpacity() + amount;
		if(opacity >= 1) {
			opacity = 1;
		}
		this.setOpacity(opacity);
	}
}

// simple wrapper for setTimeout that can be overidden
SlideShow.setTimeout = function(func, time) {
    return setTimeout(func, time);
};

SlideShow.clearTimeout = function(id) {
	clearTimeout(id);
};

SlideShow.setInterval = function(func, time) {
	return setInterval(func, time);
};

SlideShow.clearInterval = function(id) {
	clearInterval(id);
};

