//global object
var ixf = { 'clock' : null, 'count' : 1 }
// see html file for imgsLen def - nos images for each dept
// see html file for order def - order for sequence
nosDepts=imgsLen.length;
lenOrder=order.length;
ordIdx=0;
imgIDNo=order[0];

imgIdx = [];
for(var i=0; i<nosDepts; i++) imgIdx[i] = 1;

ixf.cache = []; //create a cache the images


ixf.getRealPosition = function()
{
	this.pos = (arguments[1] == 'x') ? arguments[0].offsetLeft : arguments[0].offsetTop;
	this.tmp = arguments[0].offsetParent;
	while(this.tmp != null)
	{
		this.pos += (arguments[1] == 'x') ? this.tmp.offsetLeft : this.tmp.offsetTop;
		this.tmp = this.tmp.offsetParent;
	}
	
	return this.pos;
};


ixf.crossfade = function() {
	ixf.count -= (1 / ixf.resolution);
	if(ixf.count < (1 / ixf.resolution))
	{
		clearInterval(ixf.clock);
		ixf.clock = null;
		ixf.count = 1;
		ixf.obj.src = ixf.src;
	}
	switch(ixf.type)
	{
		case 'ie' :
			ixf.obj.filters.alpha.opacity = ixf.count * 100;
			ixf.newimg.filters.alpha.opacity = (1 - ixf.count) * 100;
			break;
			
		case 'khtml' :
			ixf.obj.style.KhtmlOpacity = ixf.count;
			ixf.newimg.style.KhtmlOpacity = (1 - ixf.count);
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			ixf.obj.style.MozOpacity = (ixf.count == 1 ? 0.9999999 : ixf.count);
			ixf.newimg.style.MozOpacity = (1 - ixf.count);
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			ixf.obj.style.opacity = (ixf.count == 1 ? 0.9999999 : ixf.count);
			ixf.newimg.style.opacity = (1 - ixf.count);
	}
	ixf.newimg.style.visibility = 'visible';
	ixf.newimg.style.left = ixf.getRealPosition(ixf.obj, 'x') + 'px';
	ixf.newimg.style.top = ixf.getRealPosition(ixf.obj, 'y') + 'px';
	if(ixf.count == 1)
	{
		ixf.newimg.parentNode.removeChild(ixf.newimg);
	}
};


function crossfade() {
	//if the timer is not already going
	if(ixf.clock == null)
	{
		imgID='link'+imgIDNo;
		//copy the image object
		if (document.getElementById(imgID)) {
			ixf.obj=document.getElementById(imgID)
			if (ixf.obj.src.indexOf('.gif')>0) {
				var ending='.gif';
			} else {
				var ending='.jpg';
			}
			nextImage='img'+imgIDNo+imgIdx[imgIDNo];		
			//copy the image src argument
			if (!(ixf.cache[nextImage])) {
				ixf.cache[nextImage] = new Image;
				ixf.cache[nextImage].src = 'images/'+nextImage+ending; //if it hasn't been cached before then cache the image just before changing it
			}
			ixf.src = 'images/'+nextImage+ending;
			if(typeof ixf.obj.style.opacity != 'undefined')
			{
				ixf.type = 'w3c';
			}
			else if(typeof ixf.obj.style.MozOpacity != 'undefined')
			{
				ixf.type = 'moz';
			}
			else if(typeof ixf.obj.style.KhtmlOpacity != 'undefined')
			{
				ixf.type = 'khtml';
			}
			else if(typeof ixf.obj.filters == 'object')
			{
				ixf.type = (ixf.obj.filters.length > 0 && typeof ixf.obj.filters.alpha == 'object' && typeof ixf.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
			}
			else
			{
				ixf.type = 'none';
			}
		
			if(ixf.type != 'none')
			{
				ixf.newimg = document.getElementsByTagName('body')[0].appendChild((typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img'));
				ixf.newimg.className = 'idupe';
				ixf.newimg.src = ixf.src
				ixf.newimg.style.left = ixf.getRealPosition(ixf.obj, 'x') + 'px';
				ixf.newimg.style.top = ixf.getRealPosition(ixf.obj, 'y') + 'px';
				ixf.length = parseInt(2, 10) * 1000;	// set fade time to 2
				ixf.resolution = parseInt(2, 10) * 20;
				ixf.clock = setInterval('ixf.crossfade()', ixf.length/ixf.resolution);
			}
			else
			{
				//just do the image swap
				ixf.obj.src = ixf.src;
			}
			imgIdx[imgIDNo] +=1;
			if (imgIdx[imgIDNo]>imgsLen[imgIDNo]-1) imgIdx[imgIDNo]=0;
		}
		ordIdx +=1;
		if (ordIdx>lenOrder-1) ordIdx=0;
		imgIDNo=order[ordIdx];
	}
};

setInterval(crossfade, 2500);