
var swapfade = {
	cache: [],
	
	clock: null,

	count: 1,

	fade: true,

	length: 1000,

	obj: null,

	resolution: 20,

	src: null,

	type: '',

	swap: function() {
		//if the timer is not already going
		if ( swapfade.clock === null ) {
			//copy the image object 
			swapfade.obj = arguments[0];

			//copy the image src argument 
			swapfade.src = arguments[1];

			//store the supported form of opacity
			if ( typeof swapfade.obj.style.opacity != 'undefined') {
				swapfade.type = 'w3c';
			} else {

				if ( typeof swapfade.obj.style.MozOpacity != 'undefined' ) {
					swapfade.type = 'moz';
				} else {

					if ( typeof swapfade.obj.style.KhtmlOpacity != 'undefined') {
						swapfade.type = 'khtml';
					} else {

						if ( typeof swapfade.obj.filters == 'object' ) {
							//weed out win/ie5.0 by testing the length of the filters collection (where filters is an object with no data)
							//then weed out mac/ie5 by testing first the existence of the alpha object (to prevent errors in win/ie5.0)
							//then the returned value type, which should be a number, but in mac/ie5 is an empty string
							swapfade.type = (swapfade.obj.filters.length > 0 && typeof swapfade.obj.filters.alpha == 'object' && typeof swapfade.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
						} else {
							swapfade.type = 'none';
						}
					}
				}
			}

			//change the image alt text if defined
			if ( typeof arguments[3] !== 'undefined' && arguments[3] !== '' ) {
				swapfade.obj.alt = arguments[3];
			}

			//if any kind of opacity is supported
			if ( swapfade.type != 'none' ) {
				//copy and convert fade duration argument 
				//the duration specifies the whole transition
				//but the swapfade is two distinct transitions
				swapfade.length = Math.round(parseInt(arguments[2], 10) * 0.5);

				//create fade resolution argument as 20 steps per transition
				//again, split for the two distrinct transitions
				swapfade.resolution = swapfade.length / 50;

				//start the timer
				swapfade.clock = setInterval(swapfade.update, swapfade.length / swapfade.resolution);
			} else {
				//otherwise if opacity is not supported
				//just do the image swap
				swapfade.obj.src = swapfade.src;
			}
		}
	},

	//swapfade timer function
	update: function() {
		//increase or reduce the counter on an exponential scale
		swapfade.count = (swapfade.fade) ? swapfade.count * 0.9 : (swapfade.count * (1 / 0.9)); 

		//if the counter has reached the bottom
		if ( swapfade.count < (1 / swapfade.resolution) ) {
			//clear the timer
			clearInterval(swapfade.clock);
			swapfade.clock = null;

			//do the image swap
			swapfade.obj.src = swapfade.src;

			//reverse the fade direction flag
			swapfade.fade = false;

			//restart the timer
			swapfade.clock = setInterval(swapfade.update, swapfade.length / swapfade.resolution);
		}

		//if the counter has reached the top
		if ( swapfade.count > (1 - (1 / swapfade.resolution)) ) {
			//clear the timer
			clearInterval(swapfade.clock);
			swapfade.clock = null;

			//reset the fade direction flag
			swapfade.fade = true;

			//reset the counter
			swapfade.count = 1;
		}

		//set new opacity value on element
		//using whatever method is supported
		switch ( swapfade.type ) {
			case 'ie' :
				swapfade.obj.filters.alpha.opacity = swapfade.count * 100;
				break;

			case 'khtml' :
				swapfade.obj.style.KhtmlOpacity = swapfade.count;
				break;

			case 'moz' : 
				//restrict max opacity to prevent a visual popping effect in firefox
				swapfade.obj.style.MozOpacity = (swapfade.count == 1 ? 0.9999999 : swapfade.count);
				break;

			default : 
				//restrict max opacity to prevent a visual popping effect in firefox
				swapfade.obj.style.opacity = (swapfade.count == 1 ? 0.9999999 : swapfade.count);
		}
	}
};

var banner = {
	cache: [],
	
	current: 0,
	
	first: null,

	timer: null,

	init: function() {
		var container,
			img,
			imgs;

		if ( document.getElementById && document.getElementsByTagName ) {
			helper.addEvent(window, 'unload', function() { clearInterval(banner.timer); }, false);
			container = document.getElementById('banner-container');

			if ( container ) {
				imgs = container.getElementsByTagName('img');

				if ( imgs.length > 0 ) {

					for ( var i = imgs.length; i-- > 0; ) {
						img = new Image();
						img.alt = imgs[i].alt;
						img.src = imgs[i].src;
						banner.cache.push(img);
					}

					banner.first = imgs[0];
					banner.current = banner.cache.length - 1;
					banner.timer = setInterval(banner.swap, 5000);
				}
			}
		}
	},

	swap: function() {
		var img;

		clearInterval(banner.timer);
		banner.timer = null;

		if ( banner.first ) {
			
			if ( --banner.current < 0 ) {
				banner.current = banner.cache.length - 1;
			}

			img = banner.cache[banner.current];
			swapfade.swap(banner.first, img.src, 2500, img.alt);
			banner.timer = setInterval(banner.swap, 7500);
		}
	}
};

if ( helper !== undefined ) {
	helper.addEvent(window, 'load', banner.init, false);
}
