var xmlhttp;

$(function() {
	$(window).history(function(e, hash) {
		$("#div_main_content").html("<img src='images/load_progress1.gif' />");
		var lang = document.URL;
		if (lang.match("lang=chi")) lang="chi";
		else lang="eng";
		restore_main(hash, lang);
	});
});

$(window).resize(function() {
	var left = set_margin();
	$("#margin_container").css("marginLeft", left);
	$("#promotion_image_frame").css("marginLeft", left);
})

function set_margin() {
	var margin_left = $(window).width()/2 -$("#div_tb_menu").width()/2;
	if (margin_left < 0 ) margin_left = 0;
	return margin_left;
}

function set_css(is_change_user) {
	var left = set_margin();
	$("#margin_container").css("margin-left", left);
	$("#promotion_image_frame").css("marginLeft", left);
	if (!is_change_user) {
		//$("#contents_53kf").css("left", "240px");
		//if (jQuery.browser.msie) $("#contents_53kf").css("left", "10px");
		//else $("#contents_53kf").css("left", "8px");
	}
	if (!jQuery.browser.msie) {
		$("#home").removeClass("content_top_ie").addClass("content_top");
		$("#div_main_content").removeClass("content_top_ie").addClass("content_top");
		$("#div_tb_menu").removeClass("div_tb_menu_top_ie").addClass("div_tb_menu_top");
		$("#div_login").removeClass("div_login_top_ie").addClass("div_login_top");
		$("#div_sub_menu").removeClass("sub_menu_top_ie").addClass("sub_menu_top");
		$("#div_sub_menu_e").removeClass("sub_menu_top_e_ie").addClass("sub_menu_e_top");
		if (jQuery.browser.safari) {
			$("#div_sub_menu").css("top", parseInt($("#div_sub_menu").css("top"))-1);
			$("#div_sub_menu_e").css("top", parseInt($("#div_sub_menu_e").css("top"))-1);
		}
//			$("#sub_login").addClass("sub_menu_top");
//			$("#sub_login_e").addClass("sub_menu_top");
//			$("#eq_header").addClass("sub_menu_top");
		
	}
	else {
//		$("#home").css("marginLeft", left+7);
		$("#home").addClass("content_top_ie");
		if (parseInt($.browser.version) < 8) {
			$("#div_sub_menu").removeClass("sub_menu_top_ie").addClass("sub_menu_top_ie7");
			$("#div_sub_menu_e").removeClass("sub_menu_top_e_ie").addClass("sub_menu_top_e_ie7");
		}
		
		//$("#div_main_content").addClass("content_top_ie");
		//$("#div_tb_menu").addClass("div_tb_menu_top_ie");
		//$("#div_login").addClass("div_login_top_ie");
		//$("#div_sub_menu").addClass("sub_menu_top_ie");
		//$("#div_sub_menu_e").addClass("sub_menu_top_e_ie");
		
//			$("#sub_login").addClass("sub_menu_top_ie");
//			$("#sub_login_e").addClass("sub_menu_top_ie");
//			$("#eq_header").addClass("sub_menu_top_ie");
	}
}

function div_53kf() {
	$("#contents_53kf").height(755);
	$("#margin_container").height(50);
	$(document).ready(function() {
		$("#contents_53kf").height($(document).height()-130);
	})
}

function div_53kf2() {
	if (page != "home") {
		$("#contents_53kf").height(755);
		$("#margin_container").height(50);
		$(document).ready(function() {
			setTimeout(function() {
					$("#contents_53kf").height($(document).height()-90);
					$("#margin_container").height($(document).height());
			}, 1000);
		})
		$("#contents_53kf").css("top", 80);
	}
	else {
		if (!jQuery.browser.msie) {
			$("#contents_53kf").height(245);
			$("#contents_53kf").css("top",350);
		}
		else {
			$("#contents_53kf").height(200);
			$("#contents_53kf").css("top",370);
		}
	}
}

function load_banner(lang) {
	xmlhttp=GetXmlHttpObject();
	if (lang=="chi") {
		var url="index_banner.php";
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				$("#div_banner").html(xmlhttp.responseText);
				preload_cursor();
				set_css(1);
			}
		}
	}
	else {
		var url="index_banner_e.php";
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				$("#div_banner").html(xmlhttp.responseText);
				preload_cursor();
				set_css(1);
			}
		}
	}
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function load_content(lang) {
	$("#home").css("display", "block");
	//$("#contents_53kf").css("left", "240px");
	$("#div_main_content").css("display", "none");
	xmlhttp=GetXmlHttpObject();

	//var url="home.php";
	var url = "";
	if (lang=="chi") url="Profile/profile.php";
	else url="Profile/profile_e.php";
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			//$("#home").html(xmlhttp.responseText);
			$("#div_main_content").html(xmlhttp.responseText);
		}
	}
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);

}

function load_home() {
	$("#div_main_content").css("display","none");
	$("#home").css("display","block");
}

function preload_cursor() {
	for (i=1; i<12; i++) {
		_id = "a" + i;
		document.getElementById(_id).style.cursor="pointer";
	}
	document.getElementById("a_sub_login").style.cursor="pointer";
	//change_onReload();
}

function restore_main(page, lang) {
//	alert("restore_main 1 |" + page + " | " +lang);

	if (!page.match("home")) {
		$("#div_main_content").css("display", "block");
		$("#home").css("display", "none");
		//$("#contents_53kf").css("left", "-200");
	}
	else {
		$("#div_main_content").css("display", "none");
		$("#home").css("display", "block");
		//$("#contents_53kf").css("left", "240px");
	}

	var url;
	if (page.match("schedule")) {
		url = "Schedule/" + page + ".php";
	}
	else if (page.match("profile") || page.match("profile2") || page=="") {
//		alert(lang);
		if (page=="") {
			if (lang=="chi") url = "Profile/profile.php";
			else url = "Profile/profile_e.php";
		}
		else {
			if (lang=="chi") url = "Profile/" + page + ".php";
			else url = "Profile/" + page + "_e.php";
		}
	}
	else if (page.match("download")) {
		if (lang=="chi") url = "download.php";
		else url="download_e.php";
	}
	else if (page.match("information")) {
		if (lang=="chi") url = "information.php";
		else url = "information_e.php";
	}
	else if (page.match("network")) {
		if (lang=="chi") url="Network/agent_China.php";
		else url="Network/agent_China_e.php";
	}
	else if (page.match("promotion")) {
		url="promote/promote.php?lang="+lang;
	}
	else if (page.match("eq_") && page.match("redirect") ) {
		url = page;
	}
	else if (page.match("enquiry") || page.match("register") || page.match("submit_enquiry")) {
		if (page.match("enquiry")) {
			if (lang=="chi") url="enquiry/" + page + ".php";
			else url="enquiry/" + page + "_e.php";
		}
		else url="enquiry/" + page + ".php";
	}
	else if (page.match("contact_hk")) {
		if (lang=="chi") url= page + ".php";
		else url = page + "_e.php";
	}
	else if (page.match("login")) {
		if (lang=="chi") url = "login.php";
		else url = "login_e.php";
	}
	else if (page.match("eq_")) {
		if (page.match("eq_sea")) {
			if (lang=="chi") url = "enquiry/" +page+".php?lang=chi";
			else url = "enquiry/" +page+".php?lang=eng";
		}
		else if (lang=="chi") url= "enquiry/" + page + ".php"; 
		else url= "enquiry/" + page + "_e.php"; 
	}
	else if (page.match("equitment") || page.match("DG") || page.match("words") || page.match("ChinaImp")) {
		if (lang=="chi") url="Information/" + page + ".php";
		else if (page.match("equitment") || page.match("ChinaImp")) {
			url="Information/" + page + "_e.php";
		}
		else url="Information/" + page + ".php";
	}
	else url=page+".php";

//	alert("restore_main 2 | " + url);
	xmlhttp=GetXmlHttpObject();
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			if (!page.match("home")) $("#div_main_content").html(xmlhttp.responseText);
			else $("#home").html(xmlhttp.responseText);
		}
	}
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function change_onclick(tb_id, a_class, location) {
	$("#div_main_content").css("display", "block");
	$("#home").css("display", "none");
	//$("#contents_53kf").css("left", "-200");
	//var history_url = location.substr(location.indexOf('/')+1,location.length);
	//alert("change_onclick | " + history + " | " + location);
//	alert(location + " | " + location.substring(location.indexOf('/')+1,location.indexOf('.')));
//	$.history.add(location.substring(location.indexOf('/')+1,location.indexOf('.')));//location.substr(location.indexOf('/')+1,location.length));
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url;
	//if (!(location.match("promote"))) url=location + ".php";
	//else url=location;
	url=location;
	//alert(url);
	xmlhttp.onreadystatechange= function() {
		if (xmlhttp.readyState==4) {
			//$("#div_main_content").html(xmlhttp.responseText);
			//page = tb_id;
			a_id = "a" + 11;
			if (tb_id == 11) {
				document.getElementById(a_id).className = a_class;
			}
			else {
				document.getElementById(a_id).className = "normal_login";
			}
			for (i=1; i<10; i++) {
				a_id = "a" + i;
				if (i==tb_id) {
					document.getElementById(a_id).className = a_class;
				}
				else {
					document.getElementById(a_id).className = "normal_a";
				}
			}
			//document.getElementById("div_main_content").innerHTML = xmlhttp.responseText;
			$("#div_main_content").html(xmlhttp.responseText);
		}
	}
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function change_content(page, lang) {
	if (!page.match("home")) {
		$("#div_main_content").css("display", "block");
		$("#home").css("display", "none");
		//$("#contents_53kf").css("left", "-200");
	}
	else {
		$("#div_main_content").css("display", "none");
		$("#home").css("display", "block");
		//$("#contents_53kf").css("left", "240px");
	}
	
	var history = page.substr(page.indexOf('/')+1,page.length-4);
//	alert(page.substring(page.indexOf('/')+1,page.indexOf('.')));
//	$.history.add(page.substring(page.indexOf('/')+1,page.indexOf('.')));
	
	var xmlhttp1=GetXmlHttpObject();
	if (xmlhttp1==null) {
		alert("Browser does not support HTTP request");
		return;
	}
	var url=page;
	xmlhttp1.onreadystatechange=function() {
		if (xmlhttp1.readyState==4) {
			if (!page.match("home")) $("#div_main_content").html(xmlhttp1.responseText);
			else $("#home").html(xmlhttp1.responseText);
		}
	}
	xmlhttp1.open("GET",url,true);
	xmlhttp1.send(null);
}

function hide_promotion() {
	$('#show_promotion').hide();
	$('#div_show_promotion').hide();
	$('#promotion_image_frame').hide();
}
