﻿/* Set Up Transition Images */

/* Settings */
var aryTransitionImageDimensions = new Array();
aryTransitionImageDimensions[1] = new Array(606, 260);

var gnNumTransiotionSlots = 1;
var gnImagesPerSlot = 2;
var gnTransitionDuration = 2; // Seconds
var gnTransitionImageLength = 4; // Seconds

// Global State Variables
var aryTransitionSlotImages = new Array();
var aryTransitionSlotState = new Array();
function funPreLoadTransitionImages() {
	if (aryTransitionSlotImages.length == 0) {
		for (var i = 1; i <= gnNumTransiotionSlots; i++) {
			aryTransitionSlotImages[i] = new Array();
			for (var j = 1; j <= gnImagesPerSlot; j++) {
				aryTransitionSlotImages[i][j] = new Image();
				aryTransitionSlotImages[i][j].src = "images/global/g-header-frame" + j + ".jpg";
			}
			
			// Set the Current state
			aryTransitionSlotState[i] = new Array(1, 1); //(Current Holder, Current Index)
			// Kick off the Transition
			setTimeout("funFadeTransitionImage(" + i + ");", ((gnTransitionImageLength * 1000) / 4));
		}
	}
}

function funFadeTransitionImage(i) {
	var objImg_1 = funGetObject("transitionImage_" + i + "_1");
	var sAltText = (objImg_1.alt != null) ? objImg_1.alt : "";
	var objImg_2 = funGetSecondTransitionImg(i, sAltText);

	if (objImg_1 != null && objImg_2 != null) {
		// Determine the Hidden Img Element
		var sIdA, sIdB;
		var nCurrentImg = aryTransitionSlotState[i][0];
		var nNextImg = (nCurrentImg == 1) ? 2 : 1;
		// A = The Current Image Element
		// B = The Next Image Element
		sIdA = "transitionImage_" + i + "_" + nCurrentImg;
		sIdB = "transitionImage_" + i + "_" + nNextImg;
		var objImgB = funGetObject(sIdB);

		// Update the Hidden Img Element's Current Image
		var nNextIndex = aryTransitionSlotState[i][1] + 1;
		if (nNextIndex > gnImagesPerSlot) nNextIndex = 1;
		objImgB.src = aryTransitionSlotImages[i][nNextIndex].src;

		// Ok, start the fade out of the current image and fade in the new one
		funFadeOpacity(sIdA, 100, 0, gnTransitionDuration);
		funFadeOpacity(sIdB, 0, 100, gnTransitionDuration);

		// Update the Current State
		aryTransitionSlotState[i][0] = nNextImg;
		aryTransitionSlotState[i][1] = nNextIndex;
	}
	setTimeout("funFadeTransitionImage(" + i + ");", gnTransitionImageLength * 1000);
}

// Returns the Image Element object for the second transition
function funGetSecondTransitionImg(i, sAltText) {
	// Check to see if the image element already exists
	var sImageID = "transitionImage_" + i + "_2";
	var objImage = funGetObject(sImageID);
	if (objImage == null) {
		// Try to create the image element
		var objHolderDiv = funGetObject("transitionImage_Fade_" + i);
		if (document.createElement && objHolderDiv != null) {
			var objImage = document.createElement("img");
			objImage.id = sImageID;
			objImage.width = aryTransitionImageDimensions[i][0];
			objImage.height = aryTransitionImageDimensions[i][1];
			objImage.alt = sAltText;
			// Now add it to the DOM
			if (objHolderDiv.appendChild) objHolderDiv.appendChild(objImage);
		}
		else if (objHolderDiv.innerHTML) {
		objHolderDiv.innerHTML = "<img id=\"" + sImageID + "\" width=\"" + aryTransitionImageDimensions[i][0] + "\" height=\"" + aryTransitionImageDimensions[i][1] + "\" alt=\"C" + sAltText + "\" />";
			objImage = funGetObject(sImageID);
		}

		// Initialize the Opacity to 0
		funSetOpacity(sImageID, 0);
	}

	return objImage;
}

/* Opacity Functions */

// Updates the Opacity of an object
function funSetOpacity(id, opacity) {
	var object = funGetObject(id);
	if (object != null) {
		if (object.style) {
			object = object.style;
		}
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.filter = "alpha(opacity=" + opacity + ")";
	}
}

// Fades the Opacity from one value to another
function funFadeOpacity(id, opacStart, opacEnd, sec) {
	//speed for each frame
	var millisec = sec * 1000;
	var speed = Math.round(millisec / 20);
	var timer = 0;
	var i = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if (opacStart > opacEnd) {
		//Fade OUT
		var i = opacStart;
		while (i > opacEnd) {
			setTimeout("funSetOpacity('" + id + "'," + i + ")", (timer * speed));
			timer++;
			i = i - 5;
		}
	}
	else if (opacStart < opacEnd) {
		//Fade IN
		var i = opacStart;
		while (i < opacEnd) {
			setTimeout("funSetOpacity('" + id + "'," + i + ")", (timer * speed));
			timer++;
			i = i + 5;
		}
	}
	setTimeout("funSetOpacity('" + id + "'," + opacEnd + ")", (timer * speed));
}

/* General Helper Functions */
function funGetObject(obj) {
	var theObj;
	if (typeof obj == "string") {
		if (document.getElementById) {
			theObj = document.getElementById(obj);
		}
		else if (document.all) {
			theObj = document.all(obj);
		}
		else if (document.layers) {
			theObj = seekLayer(document, obj);
		}
		if (theObj == null) theObj = eval("document." + obj);
	}
	else {
		// pass through object reference
		theObj = obj;
	}
	return theObj;
}

function seekLayer(doc, name) {
	var theObj;
	for (var i = 0; i < doc.layers.length; i++) {
		if (doc.layers[i].name == name) {
			theObj = doc.layers[i];
			break;
		}
		// dive into nested layers if necessary
		if (doc.layers[i].document.layers.length > 0) {
			theObj = seekLayer(document.layers[i].document, name);
		}
	}
	return theObj;
}