/*
	Image Cross Fade Redux
	Version 1.0
	Last revision: 02.15.2006
	steve@slayeroffice.com

	Rewrite of old code found here: http://slayeroffice.com/code/imageCrossFade/index.html
*/

window.addEventListener?window.addEventListener('load',so_init,false):window.attachEvent('onload',so_init);

var d=document, imgs = new Array(), zInterval = null, current_1=0, current_2=0, current_3=0, pause=false;

function so_init()
{
	if(!d.getElementById || !d.createElement)return;

	css = d.createElement('link');
	css.setAttribute('href','http://www.wood-newton.com/js/slideshow2.css');
	css.setAttribute('rel','stylesheet');
	css.setAttribute('type','text/css');
	d.getElementsByTagName('head')[0].appendChild(css);

	try
		{
		imgs_1 = d.getElementById('rotator_1').getElementsByTagName('img');
		imgs_2 = d.getElementById('rotator_2').getElementsByTagName('img');
		imgs_3 = d.getElementById('rotator_3').getElementsByTagName('img');
		}
	catch(e)
		{
		return false;
		}
	
	for(i=1;i<imgs_1.length;i++)
		{
		imgs_1[i].xOpacity = 0;
		}
	imgs_1[0].style.display = 'block';
	imgs_1[0].xOpacity = .99;

	for(i=1;i<imgs_2.length;i++)
		{
		imgs_2[i].xOpacity = 0;
		}
	imgs_2[0].style.display = 'block';
	imgs_2[0].xOpacity = .99;

	for(i=1;i<imgs_3.length;i++)
		{
		imgs_3[i].xOpacity = 0;
		}
	imgs_3[0].style.display = 'block';
	imgs_3[0].xOpacity = .99;

	setTimeout(so_xfade_rand,4000);
}

function so_xfade_rand()
	{
	// pick a random image to change
	var rand=Math.floor(Math.random()*3);
	if (rand == 0)
		{
		so_xfade_1();
		}
	else if (rand == 1)
		{
		so_xfade_2();
		}
	else if (rand == 2)
		{
		so_xfade_3();
		}
	}

function so_xfade_1()
{
	cOpacity_1 = imgs_1[current_1].xOpacity;
	nIndex_1 = imgs_1[current_1+1]?current_1+1:0;
	nOpacity_1 = imgs_1[nIndex_1].xOpacity;

	cOpacity_1-=.05;
	nOpacity_1+=.05;

	imgs_1[nIndex_1].style.display = 'block';
	imgs_1[current_1].xOpacity = cOpacity_1;
	imgs_1[nIndex_1].xOpacity = nOpacity_1;

	setOpacity_1(imgs_1[current_1]);
	setOpacity_1(imgs_1[nIndex_1]);

	if(cOpacity_1<=0)
	{
		imgs_1[current_1].style.display = 'none';
		current_1 = nIndex_1;
		setTimeout(so_xfade_rand,6000);
	}
	else
	{
		setTimeout(so_xfade_1,50);
	}

	function setOpacity_1(obj_1)
	{
		if(obj_1.xOpacity>.99)
		{
			obj_1.xOpacity = .99;
			return;
		}

		obj_1.style.opacity = obj_1.xOpacity;
		obj_1.style.MozOpacity = obj_1.xOpacity;
		obj_1.style.filter = 'alpha(opacity=' + (obj_1.xOpacity*100) + ')';
	}
}

function so_xfade_2()
{
	cOpacity_2 = imgs_2[current_2].xOpacity;
	nIndex_2 = imgs_2[current_2+1]?current_2+1:0;
	nOpacity_2 = imgs_2[nIndex_2].xOpacity;

	cOpacity_2-=.05;
	nOpacity_2+=.05;

	imgs_2[nIndex_2].style.display = 'block';
	imgs_2[current_2].xOpacity = cOpacity_2;
	imgs_2[nIndex_2].xOpacity = nOpacity_2;

	setOpacity_2(imgs_2[current_2]);
	setOpacity_2(imgs_2[nIndex_2]);

	if(cOpacity_2<=0)
	{
		imgs_2[current_2].style.display = 'none';
		current_2 = nIndex_2;
		setTimeout(so_xfade_rand,6000);
	}
	else
	{
		setTimeout(so_xfade_2,50);
	}

	function setOpacity_2(obj_2)
	{
		if(obj_2.xOpacity>.99)
		{
			obj_2.xOpacity = .99;
			return;
		}

		obj_2.style.opacity = obj_2.xOpacity;
		obj_2.style.MozOpacity = obj_2.xOpacity;
		obj_2.style.filter = 'alpha(opacity=' + (obj_2.xOpacity*100) + ')';
	}
}

function so_xfade_3()
{
	cOpacity_3 = imgs_3[current_3].xOpacity;
	nIndex_3 = imgs_3[current_3+1]?current_3+1:0;
	nOpacity_3 = imgs_3[nIndex_3].xOpacity;

	cOpacity_3-=.05;
	nOpacity_3+=.05;

	imgs_3[nIndex_3].style.display = 'block';
	imgs_3[current_3].xOpacity = cOpacity_3;
	imgs_3[nIndex_3].xOpacity = nOpacity_3;

	setOpacity_3(imgs_3[current_3]);
	setOpacity_3(imgs_3[nIndex_3]);

	if(cOpacity_3<=0)
	{
		imgs_3[current_3].style.display = 'none';
		current_3 = nIndex_3;
		setTimeout(so_xfade_rand,6000);
	}
	else
	{
		setTimeout(so_xfade_3,50);
	}

	function setOpacity_3(obj_3)
	{
		if(obj_3.xOpacity>.99)
		{
			obj_3.xOpacity = .99;
			return;
		}

		obj_3.style.opacity = obj_3.xOpacity;
		obj_3.style.MozOpacity = obj_3.xOpacity;
		obj_3.style.filter = 'alpha(opacity=' + (obj_3.xOpacity*100) + ')';
	}
}
