﻿// JavaScript Document, Produced by Francis Kwok Chi Fai 2004-2005 LDCWKMSS HK.
var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;

// 處理 Flash 影片中的所有 FSCommand 訊息。
function MenuBar_DoFSCommand(command, args) {
	if(command == "RetrieveURL"){
		GetURLForFlashMenuBar(args);
	}else if(command == "UpdateSubItemViewstate"){
		TopMenuBar_SetCookie(args);
	}
}

// Hook for Internet Explorer.
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<script language=\"VBScript\"\>\n');
	document.write('On Error Resume Next\n');
	document.write('Sub MenuBar_FSCommand(ByVal command, ByVal args)\n');
	document.write('	Call MenuBar_DoFSCommand(command, args)\n');
	document.write('End Sub\n');
	document.write('</script\>\n');
}

// ************** Functions of using cookie to preserve viewstate of top MenuBar **************
function TopMenuBar_SetCookie(SubItemCode){
	document.cookie = "TopMenuBarSubItem=" + escape(SubItemCode) + ";path=" + escape("/");
}

function TopMenuBar_GetCookie(){

	var MenuBarObj = null;
	if(isInternetExplorer){
		MenuBarObj = window.MenuBar;
	}else{
		MenuBarObj = window.document.MenuBar;
	}

	var dc = document.cookie;
	var search = "TopMenuBarSubItem=";
	if (dc.length > 0) { // if there are any cookies
		offset = dc.indexOf(search);
		if (offset != -1) { // if cookie exists
			offset += search.length;
			// set index of beginning of value
			end = dc.indexOf(";", offset);
			// set index of end of cookie value
			if (end == -1)
				end = dc.length;
			MenuBarObj.SetVariable("MenuBarSubItem", unescape(dc.substring(offset, end)));
			return;
		}
	}
	MenuBarObj.SetVariable("MenuBarSubItem", "0");

}
// ************** End of Functions of using cookie to preserve viewstate of top MenuBar **************

// ************** Functions of retriving relative path of targeting URL **************
function GetURLForFlashMenuBar(url){
	if (navigator.appName.indexOf("Microsoft") != -1){
		// IE
		var layerNum = CountLayer();
		var relativeParent = "";
		for (var i = 0;i < layerNum ;i++){
			relativeParent += "../";
		}
		window.location = relativeParent + url;
	}else{
		// Netscape
		window.location = "http://210.0.205.74/LingnanWebsite/Pages/" + url;
	}
}

function CountLayer(){
  var absolutePath = window.document.location.toString();
  var nodes = absolutePath.split("/");
  var layerNum = 0;
  //Loop from reverse order
  for (var i = nodes.length-2;i >= 0 ;i--){
    if (nodes[i] == "Pages"){
      return layerNum;
    }else{
		layerNum++;
	}
  }
  return 0;
}
// ************** End of Functions of retriving relative path of targeting URL **************
