//SwapFade setup function

var gpClsSwapFade = null;

function clsSwapFade( vLength, vResolution ) {
	var self = this;
	gpClsSwapFade = this;
	
	this.clock = null;
	this.count = 1;
	this.cache = [];
	this.processing = 0;
	this.fade = true;
	
	//self.timelength = vLength * 500;
	//self.resolution = vResolution * 45;
	
	this.timelength = 20;
	this.resolution = 100;
	
	this.cacheImage = function( src ) {
		var cacheLength = this.cache.length;
		this.cache[cacheLength] = new Image;
		this.cache[cacheLength].src = src;
	};
	
	this.swapImage = function( pImg, vSrc ) {
		if( this.processing ) {
			//alert( "Processing" );
			return;
		}
		self.processing = 1;
		
		this.obj = pImg;
		this.src = vSrc;
		
		// Store the supported form of opacity
		if( typeof self.obj.style.opacity != 'undefined' ) {
			this.type = 'w3c';
		} else if(typeof self.obj.style.MozOpacity != 'undefined') {
			this.type = 'moz';
		} else if(typeof self.obj.style.KhtmlOpacity != 'undefined') {
			this.type = 'khtml';
		} else if(typeof self.obj.filters == 'object') {
			this.type = (self.obj.filters.length > 0 && typeof self.obj.filters.alpha == 'object' && typeof self.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		} else {
			this.type = 'none';
		}
		
		// If any kind of opacity is supported
		if(this.type != 'none') {
			this.clock = setInterval('swapfade_timer()', self.timelength);
		} else {
			this.obj.src = vSrc;
			this.processing = 0;
		}
	};
};


//swapfade timer function
function swapfade_timer() {
	var self = gpClsSwapFade;
	
	//increase or reduce the counter on an exponential scale
	//self.count = (true) ? self.count * 0.9 : (self.count * (1/0.9)); 
	if( self.fade ) { 
		self.count = self.count * 0.6;
	} else {
		self.count = self.count * (1/0.6);
	}
	
	//if the counter has reached the bottom
	if( self.count < 0.05 )  {
		self.obj.src = self.src;
		self.fade = false;
	}
	
	//if the counter has reached the top
	if( self.count > 1 ) {
		clearInterval(self.clock);
		self.clock = null;
		self.fade = true;
		self.count = 1;
		self.processing = 0;
	}

	//set new opacity value on element
	//using whatever method is supported
	switch(self.type)
	{
		case 'ie' :
			self.obj.filters.alpha.opacity = self.count * 100;
			break;
			
		case 'khtml' :
			self.obj.style.KhtmlOpacity = self.count;
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			self.obj.style.MozOpacity = (self.count == 1 ? 0.9999999 : self.count);
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			self.obj.style.opacity = (self.count == 1 ? 0.9999999 : self.count);
	}
};



