var page = getNavigation();
var lang = "en";
var WIDTH = 1000;
var HEIGHT = 700;

$(document).ready(function()
{
	if ( page == "zh" )
	{
		$(".btn_lang").find("a").attr("href", "#" + page );
		chlang($(".btn_lang").find("a"));
	}
});

var IS_RENDER = false;
function resize ( buffer, self )
{
	var completeTween = function(){ if ( self != true ) resize(false, true); };
	
	if (!IS_RENDER)
	{
		var bodyWidth  = $(window).width();
		var bodyHeight = $(window).height();
		var diff = ( bodyHeight < HEIGHT ) ? 0 : 0;
	
		var mode = (buffer) ? "animate" : "css";
		
		$("#menu").css ( "top", 63 - diff + "px" );
		$("#content").css ( "marginTop", 104 - diff + "px" );
		
		/* Header */
			$("#header").css ( { height:100 - diff + "px" } );
			$("#header .container").css ( "height", 100 - diff + "px" );
		
			$("#header .container .left").css ( "top", 40 - diff + "px" );
			if ($.browser.msie && $.browser.version.substr(0,1)<7)
			{ 
				$("#header_bg").css( { height:$("#header").offset().top + parseInt($("#header").css("height")) + "px" } );
			} else {
				$("#header_bg").css( { height:$("#header").offset().top + parseInt($("#header").css("height")) - $(window).scrollTop() + "px" } );
			}
		
		if ( bodyHeight > HEIGHT ) $("html").css({"overflow-y":"hidden"});
		else $("html").css({"overflow-y":"scroll"});
		
		
		$("#core").css( { width:bodyWidth + "px" } );
		
		// For Product Page 
			if ( PAGE == "products" || PAGE == "products_list" || PAGE == "search" || PAGE == "new_products" )
			{
				var pW = $(".product_category").get(0).offsetWidth;

				resize_AutoScroll ( pW );
				
				if ( bodyWidth > pW )
				{
					$("#content").css ( { left:( bodyWidth - pW ) / 2 + "px" }, SPEED, null, completeTween  );
				} else {
					if ( PAGE == "product_list" )
					{
						$("#content").stop().animate ( { left:0 }, SPEED );
					}
					
					// Disable feature of IE6
					/*
					if ($.browser.msie && $.browser.version.substr(0,1)>6)
					{
						if ( buffer )
						{
							$("#content").stop().animate ( { left:25 + "px" }, SPEED, null, completeTween );
						} else {
							$("#content").css ( { left:25 + "px" } );
							$("#core").css ( { width:pW + "px" } );
						}
					} 
					*/
				} 
			}
			
		// For Contact Us Page 
			if ( PAGE == "contactus" )
			{
				var W = ( parseInt($("#enquiry_form_area").css("width")) ) ? parseInt($("#enquiry_form_area").css("width")) : 0;
				var pW = $("#contactus").get(0).offsetWidth + W;
				if ( bodyWidth > pW )
				{
					$("#content").stop();
					$("#content")[mode] ( { left:(bodyWidth - pW ) / 2 + "px" } );
				} else {
					$("#core").stop();
					$("#core")[mode] ( { width:pW + parseInt ($("#content").css("left")) + "px" } );
				}
			}
		
		
		if ($.browser.msie && $.browser.version.substr(0,1)<7)
		{
			ie6_compatible();
		}
		
		
		/* Extend Content Area */
		//if ( PAGE != "index" )
		//{
			if ( bodyHeight > HEIGHT )
			{
				$("#content").css ( { height:bodyHeight - 200 + "px" } );
			} else {
				$("#content").css ( { height:"436px" } );
			}
				// For Index
					if ( PAGE == "index" ) resize_Index ( );
			
				// For About 
					if ( PAGE == "about" ) resize_About ( );
		
				// For Projects 
					if ( PAGE == "projects" ) resize_Projects ( );
		
				// For Download 
					if ( PAGE == "download" ) resize_Download ( );
					
				// For Certificate
					if ( PAGE == "certificate" ) resize_Certificate ( );
					
				// For Contactus
					if ( PAGE == "contactus" ) resize_Contactus ( );
					
				// For Products
					if ( PAGE == "products" ) resize_Products ( );
					if ( PAGE == "products_list" ) resize_ProductsList ( );
					if ( PAGE == "search" ) resize_ProductsList ( );
					if ( PAGE == "new_products" ) resize_ProductsList ( );
					
				// For News
					if ( PAGE == "news" ) resize_News ( );
		//}

		if ( self != true ) resize (false, true );
		if ( buffer ) resize();
	}
	
	if ( $("#core").css("visibility") != "visible" )
	{
		$("#core").css({visibility:"visible"});
	}

}
window.onresize = resize;
if ($.browser.msie && $.browser.version.substr(0,1)<7) 
{ 
	setInterval ( "resize()", 2000);
	$("html,body").css({overflow:"hidden", "overflow-y":"auto"});
}


/* Resize Functions */
	function resize_Index ( )
	{
		$("#main_area").css ( { height:parseInt($("#content").css("height")) + 6 + "px" });
		$("#content_area").css ( { height:parseInt($("#main_area").css("height")) + "px" } );
		if ( j1 != undefined ) j1.reset();
		
		var column = 2;
		var p = Math.floor ( ( parseInt($("#content").css("height")) + 6 - 350 ) / 80  ) * column;
		if ( p != perpage )
		{
			var pp = Math.ceil ( perpage / p );
			perpage = p;
			$("#new_product_list").load ( "index_new_products.php", { perpage:perpage } );
		}

		var column = 1;
		var p = Math.floor ( ( parseInt($("#content").css("height")) + 6 - 335 ) / 40  ) * column;
		if ( p != news_perpage )
		{
			var pp = Math.ceil ( news_perpage / p );
			news_perpage = p;
			$("#news_list").load ( "index_news.php", { perpage:news_perpage } );
		}
		
		$("#split_line").css({height:parseInt($("#content").css("height")) + 6 - 300 });

	}
	function resize_About ( )
	{
		$("#main_area").css ( { height:parseInt($("#content").css("height")) + 6 + "px" });
		$("#content_area").css ( { height:parseInt($("#main_area").css("height")) - 232 + "px" } );
		if ( j1 != undefined ) j1.reset();
		if ( j2 != undefined ) j2.reset();
		if ( s1 != undefined ) s1.reset( parseInt ( $("#content_area").css("height") ) );
	}
	function resize_Projects ( )
	{
		$("#main_area").css ( { height:parseInt($("#content").css("height")) + 6 + "px" });
		$("#content_area").css ( { height:parseInt($("#main_area").css("height")) - 77 + "px" } );
		if ( j1 != undefined ) j1.reset();
		if ( j2 != undefined ) j2.reset();
		if ( s1 != undefined ) s1.reset( parseInt ( $("#content_area").css("height") ) );
	}
	function resize_Download ( )
	{
		$("#main_area_drawing").css ( { height:parseInt($("#content").css("height")) + 6 + "px" });
		$("#main_area_manual").css ( { height:parseInt($("#content").css("height")) + 6 + "px" });
		$("#content_area").css ( { height:parseInt($("#main_area_drawing").css("height")) - 115 + "px" } );
		$("#content_area_2").css ( { height:parseInt($("#main_area_manual").css("height")) - 115 + "px" } );
		if ( j1 != undefined ) j1.reset();
		if ( j2 != undefined ) j2.reset();
		if ( j3 != undefined ) j3.reset();
		if ( j4 != undefined ) j4.reset();
		if ( s1 != undefined ) s1.reset( parseInt ( $("#content_area").css("height") ) + 20 );
		if ( s2 != undefined ) s2.reset( parseInt ( $("#content_area_2").css("height") ) + 20 );
		$(".reader").css ( { top:parseInt( $("#content").css("height") ) - 20 + "px" } );
	}
	function resize_Certificate ( )
	{
		$("#main_area").css ( { height:parseInt($("#content").css("height")) + 6 + "px" });
		$("#content_area").css ( { height:parseInt($("#main_area").css("height")) - 128 + "px" } );
		if ( j1 != undefined ) j1.reset();
		if ( j2 != undefined )
		{
			if ( parseInt($("#certificate_list").css("left")) === 0 ) j2.reset();
		}
		if ( s1 != undefined ) 
		{
			s1.reset( parseInt ( $("#content_area").css("height") ) );
		}
		//$(".reader").css ( { top:parseInt( $("#content").css("height") ) - 20 + "px" } );
			
		$("#detail_data").css({height:parseInt($("#main_area").css("height")) - 120 });
		$("#detail_holder").css({height:$("#detail_data").css("height")});
		if ( s10 != undefined ) s10.reset( parseInt( $("#detail_data").css("height"))  );
		
	}
	function resize_Contactus ( )
	{
		$("#main_area").css ( { height:parseInt($("#content").css("height")) + 6 + "px" });
		$("#content_area").css ( { height:parseInt($("#main_area").css("height")) - 87 + "px" } );
		$("#enquiry_form_area").css ( { height:parseInt($("#content").css("height")) + 6 + "px" });
		if ( j1 != undefined ) j1.reset();
		if ( j2 != undefined ) j2.reset();
		if ( s1 != undefined ) s1.reset( parseInt ( $("#content_area").css("height") ) + 20 );
		$("#enquiry_bar").css ( { height:parseInt($("#content").css("height")) + 6 + "px" });
		$(".left_shadow_bar").css ( { height:parseInt($("#content").css("height")) - 8 + "px" } );
		$(".right_shadow_bar").css ( { height:parseInt($("#content").css("height")) - 8 + "px" } );

		if ($.browser.msie && $.browser.version.substr(0,1)<7) 	{ } else 
		{
			$(".show_map").css({height:parseInt($("#content").css("height")) + "px" } );
		}
	}
	function resize_Products ( )
	{
		$("#main_area").css ( { height:parseInt($("#content").css("height")) + "px" });
		$(".product_category").find(".cate").css  ( { height:parseInt($("#content").css("height")) + "px" });
		
		var top = parseInt($("#content").css("height")) - 24 + "px";
		var height = parseInt($("#content").css("height")) + 6 + "px";
		for ( var i = 1; i <= 9; i++ )
		{
			$(".category_"+i+"_name").css( { top:top });
			$(".board").css({height:height});
		}
		if ( j1 != undefined ) j1.reset();
//		$(".next_button").stop().animate({height:height}, SPEED);
//		$(".prev_button").stop().animate({height:height}, SPEED);
		
		$(".list_container").css({height:height});
		
		var p = Math.floor ( ( parseInt($("#content").css("height")) + 6 - 70 ) / 175  ) * 2;
		if ( p != perpage )
		{
			var pp = Math.ceil ( perpage * selected_page / p );
			perpage = p;
			if ( selected_subcate != null )
			{
				$(selected_subcate).load ( "subcategory.php", { cid:selected_category, perpage:perpage, page:pp } );
			}
		}

	}
	function resize_ProductsList ( )
	{
		$("#main_area").css ( { height:parseInt($("#content").css("height")) + "px" });
		$(".product_category").find(".cate").css  ( { height:parseInt($("#content").css("height")) + "px" });
		
		var top = parseInt($("#content").css("height")) - 24 + "px";
		var height = parseInt($("#content").css("height")) + 6 + "px";
		$(".list_board").css({height:height});
		
		if ( j1 != undefined ) j1.reset();
		if ( j2 != undefined ) j2.reset();
		if ( j3 != undefined ) j3.reset();
		$(".next_button").stop().css({height:height}, SPEED);
		$(".prev_button").stop().css({height:height}, SPEED);
		
		//$(".list_container").css({height:height});
		$(".detail_board").css({height:height});
		
		/* List */
		$(".mc_label").css({height:parseInt(height)-6+"px"});
		$("#main_category_photo").css({height:height});
		//$(".list_container").find("[class=enlarge_photo]").css({ top:height, height:height } );
		var enlarge = $(".list_container").find("[class=enlarge_photo]").html();
		if ( enlarge == null || enlarge == "" )
		{
			var column = ( PAGE == "search" || PAGE == "new_products" ) ? 3 : 2;
			var p = Math.floor ( ( parseInt($("#content").css("height")) + 6 - 70 ) / 175  ) * column;
			if ( p != perpage )
			{
				var pp = Math.ceil ( perpage * selected_page / p );
				perpage = p;
				if ( PAGE == "search" )
				{
					$("#subcate_list").load ( "list_subcategory.php", { perpage:perpage, page:pp, area:area, type:type, product:product, keyword:keyword, mode:"search" } );
				} else if ( PAGE == "new_products" ) {
					$("#subcate_list").load ( "list_subcategory.php", { perpage:perpage, page:pp, mode:"new_products" } );
				} else {
					$("#subcate_list").load ( "list_subcategory.php", { cid:cid, sid:sid, perpage:perpage, page:pp, pos:pos } );
				}
			}
		} else {
			//$(".list_container").find("[class=enlarge_photo]").parent().css({top:-parseInt(height) + "px", height:height });
			//$(".list_container").find("[class=enlarge_photo]").css({height:height});
		}
		
		$("#detail_content_list").css({height:parseInt(height) - (442 - baseHeight) + "px"});
		if ( s1 != undefined ) s1.reset( parseInt ( $("#detail_content_list").css("height") ) );
		
		$("#list_category").css({height:parseInt(height) + "px"});
		$("#enlarge_photocategory").css({height:parseInt(height) + "px"});
		
		$("#list_detail").css({height:$(".list_container").css("height")});
		
		$("#detail_data").css({height:parseInt($("#main_area").css("height")) - 120 });
		$("#detail_holder").css({height:$("#detail_data").css("height")});
		if ( s10 != undefined ) s10.reset( parseInt( $("#detail_data").css("height"))  );
		
		
	}
	
	
	
	
	function resize_News ( )
	{
		var height = parseInt($("#content").css("height"));
		$("#main_area").css ( { height:height + "px" });
		$(".next_button").stop();
		$(".next_button").animate({height:height}, SPEED);
		$(".prev_button").stop();
		$(".prev_button").animate({height:height}, SPEED);
		
		height += 6;
		$("#list_area").css ( { height:height + "px" });
		$("#content_area").css ( { height:height + "px" } );
		if ( j1 != undefined ) j1.reset();
		if ( j2 != undefined ) j2.reset(height);
		
		height -= diffH;
		if ( $(".data").length > 0 ) $(".data").css({height:height + "px" } );
		if ( s1 != undefined ) s1.reset( height );
		
		var p = Math.floor ( ( height + 6 - 40 ) / 32  );
		if ( p != perpage && $("#list_area_container").html() == "" )
		{
			perpage = p;
			$("#list_area_container").load ( "news_list.php", { id:news_id, perpage:p, page:page }, function()
			{
				$("#news_list_container").stop();
				$("#news_list_container").css({top:"320px"});
				$("#news_list_container").animate({top:"0px"}, SPEED );
			} );
		}
	}


	function resize_AutoScroll ( pW )
	{
		var wW = $(window).width();
		if ( pW < wW )
		{
			$("#hidden_auto_left").css({display:"none"});
			$("#hidden_auto_right").css({display:"none"});
		} else {
			$("#hidden_auto_left").css({display:"block"});
			$("#hidden_auto_right").css({display:"block"});
		}
	}




function ie6_compatible ()
{
	var T = ($.browser.msie && $.browser.version.substr(0,1)<7) ? 0 : parseInt($("#header .container").css("height"));
	$("#header").css( { top:($("#container").offset().top - T) + "px" } );

	$("#header_bg").css({ top:"0px", left:"0px" });
	
	try
	{
		var H = (parseInt($("#header").css("height")) != undefined ) ? parseInt($("#header").css("height")) : 0;
		var T = ($("#header").offset().top != undefined ) ? $("#header").offset().top : 0;
		var C = parseInt($("#content").css("height"));
		var E = (parseInt($("#header").css("top")) + C) + parseInt($("#header").css("height")) + 4;
		
		var fH = $(window).height() - E;
		if ( fH < 0 ) fH = 20;
		$("#footer").css({ height:fH + "px", top:E + "px" });
	} catch ( e ) {}
	
	$("#top_bar").css({left:$("html,body").scrollLeft() + "px"});
	$("#top_bar .container").css({left:($(window).width() - parseInt($("#top_bar .container").css("width"))) / 2 });
	
	$("#hidden_auto_right").css({left:$(window).width() - parseInt($("#hidden_auto_right").css("width"))});

	// Header Fix
	/*
		var width = $("#header .container").css("width");
		var L = ( width > $(window).width() ) ? ($(window).width() - width) / 2 : 0;
		$("#header .container").css({ left:L + "px" } );
	*/
	
//	$(window).scrollTop(0);
}

function chlang (obj)
{
	lang = ( lang == "en" ) ? "zh" : "en";
	obj.href = "#" + lang;
	var d = document.getElementById("menu");
	d.className = lang + "_menu";

	$("#menu").find("a").each( function(i)
	{
		var href = this.href;
		href = href.replace("#en","");
		href = href.replace("#zh","");
		$(this).attr("href", href + "#" + lang );
	});
	
	$("#swap_lang").load ( "swap_lang.php", { lang:lang } );
}

function getURL ( url, target )
{
	switch ( target )
	{
		case "_blank":
			window.open ( url );
			break;
		default:
			location.href = url;
			break;
	}
}

function getNavigation () { if ( location.href.indexOf ( "#" ) > -1 ) { var page = location.href.replace ( /.*#/gi, "" ); page = page.substring ( 0, page.length ); } else { var page = "";} return page; }

var message="";
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}

document.oncontextmenu=new Function("return false")
