
var tran =
{
	'clock'	: null,
	'fade'	: true,
	'count'	: 1
};

var tm = null;
var ttime = 30;

tran.imgs = [];
tran.caps = [];

function traninit (ttm, pix, caps)
{
	ttime = ttm;

	tran.imgs = pix;
	tran.caps = caps;

	// Cache the images

	tran.imgsLen = tran.imgs.length;
	tran.cache = [];

	for (var i = 0; i < tran.imgsLen; i++)
	{
		tran.cache [i] = new Image;
		tran.cache [i].src = tran.imgs [i];
		tran.cache [i].alt = tran.caps [i];
	}
}

// Do random imgTranss

var lastImgNo = -1;

function doTrans (id)
{
	var testObj = document.getElementById (id);
	clearTimeout (tm);

	// Pick an image

	do
	{
		imgNo = rand (0, tran.imgs.length - 1);
	} while (imgNo == lastImgNo);

	var im = tran.imgs [imgNo];
	var cap = tran.caps [imgNo];

	lastImgNo = imgNo;

	// Randomly choose a imgTrans

	var tt = rand (0, 1);

	var fade = Math.random () * 2;

	switch (tt)
	{
		case 0 : // Cross wipe
			var sda = ['lr', 'rl', 'tb', 'bt', 'tlbr', 'trbl', 'bltr', 'brtl', 'cve', 'che', 'cc'];
			var sd = sda [rand (0, sda.length - 1)]
			imgTrans ('w', testObj, im, fade, sd, cap);
			break;

		case 1 : // Cross fade
			imgTrans ('c', testObj, im, fade, '', cap);
			break;

		case 2 : // Swap fade
			while (fade > 2) fade--;

			imgTrans ('s', testObj, im, fade, '', cap);
			break;
	}

	tm = setTimeout ("doTrans('" + id + "')", 1000 * ttime);
}

// imgTrans setup
//	tt =	'w' (crosswipe)
//			'f' (crossfade)
//			's' (swapfade)

function imgTrans (tt, ob, imgsrc, fadeDuration, slideDir, imgalt)
{
	// If the timer is not already going

	if (tran.clock == null)
	{
		// Copy the imgTrans type

		tran.tt = tt;

		// Copy the image object

		tran.obj = ob;

		// Get its dimensions

		tran.size =
		{
			'w' : tran.obj.width,
			'h' : tran.obj.height
		};

		if (tran.size.w < 10)
		{
			tran.size.w = 620;
			tran.size.h = 221;
		}

//alert ([tran.obj.width, tran.obj.height]);
		// Change the image alt text if defined

		if (typeof imgalt != 'undefined' && imgalt != '')
		{
			tran.obj.alt = tran.obj.title = imgalt;
			document.getElementById ("headercap").innerHTML = imgalt;
		}

		// Copy the image source

		tran.src = imgsrc;

		// Store the supported form of opacity

		if (typeof tran.obj.style.opacity != 'undefined')
		{
			tran.type = 'w3c';
		}
		else
		if (typeof tran.obj.style.MozOpacity != 'undefined')
		{
			tran.type = 'moz';
		}
		else
		if (typeof tran.obj.style.KhtmlOpacity != 'undefined')
		{
			tran.type = 'khtml';
		}
		else
		if (typeof tran.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

			tran.type = (tran.obj.filters.length > 0 && typeof tran.obj.filters.alpha == 'object' &&
				typeof tran.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		}
		else
		{
			tran.type = 'none';
		}

		// If any kind of opacity and dynamic element creation is supported

		if (tran.type != 'none' && (typeof document.createElementNS != 'undefined' || typeof document.createElement != 'undefined'))
		{
			if (tran.tt != 's')
			{
				// Create a new image object and append it to body
				// Detecting support for namespaced element creation, in case we're in the XML DOM

				tran.newimg = document.getElementsByTagName('body')[0].appendChild((typeof document.createElementNS != 'undefined') ?
					document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img'));

				// Set positioning classname

				tran.newimg.className = 'idupe';

				// Set src to new image src

				tran.newimg.src = tran.src

				// Move it to superimpose original image

				tran.newimg.style.left = tran.getRealPosition(tran.obj, 'x') + 'px';
				tran.newimg.style.top = tran.getRealPosition(tran.obj, 'y') + 'px';

				if (tran.tt == 'w')
				{
					// Set it to be completely hidden with clip

					tran.newimg.style.clip = 'rect(0, 0, 0, 0)';

					// Show the image

					tran.newimg.style.visibility = 'visible';
				}
			}

			// Copy and convert fade duration

			tran.length = parseInt (parseFloat (fadeDuration, 10) * 1000);

			// Create fade resolution as 20 steps per imgTrans

			tran.resolution = parseInt (parseFloat (fadeDuration, 10) * 20);

			if (tran.tt == 'w')
			{
				// Copy slide direction

				tran.dir = slideDir;
			}

			// Start the timer

			switch (tran.tt)
			{
				case 'c' : // Cross fade
					tran.clock = setInterval ('tran.crossfade()', tran.length / tran.resolution);
					break;

				case 'w' : // Cross wipe
					tran.clock = setInterval ('tran.crosswipe()', tran.length / tran.resolution);
					break;

				case 's' : // Swap fade
					tran.clock = setInterval ('tran.swapfade()', tran.length / tran.resolution);


			}
		}
		else	// Otherwise if opacity is not supported
		{
			//just do the image swap
			tran.obj.src = tran.src;
		}
	}
};


// Crossfade timer function

tran.crossfade = function()
{
	// Decrease the counter on a linear scale

	tran.count -= (1 / tran.resolution);

	// If the counter has reached the bottom

	if (tran.count < (1 / tran.resolution))
	{
		// Clear the timer

		clearInterval (tran.clock);
		tran.clock = null;

		// Reset the counter

		tran.count = 1;

		//set the original image to the src of the new image

		tran.obj.src = tran.src;
	}

	// Set new opacity value on both elements using whatever method is supported

	switch (tran.type)
	{
		case 'ie' :
			tran.obj.filters.alpha.opacity = tran.count * 100;
			tran.newimg.filters.alpha.opacity = (1 - tran.count) * 100;
			break;

		case 'khtml' :
			tran.obj.style.KhtmlOpacity = tran.count;
			tran.newimg.style.KhtmlOpacity = (1 - tran.count);
			break;

		case 'moz' :
			// Restrict max opacity to prevent a visual popping effect in firefox

			tran.obj.style.MozOpacity = (tran.count == 1 ? 0.9999999 : tran.count);
			tran.newimg.style.MozOpacity = (1 - tran.count);
			break;

		default :
			// Restrict max opacity to prevent a visual popping effect in firefox

			tran.obj.style.opacity = (tran.count == 1 ? 0.9999999 : tran.count);
			tran.newimg.style.opacity = (1 - tran.count);
	}

	// Now that we've gone through one fade iteration we can show the image that's fading in

	tran.newimg.style.visibility = 'visible';

	// Keep new image in position with original image in case text size changes mid imgTrans or
	// something

	tran.newimg.style.left = tran.getRealPosition (tran.obj, 'x') + 'px';
	tran.newimg.style.top = tran.getRealPosition (tran.obj, 'y') + 'px';

	// If the counter is at the top, which is just after the timer has finished

	if (tran.count == 1)
	{
		// Remove the duplicate image

		tran.newimg.parentNode.removeChild (tran.newimg);
	}
};

// Crosswipe timer function

tran.crosswipe = function ()
{
	// Decrease the counter on a linear scale

	tran.count -= (1 / tran.resolution);

	// If the counter has reached the bottom

	if (tran.count < (1 / tran.resolution))
	{
		// Clear the timer

		clearInterval (tran.clock);
		tran.clock = null;

		// Reset the counter

		tran.count = 1;

		// Set the original image to the src of the new image

		tran.obj.src = tran.src;
	}

	// Animate the clip of the new image using the width and height properties we saved earlier

	tran.newimg.style.clip = 'rect('
		+ ((/bt|bltr|brtl/.test(tran.dir)) ? (tran.size.h * tran.count) : (/che|cc/.test(tran.dir)) ? ((tran.size.h * tran.count) / 2) : (0))
		+ 'px, '
		+ ((/lr|tlbr|bltr/.test(tran.dir)) ? (tran.size.w - (tran.size.w * tran.count)) : (/cve|cc/.test(tran.dir)) ? (tran.size.w - ((tran.size.w * tran.count) / 2)) : (tran.size.w))
		+ 'px, '
		+ ((/tb|tlbr|trbl/.test(tran.dir)) ? (tran.size.h - (tran.size.h * tran.count)) : (/che|cc/.test(tran.dir)) ? (tran.size.h - ((tran.size.h * tran.count) / 2)) : (tran.size.h))
		+ 'px, '
		+ ((/lr|tlbr|bltr/.test(tran.dir)) ? (0) : (/tb|bt|che/.test(tran.dir)) ? (0) : (/cve|cc/.test(tran.dir)) ? ((tran.size.w * tran.count) / 2) : (tran.size.w * tran.count))
		+ 'px)';

	// Keep new image in position with original image in case text size changes mid imgTrans or
	// something

	tran.newimg.style.left = tran.getRealPosition (tran.obj, 'x') + 'px';
	tran.newimg.style.top = tran.getRealPosition (tran.obj, 'y') + 'px';

	// If the counter is at the top, which is just after the timer has finished

	if (tran.count == 1)
	{
		// Remove the duplicate image

		tran.newimg.parentNode.removeChild (tran.newimg);
	}
};

// Swapfade timer function

tran.swapfade = function ()
{
	// Increase or reduce the counter on an exponential scale

	tran.count = (tran.fade) ? tran.count * 0.9 : (tran.count * (1 / 0.9));

	// If the counter has reached the bottom

	if (tran.count < (1 / tran.resolution))
	{
		// Clear the timer

		clearInterval (tran.clock);
		tran.clock = null;

		// Do the image swap

		tran.obj.src = tran.src;

		// Reverse the fade direction flag

		tran.fade = false;

		// Restart the timer

		tran.clock = setInterval ('tran.swapfade()', tran.length / tran.resolution);
	}

	// If the counter has reached the top

	if (tran.count > (1 - (1 / tran.resolution)))
	{
		// Clear the timer

		clearInterval (tran.clock);
		tran.clock = null;

		// Reset the fade direction flag

		tran.fade = true;

		// Reset the counter

		tran.count = 1;
	}

	// Set new opacity value on element using whatever method is supported

	switch (tran.type)
	{
		case 'ie' :
			tran.obj.filters.alpha.opacity = tran.count * 100;
			break;

		case 'khtml' :
			tran.obj.style.KhtmlOpacity = tran.count;
			break;

		case 'moz' :
			//restrict max opacity to prevent a visual popping effect in firefox
			tran.obj.style.MozOpacity = (tran.count == 1 ? 0.9999999 : tran.count);
			break;

		default :
			//restrict max opacity to prevent a visual popping effect in firefox
			tran.obj.style.opacity = (tran.count == 1 ? 0.9999999 : tran.count);
	}
};


// Get real position method

tran.getRealPosition = function (ob, coord)
{
	this.pos = (coord == 'x') ? ob.offsetLeft : ob.offsetTop;
	this.tmp = ob.offsetParent;

	while (this.tmp != null)
	{
		this.pos += (coord == 'x') ? this.tmp.offsetLeft : this.tmp.offsetTop;
		this.tmp = this.tmp.offsetParent;
	}

	return this.pos;
};

function rand (from, to)
{
	return parseInt (from + Math.round (Math.random () * (to - from)));
}


