var animating = false;
var timerId;
var textRollOvers = ["track","ship","locations","quote"];
var textRollOverImages = new Array();
var n = textRollOvers.length;
for(var i = 0; i < n; i++)
{
	var img = new Image();
	img.src = "images/" + textRollOvers[i] + "_off.gif";
	textRollOverImages.push(img);
}

var buttonRollOverImage = new Image();
buttonRollOverImage.src = "images/download-button_on.gif"

function buttonRollOut()
{
	var img = this.document.getElementById("download-button");	
	if(img)
		img.src = "images/download-button.gif";
}

function buttonRollOver()
{
	var img = this.document.getElementById("download-button");	
	if(img)
		img.src = buttonRollOverImage.src;
}

function menuRollOut()
{
	for(var i = 0; i < n; i++)
	{
		rollOverId = textRollOvers[i];
		element = this.document.getElementById(rollOverId);
		if(element)
			element.src = "images/" + rollOverId + ".gif";
	}
	startAnimation();
	return false;
}

function menuRollOver(id)
{
	var screens = this.document.getElementById("iphone-screens");
	screens.className = "iphone-screens-" + id;
	
	for(var i = 0; i < n; i++)
	{
		rollOverId = textRollOvers[i];
		element = this.document.getElementById(rollOverId);
		if(element)
		{
			if(rollOverId == id)
			{
				element.src = "images/" + rollOverId + ".gif";
			}
			else
			{
				element.src = textRollOverImages[i].src;
			}
		}
		
	}
	stopAnimation();
	return false;
}

function startAnimation()
{
	if(!animating)
	{
		timerId = setInterval("animate()",3000);
		animating = true;
	}
}

function animate()
{
	var screens = this.document.getElementById("iphone-screens");
	var currentClass = screens.className;
	for(var i = 0; i < n; i++)
	{
		var rollOverString = textRollOvers[i];
		if(currentClass.indexOf(rollOverString) > -1)
		{
			i++;
			if(i >= textRollOvers.length)
				i = 0;
			screens.className = "iphone-screens-" + textRollOvers[i];
			break;	
		}
	}
	
}

function stopAnimation()
{
	animating = false;
	clearInterval(timerId);
}

//Taken from http://ups.com
/* -----------------------------------------------
FUNCTION: popWindow
DESC: Opens a window and positions it on the screen

ARGS:
        hrefTarget - url to be opened
        name - name of popup window (becomes popupType)
        width - width of the pop up window
        height - height of the pop up window
-------------------------------------------------*/
function popWindow(hrefTarget,name,width,height) {
// Create offset
        if (document.all) {
                xMax = screen.width, yMax = screen.height;
        } else {
                if (document.layers) {
                        xMax = window.outerWidth, yMax = window.outerHeight;
                } else {
                        xMax = 640, yMax=480;
                }
        }
        var xOffset = (xMax - 586)/2, yOffset = (yMax - 700)/2;
//Check if pop up exists
        if (!popWin||popWin.closed) {
                // no pop up exists set the popWinType to the current type
                popWinType = name;
                // open pop up window
                popWin = window.open(hrefTarget,name,'width='+width+',height='+height+',screenX='+xOffset+',screenY='+yOffset+
', top='+yOffset+',left='+xOffset+',scrollbars=yes,menubar=yes,resizable=yes');
        } else {
                // Check if the pop up is the same type
                if (popWinType != name) {
                // it is not the same type so close the current pop up
                popWin.close();
                // set the popWinType to the current type
                popWinType = name;
                // open pop up window
                popWin = window.open(hrefTarget,name,'width='+width+',height='+height+',screenX='+xOffset+',screenY='+yOffset+
', top='+yOffset+',left='+xOffset+',scrollbars=yes,menubar=yes,resizable=yes');
                return;
                }
                // Bring pop up to the foreground
                popWin.focus();
                // Change the location of the pop up to the location being requested
                popWin.location = hrefTarget;
        }
}
