﻿// Start: Insert xml:lang
if (scServerInUse()){
	var tags = document.getElementsByTagName("html");
	tags[0].setAttribute("lang", "zh-CN");
	tags[0].setAttribute("xml:lang", "zh-CN");
}
// End: Insert xml:lang

// Start: Change banner path for SC
function changeBannerPathSC(){
	if (typeof(zmsBannerGroups) != "undefined"){
		var i, groups;
		groups = zmsBannerGroups;
		for (i=0; i < groups.length; i++){
			var filepath = groups[i][0];
			groups[i][0] = filepath.replace("/filemanager/banner/tc/", "/filemanager/banner/sc/");
		}
	}
}
if (scServerInUse()){
	changeBannerPathSC();
}
// End: Change banner path for SC

jQuery.preloadImages = function() {
	$('.btn_over').each(function() {
		jQuery("<img>").attr("src", replaceFilename($(this).attr('src'), ".", "_over."));
	});
	$('.btn_o').each(function() {
		jQuery("<img>").attr("src", replaceFilename($(this).attr('src'), ".", "_o."));
	});
}

$(document).ready(function() {
	$('.btn_o').mouseover(function(){
		$(this).attr('src', replaceFilename($(this).attr('src'), ".", "_o."));
	}).mouseout(function(){
		$(this).attr('src', replaceFilename($(this).attr('src'), "_o.", "."));
	});
	$('.btn_over').mouseover(function(){
		$(this).attr('src', replaceFilename($(this).attr('src'), ".", "_over."));
	}).mouseout(function(){
		$(this).attr('src', replaceFilename($(this).attr('src'), "_over.", "."));
	});

	$("object:not(.activated), embed:not(.activated)").each(function() {
		this.outerHTML = this.outerHTML;
	});
	configureScSectionImage();
	if (typeof(zmsSectionImage) != 'undefined' && zmsSectionImage.length > 0){
		$("#div_sectionimage").html("<img src=\""+zmsSectionImage+"\" border=\"0\" />");
	}

	// Configure button for sc version
	configureScButtons();
	// End of Configure button for sc version
	configureRevisionDate();
	configureLeftMenu();
	altTableRowStyle();

	$.preloadImages();

	if (location.href.indexOf("?print=1") >= 0 
			|| location.href.indexOf("&print=1") >= 0) {
		window.print();
	}

	if (document.getElementById("qu")) {
		document.getElementById("qu").onkeydown=function(e) {
			if ((e && e.which && e.which==13) || (typeof event!='undefined' && event.keyCode==13)) {
				search();
			}
		}
	}
});

function replaceFilename(url, fromstr, tostr){
	var filestart = url.lastIndexOf("/");
	var newUrl = 
		url.substring(0, filestart)
		+ url.substring(filestart)
		.replace(fromstr, tostr);
	return newUrl
}

function altTableRowStyle(){
	$("table.altrowcolornoheader").each(
			function(){
			$(this).children("tbody").children("tr:even").addClass("odd");
			});

	$("table.altrowcolor").each(
			function(){
			$(this).children("tbody").children("tr:odd").addClass("odd");
			});
}

function configureLeftMenu(){
	$(".menu .sub").each(function() { 
		var currSub = $(this);

		$(this).parent().hover(function(){
			var mainMenu = $(this).parent(".menu");
			mainMenu.attr("active", 1)
			var iWidth = mainMenu.width();
			currSub.css({display: "block", top: getY(this), left: getX(this) + iWidth});
			currSub.show();

			var subMenuWidth = currSub.width();
			var subMenuHeight = currSub.height();
			currSub.children("li:eq(0)").children("iframe").css("width", subMenuWidth+"px");
			currSub.children("li:eq(0)").children("iframe").css("height", subMenuHeight+"px");
		}, function() {
			currSub.hide();
		});
	});
}

function getY(arg)
{
	var y = 0;
	if (arg.offsetParent){
		y = getY(arg.offsetParent);
	}
	return arg.offsetTop + y;
}

function getX(arg)
{
	var x = 0;
	if (arg.offsetParent){
		x = getX(arg.offsetParent);
	}
	return arg.offsetLeft + x;
}


function configureRevisionDate(){
	var phReviewDate = document.getElementById("phReviewDate");
	var phRevisionDate = document.getElementById("phRevisionDate");

	if (typeof(reviewDateExists) != "undefined"
			&& reviewDateExists){
		var divDate = document.getElementById("div_reviewdate");
		if (phRevisionDate != null && phReviewDate != null && divDate != null){
			divDate.innerHTML = loadDateString(reviewDate_Year, reviewDate_Month, reviewDate_Day);
			phReviewDate.style.display = "inline";
			phRevisionDate.style.display = "none";
		}
	}else if (typeof(revisionDateExists) != "undefined"
			&& revisionDateExists){
		var divDate = document.getElementById("div_revisiondate");
		if (phRevisionDate != null && phReviewDate != null && divDate != null){
			divDate.innerHTML = loadDateString(revisionDate_Year, revisionDate_Month, revisionDate_Day);
			phRevisionDate.style.display = "inline";
			phReviewDate.style.display = "none";
		}
	}
}

function loadDateString(yyyy, mm, dd){
	if (zmsCharsetID == 1){
		var monthName = new Array("", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
		return dd+" "+monthName[mm]+" "+yyyy;
	}else{
		return yyyy+"年"+mm+"月"+dd+"日";
	}
}

function configureScSectionImage(){
	if (typeof(zmsSectionImage ) != "undefined"){
		if (scServerInUse()){
			var url = zmsSectionImage.substring(zmsSectionImage.lastIndexOf("/_sec_")+6);
			zmsSectionImage = "/filemanager/template/sc/images/sectionimage/" + url;
		}
	}
}

function configureScButtons(){
	if (typeof(zmsCharsetID) != "undefined" && zmsCharsetID == 2){
		var btnScTextSwitch, btnTcTextSwitch;
		btnTcTextSwitch = document.getElementById("btnTcTextSwitch");
		btnScTextSwitch = document.getElementById("btnScTextSwitch");
		if (btnTcTextSwitch && btnScTextSwitch){
			if (scServerInUse()){
				btnTcTextSwitch.style.display = "none";
				btnScTextSwitch.style.display = "inline";
			}else{
				btnTcTextSwitch.style.display = "inline";
				btnScTextSwitch.style.display = "none";
			}
		}

		var btnToSC, btnToTC;
		btnToTC = document.getElementById("btnToTC");
		btnToSC = document.getElementById("btnToSC");
		if (btnToTC && btnToSC){
			if (scServerInUse()){
				btnToTC.style.display = "inline";
				btnToSC.style.display = "none";
			}else{
				btnToSC.style.display = "inline";
				btnToTC.style.display = "none";
			}
		}
	}
}

function jump(section){
	var langFolder = "en";
	var filename = "index.html";
	var isTextOnly = false;

	if (1 == zmsCharsetID){
		langFolder = "en";
	}else if (2 == zmsCharsetID){
		langFolder = "tc";
	}else if (3 == zmsCharsetID){
		langFolder = "sc";
	}

	if (window.location.toString().indexOf("/t_index") >= 0
			|| window.location.toString().indexOf("textonly=true") >= 0){
		filename = "t_index.html";
		isTextOnly = true;
	}

	if (section == 'importantnotices'){
		window.location = "/"+langFolder+"/important/" + filename;
	}else if (section == 'sitemap'){
		window.location = "/"+langFolder+"/sitemap/" + filename;
	}else if (section == 'contactus'){
		if (isTextOnly){
			window.location = "/contactusform.aspx?section=76&lang=" + zmsCharsetID + "&textonly=true";
		}else{
			window.location = "/contactusform.aspx?section=76&lang=" + zmsCharsetID;
		}
	}
}

function scServerInUse(){
	var index = window.location.toString().indexOf(_scservername);
	return (index >= 0 && index <= ("ht"+"tp://").length);
}

function switchLang(charsetID){
	var scserver = _scservername;
	var url = window.location.toString();
	var isStaticURL = false;
	var langPaths = new Array("en", "tc", "sc");
	var oldLangPath = "";

	// Check url type (is static page or dynamic page)
	for (i=0; i < langPaths.length; i++){
		if (url.indexOf("/"+langPaths[i]+"/") != -1){
			oldLangPath = "/"+langPaths[i]+"/";
			isStaticURL = true;
			break;
		}
	}

	if (isStaticURL){
		var path;
		if (charsetID<3) {
			path = window.location.href;
			path = path.replace(scserver+"/TuniS/","");
			window.location = path.replace(oldLangPath, "/"+langPaths[charsetID - 1]+"/");
		} else if (charsetID==3) {
			if (window.location.href.indexOf(scserver)<0) {
				var httpPos;
				httpPos = url.indexOf("://");
				path = url.substring(0, httpPos+3);
				window.location = path + scserver + "/TuniS/" + url.substring(httpPos+3).replace(oldLangPath, "/tc/");
			}
		}
	}else{
		if (url.indexOf("lang=") != -1){
			if (charsetID<3) {
				path = window.location.href;
				path = path.replace(scserver+"/TuniS/","");
				window.location = 
					path.replace(/lang=\d/, "lang="+charsetID);
			}else{
				if (window.location.href.indexOf(scserver)<0) {
					var httpPos;
					httpPos = url.indexOf("://");
					path = url.substring(0, httpPos+3);
					window.location = path + scserver + "/TuniS/" + url.substring(httpPos+3).replace(/lang=\d/, "lang=2");
				}
			}
		}
	}
}

function search(){
	var query = document.getElementById("qu").value;
	if (query=='') {
		if (1 == zmsCharsetID){
			alert('Please enter search keyword.');
		} else {
			alert('請輸入查詢字串.');
		}
	} else {
		doSearch(query, false);
	}
}

function doSearch(keyword, isCirculars){
	var isTextOnly = false;

	if (window.location.toString().indexOf("/t_index") >= 0
			|| window.location.toString().indexOf("textonly=true") >= 0){
		isTextOnly = true;
	}

	var url = "http://search.gov.hk/search.html?";
	url += "tpl_id=devbii&";
	url += "gp0=devbii_home&";
	if (isCirculars){
		url += "gp1=devbii_circulars&";
		url += "a_submit=true&";
	}else{
		url += "gp1=devbii_home&";
	}
	url += "ui_charset=utf-8&";
	url += "web=this&";
	if (zmsCharsetID == 1){
		url += "ui_lang=en&";
	}else if (zmsCharsetID==2){
		if (scServerInUse()){
			url += "ui_lang=zh-cnhk&";
		}else{
			url += "ui_lang=zh-hk&";
		}
	}
	if (isTextOnly){
		url += "txtonly=1&";
	}
	url += "query=" + encodeURIComponent(keyword);
	location.href=url;
}

function searchCirculars(){
	var obj = document.getElementById("txtTechnicalFreeText");
	if (obj){
		var keyword;
		keyword = obj.value;

		if (keyword=='') {
			if (1 == zmsCharsetID){
				alert('Please enter search keyword.');
			} else {
				alert('請輸入查詢字串.');
			}
		} else {
			doSearch(keyword, true);
		}
	}
}

function textSwitch(){
  var url = window.location.toString();
  if (url.indexOf(".aspx") != -1){
    // dynamic
    if (url.indexOf("textonly=") != -1){
      window.location = url.replace(/textonly=\w*/, "textonly=true");
    }else{
      window.location = url + "&textonly=true";
    }
  }else{
    // static
    window.location = url.replace(/\/index/, "/t_index");
  }
}

function graphicSwitch(){
  var url = window.location.toString();
  if (url.indexOf(".aspx") != -1){
    // dynamic
    if (url.indexOf("textonly=") != -1){
      window.location = url.replace(/textonly=\w*/, "textonly=false");
    }else{
      window.location = url + "&textonly=false";
    }
  }else{
    // static
    window.location = url.replace(/\/t_index/, "/index");
  }
}

// Start: Print Version
function ShowPrintHeader(){
	ShowPrintHeaderGeneric();
}

function ShowPrintHeaderGeneric(){
	document.body.style.background = "none";

	var langFolder = "en";
	if (1 == zmsCharsetID){
		langFolder = "en";
		altText = "Development Bureau - The Government of the Hong Kong Special Administrative Region";
	}else if (2 == zmsCharsetID){
		langFolder = (scServerInUse() ? "sc" : "tc");
		altText = "香港特別行政區政府 - 發展局";
	}
	
	var imageFolder = "/filemanager/template/"+langFolder+"/images/";
	var html = '';
	html += '<table border="0" cellpadding="0" cellspacing="0" width="600">';
	html += '<tbody>';
	html += '<tr align="left" valign="top">';
	html += '<td><img src="'+imageFolder+'print_header.gif" width="600" height="67" alt="'+altText+'" title="'+altText+'" border="0" /></td>';
	html += '</tr>';
	html += '<tr align="left" valign="top">';
	html += '<td>';
	html += '<br /><br />';
	html += '<div id="div_pagename" class="heading_whatsnew"></div>';
	html += '<div style="font-size: 5px; background-image: url(/filemanager/template/common/images/title_bottom_line_print.gif); width: 600px; line-height: 5px; height: 5px;">';
	html += '</div>';

	html += '<div class="content">';
	document.writeln(html);
}

function ShowPrintFooter(){
	ShowPrintFooterGeneric();
}

function ShowPrintFooterGeneric(){
	var langFolder = "en";
	var strImportantNotices = '';
	var strLastReviewDate = '';
	var strLastRevisionDate = '';

	if (1 == zmsCharsetID){
		langFolder = "en";
		strImportantNotices = 'Important Notices';
		strLastReviewDate = 'Last review date: ';
		strLastRevisionDate = 'Last revision date: ';
	}else if (2 == zmsCharsetID){
		strImportantNotices = '重要告示';
		langFolder = (scServerInUse() ? "sc" : "tc");
		strLastReviewDate = '覆檢日期: ';
		strLastRevisionDate = '修訂日期: ';
	}

	var html = '';
	html += '</div>';
	html += '<br /><br />';
	html += '</td>';
	html += '</tr>';
	html += '<tr align="left" valign="top">';
	html += '<td>';
	html += '	<table border="0" cellpadding="0" cellspacing="0" width="600px">';
	html += '	<tbody>';
	html += '	<tr style="height: 6px;" class="layout">';
	html += '	<td class="layout" colspan="2" valign="middle">';
	html += '	<img class="vmiddle" src="/filemanager/template/common/images/botdot2_print.gif" alt="" border="0" height="3" width="600">';
	html += '	</td>';
	html += '	</tr>';
	html += '	<tr>';
	html += '	<td class="footer">2010<img src="/filemanager/template/common/images/copy.gif" ';
	html += '     alt="" border="0" height="11" width="12"> | <a href="javascript:jump(\'importantnotices\');">';
	html += strImportantNotices +'</a></td>';
	html += '	<td class="footer" align="right"><span id="phReviewDate" style="display: none;">'+strLastReviewDate+'<span id="div_reviewdate"><!-- Review Date --></span></span><span id="phRevisionDate" style="display: none;">'+strLastRevisionDate+'<span id="div_revisiondate"></span></span></td>';
	html += '	</tr>';
	html += '	</tbody>';
	html += '	</table>';
	html += '</td>';
	html += '</tr>';
	html += '</tbody>';
	html += '</table>';

	document.writeln(html);
}

function printPage(){
	var URL;
	URL = location.href;
	if (URL.indexOf("?")>0) {
		URL += "&print=1";
	} else {
		URL += "?print=1";
	}
	window.open(URL);
}
// End: Print Version

// Start: Menu
function Menu() {
	this.contentId = 0;
	this.text = "";
	this.alt = "";
	this.href = "";
	this.target = "";
	this.childs = new Array();
}

Menu.prototype = {
	addChild : function(m) {
		this.childs[this.childs.length] = m;
	}
}

function generateMenu(rootMenu) {
	// Must override
}
// End: Menu

function openPopup(path,width,height,targetname) {
	window.open(path,targetname,'height='+height+', width='+width+', toolbar=no, menubar=no, location=no, status=no, scrollbars=no, resizable=yes');
}

function kanhanNewWindow(tcpath, scpath, targetname){
	var thePath;
	if (scServerInUse()){
		thePath = scpath;
	}else{
		thePath = tcpath;
	}
	window.open(thePath,targetname,'');
}

// Start: Reverts links for .html and .htm, replaced by kanhan
function revertArticleLinks(){
	var scserver = _scservername;
	if (typeof(zmsCurrentContentId) != "undefined"){
		var contentID = zmsCurrentContentId;
		if (typeof(masterPageContentID) != "undefined"){
			if (masterPageContentID > 0){
				contentID = masterPageContentID;
			}
		}

		if (scServerInUse()
				&& (69 == contentID || 71 == contentID || 73 == contentID)){
			for (i=0; i < document.links.length; i++){
				var l=document.links[i];
				var url = l.href;
				if (url.indexOf("/filemanager/sc/") >= 0){
					if (url.match(/\.htm$/i) || url.match(/\.html$/i)){
						var httpPos = url.indexOf("://");
						if (httpPos >= 0){
							url = url.substring(httpPos+3);
						}

						url = url.replace("/filemanager/sc/", "/filemanager/tc/");
						url = url.replace(scserver+"/TuniS/", "");
						url = "http" + "://" + scserver + "/TuniS/" + url;
						l.href = url;
					}
				}
			}
		}
	}
}

$(document).ready(function(){
	revertArticleLinks();
});
// End: Reverts links for .html and .htm, replaced by kanhan
