//Sample usage syntax:
//addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
//addLoadEvent(function() {
//  /* more code to run on page load */
//});
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

//--------------Video content/sneak-peak page function---------
//-------------     Begin     ----------------------
//--------------------------------------------------
var readyPlayerIds = new Array();
var currentVideoDivId;

function playerReady(thePlayer) {
	readyPlayerIds[readyPlayerIds.length] = thePlayer.id;
}
function playVideo(videoDivIdToShow) {
	if (currentVideoDivId == videoDivIdToShow) return;
	
	var isIE;
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
		isIE = true;
	} else {
		isIE = false;
	}
	
	if (currentVideoDivId && document.getElementById(currentVideoDivId)) {
		document.getElementById(currentVideoDivId).className = "none";
	}
	
    if (isIE) {
 		//We need stop current player in IE.
	    for (var i = 0; i < readyPlayerIds.length; i ++) {
	    	var playerObj = document.getElementById(readyPlayerIds[i]);
	    	if (playerObj) {
	    		playerObj.sendEvent('STOP');
	    	}
	    }
	}

	var newVideoDivToDisplay = document.getElementById(videoDivIdToShow);
	if (newVideoDivToDisplay) {
		newVideoDivToDisplay.className = "";
		currentVideoDivId = videoDivIdToShow;
	}
}
//--------------Video content/sneak-peak page function---------
//-------------     End     ----------------------
//--------------------------------------------------

// --------------Carousel function---------
//-------------     Begin     ----------------------
//--------------------------------------------------
var srcULsCurrentDisplayLiIndex = new Array();
function switchDisplayItem(parentContainerId, itemTagName, numberToDisplay, direction, previousBtnId, nextBtnId) {
	var parentContainerNode = document.getElementById(parentContainerId);
	if (!parentContainerNode) return;

	var allChildren = parentContainerNode.childNodes;
	if (!allChildren) return;

	var liChildren = new Array();
	for ( var i = 0; i < allChildren.length; i++) {
		if (allChildren[i].tagName == itemTagName.toUpperCase()) liChildren[liChildren.length] = allChildren[i];
	}

	var currentDisplayLiIndex = srcULsCurrentDisplayLiIndex["_" + parentContainerId];
	if (currentDisplayLiIndex == undefined)	currentDisplayLiIndex = -1;
	
	currentDisplayLiIndex += direction;

	setVisibility(previousBtnId, true);
	setVisibility(nextBtnId, true);
	if (liChildren.length <= numberToDisplay) {
		setVisibility(previousBtnId, false);
		setVisibility(nextBtnId, false);
	} else if (currentDisplayLiIndex == 0) {
		setVisibility(previousBtnId, false);
	} else if (currentDisplayLiIndex >= liChildren.length - numberToDisplay) {
		setVisibility(nextBtnId, false);
	}
	
	if (currentDisplayLiIndex < 0) {
		// We're already at the first <li> element.
		setVisibility(previousBtnId, false);
		return;
	} else if (currentDisplayLiIndex > liChildren.length - numberToDisplay && liChildren.length > numberToDisplay) {
		// total children is more than numberToDisplay and we're already at the last batch of the <li> nodes
		setVisibility(nextBtnId, false);
		return;
	} else if (liChildren.length <= numberToDisplay) {
		//total children is no more than numberToDisplay, so there is no pagination.
		currentDisplayLiIndex = 0;
	}

	srcULsCurrentDisplayLiIndex["_" + parentContainerId] = currentDisplayLiIndex;

	var results = new Array();
	for ( var i = 0; i < liChildren.length; i++) {
		if (i >= currentDisplayLiIndex && i < currentDisplayLiIndex + numberToDisplay) {
			liChildren[i].className = "";
		} else {
			liChildren[i].className = "none";
		}
	}
	if (liChildren.length == 1) {
		liChildren[0].className = "none";
	}
}

function setVisibility(elementId, visible) {
	if (elementId == undefined || elementId == null) return;
	var theElement = document.getElementById(elementId);
	if (theElement == undefined || theElement == null) return;
	theElement.style.visibility = visible ? 'visible' : 'hidden';
}

//--------------Carousel function--------- 
//-------------     End     ----------------------
//--------------------------------------------------


// --------------Top navigation bar function---------
//-------------     Begin     ----------------------
//--------------------------------------------------
function showMenu(index){
	var nav = document.getElementById("nav"+index.toString());
	if (nav) nav.className="nav current";
	var secondMenu = document.getElementById("menu"+index.toString());
	if (secondMenu) {
		//if it has second level tabs
		secondMenu.className="second-menu block";
		if (index != selectedRootTabIndex) closeMenu(selectedRootTabIndex);
	}	
}

function closeMenu(index){
	var nav = document.getElementById("nav"+index.toString());
	if (nav) nav.className="nav";
	var secondMenu = document.getElementById("menu"+index.toString());
	if (secondMenu) secondMenu.className="second-menu none";
}

function noneMenu(index){
	var nav = document.getElementById("nav"+index.toString());
 	nav.className="nav";
	var secondMenu = document.getElementById("menu"+index.toString());
	if (secondMenu != null) {
		//if it has second level tabs
		secondMenu.className="second-menu none";
		showMenu(selectedRootTabIndex);
	} 
}
//--------------Top navigation bar function--------- 
//-------------     End     ----------------------
//--------------------------------------------------
