var GCookieMinToLive = 20;	// cookie life in minutes
var GcurPageLC;				// current page filename
var Gurl = location.href;
var GPathStartPos = 7; // pos after http://
if (Gurl.indexOf("https://") == 0) GPathStartPos = 8;
var GlinkWin = null;
var GnavTimer = null;
var GoMainMenu = null;
var GCurrMainMenu = "def";	 // current main menu item id
var Gpref = "";				 // path prefix to get back to top level (eg. ../../)
var GelTarget = null;		 // mouseover target element
var GnavResetTimer = null;	 // mouseover timer
var GnavReset = 620;		    // mouseover timeout
var GnavDelayTimer = null;	 	// mouseout delay timer
var GnavDelay = 320;		    // mouseout timeout
// -------- nav functions --------
// menuItem constructor
function menuItem(sMenuID, sElemID, sText, sClassName, isHTTPS, oPrevItem, sFolder1, sDefaultFile1) {	
	this.menuID = sMenuID; this.elemID = sElemID; this.text = sText; this.sClassName = sClassName;
	this.prevItm = oPrevItem; this.nxtItem = null;
	this.linkURL = getAbsoluteURL(sFolder1 + sDefaultFile1, isHTTPS);
	/*
	var folderList = sFolder1;
	var defFileList = sDefaultFile1;
	var argv = menuItem.arguments;
	var argc = argv.length;
	for (var i=10; i<argc; i+=2) {folderList += "^" + argv[i-1]; defFileList += "^" + argv[i];}	
	this.aFolder = folderList.split("^"); this.aFile = defFileList.split("^");
	*/
	this.aFolder = new Array(sFolder1);
	this.aFile = new Array(sDefaultFile1); 
	if (oPrevItem != null) oPrevItem.setNxtItem(this);
	menuItem.prototype.setNxtItem = function (oItem) {this.nxtItem = oItem;}
}
/* Highlight current main menu item and show secondary nav 
	secondary nav div id = <main menu item id>_2
	for main menu items with no secondary navs, show div def-2, which is just a blank line
*/
function showSecNav(elMainMenu) {
	var oNav;
	var mainNavNodes = document.getElementById("mainNav").getElementsByTagName("li");
	for (var i=0; i<mainNavNodes.length; i++) {
		oNav = mainNavNodes.item(i);
		if (oNav == elMainMenu) {oNav.className = "curMainCat";}
		else oNav.className = "";
	}
	var secNavNodes = document.getElementById("secNav").getElementsByTagName("div");
	var showChildID = "";
	if (elMainMenu != null) showChildID = elMainMenu.id + "_2";
	if (!document.getElementById(showChildID)) showChildID = "def-2";	// secondary nav not found, default to def-2
	for (i=0; i<secNavNodes.length; i++) {
		oNav = secNavNodes.item(i);
		if (oNav.id == showChildID) {oNav.style.display = "block";}
		else oNav.style.display = "none";
	}
	if (showChildID == "def-2") {
		document.getElementById("secNav").className = "noItems";	// no background color
	}
	else {
		document.getElementById("secNav").className = "hasItems";
	}
}
// get current main menu id based on current page's folder+filename
function getCurrMainMenuID() {
	if (GcurPageLC == "") GcurPageLC = "index_e.php";			// homepage
	var oTmp =  GoMainMenu;
	var curCat = null;
	var curCatFromFile = null;
	var aFolder, aFile;	
	while (oTmp != null) {
		aFolder = oTmp.aFolder;
		aFile = oTmp.aFile;
		for (var i=0; i<aFolder.length; i++) {	
			if (aFolder[i] != "") {
				if (Gurl.indexOf(aFolder[i]) != -1) {curCat = oTmp; break;}
			}
			else if (GcurPageLC == aFile[i].toLowerCase()) curCatFromFile = oTmp;
		}
		if (curCat != null) break;
		oTmp = oTmp.nxtItem;
	}
	if (curCat != null) return curCat.elemID;
	else if (curCatFromFile != null) return curCatFromFile.elemID;
	else return null;
}
// display main and secondary navs
function wrtNavs(lvl, sSecMenuID) {
	Gpref = "";		// global, defined at top
	if (lvl == -1) Gpref = "/";	// used in error pages
	for (var i=1;i<=lvl;i++) Gpref = Gpref + "../";
	wrtTopBanner(); 
	document.writeln('<div id="nav">');
	wrtMainNav(); wrtSecNav(sSecMenuID); 
	document.writeln('</div>');	
	if (sSecMenuID != "") {
		var pos = sSecMenuID.indexOf("_");
		GCurrMainMenu = sSecMenuID.substr(0,pos);	// e.g. if sSecMenuId = 'about_facilities', this is set to 'about'
	}	
	showSecNav(document.getElementById(GCurrMainMenu));
}
// display top banner (before navs)
function wrtTopBanner() {
	var str1 = '<div id="customizerLine"><div id="customizers">Text Size: &nbsp;';
	// text size selector
	for (var i=1; i<=3; i++) {
		str1 += "<span id='sampleFntShell" + i + "' class='pointer'><span class='content-TextSize-" + i + "' onclick='chgTextSize(" + i + ");'> A</span></span>&nbsp;";
	}	
	str1 += "</div>";	// end div customizers
	str1 += '<br class="clearB" /></div>';	// end div customizerLine	
	str1 += '<div id="topBanner">';
	var homepage = "index_e.php";
	str1 += '<a href="' + Gpref + homepage + '"><img src="' + Gpref + 'images/topBanner_01.jpg" alt="" /></a><img src="' + Gpref + 'images/topBanner_02.jpg" alt="" />';
	str1 += '</div>';	// end div topBanner
	document.writeln(str1);
}
// init text size based on cookie val
function initTextSize() {setTextSize(getCookie("textSize"));}
// set text size
function setTextSize(intFontNum) {
	if (intFontNum == null) intFontNum = 1;
	intFontNum = parseInt(intFontNum);
	var el = document.getElementById("wrap0");
	el.className = "content-TextSize-" + intFontNum;
	for (var i=1; i<=3; i++) {
		if (i == intFontNum) document.getElementById("sampleFntShell" + i).className = "textSizeOn pointer";
		else document.getElementById("sampleFntShell" + i).className = "textSizeOff pointer";
	}
}
// change text size; update cookie
function chgTextSize(intFontNum) {
	setTextSize(intFontNum);
	setCookie("textSize", intFontNum, GCookieMinToLive, "/");
}
// display main nav
function wrtMainNav() {
	var str1 = '<div id="mainNav"><ul>';
	var oTmp = GoMainMenu;
	while (oTmp != null) {
		str1 += '<li id="' + oTmp.elemID + '"';
		if (oTmp.elemID == GCurrMainMenu) str1 += ' class="curMainCat"';
		str1 += '><a href="' + oTmp.linkURL + '">';
		if (oTmp.sClassName != "") str1 += '<span class="' + oTmp.sClassName + '">';
		str1 += oTmp.text;
		if (oTmp.sClassName != "") str1 += '</span>';
		str1 += '</a></li>\n';
		oTmp = oTmp.nxtItem;
	}
	document.writeln(str1 + '</ul></div>');
}
// Get absoluteURL
function getAbsoluteURL(relativeURL, isHTTPS) {	
	var upNumLevels = relativeURL.match(/\.\./g);	// no. of levels to traverse up
	if (upNumLevels == null) upNumLevels = 0;	
	var inFullPath = Gurl.substring(GPathStartPos);
	var outFullPath = "http";
	if (isHTTPS) outFullPath += "s://";
	else outFullPath += "://";	
	var iSlashPos;
	for (var i=0;i<=upNumLevels;i++) {
		iSlashPos = inFullPath.indexOf("/") + 1;
		outFullPath += inFullPath.substring(0,iSlashPos);
		inFullPath = inFullPath.substring(iSlashPos+1);
	}	
    outFullPath += relativeURL;	
	return outFullPath;
}
/* write out one secondary nav
// parameters:
//		oMenu - a menu object
//		strSecMenuID - if a page is not directly accessible from the secondary nav, specify the secondary nav menu ID to link it to one
*/
function wrtSecMenu(oMenu, strSecMenuID) {
	var oTmp = oMenu;
	var str1 = "";
	var isCurPage;
	var strPartialPath;
	var GurlLC = Gurl.toLowerCase();
	var isFirstItem = true;
	while (oTmp != null) {
		isCurPage = false;
		strPartialPath = (oTmp.aFolder[0] + oTmp.aFile[0]).toLowerCase();
		if (GurlLC.indexOf(strPartialPath) >= 0) isCurPage = true;	
		if (!isCurPage) {
			if (strSecMenuID != "") {
				if (strSecMenuID == oTmp.menuID) isCurPage = true;
			}
		}
		str1 += '<li';
		if (isFirstItem) str1 += ' id="firstItm"';
		if (isCurPage) str1 += ' class="curSecPage"';
		str1 += '><a href="' + oTmp.linkURL + '">';				
		if (oTmp.sClassName != "") str1 += '<span class="' + oTmp.sClassName + '">';
		str1 += oTmp.text;
		if (oTmp.sClassName != "") str1 += '</span>';
		str1 += '</a></li>\n';		
		oTmp = oTmp.nxtItem;
		isFirstItem = false;
	}
	document.writeln(str1);
}
// Build main menu
//*** add one 'oItem = new menuItem' line for each main menu item
function bldMainMenu() {
	var oItem;
	var oMenu = new menuItem("", "def", "Home", "", false, null, "", "");
	oItem = new menuItem("", "about", "About Us", "", false, oMenu, "about/", "location_e.php");
	oItem = new menuItem("", "montessori", "About Montessori", "", false, oItem, "mon/", "methods_e.php");
	oItem = new menuItem("", "programs", "Programs", "", false, oItem, "pgm/", "programs_e.php");
	oItem = new menuItem("", "calendar", "Calendar", "", false, oItem, "", "calendar_e.php");
	oItem = new menuItem("", "admissions", "Admissions", "", true, oItem, "admissions/", "defAdm_e.php");
	oItem = new menuItem("", "gallery", "Gallery", "", false, oItem, "gallery/", "gallery_e.php");
	oItem = new menuItem("", "admissions-c", "招生簡章", "", false, oItem, "", "enroll_c.php");
	return oMenu;
}
// write out all secondary navs, but do not display them yet.
function wrtSecNav(strSecMenuID) {
	document.writeln('<div id="secNav">');
	document.writeln('<div id="def-2"><ul><li>&nbsp;</li></ul></div>');
	document.writeln('<div id="about_2"><ul>'); wrtSecMenu(bldSecMenu_about(), strSecMenuID); document.writeln('</ul></div>');
	document.writeln('<div id="montessori_2"><ul>'); wrtSecMenu(bldSecMenu_montessori(), strSecMenuID); document.writeln('</ul></div>');
	document.writeln('<div id="programs_2"><ul>'); wrtSecMenu(bldSecMenu_programs(), strSecMenuID); document.writeln('</ul></div>');	
	document.writeln('<div id="gallery_2"><ul>'); wrtSecMenu(bldSecMenu_gallery(), strSecMenuID);	document.writeln('</ul></div>');
	document.writeln('</div>');
}
//*** for each main menu item that has subitems, add a function here, like the one below

// build secondary nav for category ABOUT
function bldSecMenu_about() {
	var oItem;
	var oMenu = new menuItem("about_location", "", "Location / Contact Info", "", false, null, "about/", "location_e.php");
	oItem = new menuItem("about_facilities", "", "Facilities", "", false, oMenu, "about/", "facilities_e.php");
	oItem = new menuItem("about_careers", "", "Careers", "", false, oItem, "about/", "careers_e.php");
	return oMenu;
}

// build secondary nav for category MONTESSORI
function bldSecMenu_montessori() {
	var oItem;
	var oMenu = new menuItem("montessori_outcomes", "", "Teaching Method", "", false, null, "mon/", "methods_e.php");
	oItem = new menuItem("montessori_methods", "", "Outcomes", "", false, oMenu, "mon/", "outcomes_e.php");
	oItem = new menuItem("montessori_alumni", "", "Notable Alumni", "", false, oItem, "mon/", "alumni_e.php");
	return oMenu;
}

// build secondary nav for category PROGRAMS
function bldSecMenu_programs() {
	var oItem;
	var oMenu = new menuItem("programs_list", "", "Program List", "", false, null, "pgm/", "programs_e.php");
	oItem = new menuItem("programs_toddler", "", "Toddler (N1)", "", false, oMenu, "pgm/", "toddler_e.php");
	oItem = new menuItem("programs_precasa", "", "Pre-Casa (N2)", "", false, oItem, "pgm/", "precasa_e.php");
	oItem = new menuItem("programs_casa", "", "Casa (K1-K3)", "", false, oItem, "pgm/", "preschool_e.php");
	oItem = new menuItem("programs_summer", "", "Summer Camp", "", false, oItem, "pgm/", "summer2011_e.php");
	oItem = new menuItem("programs_wingChun", "", "Martial Arts Summer", "", false, oItem, "pgm/", "wingChun_e.php");
	return oMenu;
}

// build secondary nav for category GALLERY
function bldSecMenu_gallery() {
	var oItem;
	var oMenu = new menuItem("gallery_general", "", "General Gallery", "", false, null, "gallery/", "gallery_e.php");
	oItem = new menuItem("gallery_class", "", "Class Galleries", "", false, oMenu, "gallery/byClass", "");
	return oMenu;
}

function mainMouseOver(e) {
	GelTarget = YAHOO.util.Event.getTarget(e);
	if (GnavDelayTimer != null) {clearTimeout(GnavDelayTimer); GnavDelayTimer = null;}
	GnavDelayTimer = setTimeout("doMainMouseOver()", GnavDelay);
}
function doMainMouseOver() {
	if (GnavResetTimer != null) {clearTimeout(GnavResetTimer); GnavResetTimer = null;}
	if (GnavDelayTimer != null) {clearTimeout(GnavDelayTimer); GnavDelayTimer = null;}
	while (GelTarget.id != "mainNav") {
	    if (GelTarget.nodeName.toLowerCase() == "li") {showSecNav(GelTarget); break;}
		else GelTarget = GelTarget.parentNode;
	}
}
function navMouseOver(e) {if (GnavResetTimer != null) {clearTimeout(GnavResetTimer); GnavResetTimer = null;}}
function navMouseOut(e) {
	if (GnavResetTimer == null) GnavResetTimer = setTimeout("resetNav()", GnavReset);
	if (GnavDelayTimer != null) {clearTimeout(GnavDelayTimer); GnavDelayTimer = null;}
}
function resetNav() {GnavResetTimer = null; showSecNav(document.getElementById(GCurrMainMenu));}
function getFilename(url) {
	var quesPos = url.indexOf("?");
	if (quesPos == -1) quesPos = url.length + 1;
	var localURL = url.substring(0,quesPos);
	localURL = localURL.substring(localURL.lastIndexOf("/")+1);
	return localURL;
}
function getFilenameLC(url) {
	return getFilename(url).toLowerCase();
}
function getFileExt(url) {
	var sFilename = getFilename(url);			
	var iTmpPos = sFilename.lastIndexOf(".");
	return sFilename.substr(iTmpPos+1, sFilename.length);
}
// -------- end of nav functions --------

function searchIt() {location.href = Gpref + "search.php";}

GcurPageLC = getFilenameLC(Gurl);
GoMainMenu = bldMainMenu();
GCurrMainMenu = getCurrMainMenuID();
YAHOO.util.Event.on("mainNav", "mouseover", mainMouseOver);
YAHOO.util.Event.on("nav", "mouseover", navMouseOver);
YAHOO.util.Event.on("nav", "mouseout", navMouseOut);

// --- below originally from common.js ---

var Gwin1 = null, Gwin2 = null, GlinkWin = null, GpicWin = null;
function openWin(strUrl, strWinName) {	// strWinName = 1 or 2
	eval("GWin" + strWinName + "= window.open(strUrl,'" + strWinName + "');");
	eval("Gwin" + strWinName + ".focus();"); 
}
function openLink(strUrl) {
	if (GlinkWin != null) {
		if (!GlinkWin.closed) GlinkWin.close();
	}
	GlinkWin = window.open(strUrl);	GlinkWin.focus();
}
function fixDate(date) {
	var base = new Date(0);
	var skew = base.getTime();
	if (skew > 0) date.setTime(date.getTime() - skew);
}
function setCookie(name, value) {
	var argv = setCookie.arguments;
	var argc = setCookie.arguments.length;
	var minToLive = (argc > 2) ? argv[2] : null;	// 3rd arg - minutes to live
	var path = (argc > 3) ? argv[3] : null;	// 4th arg
	var domain = (argc > 4) ? argv[4] : null;	// 5th arg
	var secure = (argc > 5) ? argv[5] : false;	// 6th arg
	var expiryDt = null;
	if (minToLive != null) {
		var nowTime = new Date;
		fixDate(nowTime);
		expiryDt = new Date(nowTime.getTime() + minToLive*60*1000);
	}
	document.cookie = name + "=" + escape(value) +
	((expiryDt == null) ? "" : ("; expires=" + expiryDt.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}
function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}
function deleteCookie(name) {
	var argv = deleteCookie.arguments;
	var argc = deleteCookie.arguments.length;
	var path = (argc > 1) ? argv[1] : null;	// 2nd arg
	var domain = (argc > 2) ? argv[2] : null;	// 3rd arg
	if (getCookie(name)) document.cookie = name + "=; expires=Wed, 10 Jan 2007 00:00:01 GMT" +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain));
}
function getURLParam(sParamName) {
	var sReturn = "";
	if (Gurl.indexOf("?") > -1) {
		var sQueryStr = Gurl.substr(Gurl.indexOf("?"));
		var aQueryStr = sQueryStr.split("&");
		for (var i=0; i<aQueryStr.length; i++) {
			if (aQueryStr[i].indexOf(sParamName + "=") > -1) {
				var aParam = aQueryStr[i].split("=");
				sReturn = aParam[1];
				break;
			}
		}
	}
	return sReturn;
}
