$(function() {
	initSearch();	//init the search box
	if($.trim($(".errorDiv").html()) != "") {
		$(".errorDiv").show();
	}
	
});


/**
 * add overlay div to body
 */
function addOverlay() {
	if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
		
		$("body","html").css({height: "100%", width: "100%"});
		
		if (document.getElementById("HideSelect") === null) {//iframe to hide select elements in ie6
			$("body").append("<iframe id='HideSelect'></iframe><div id='overlay'></div>");
		}
	}else{//all others
		if(document.getElementById("overlay") === null){
			$("body").append("<div id='overlay'></div>");
		}
	}
	
	if(tb_detectMacXFF()){
		$("#overlay").addClass("overlayMacFFBGHack");//use png overlay so hide flash
	}else{
		$("#overlay").addClass("overlayBG");//use background and opacity
	}
}

/**
 * 判断是否为苹果机
 */
function tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}

/**
 * 隐藏产品预览窗口、移除遮罩层
 */
function hideWindow() {
    $("#load").hide();
	$("#window").hide();
	$('#overlay,#HideSelect').remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	
	return false;
}

/**
 * 设置产品预览窗口位置
 */
function setWindowPosition() {

	var windowHeight = document.body.clientHeight;
	var pageHeight = document.body.scrollHeight;
	var bodyTop = getbodyTop();
	var divWindowWidth = 842;
	var divWindowHeight = 654;
	
	$("#window").css({marginLeft: '-' + parseInt((divWindowWidth / 2),10) + 'px', width: 842 + 'px'});
	
	//ie6
	if ( $.browser.msie && $.browser.version ==6 ) {
	    $("#window").css({marginLeft: '-' + parseInt((divWindowWidth / 2),10) + 'px', width: 842 + 'px'});
		
	}
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		$("#window").css({marginTop:   bodyTop +30 + 'px'});
	}
	else {
	    if((bodyTop + divWindowHeight)>pageHeight){
	    	$("#window").css({marginTop:  bodyTop + 30 + 'px'});
	    }
		else {
			$("#window").css({marginTop:  bodyTop + 30  + 'px'});
		}
	}
}

function setViewPosition() {

	var windowHeight = document.body.clientHeight;
	var pageHeight = document.body.scrollHeight;
	var bodyTop = getbodyTop();
	var divWindowWidth = 120;
	var divWindowHeight = 60;
	
	$("#viewTool").css({marginLeft:  40 + 'px'});
	
	if ( $.browser.msie && $.browser.version ==6 ) {
	    $("#viewTool").css({marginLeft: '-' + 400 + 'px'});
	}
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		$("#viewTool").css({marginTop:   0 + 'px'});
	}
	else {
	    if((bodyTop + divWindowHeight)>pageHeight){
	    	$("#viewTool").css({marginTop:  bodyTop + 30 + 'px'});
	    }
		else {
			$("#viewTool").css({marginTop:  bodyTop + 30  + 'px'});
		}
	}
	
	
}

/**
 * 取得网页滚动距离
 */
function getbodyTop() {
	var bodyTop = 0;
	if (typeof window.pageYOffset != 'undefined') {   
		bodyTop = window.pageYOffset;   
	} else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {   
		bodyTop = document.documentElement.scrollTop;   
	}   
	else if (typeof document.body != 'undefined') {   
		bodyTop = document.body.scrollTop;   
	}
	return bodyTop
}

/**
 * 返回鼠标坐标
 */
function MouseEvent(e) {
	this.x = e.clientX;
	this.y = e.clientY;
}

//init the search box
function initSearch() {
	$("#searchkey").val("关键词...");
	$("#searchkey").focus(function() {
		if(this.value=='关键词...') this.value='';}).blur(function() {
		if($.trim(this.value)=='') this.value='关键词...';}
	);
	
	$("#searchbutton").click(function() {
		if($("#searchkey").val()=='关键词...') {
			$("#searchkey").value='';
			return false;
		}
	});
}

function IsVaildEmail(mails_str) {
    var isMail_reg=/^(\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+(,(?!$)|$))+$/;
    return isMail_reg.test(mails_str);
}