// *********************************************************************************
// MERLIN SOFTWARE ENGINEERING INC. COPYRIGHT 2009, non-profit user license
// *********************************************************************************
// NTSC Javascript Menu
// ---------------------------------------------------------------------------------
// Sep 2009 - used by both static and dynamic pages
//			--- dns variables required for shared SSL compatibility
//			--- loadMenu() called from footer.js (static) or footer inc (dynamic)
//			--- showMenu() called via loadXMLDoc() for static or, directly from
//						   loadMenu() for dynamic
//
//			*** webmasters please use: openMember(s), openContact(s), openHelp(p)
//				and openGallery(p) to display these pages where ever possible
//
//          - pure CSS menu design by Peter Liepa, brainjam.ca
// *********************************************************************************

// --- webmasters may uncomment these lines for local computer/ test deployments
//top.wwwServer  = "http://www.ntsc.ca/";
//top.sslServer  = "http://www.ntsc.ca/";

// --- this should point to the web server address of your local server
//top.htmlServer = "http://localhost/";

if(top.wwwServer == undefined) top.wwwServer = "/";
if(top.sslServer == undefined) top.sslServer = "/";
if(top.htmlServer == undefined) top.htmlServer = "/";

// --- new menubar functions --- param= logged in: 0|1 , mobile user agent
function showMenu(s) {
	if(!s) s="";
	var a = s.split(","); top.uid = parseInt(a[0]);
	if(a.length>1) top.mobile = a[1];
	if(top.mobile == undefined) top.mobile = "";

	if(top.mobile != "" && top.mobile != "turn-off") {
		// --- reset static page layout if mobile mode ON (dynamic handled in server includes)
		setTimeout("mobileMenu()",100);
	} else {
		// --- display normal page layout side menu bar
		var arrow1 = "<img src='" + top.sslServer + "html/src/m_arrow.gif' align='right' width='9' height='9' hspace='5' vspace='3'>";
		var arrow2 = "<img src='" + top.sslServer + "html/src/m_arrow2.gif' align='right' width='9' height='9' hspace='5' vspace='3'>";
		var s = "<ul>"; var img = "<img width='0' height='1'>";
		s += "<li><a href='" + top.wwwServer + "html/index.html'>HOME" + img + "</a></li>";
		if(top.uid == 0) {
			s += "<li><a href=\"javascript:openMember('login')\">Member Login" + img + "</a></li>";
		} else {
			s += "<li class='account'><a href=\"javascript:openMember('update')\" class='account'>" + arrow2 + "Member Account</a>";
			s += "<ul class='account'>";
			s += "<li class='account'><a href='" + top.wwwServer + "en/newsignup.asp' class='account'>New Sign-up" + img + "</a></li>";
			s += "<li class='account'><a href='" + top.wwwServer + "en/mytrips.asp' class='account'>My Day Trips" + img + "</a></li>";
			s += "<li class='account'><a href='" + top.wwwServer + "en/myphotos.asp' class='account'>My Photos" + img + "</a></li>";
			s += "<li class='account'><a href=\"javascript:openMember('update')\" class='account'>My Profile" + img + "</a></li>";
			s += "<li class='account'><a href='" + top.wwwServer + "en/whoswho.asp' class='account'>Who's Who" + img + "</a></li>";
			s += "<li class='account'><a href=\"javascript:openHelp('2009redesign.html')\">Enhancements" + img + "</a></li>";
			s += "<li class='account'><a href='" + top.sslServer + "en/signout.asp' class='account'>Logout" + img + "</a></li>";
			s += "</ul>";
			s += "</li>";
		}
		s += "<li><a href='" + top.htmlServer + "html/about_ntsc.html'>" + arrow1 + "Club Information</a>";
		s += "<ul>";
		s += "<li><a href='" + top.htmlServer + "html/about_ntsc.html'>About NTSC " + img + "</a></li>";
		s += "<li><a href='" + top.htmlServer + "html/membership_info.html'>Membership " + img + "</a></li>";
		s += "<li><a href='" + top.htmlServer + "html/directors.html'>Directors" + img + "</a></li>";
		s += "<li><a href=\"javascript:openHelp('faq1.html')\">FAQ" + img + "</a></li>";
		s += "</ul>";
		s += "</li>";
		s += "<li><a href='" + top.htmlServer + "html/awards_dohorty.html'>" + arrow1 + "Club Honours</a>";
		s += "<ul>";
		s += "<li><a href='" + top.htmlServer + "html/awards_dohorty.html'>T. Dohorty Award" + img + "</a></li>";
		s += "<li><a href='" + top.htmlServer + "html/awards_dougferrier.html'>D. Ferrier Award" + img + "</a></li>";
		s += "<li><a href='" + top.htmlServer + "html/awards_floydlevitt.html'>Floyd/Levitt Award" + img + "</a></li>";
		s += "<li><a href='" + top.htmlServer + "html/awards_presidents.html'>Past Presidents" + img + "</a></li>";
		s += "</ul>";
		s += "</li>";
		s += "<li><a href='" + top.htmlServer + "html/snow_school_overview.html'>" + arrow1 + "Snow Schools</a>";
		s += "<ul>";
		s += "<li><a href='" + top.htmlServer + "html/snow_school_ski.html'>Alpine Ski " + img + "</a></li>";
		s += "<li><a href='" + top.htmlServer + "html/snow_school_snowboard.html'>Snowboard" + img + "</a></li>";
		s += "<li><a href='" + top.htmlServer + "html/snow_school_nordic.html'>Cross Country" + img + "</a></li>";
		s += "<li><a href='" + top.htmlServer + "html/snow_school_overview.html'>Overview " + img + "</a></li>";
		s += "</ul>";
		s += "</li>";
		s += "<li><a href='" + top.wwwServer + "en/daytrips.asp?updates=1&notes=1'>" + arrow1 + "Day Trips</a>";
		s += "<ul>";
		s += "<li><a href='" + top.wwwServer + "en/daytrips.asp?alpine=1'>Alpine/ Boarding Schedule" + img + "</a></li>";
		s += "<li><a href='" + top.wwwServer + "en/daytrips.asp?nordic=1'>Cross Country Schedule" + img + "</a></li>";
		s += "<li><a href='" + top.htmlServer + "html/trips_daytrip_alpine_overview.html'>Overview " + img + "</a></li>";
		s += "<li><a href=\"javascript:openHelp('daytrippickups.html')\">Pick-up Locations" + img + "</a></li>";
		s += "<li><a href=\"javascript:openHelp('faq2.html')\">FAQ" + img + "</a></li>";
		s += "</ul>";
		s += "</li>";
		s += "<li><a href='" + top.wwwServer + "en/charters.asp?updates=1&notes=1'>" + arrow1 + "Charter Trips</a>";
		s += "<ul>";
		s += "<li><a href='" + top.wwwServer + "en/charters.asp?updates=1&notes=1'>Updates" + img + "</a></li>";
		s += "<li><a href='" + top.wwwServer + "en/charters.asp?weekend=1'>Weekend Schedule" + img + "</a></li>";
		s += "<li><a href='" + top.wwwServer + "en/charters.asp?weeklong=1'>Weeklong Schedule" + img + "</a></li>";
		s += "<li><a href='" + top.wwwServer + "html/trips_charter_overview.html'>Overview " + img + "</a></li>";
		s += "<li><a href=\"javascript:openHelp('charterpickups.html')\">Pick-up Locations" + img + "</a></li>";
		s += "<li><a href=\"javascript:openHelp('faq3.html')\">FAQ" + img + "</a></li>";
		s += "</ul>";
		s += "</li>";
		s += "<li><a href='" + top.htmlServer + "html/socials_general.html'>" + arrow1 + "Social Functions</a>";
		s += "<ul>";
		s += "<li><a href='" + top.htmlServer + "html/socials_general.html'>General" + img + "</a></li>";
		s += "<li><a href='" + top.htmlServer + "html/socials_special.html'>Special" + img + "</a></li>";
		s += "</ul>";
		s += "</li>";
		s += "<li><a href='" + top.htmlServer + "html/member_events.html'>Member Events" + img + "</a></li>";
		s += "<li><a href='" + top.htmlServer + "html/hiking.html'>Hiking" + img + "</a></li>";
		s += "<li><a href=\"javascript:openHelp()\">Help Centre" + img + "</a></li>";
		s += "<li><a href=\"javascript:openContact()\">Contact Us" + img + "</a></li>";
		s += "</ul>";

		// --- random photo gallery image menu
		s += "<div style='margin-top: 10px; margin-bottom: 0px;' class='css_sidebar' title='Click to view our Photo Gallery' onClick=\"openGallery(1)\">";
		s += "<p style='margin-top: 0px; margin-bottom: 5px;'><img id='randomImg' src='" + top.sslServer + "_src/get_photo.asp' ";
//		s += "onload=\"setTimeout('randomPhoto()',5000)\" ";
		s += "style='border: #444444 solid 1px;' width='134' height='103' border='0'></p>";
		s += "NTSC Photo Gallery";
		s += "</div>";

		// --- newsletter image menu
		s += "<div class='css_sidebar' title='Click to view our Weekly Newsletter Update' onClick=\"openpage('" + top.sslServer + "en/newsletters.asp','parent')\">";
		s += "<p style='margin-top: 0px; margin-bottom: 5px;'><img src='" + top.sslServer + "en/img/newsletter.gif' style='border: #444444 solid 1px;' width='100' height='120' border='0'></p>";
		s += "NTSC Newsletter";
		s += "</div>";

		var obj = getHtmlObj("navigation");
		if(obj) obj.innerHTML = s;
	}
}
function randomPhoto() {
	var obj = getHtmlObj("randomImg");
	if(obj) { obj.src = top.sslServer + "_src/get_photo.asp"; }
}

// --- mobile device page layout
function mobileMenu() {
	var s = "<div class='css_mobile'>";
	s += "<a href='" + top.wwwServer + "en/daytrips.asp?mobile=off' title='Turn Small Layout OFF' class='css_mobile1'>OFF</a>&nbsp; ";
//	if(top.uid == 0) {
//		s += "<a href=\"javascript:openMember('login')\" title='Member Account Login' class='css_mobile2'>LOG</a>&nbsp; ";
//		s += "<span class='css_mobile3'>|</span>&nbsp; ";
//	} else {
//		s += "<a href='" + top.sslServer + "en/signout.asp' title='Logout of your Member Account' class='css_mobile1'>OUT</a>&nbsp; ";
//		s += "<span class='css_mobile3'>|</span>&nbsp; ";
//		s += "<a href='" + top.wwwServer + "en/newsignup.asp' title='New Trip Signup' class='css_mobile3'>Book</a>&nbsp; ";
//		s += "<a href='" + top.wwwServer + "en/mytrips.asp' title='My Day Trips' class='css_mobile3'>Hist</a>&nbsp; ";
//		s += "<a href='" + top.wwwServer + "en/myphotos.asp' title='My Photos' class='css_mobile3'>Photo</a>&nbsp; ";
//		s += "<a href='" + top.wwwServer + "en/whoswho.asp' title='Who's Who' class='css_mobile3'>Who</a>&nbsp; ";
//		s += "<a href=\"javascript:openMember('update')\" title='My Profile' class='css_mobile3'>Me</a>&nbsp; ";
//		s += "<span class='css_mobile2'>|</span>&nbsp; ";
//	}
	s += "<a href='" + top.wwwServer + "en/daytrips.asp?updates=1&notes=1' title='Day Trips' class='css_mobile2'>Day Trips</a>&nbsp; ";
	s += "<a href='" + top.wwwServer + "en/charters.asp?updates=1&notes=1' title='Charter Trips' class='css_mobile2'>Charters</a>&nbsp; ";
	s += "<a href='" + top.wwwServer + "html/socials_general.html' title='General Socials' class='css_mobile2'>Social</a>&nbsp; ";
	s += "<a href='" + top.wwwServer + "en/gallery.asp' title='Photo Gallery' class='css_mobile2'>Gallery</a>&nbsp; ";
	s += "<a href='" + top.wwwServer + "html/index.html' title='Welcome Page' class='css_mobile2'>Home</a>";
	s += "</div>";
	var obj = getHtmlObj("navbar1");
	if(obj) obj.innerHTML = s + "<style>#content {margin: 0px;} #navigation {width: 0px;}</style>";
	var obj = getHtmlObj("navbar2");
	if(obj) obj.innerHTML = s;
	var obj = getHtmlObj("container2");
	if(obj != undefined) obj.innerHTML = "";
	var obj = getHtmlObj("footer_ntsc");
	if(obj != undefined) obj.innerHTML = "NTSC";
	var obj = getHtmlObj("footer_merlin");
	if(obj != undefined) obj.innerHTML = "MERLIN";
}
// --- non-mobile device resize testing (ignore if popup window or mobile user agent)
function setLayout() {
	if(top.popupWin != undefined || top.mobile == undefined || (top.mobile != "" && top.mobile != "turn-on")) return;
	var a = top.windowRes(); var s = "";
	if(a[0] < 400 && top.mobile != "turn-on") s = "on";
	else if(a[0] > 400 && top.mobile != "") s = "off";
	if(s != "") window.location.href = top.wwwServer + "en/daytrips.asp?mobile=" + s;
}
window.onresize = setLayout;

// --- new menu load functions
var req=null;
function loadXMLDoc(url) {
	if(window.XMLHttpRequest) req = new XMLHttpRequest();
	else if(window.ActiveXObject) req = new ActiveXObject("Microsoft.XMLHTTP");
	if(req) {
		try {
			req.open("GET", url, false);
			req.send(null);
		}
		catch(err) { }
		if(req.readyState == 4 && req.status == 200) {
			showMenu(req.responseText);	return;
		}
	}
	showMenu("0,");
}
function loadMenu() {
	var a = top.windowRes();
	if(top.popupWin == true && opener == undefined) {
		openpage(self.location.href,"window");
		self.history.go(-1);
	} else if(top.popupWin == undefined) {
		if(top.uid != undefined) { showMenu((top.uid + "," + top.mobile)); } 
		else {
			var s = "wd=" + a[0] + "&ht=" + a[1] + "&pg=" + escape(self.location.href);
			loadXMLDoc(top.wwwServer + "_src/get_state.asp?" + s);
		} 
	}
}

// --- new booking system support functions
function openNTSC(s) {
	if(s == undefined || s == "") return;
	if(opener == undefined) { self.location.href = top.wwwServer + s; } 
	else if(confirm("NOTE: This window will now close, so you may continue in the main website.\n\nPress OK to continue.")) {
		opener.location.href = top.wwwServer + s;
		self.close();
	}
}
function openWin(s) {
	var a = windowRes(); a[1] -= 50;
	if(a[0] > 800) a[0] = 800;
	if(a[1] < 200) a[1] = 200;
	openpage(top.wwwServer + s, "window", a[0], a[1]);
}
function openMember(s) {
	if(s == undefined || s == "") s = "login";
	openNTSC("portal.asp?mode=" + s);
}
function openContact(s) {
	if(s == undefined) s = "";
	openNTSC("en/contact.asp?s=" + s);
}
function openHelp(s) {
	if(s == undefined || s == "") s = "help.html";
	openWin("html/help/" + s);
}
function openGallery(s) {
	if(s == undefined) s = "";
	openWin("en/gallery.asp?r=" + s);
}
var newWin; var newWiz;
function openpage(pn,frm,w,h,x,y) {
	if(pn == undefined || pn == "") return;
	if(frm == undefined || frm == "") frm="parent";
	if(x == undefined) x=20;
	if(y == undefined) y=20;
	if(frm == "wizard") {
		if(w == undefined) w=350;
		if(h == undefined) h=475;
	} else if(frm == "window") {
		if(w == undefined) w=700;
		if(h == undefined) h=450;
	}
	if(x == -1) x = Math.floor((screen.width/2) - (w/2));
	if(y == -1) y = Math.floor((screen.height/2) - (h/2));
	if(frm == "wizard") {
		if(top.newWiz && !top.newWiz.closed) top.newWiz.close();
		newWiz = window.open(pn, "newWiz", "scrollbars=yes,top=" + x + ",left=" + y + ",width=" + w + ",height=" + h);
	} else if(frm == "window") {
		if(top.newWin && !top.newWin.closed) top.newWin.close();
		newWin = window.open(pn, "newWin", "menubar=yes,toolbar=yes,status=yes,scrollbars=yes,resizable=yes,top=" + x + ",left=" + y + ",width=" + w + ",height=" + h);
	} else {
		var s = "self.";
		if(frm == "top") s = "self.top.";
		else if(frm == "parent") s = "self.parent.";
		else if(frm == "opener") s = "self.window.opener.";
		else if(frm != "self") s = "top." + frm + ".window.";
		if(window.location.replace == undefined) eval(s + "location.href=pn;");
		else eval(s + "location.replace(pn);");
	}
}
function getHtmlObj(s) {
	var obj = undefined;
	if(document.all) obj = document.all[s];
	if(obj == undefined && document.getElementById) obj = document.getElementById(s);
	if(obj == undefined) eval("obj = window." + s + ";");
	if(obj == undefined) eval("obj = self." + s + ";");
	if(obj == undefined) eval("obj = " + s + ";");
	return obj;
}
function windowRes() {
	var w=0; var h=0;
	if(self.innerHeight) {
		w = self.innerWidth; h = self.innerHeight;
	} else if(window.innerHeight) {
		w = window.innerWidth; h = window.innerHeight;
	} else if(document.documentElement && document.documentElement.clientHeight) {
		w = document.documentElement.clientWidth; h = document.documentElement.clientHeight;
	} else if(document.body) {
		w = document.body.clientWidth; h = document.body.clientHeight;
	}
	return [w,h];
}
