/**********************************************************/
/* ProjectArcix: system.js      					      */
/* This file contains all functions for navigation and    */
/* content-loading stuff								  */
/* last modified: 12/04/2006							  */
/**********************************************************/

var lastURL = window.location.href; //last location
var Loading = false;

// Top Level Site Construction
function loadSite(src, destId)
{
	var RightBar = GetBoolRBar();
	var RightBar = GetBoolRBar();
	var Anker = GetAnker();
	var SubNavL = GetSubNav();

    var n;
    if (window.location.href.split("#")[1]) {
        n = window.location.href.split("#")[1];
    }
    else { n = "main_indexcontent_0_0"; }

	var Link = n.split("_");	
	
	// Button active/passive
 	ActiveButton(Anker);	
	
	// Coordinate Sub-Navs on left side
	 if ( SubNavL != "none" ) { ShowSubMenu(SubNavL); } 
	 else { 
		if ( OpenSubMenuL != "none" ) {
			HideSubMenu(OpenSubMenuL);
			OpenSubMenuL = "none";
		}
	 }

	// case category=wildfire do not show quotes&facts
	if (Link[1] == "wildfire") {	
		document.getElementById("FactsBlock").style.display = "none";
		document.getElementById("QuotesBlock").style.display = "none";	
	}

	 if ( Link[2] == "0" ) {
	 	if ( ButtonLSActive != "none" && Link[0] != "media" && Link[0] != "about" && Link[0] != "help") {  		
	 		document.getElementById(ButtonLSActive).className = "link";
			ButtonLSActive = "none";
		}		
	}
	 	
	if ( RightBar == "1" ) { window.setTimeout("LoadNormalCSS()",10); } else { window.setTimeout("LoadNoRightCSS()",10); }

	CtrlBreadCrump(Link);

	docClickLoader.loadInto(src, "targetArea");
}

function getPageN() {
	//cal getPage with the parameters
	if (Init == false && appIE()) {
		var Hash;
		if (window.location.href.split("#")[1]) { Hash = window.location.href.split("#")[1]; } else { Hash = "main_indexcontent_0_0"; }
		window.setTimeout("LoadingMsg()", 10);

		loadSite(TranslateURL(Hash),"targetArea");

		Init = true;
    } else {
		LoadingMsg();
		getPage(getPageName());
	}
	if (appIE()){ chkURL();	} 
}

function appIE() {
	if (navigator.appName == "Microsoft Internet Explorer"){ return true; } else { return false; }	
}

function chkURL() {
	if (window.location.href != lastURL) {
		if ( window.location.hash == "" ) { window.location.hash = "#main_indexcontent_0_0"; }
		Loading = true;
        lastURL = window.location.href;
		getPageN();		
	} else { Loading = false; }
}

window.setTimeout("chkURL()", 100);
window.setTimeout("CheckURL()", 100);

//periodically check if the location has changed to change the page if so
function CheckURL() {
	if (window.location.href != lastURL) {
		ChangePage();
	}
	window.setTimeout("CheckURL()", 100);
}

// This function returns 
function TranslateURL(hash){
	var Link = hash.split("_");
	for (var i = 0; i <= 2; i++)
		Link[i] = "/" + Link[i];

	// If Pagelink = 0 set Link[3] = ""
	Link[3] = (Link[3] != "0") ? "#" + Link[3] : "";

	if (Link[2] == "/0") {
    	Link[1] = Link[1] + ".html";
		Link[2] = "";		
  	} else {
    	Link[2] = Link[2] + ".html";
  	}

	return "." + Link[0] + Link[1] + Link[2] + Link[3];
}

// Determs if the right bar is needed
function GetBoolRBar() {
    var n;
    if (window.location.href.split("#")[1]) {
        n = window.location.href.split("#")[1];
    }
    else { n = "main_indexcontent_0_0"; }
	
	var Link = n.split("_");

	if (Link[2] == "0") {
    	return "0";
  	} else {
		return "1";
	}	
}

// Determs the button on the left side-navigation
function GetAnker() {
    var n;
    if (window.location.href.split("#")[1]) {
        n = window.location.href.split("#")[1];
    }
    else { n = "main_indexcontent_0_0"; }
	
	var Link = n.split("_");
	
	return "Nav_" + Link[0];
}

// Determs if SubMenu (on left side) is needed
function GetSubNav() {
    var n;
    if (window.location.href.split("#")[1]) {
        n = window.location.href.split("#")[1];
    }
    else { n = "main_indexcontent_0_0"; }
	
	var Link = n.split("_");
	
	switch (Link[0]) {
	  case "main":
		return "none";
		break;
	  case "terminology":
		return "none";
		break;
	  default:
		return "LSubNav_" + Link[0];
		break;
	}
}

// Controlls the whole submenu for the left navigation
function ShowSubMenu(SubMenu){		
	if (OpenSubMenuL != "none") { HideSubMenu(OpenSubMenuL) }
	document.getElementById(SubMenu).style.display = "block";
	OpenSubMenuL = SubMenu;

	var Link = getPageHash().split("_");
	
	if ( Link[0] == "media" && Link[0] == "about" && Link[0] == "help" && Link[1] != "index" ) {
		if (ButtonLSActive == "none" ) {
				document.getElementById("LSubList_" + Link[1]).className = "active";
				ButtonLSActive = "LSubList_" + Link[1];
			} else {
				if (ButtonLSActive != "LSubList_" + Link[1]) {
					document.getElementById("LSubList_" + Link[1]).className = "active";
					document.getElementById(ButtonLSActive).className = "link";
					ButtonLSActive = "LSubList_" + Link[1];
				}
			}
	}

    if (Link[2] != "0") {

	if (ButtonLSActive == "none") {
			document.getElementById("LSubList_" + Link[1]).className = "active";
			ButtonLSActive = "LSubList_" + Link[1];
		} else {
			if (ButtonLSActive != "LSubList_" + Link[1]) {
				document.getElementById("LSubList_" + Link[1]).className = "active";
				document.getElementById(ButtonLSActive).className = "link";
				ButtonLSActive = "LSubList_" + Link[1];
			}
		}		

		document.getElementById("SubMenu").style.display = "block";

		document.getElementById("SubNav_index").href = "#" + Link[0] + "_" + Link[1] + "_index_0";
		document.getElementById("SubNav_consequences").href = "#" + Link[0] + "_" + Link[1] + "_consequences_0";
		document.getElementById("SubNav_citizenresponse").href = "#" + Link[0] + "_" + Link[1] + "_citizenresponse_0";
		document.getElementById("SubNav_govtresponse").href = "#" + Link[0] + "_" + Link[1] + "_govtresponse_0";
		document.getElementById("SubNav_pastincidents").href = "#" + Link[0] + "_" + Link[1] + "_pastincidents_0";
		document.getElementById("SubNav_casestudy").href = "#" + Link[0] + "_" + Link[1] + "_casestudy_0";
		
		document.getElementById(ButtonSActive).className = "link";
		document.getElementById("SubNav_" + Link[2]).className = "active";
		
		ButtonSActive = "SubNav_" + Link[2];
	} else { 
		document.getElementById("SubMenu").style.display = "none";
	}
}

function HideSubMenu(SubMenu){
	document.getElementById(SubMenu).style.display = "none";
}

// Make Buttons active/passive
function ActiveButton(Button){	
	// Buttons on left navigation	
	document.getElementById(ButtonLActive).className = "link";
	document.getElementById("L" + Button).className = "active";
	ButtonLActive = "L" + Button;
	
	// Buttons on right navigation	
	document.getElementById(ButtonTActive).className = "help";
	document.getElementById("T" + Button).className = "active";
	ButtonTActive = "T" + Button;	
}

// Load CSS
var script_element=0;
function LoadCSS(mode)
{
	setActiveStyleSheet(mode);
}

function SetHash(src){
	window.location.hash = src;
}

// Delete a javascript - primary used for the facts&quotes
function DeleteJavaScript(id)
{
  heads=document.getElementsByTagName("head");

  var Knoten = document.getElementById(id);

  heads[0].removeChild(Knoten);
  delete Knoten;
}

// Load different javascripts depending on the browser
function LoadMainJS()
{
  if (navigator.appName == "Microsoft Internet Explorer"){
  	LoadJavaScript("main_ie");
  } else {
  	LoadJavaScript("main_moz");
  }
}

// Init
function Load(){
  //if (Init == true) { alert("Load()"); getPageN(); }
  Init = true;
}

// Sets everything in breadcrump bar
function CtrlBreadCrump(Link) {
	if (Link[0] == "main") { 
		document.getElementById("bd_level2").style.display = "none";
	} else { 
		var rd_Laenge = document.getElementById("bd_a2").firstChild.nodeValue.length;
		document.getElementById("bd_a2").firstChild.replaceData(0, rd_Laenge, BDTranslate_Level2(Link[0]));
		document.getElementById("bd_a2").href = "#" + Link[0] + "_index_0_0";
		document.getElementById("bd_level2").style.display = "inline";
	}	
	
	if (Link[1] != "0" && Link[1] != "indexcontent" && Link[1] != "index") { 
		var rd_Laenge = document.getElementById("bd_a3").firstChild.nodeValue.length;
  		document.getElementById("bd_a3").firstChild.replaceData(0, rd_Laenge, BDTranslate_Level3(Link[1]));
		document.getElementById("bd_a3").href = "#" + Link[0] + "_" + Link[1] + "_index_0";
		document.getElementById("bd_level3").style.display = "inline"; 
		
		// Set SubMenu-Head on the right
		var rd_Laenge = document.getElementById("SubMenuHead").firstChild.nodeValue.length;
  		document.getElementById("SubMenuHead").firstChild.replaceData(0, rd_Laenge, BDTranslate_Level3(Link[1]));
	} else { 
		document.getElementById("bd_level3").style.display = "none"; 
	}

	if (Link[2] != "0" && Link[2] != "index") { 
		var rd_Laenge = document.getElementById("bd_a4").firstChild.nodeValue.length;
  		document.getElementById("bd_a4").firstChild.replaceData(0, rd_Laenge, BDTranslate_Level4(Link[2]));
		document.getElementById("bd_a4").href = "#" + Link[0] + "_" + Link[1] + "_" + Link[2] + "_0";
		document.getElementById("bd_level4").style.display = "inline"; 
	} else { 
		document.getElementById("bd_level4").style.display = "none"; 
	}	
	
	WriteTitle(Link);
}

function WriteTitle(Link) {
	var title_str="ProjectArcix: ";	
	
	if (Link[0] == "main") { 
		title_str = title_str + "Home";
	} else { 
		title_str = title_str + BDTranslate_Level2(Link[0]);
	}	
	
	if (Link[1] != "0" && Link[1] != "indexcontent" && Link[1] != "index") { 
		title_str = title_str + " > " +  BDTranslate_Level3(Link[1]);
	}
	
	if (Link[2] != "0" && Link[2] != "index") { 
		title_str = title_str + " > " +  BDTranslate_Level4(Link[2]);
	}	
	
	document.getElementsByTagName("title")[0].text = title_str;
}

function BDTranslate_Level4(Link) {
	switch (Link) {
	  case "consequences":
		return "影響";
		break;
	  case "citizenresponse":
		return "市民的應變措施";
		break;
	  case "govtresponse":
		return "政府的應變措施";
		break;
	  case "pastincidents":
		return "事例";
		break;
	  case "casestudy":
		return "個案研究";
		break;
	  default:
	  	return BigFirstLetter(Link);
		break;	
	}
}

function BDTranslate_Level3(Link) {
	switch (Link) {
	  case "drought":
		return "乾旱";
		break;
	  case "flood":
		return "洪水";
		break;
	  case "earthquake":
		return "地震";
		break;
	  case "hurricane":
		return "颶風";
		break;
	  case "tornado":
		return "龍捲風";
		break;
	  case  "tsunami":
		return "海嘯";
		break;
	  case "volcano":
		return "火山";
		break;
	  case "wildfire":
		return "火災";
		break;
	  case "landmudslide":
		return "山崩和泥流";
		break;
	  case "biochem":
		return "生物化學侵襲";
		break;
	  case "normalwar":
		return "普通戰爭";
		break;
	  case "nuclearwar":
		return "核武器";
		break;
	  case "epidemic":
		return "流行病和全面流行疫症";
		break;
	  case "genocide":
		return "種族滅絕";
		break;
	  case "terroristattack":
		return "恐怖襲擊";
		break;

	  default:
	  	return BigFirstLetter(Link);
		break;	
	}
}

function BDTranslate_Level2(Link) {

	switch (Link) {
	  case "naturaldisasters":
		return "自然災害";
		break;
	  case "syntheticdisasters":
		return "人造災害";
		break;
	  default:
	  	return BigFirstLetter(Link);
		break;	
	}
}

function BigFirstLetter(Word){
	var First = Word.charAt(0);
	return First.toUpperCase() + Word.substring(1, Word.length);
}

function ScrollToAnchor() {
    var n;
    if (window.location.href.split("#")[1]) {
        n = window.location.href.split("#")[1];
    }
    else { n = "main_indexcontent_0_0"; }
	
	var Link = n.split("_");
	
	if ( Link[3] != "0" ) {
		var height = document.getElementById(Link[3]).offsetTop;
		window.scrollTo(0, height);
	}
	
	if ( Link[0] == "terminology" ) {
		document.getElementById(Link[3]).className = "highlight_term";
		document.getElementById(Link[3]).nextSibling.className = "highlight_def";		
	}
}

// function to show a loading message while the content-file is loaded
function LoadingMsg() {
	// removes the content-page
	var Knoten = document.getElementById("targetArea");
	while (Knoten.firstChild) 
	{
		Knoten.removeChild(Knoten.firstChild);
	}
	// and shows the loading image instead
	var newImg = document.createElement("img");
	newImg.setAttribute("id", "LoadingImg");
	newImg.setAttribute("src", "images/loading.gif");
	newImg.setAttribute("width", "32");
	newImg.setAttribute("height", "32");
		
  	document.getElementById("targetArea").appendChild(newImg);
}

function loadInto(src, destId, evt)
{
 // Called to when a link with class="loadinto-IdOfTarget" is clicked.
 // Parameters: src = reference to link, destId = ID of target element, evt = event object.
 var ok = docClickLoader.loadInto(src.href || src.getAttribute('href'), destId);
 if (ok) cancelEvent(evt);
};


// addEvent is defined within htmlhttprequest.js, feel free to reuse :).
addEvent(document, 'click', function(evt)
{
 // Here we capture all clicks on the document, scanning for links with a CLASS
 // attribute of "loadinto-IdOfTarget" and routing them to loadInto() above.
 evt = evt || window.event;
 var src = evt.target || evt.srcElement;
 if (src.nodeType && src.nodeType != 1) src = src.parentNode;
 // Loop up the DOM tree scanning all elements to find a matching one.
 while (src)
 {
  var srcName = (src.nodeName||src.tagName||'').toLowerCase();
  if (srcName == 'a' && src.className && src.className.match(/^(load|toggle)into-(.+)$/))
  {
   // Call our load handlers if we have a match; they'll cancel the normal action.
   if (RegExp.$1 == 'load') return loadInto(src, RegExp.$2, evt);
   if (RegExp.$1 == 'toggle') return toggleInto(src, RegExp.$2, evt);
  }
  src = src.parentNode;
 }
}, 1);

// Detect Client Browser type
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
jsVersion = 1.1;
// JavaScript helper required to detect Flash Player PlugIn version information
function JSGetSwfVer(i){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
      		var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			descArray = flashDescription.split(" ");
			tempArrayMajor = descArray[2].split(".");
			versionMajor = tempArrayMajor[0];
			versionMinor = tempArrayMajor[1];
			if ( descArray[3] != "" ) {
				tempArrayMinor = descArray[3].split("r");
			} else {
				tempArrayMinor = descArray[4].split("r");
			}
      		versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
            flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
      	} else {
			flashVer = -1;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	// Can't detect in all other cases
	else {
		
		flashVer = -1;
	}
	return flashVer;
} 
