/*
 * ====================================================================
 *
 */

/*
 * The id of the coming frame of the scroller.
 */
var COMING_FRAME_ID = "comingFrame";

/*
 * The id of the leaving frame of the scroller.
 */
var LEAVING_FRAME_ID = "leavingFrame";

/*
 * The time in milliseconds of the scroller's delay on each frame.
 */
var SCROLLER_DELAY = 14000;

/*
 * Scroller speed in pixels per millisecond.
 */
var SCROLLER_SPEED = 3;

/*
 * Private variables
 */
var comingFrameQuoteId = "comingFrameQuote";
var comingFrameUserId = "comingFrameUser";
var comingImageId = "comingImage";
var leavingFrameQuoteId = "leavingFrameQuote";
var leavingFrameUserId = "leavingFrameUser";
var leavingImageId = "leavingImage";

var quoteCounter = 0;

var imageArray = new Array();
var quoteArray = new Array();
var userArray = new Array();

/*
 * Opens a popup window with the given url and dimensions in the specified browser window. The
 * window will be centered over the parent window. 
 */
function doCorporatePopup(url, width, height)
{
    var windowWidth = isMozillaDerivative() ? window.outerWidth : document.body.clientWidth;
    var windowHeight = isMozillaDerivative() ? window.outerHeight : document.body.clientHeight;

    var windowX = isMozillaDerivative() ? window.screenX : window.screenLeft;
    var windowY = isMozillaDerivative() ? window.screenY : window.screenTop;

    var left = windowX + (windowWidth - width) / 2;
    var top = windowY + (windowHeight - height) / 2;

    var windowOptions = "scrollbars=1,resizable=1,menubar=0,location=0" +
                        ",width=" + width +
                        ",height=" + height +
                        ",left=" + left +
                        ",top=" + top;

    /*
     * If present, pass the client-side deployment ID into the popup action. This will allow
     * the server to check for client-server deployment synchronization problems.
     */
	var paramChar = "?";
	if (url.indexOf(paramChar) != -1)
	{
		paramChar = "&";
	}
	
	var form = document.forms[0];
	if (form != null && form.elements['deploymentId'] != null)
	{
		url += paramChar + "deploymentId=" + form.elements['deploymentId'].value; 
	}

    popup = window.open(url, '', windowOptions);
    popup.focus();
}

/*
 * Increments the quote counter by one or resets it to 0 if reached the end of the quotes.
 */
function incrementQuoteCounter()
{
	quoteCounter++;
	if (quoteCounter == quoteArray.length || quoteCounter == userArray.length)
	{
		quoteCounter = 0;
	}
}

/*
 * Returns whether the browser is IE6.
 */
function isIe6()
{
	var isIe6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	
	return isIe6;
}

/*
 * Scrolls through the pictures and quotes in the home page.
 */
function scrollHomepageContent()
{
	var position = 0;	
	
	var leavingFrame = document.getElementById(LEAVING_FRAME_ID);
	position = parseInt(leavingFrame.style.bottom) + SCROLLER_SPEED;
	leavingFrame.style.bottom = position.toString();
	
	var comingFrame = document.getElementById(COMING_FRAME_ID);
	position = parseInt(comingFrame.style.top) - SCROLLER_SPEED;
	comingFrame.style.top = position.toString();
	
	if (position <= 0)
	{
		/*
		 * Swap the content of the two DIVs in the scroll container.
		 */
		var height = leavingFrame.style.bottom;

		document.getElementById("scrollContainer").innerHTML = "<div id=\"" + LEAVING_FRAME_ID + "\" style=\"bottom: 0px; position: relative;\">" + 
		                                                       comingFrame.innerHTML + 
		                                                       "</div>" + 
		                                                       "<div id=\"" + COMING_FRAME_ID + "\" style=\"top: " + height + "; position: relative; margin-top: -" + height + ";\">" + 
		                                                       leavingFrame.innerHTML +
		                                                       "</div>";
		
		/*
		 * Replace the image, quote and user information on the new coming frame with the next one 
		 * in the arrays.
		 */
		incrementQuoteCounter();
		
		document.getElementById(leavingFrameQuoteId).innerHTML = quoteArray[quoteCounter];
		document.getElementById(leavingFrameUserId).innerHTML = "<br>" + userArray[quoteCounter];
		document.getElementById(leavingImageId).setAttribute("src", imageArray[quoteCounter]);
		
		var tempId = leavingFrameQuoteId;
		leavingFrameQuoteId = comingFrameQuoteId;
		comingFrameQuoteId = tempId;
		
		tempId = leavingFrameUserId;
		leavingFrameUserId = comingFrameUserId;
		comingFrameUserId = tempId;
		
		tempId = leavingImageId;
		leavingImageId = comingImageId;
		comingImageId = tempId;
		
		setTimeout("scrollHomepageContent()", SCROLLER_DELAY);
	}
	else
	{
        setTimeout("scrollHomepageContent()", 1);
	}
}

/*
 * Sets the image highlight on or off.
 */
function setImageHighlight(id, on)
{
	if (on)
	{
		document.getElementById(id).style.borderColor = "#295590";
	}
	else
	{
		document.getElementById(id).style.borderColor = "#e5e5e5";
	}
}

/*
 * Sets the inner HTML of an element.
 */
function setInnerHtml(id, innerHtml)
{
	document.getElementById(id).innerHTML = innerHtml;
}

/*
 * Updates the video source for the given element.
 */
function updateVideoBox(id, base, source, width, height)
{
    document.getElementById(id).innerHTML = "<div>" +
                                            "<object id=\"scPlayer\" class=\"embeddedObject\" width=\"" + width + "\" height=\"" + height + "\" type=\"application/x-shockwave-flash\" data=\"" + base + "mp4h264player.swf\" >" +
                                            " <param name=\"movie\" value=\"" + base + "mp4h264player.swf\" />" +
                                            " <param name=\"quality\" value=\"high\" />" +
                                            " <param name=\"allowScriptAccess\" value=\"always\" />" +
                                            " <param name=\"allowFullScreen\" value=\"true\" />" +
                                            " <param name=\"scale\" value=\"showall\" />" +
                                            " <param name=\"bgcolor\" value=\"#FFFFFF\" />" +
                                            " <param name=\"base\" value=\"" + base + "\" />" +
                                            " <param name=\"flashVars\" value=\"thumb=" + base + "FirstFrame.jpg&content=" + base + source + "\" />" +
                                            " <video width=\"" + width + "\" height=\"" + height + "\" controls=\"controls\">" +
                                            " <source src=\"" + base + source + "\" type=\"video/mp4;\" />" +
                                            " </video>" +
                                            "</object>" +
                                            "</div>";
}
