var myBlankImage = '/images/spot/blank.gif';

var myObj = new Array();
myObj[0] = { 'enable_shadow' : 1, 'image' : '/images/spot/about_upbringing.jpg',  'link' : '/popovers/about_upbringing.html'};
myObj[1] = { 'enable_shadow' : 1, 'image' : '/images/spot/about_hp.jpg',    'link' : '/popovers/about_hp.html'};
myObj[2] = { 'enable_shadow' : 1, 'image' : '/images/spot/about_blessings.jpg',   'link' : '/popovers/about_blessings.html'};
myObj[3] = { 'enable_shadow' : 1, 'image' : '/images/spot/about_career.jpg',    'link' : '/popovers/about_secretary.html'};
myObj[4] = { 'enable_shadow' : 1, 'image' : '/images/spot/about_cleanhealth.jpg', 'link' : '/popovers/about_cleanbillofhealth.html'};
myObj[5] = { 'enable_shadow' : 1, 'image' : '/images/spot/about_grandkids.jpg',       'link' : '/popovers/about_grandkids.html'};
myObj[6] = { 'enable_shadow' : 1, 'image' : '/images/spot/about_lifefrank.jpg',       'link' : '/popovers/about_lifewithfrank.html'};
myObj[7] = { 'enable_shadow' : 1, 'image' : '/images/spot/about_opportunities.jpg',       'link' : '/popovers/about_opportunities.html'};
myObj[8] = { 'enable_shadow' : 1, 'image' : '/images/spot/about_toughtimes.jpg',       'link' : '/popovers/about_toughtimes.html'};



var myISF = new Array();
myISF[0] = { 'clock' : null, 'fade' : true, 'count' : 1 , 'n' : 1  , 'link' : '', 'currentId' : 1};
myISF[1] = { 'clock' : null, 'fade' : true, 'count' : 1 , 'n' : 1  , 'link' : '', 'currentId' : 2};
myISF[2] = { 'clock' : null, 'fade' : true, 'count' : 1 , 'n' : 1  , 'link' : '', 'currentId' : 3};


var myLevels  = Math.ceil( ( myObj.length > 0 ? myObj.length / 3 : 0 ) );
var myCurrent = -1;

initStuff();

function initStuff()
{

	str = '';

	var myStyle = new Array();

	myStyle[0] = 'margin:0px;overflow:hidden;position:absolute;width:173px;height:82px;left:0px;'
	myStyle[1] = 'margin:0px;overflow:hidden;position:absolute;width:173px;height:82px;left:178px;'
	myStyle[2] = 'margin:0px;overflow:hidden;position:absolute;width:173px;height:82px;left:356px;'

	v = -1;


	for(var t=0;t<(myLevels*3);t++){

		v++;
		if(v >2)
		v = 0;

		if(t < myObj.length && t < 3){
			str += '<div id="spot'+(t+1)+'" style="'+myStyle[v]+'"><a href="'+myObj[t].link+'" id="spotURL'+t+'" '+( myObj[t].enable_shadow ? 'rel="shadowbox;height=500;width=990;"' :'' )+'><img id="spotImg'+t+'" border="0" src="'+myBlankImage+'"></a></div>';
		}
		else if(t < myObj.length){
			str += '<div id="spot'+(t+1)+'" style="display:none;'+myStyle[v]+'"><a href="'+myObj[t].link+'" id="spotURL'+t+'" '+( myObj[t].enable_shadow ? 'rel="shadowbox;height=500;width=990;"' :'' )+'><img id="spotImg'+t+'" border="0" src="'+myBlankImage+'"></a></div>';
		}
		else{
			if(t < 3){
				str += '<div id="spot'+(t+1)+'" style="'+myStyle[v]+'"><a href="#" id="spotURL'+t+'" '+( myObj[t].enable_shadow ? 'rel="shadowbox;height=500;width=990;"' :'' )+'><img id="spotImg'+t+'" border="0" src="/images/spot/blank.gif"></a></div>';
			}
			else{
				str += '<div id="spot'+(t+1)+'" style="display:none;'+myStyle[v]+'"><a href="#" id="spotURL'+t+'" '+( myObj[t].enable_shadow ? 'rel="shadowbox;height=500;width=990;"' :'' )+'><img id="spotImg'+t+'" border="0" src="/images/spot/blank.gif"></a></div>';
			}
		}

	}

	str += '<div id="spotMore" class="spotMore"><a href="JavaScript:spot_change();">More</a></div>';
	
	document.getElementById('spotMain').innerHTML = str;



	var tmpCache  = [];

	for(var i=0; i<myObj.length; i++)
	{
		tmpCache[i] = new Image;
		tmpCache[i].src = myObj[i].image;
	}

	tmpCache[myObj.length] = new Image;
	tmpCache[myObj.length].src = myBlankImage;

}



function swapfade()
{

	isf = myISF[arguments[4]];

	if(isf.clock == null)
	{
		isf.id = arguments[4];

		isf.link = arguments[5];

		isf.currentId = arguments[6];

		//copy the image object 
		isf.obj = arguments[0];
		
		//copy the image src argument 
		isf.src = arguments[1];
		
		//store the supported form of opacity
		if(typeof isf.obj.style.opacity != 'undefined')
		{
			isf.type = 'w3c';
		}
		else if(typeof isf.obj.style.MozOpacity != 'undefined')
		{
			isf.type = 'moz';
		}
		else if(typeof isf.obj.style.KhtmlOpacity != 'undefined')
		{
			isf.type = 'khtml';
		}
		else if(typeof isf.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
			isf.type = (isf.obj.filters.length > 0 && typeof isf.obj.filters.alpha == 'object' && typeof isf.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		}
		else
		{
			isf.type = 'none';
		}
		
		//change the image alt text if defined
		if(typeof arguments[3] != 'undefined' && arguments[3] != '')
		{
			isf.obj.alt = arguments[3];
		}


		//if any kind of opacity is supported
		if(isf.type != 'none')
		{
			//copy and convert fade duration argument 
			//the duration specifies the whole transition
			//but the swapfade is two distinct transitions
			isf.length = parseInt(arguments[2], 10) * 100;
			
			//create fade resolution argument as 20 steps per transition
			//again, split for the two distrinct transitions
			isf.resolution = parseInt(arguments[2], 10) * 10;

			if(arguments[7]){
				if(isf.id == (myISF.length-1))
					syncIt();
			}
			else{
				//start the timer
				isf.clock = setInterval('fadeOut('+isf.id+')', isf.length/isf.resolution);
			}
		}
		
		//otherwise if opacity is not supported
		else
		{
			//just do the image swap
			isf.obj.src = isf.src;
		}
		
	}
}

function syncIt(){

	bool = 0;

	for(var j=0; j<myObj.length; j++){
		document.getElementById('spot'+(j+1)+'').style.display = 'none';
	}

	for(var i=0; i<myISF.length; i++)
	{

		isf = myISF[i];
		document.getElementById('spot'+(isf.currentId+1)+'').style.display = 'inline';
		isf.obj = document.getElementById('spotImg'+isf.currentId+'');
		isf.n = 1;
		isf.count = 0.1;
		isf.clock = setInterval('fadeIn('+isf.id+')', isf.length/isf.resolution);

	}

}


function fadeOut(id)
{

	isf = myISF[id];

	isf.n++;

	delay = 10+(id*25);

	if(isf.n > delay)
	{

		isf.count = isf.count * 0.9;

		if(isf.count < (1 / isf.resolution))
		{
			clearInterval(isf.clock);
			isf.clock   = null;
			isf.obj.src = myBlankImage;
			last_index  = myISF.length-1;

			if(isf.id == last_index){
				syncIt();
			}

		}


		switch(isf.type)
		{
			case 'ie' :
				isf.obj.filters.alpha.opacity = isf.count * 100;
				break;

			case 'khtml' :
				isf.obj.style.KhtmlOpacity = isf.count;
				break;

			case 'moz' : 
				//restrict max opacity to prevent a visual popping effect in firefox
				isf.obj.style.MozOpacity = (isf.count == 1 ? 0.9999999 : isf.count);
				break;

			default : 
				//restrict max opacity to prevent a visual popping effect in firefox
				isf.obj.style.opacity = (isf.count == 1 ? 0.9999999 : isf.count);
		}

	}

}


function fadeIn(id){

	isf = myISF[id];

	isf.n++;

	delay = 10+(id*25);

	if(isf.n >= delay)
	{

		if(isf.n == delay){
			isf.obj.src = isf.src;
		}

		isf.count = (isf.count * (1/0.9));
	
		if(isf.count > (1 - (1 / isf.resolution)))
		{

				clearInterval(isf.clock);
				isf.clock = null;

				isf.count  = 1;
				isf.n      = 1;
		}

		switch(isf.type)
		{
			case 'ie' :
				isf.obj.filters.alpha.opacity = isf.count * 100;
				break;

			case 'khtml' :
				isf.obj.style.KhtmlOpacity = isf.count;
				break;

			case 'moz' : 
				//restrict max opacity to prevent a visual popping effect in firefox
				isf.obj.style.MozOpacity = (isf.count == 1 ? 0.9999999 : isf.count);
				break;

			default : 
				//restrict max opacity to prevent a visual popping effect in firefox
				isf.obj.style.opacity = (isf.count == 1 ? 0.9999999 : isf.count);
		}

	}

}


function spot_change(init){

	if(init && myObj.length > 3)
	document.getElementById("spotMore").style.display = 'block';

	if(myLevels > 0){

		myOriginal = ( init ? 0 : myCurrent );

		myCurrent++;

		if(myCurrent >= myLevels)
			myCurrent = 0;

		for(i=0;i<3;i++){

			c = i+(myCurrent*3);
			oj = i+(myOriginal*3);

			imgElm  = document.getElementById('spotImg'+oj+'');
			linkElm = document.getElementById('spotURL'+oj+'');

			if(c < myObj.length){
				swapfade(imgElm, myObj[c].image, '1', '[Spotlight '+i+']',i, myObj[c].link, c, init);

			}
			else{
				swapfade(imgElm, myBlankImage, '1', '[Spotlight '+i+']',i, myObj[c].link, c, init);
			}
		}

	}

}

function linkIt(id){

	location.href = myISF[id].link;

}


document.onload = spot_change(1);
