// Global collection of instances for functions that need global access
window.__ImageSwitchers__ = [];

/**
 * Image source switcher
 *
 * Waits until image is loaded before displaying.
 * Crossfade transition filter applied in IE >= 5.5 Win only.
 *
 * @param HTMLImageElement	reference to subject
 * @param array				array containing image source paths
 * @param integer			optional, time in seconds to display each image, defaults to 4
 * @param integer			optional, time in seconds to play transition filter, defaults to 2
 * @return null|false
 */
function ImageSwitcher(img, img_paths, pause_duration, transition_duration)
{
	if (!img)
		return false;
	if (typeof img == 'string')
		var img = document.getElementById(img);
	this.img = img;

	if (typeof img_paths != 'undefined')
		this.img_paths = img_paths;
	if (typeof pause_duration != 'undefined')
		this.pause_duration = pause_duration;
	if (typeof transition_duration != 'undefined')
		this.transition_duration = transition_duration;

	// IE version
	if (document.all) {
		var ua = navigator.userAgent.toLowerCase(), start;
		start = ua.indexOf('msie') + 1;
		this._ie_ver = ua.substr(start + 'msie'.length, 3);
	}

	// Store a reference to this instance globally for setTimeout()
	this._id = window.__ImageSwitchers__.length;
	window.__ImageSwitchers__[this._id] = this;
}

ImageSwitcher.prototype = {

	// Image object reference
	img : null,
	debug : false,
	// list of image paths
	img_paths : null,
	pause_duration : 4,
	transition_duration : 2,
	// unique id to distinguish instances
	_id : null,
	// collection of image objects
	_imgs : [],
	_count : 0,
	// Internet Explorer version id
	_ie_ver : -1,

	/**
	 * Switches image source to next in list and applies
	 * visual transition effects
	 */
	next : function()
	{
		this.startTransition();
		this.switchSource();
		this.endTransition();

		window.setTimeout(
			'window.__ImageSwitchers__[' + this._id + '].next()',
			this.pause_duration * 1000
		);
	},

	startTransition : function()
	{
		// IE 5.5+ only
		if (document.all && this._ie_ver > 5) {
		   this.img.style.filter = 'blendTrans(duration=' + this.transition_duration + ')';
		   this.img.filters.blendTrans.Apply();
		}
	},

	switchSource : function()
	{
		var key = (this._count + 1) % this.img_paths.length, src;
		if (this._imgs[key] && (this._imgs[key].complete || this._imgs[key].complete == null))
			this.img.src = this._normalizeUrl(this._imgs[this._count = key].src);

		this._imgs[key = (this._count + 1) % this.img_paths.length] = new Image;
		this._imgs[key].src = this.img_paths[key];

		if (this.debug)
			alert('ImageSwitcher switched source of image "' + this.img.id + '" source "' + this.img.src + '"');
	},

	endTransition : function()
	{
		// IE 5.5+ only
		if (document.all && this._ie_ver > 5)
			this.img.filters.blendTrans.Play();
	},

	// Strip current location (full)
	_normalizeUrl : function(url)
	{
		return url.replace(new RegExp(document.location.href), '/');
	}

}
