
var activeImg = null;

function init() {
    try
    {
	    //drawMenuOn('mainMenu');
	    //drawBottomMenu();
	    displayPlayer($("imgFirst"), 0);
	    $("loadingStartup").style.display="none";
	}
	catch(err)
	{
	  // eat it
	}
	
    adjustOverlayPosition();
}



function displayPlayer(fromImg, id) {
	

	if (activeImg != null) { activeImg.className = ''; activeImg.previousSibling.className='unselected'; }

	$("playVideo" + id).onclick = function() {$('videoContent').style.display='block'};
	fromImg.className = 'selected';
	
	$("loading").style.display = "block";
	$("mainSite").style.backgroundImage = "url()";
	// ajax call to load image
	new Ajax.Request(themeContent[id].bkgImg,
		{
			method:'get',
			onSuccess: function(transport) {
		  		preloadImages(themeContent[id].bkgImg);
				$("mainSite").style.backgroundImage = "url(" + themeContent[id].bkgImg + ")";
				$("subSite").style.backgroundImage = "url(" + themeContent[id].bkgImg + ")";
				$("loading").style.display = "none";
			},
			onFailure: function() { 
				alert("Can't load image") ;
			}
		});
	$("textContent").innerHTML = themeContent[id].text;
	
	activeImg = fromImg;
}


function switchToNewWindow(pageName) {
	/*$("mainMenu").className='overlay';
	$("mainSiteLogo").style.display = 'none';
	$("subSite").style.display = 'block';
	$("mainContent").innerHTML = "External window opened.  Please choose a new menu item to continue browsing content.";*/
	
	window.open("" + pageName + ".aspx", pageName);
	/*
	new Ajax.Request(pageName + ".aspx?nocache=" + Math.random(),
		{
			method:'get',
			onSuccess: function(transport) {
				$("mainContent").innerHTML = transport.responseText;
			},
			onFailure: function() { 
				alert("Can't load the page") ;
			}
		}); */
}


function switchToSubSite(pageName) {
	/*$("mainMenu").className='overlay';
	$("mainSiteLogo").style.display = 'none';
	$("subSite").style.display = 'block';*/
	PrepareForSubSite();
	$("mainContent").innerHTML = $("loadingMainContent").innerHTML;
	// Temporarily
	new Ajax.Request("" + pageName + ".aspx?nocache=" + Math.random(),
		{
			method:'get',
			onSuccess: function(transport) {
				$("mainContent").innerHTML = transport.responseText;
			},
			onFailure: function() { 
				alert("Can't load the page") ;
			}
		});
}

function PrepareForSubSite() {
    $("mainMenu").className='overlay';
	$("mainSiteLogo").style.display = 'none';
	$("subSite").style.display = 'block';
}

function switchToMainSite() {
	$("subSite").style.display = 'none';
	$("mainSiteLogo").style.display = 'block';
	$("mainMenu").className='normal';
}

/**
 * draw the main menu on a div
 **/
function drawMenuOn(divID) {	
	var prop = cmClone (cmHPCTheme);
	prop.effect = new CMSlidingEffect (8);
	cmDraw (divID, mainMenu, 'hbr', prop);	
}

function drawBottomMenu() {
	
	$('bottomMenu').style.left = (getAbsoluteLeft('playVideo0') - 6) + "px";
	$('bottomMenu').style.top = (getAbsoluteTop('playVideo0') - 20) + "px";
	$('bottomMenu').style.display = 'block';
	$('bottomMenu').style.fontSize = '9px';
	var prop = cmClone (cmHPCThemeBottom); // use another theme for this menu
	prop.effect = new CMSlidingEffect (8);
	cmDraw ('bottomMenu', bottomMenu, 'hur', prop);		
}

function adjustOverlayPosition() {
	$("overlayDiv").style.left = $("mainSite").offsetLeft + "px";
	$("wrappedMainContent").style.left = $("mainSite").offsetLeft + "px";
	$("backgroundDiv").style.left = $("mainSite").offsetLeft  + "px";
}

/*
 * Preload images
 */
function preloadImages() {
	var d=document,a=arguments; if(!d.FP_imgs) d.FP_imgs=new Array();
	
	for(var i=0; i<a.length; i++) { d.FP_imgs[i]=new Image; d.FP_imgs[i].src=a[i]; }

}


Event.observe(window, 'resize', adjustOverlayPosition, false);
Event.observe(window, 'load', init, false);
