// controls whether debugging messages enabled ('dbg' param = enable)
var DBG = (location.search.indexOf("dbg") != -1);

// if(DBG) alert("main.js running")

// intercept error messages if debugging disabled
if(!DBG) window.onerror = HandleWinError;
function HandleWinError() { ErrMsg("An unhandled exception occurred"); return false; }

// browser detection
BrowserDetect();
if(bIE5 && !bIE6) ExtendIE5Array();
BreakFromFrames();

Msg("Page last modified: " + document.fileModifiedDate);
window.onload = WindowOnLoad;
window.onresize = WindowOnResize;
if(document.readyState == "complete") WindowOnLoad();		// if window already loaded, call WindowOnLoad()

// messaging routines
function Msg(sText) { if(DBG) window.status = sText }
function WarnMsg(sText) { window.status = "WARNING: " + sText }
function ErrMsg(sText) { window.status = "ERROR: " + sText }

function BreakFromFrames() {
	if(!top.location || (top.location != location)) top.location = location;
}

function WindowOnLoad() {
	// Msg("Window loaded");
	if(bNetscape) {
		SetupNSPointers();								// set up Netscape global pointers
		InitTopLevelLinksNS();
	}
	DisplayTodaysDate();
	InitFlash();
}

function WindowOnResize() {
	if(bIE) tblLayout.style.tableLayout = "auto";
	if(bNetscape6) FrameOnLoad();			// fix content frame sizing
}

function FrameOnLoad() {
	if(bNetscape6) {
		SetupNSPointers();							// set up Netscape global pointers
		FitFrameToContentNS();
	}
	if(bIE) {
		SyncMenu();
		// if flash not yet started, start the default sequence
		if(!window.bFlashStarted)
			if((top.document.readyState == "complete") && top.flashBanner1)
				LoadFlash("home.xml", "cheese.xml");
			else
				top.attachEvent("onload", function() { LoadFlash("home.xml", "cheese.xml") } );
	}
}

// returns scroll height of content in content frame. if not accessible, returns "auto" instead
function GetContentFrameHeight() {
	try {				return contentFrame.document.body.scrollHeight+'px';	}
	catch(e) {	return "auto";	}
}

// if IE, clones flashBanner and defines pointers 'top.flashBanner1' and 'top.flashBanner2'
function InitFlash() {
	if(bIE5) {
		Msg("InitFlash()");
		top.flashBanner1 = flashBanner;		// store reference to first banner before cloning
		top.flashBanner2 = flashBanner.cloneNode(true);
		HideBanner(top.flashBanner2);
		divImagesBanner.appendChild(top.flashBanner2);
	}
}

function LoadFlash(sXmlFile1, sXmlFile2) {
	Msg("main.js LoadFlash('"+ sXmlFile1 +"','"+ sXmlFile2 +"')");
	if(document.all) {
		// if specified XML config files different from current, switch to specified
		if( !((window.sXmlFile1 == sXmlFile1) && (window.sXmlFile2 == sXmlFile2)) ) {
			window.bFlashStarted = true;
			
			// clear existing interval timer, if exists
			window.clearTimeout(window.bannerInterval);
			window.bannerInterval = undefined;

			// almost immediately begin loading first flash movie
			setTimeout("top.flashBanner1.movie = 'flash/Final.swf?flashPath=flash/&xmlFileName="+ sXmlFile1 +"'", 200);
			// after 60s, start loading second movie
			setTimeout("top.flashBanner2.movie = 'flash/Final.swf?flashPath=flash/&xmlFileName="+ sXmlFile2 +"'", 60000);
			// after 120s, swap banners and schedule further swaps every 30s
			setTimeout("SwapBanner(); window.bannerInterval = setInterval(SwapBanner, 30000)", 120000);
			
			// record names of new config files
			window.sXmlFile1 = sXmlFile1;
			window.sXmlFile2 = sXmlFile2;
		}
	}
}

function SwapBanner() {
	// alert("Swapping Flash banner (top.flashBanner2.style.height = "+ top.flashBanner2.style.height +")");
	if(top.flashBanner2.style.height == "0px") {
		ShowBanner(top.flashBanner2); HideBanner(top.flashBanner1);
	}
	else {
		ShowBanner(top.flashBanner1); HideBanner(top.flashBanner2);
	}
}
function HideBanner(oEl) { 	oEl.style.height = "0px"; oEl.style.width = "0px"; }
function ShowBanner(oEl) { 	oEl.style.height = "150px"; oEl.style.width = "100%"; }


function GetPageName(sURL) {
	try {
		// split URL on / and return last segment (the page name), unless is local URL, in which case split on \ instead
		return ((sURL.indexOf(":\\") == -1)? sURL.split("/") : sURL.split("\\")).pop();
	}
	catch(oErr) {
		return "";
	}
}

function SyncMenu() {
	var bSucceeded = false, oMenuItem, oMenuItems
	try {
		var sPathName = contentFrame.location.pathname;
		var sPageName = GetPageName(sPathName);

		// NOT NEEDED?		DeselectAll();		// deselect/close all other menu items

		oMenuItems = ulMenu.getElementsByTagName("LI");
		for(var x in oMenuItems) {
			oMenuItem = oMenuItems[x];
			if(typeof oMenuItem.getElementsByTagName != "undefined") {
				if( GetPageName(oMenuItem.getElementsByTagName("A")[0].href) == sPageName ) {
					bSucceeded = true;
					// Msg("Synching menu to page name \""+ sPageName +"\" (item #"+ x +" in menu)");
					
					// select the matching menu item (wait till loaded if necessary)
					if(typeof oMenuItem.Select == "function")
						oMenuItem.Select();
					else
						oMenuItem.onload = function() { this.Select() }

					break;		// stop searching through menu items (only select first item found)
				}
			}
		}
		if(!bSucceeded) Msg("Couldn't sync menu with page \""+ sPageName +"\"");
	}
	catch(oErr) {}
}

function DisplayTodaysDate() {
	var sTodayDate, dToday = new Date();
	// convert today's date to GMT string (with time removed from end)
	sTodayDate = dToday.toGMTString().replace(/\d{2}:\d{2}:\d{2} \w{3}/, "");
	divTodaysDate.innerHTML = sTodayDate;			// write to date DIV
}

function OnSearchSubmit() {
	var q = document.frmSearch.q;
	try { if(!(q.value && q.IsValid())) return false; } catch(e) {}
	
	setTimeout("try{ document.frmSearch.q.ResetSearch() } catch(e) { document.frmSearch.q.value='' }", 500);
	return true;
}
