//index.js
// variables
var tmImageSwapTimer = null;
var tmGoToHomeTimer = null;
var arrOrder = new Array(1,4,3,2);
var arrImages = new Array("1.gif","2.jpg","3.jpg","4.jpg");
var strHomeLocation = "site/frames.html";
var arrIndex = -1;
var divPrefix = "divImageHolder";
var intInterval = 1000;
var intGoToHomeInterval = 5; // in seconds
var intImageWidth = 480;
var intImageHeight = 380;
var intEnterLinkSpace = 40;
var intPageBottom = 600;
var intWidthPadding = 50;

// functions
function initializeDivLocations()
{
	// placement is based on the following layout
	// ---------
	// | 1 | 2 |
	// ---------
	// | 3 | 4 |
	// ---------
	//
	// image	top						left
	// 1		40						width padding
	setDivLocation(1,intEnterLinkSpace,intWidthPadding);
	// 2		40						(width - image width) centered
	setDivLocation(2,intEnterLinkSpace,getRightImageLeft());
	// 3		bottom - image height	x
	setDivLocation(3,getBottomImageTop(),intWidthPadding);
	// 4		bottom - image height	(width - image width) centered
	setDivLocation(4,getBottomImageTop(),getRightImageLeft());
}

function getRightImageLeft()
{
	return screen.availWidth - (intImageWidth + intWidthPadding);
}

function getBottomImageTop()
{
	return intPageBottom - (intImageHeight + intEnterLinkSpace);
}

function setDivLocation(intDivId, intTop, intLeft)
{
	var divObj = null;
	
	try
	{
		divObj = document.all[divPrefix + intDivId];
		divObj.style.top = intTop;
		divObj.style.left = intLeft;
	}
	catch(e){alert(e.discription);}
}

// starts the timer and displays 1st image
function beginDisplay()
{
	// initialize div location
	initializeDivLocations();
	// display 1st image
	arrIndex = 0;
	showNext(arrIndex);
	// start timer
	tmImageSwapTimer = setTimeout("handleNext()",intInterval);
	tmGoToHomeTimer = setTimeout("goToHome()",(intGoToHomeInterval * 1000));
}

function goToHome()
{
	clearTimeout(tmImageSwapTimer);
	clearTimeout(tmGoToHomeTimer);
	
	window.location.href = strHomeLocation;
}

// handle next selection
function handleNext()
{
	// adjust images
	// change the z-index for current one
	setNegZIndex(arrIndex);
	// clear previous image
	clearPreImage(arrIndex);
	
	// increment array index
	arrIndex++;
	// correct if passed end of array
	if(arrIndex >= arrOrder.length)
	{
		arrIndex = 0;
	}
	// show next
	showNext(arrIndex);
	// reset timer
	tmImageSwapTimer = setTimeout("handleNext()",intInterval);
}

//  set z-index
function clearPreImage(imgIndex)
{
	var divObj = null;
	var prevIndex = imgIndex - 1;
	
	// correct if less than 0
	if(prevIndex < 0)
	{
		prevIndex = arrOrder.length - 1;
	}
	
	try
	{
		divObj = document.all[divPrefix + arrOrder[prevIndex]];
		divObj.style.display = "none";
	}
	catch(e){alert(e.discription);}
}

//  set z-index
function setNegZIndex(imgIndex)
{
	var divObj = null;
	
	try
	{
		divObj = document.all[divPrefix + arrOrder[imgIndex]];
		divObj.style.zIndex = "-1";
	}
	catch(e){alert(e.discription);}
}

// show image and set z-order
function showNext(imgIndex)
{
	var divObj = null;
	
	try
	{
		divObj = document.all[divPrefix + arrOrder[imgIndex]];
		//alert("Looking for: " + divPrefix + arrOrder[imgIndex] + "~" + divObj);
		divObj.style.display = "block";
		divObj.style.zIndex = "1";
	}
	catch(e){alert(e.discription);}
}
/* end of file */ 