var errHideTimeOut;
var tooltipTop = 28;

(function($) {
	$.getStringLength = function(str) {
		str = $.trim(str);

		if (str == "")
			return 0;

		var length = 0;
		for ( var i = 0; i < str.length; i++) {
			if (str.charCodeAt(i) > 255)
				length += 2;
			else
				length++;
		}

		return length;
	}

	$.getLengthString = function(str, length, isSpace) {
		if (arguments.length < 3)
			var isSpace = true;

		if ($.trim(str) == "")
			return "";

		var tempStr = "";
		var strLength = 0;

		for ( var i = 0; i < str.length; i++) {
			if (str.charCodeAt(i) > 255)
				strLength += 2;
			else {
				if (str.charAt(i) == " ") {
					if (isSpace)
						strLength++;
				} else
					strLength++;
			}

			if (length >= strLength)
				tempStr += str.charAt(i);
		}

		return tempStr;
	}

	$.getBodyScrollTop = function() {
		var scrollPos;
		if (typeof window.pageYOffset != 'undefined') {
			scrollPos = window.pageYOffset;
		} else if (typeof document.compatMode != 'undefined'
				&& document.compatMode != 'BackCompat') {
			scrollPos = document.documentElement.scrollTop;
		} else if (typeof document.body != 'undefined') {
			scrollPos = document.body.scrollTop;
		}
		return scrollPos;
	}

	$.copyText = function(id) {
		var txt = $(id).val();
		if (window.clipboardData) {
			window.clipboardData.clearData();
			var judge = window.clipboardData.setData("Text", txt);
			if (judge === true)
				alert("复制成功！");
			else
				alert("不允许复制，请您手动进行");
		} else if (navigator.userAgent.indexOf("Opera") != -1) {
			window.location = txt;
		} else if (window.netscape) {
			try {
				netscape.security.PrivilegeManager
						.enablePrivilege("UniversalXPConnect");
			} catch (e) {
				alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");
			}
			var clip = Components.classes['@mozilla.org/widget/clipboard;1']
					.createInstance(Components.interfaces.nsIClipboard);
			if (!clip)
				return;
			var trans = Components.classes['@mozilla.org/widget/transferable;1']
					.createInstance(Components.interfaces.nsITransferable);
			if (!trans)
				return;
			trans.addDataFlavor('text/unicode');
			var str = new Object();
			var len = new Object();
			var str = Components.classes["@mozilla.org/supports-string;1"]
					.createInstance(Components.interfaces.nsISupportsString);
			var copytext = txt;
			str.data = copytext;
			trans.setTransferData("text/unicode", str, copytext.length * 2);
			var clipid = Components.interfaces.nsIClipboard;
			if (!clip)
				return false;
			clip.setData(trans, null, clipid.kGlobalClipboard);
			alert("复制成功！");
		}
	};

	$(window).scroll(function() {
		if ($("#sysmsg-error") != "none" || $("#sysmsg-success") != "none") {
			var top = $.getBodyScrollTop();
			if (top < tooltipTop)
				top = tooltipTop;
			$("#sysmsg-box").stop();
			$("#sysmsg-box").animate( {
				"top" : top
			}, {
				duration : 300
			});
		}
	});

	$.showErr = function(str) {
		var top = $.getBodyScrollTop();

		if (top < tooltipTop)
			top = tooltipTop;
		$("#sysmsg-box").css( {
			"top" : top
		});
		$("#sysmsg-error span:first").html(str);
		$("#sysmsg-error").show();
		$("#sysmsg-success").hide();
		$("#sysmsg-box").show();

		clearTimeout(errHideTimeOut);

		var hideErr = function() {
			$("#sysmsg-box").slideUp(300);
		};

		errHideTimeOut = setTimeout(hideErr, 5000);

		$("#sysmsg-box .close").one("click", function() {
			$("#sysmsg-box").hide();
		});
	}

	$.showSuccess = function(str) {
		var top = $.getBodyScrollTop();

		if (top < tooltipTop)
			top = tooltipTop;

		$("#sysmsg-box").css( {
			"top" : top
		});
		$("#sysmsg-success span:first").html(str);
		$("#sysmsg-success").show();
		$("#sysmsg-error").hide();
		$("#sysmsg-box").show();

		clearTimeout(errHideTimeOut);

		var hideErr = function() {
			$("#sysmsg-box").slideUp(300);
		};

		errHideTimeOut = setTimeout(hideErr, 5000);

		$("#sysmsg-box .close").one("click", function() {
			$("#sysmsg-box").hide();
		});
	}

	$.ShowDialog = function(option) {
		option = $.extend( {
			dialog : null,
			html : null,
			closeFun : null
		}, option || {});

		var bgDiv = document.createElement("DIV");
		var selfObj = $("." + option.dialog);
		if (selfObj.length == 0) {
			$("body").append(option.html);
			selfObj = $("." + option.dialog);
		}

		$("body").append(bgDiv);
		$(bgDiv).css( {
			position : "absolute",
			width : $(document).width(),
			height : $(document).height(),
			top : "0",
			left : "0",
			opacity : 0.3,
			background : "#000",
			display : "none",
			"z-index" : 100
		});
		$.windowCenter(selfObj);
		selfObj.bgiframe();
		$(bgDiv).bgiframe();

		selfObj.show();
		$(bgDiv).show();

		$(".close", selfObj).click(function() {
			$(bgDiv).remove();
			selfObj.css( {
				display : "none"
			});
			if (option.closeFun)
				option.closeFun.call(this);
		});

		$(window).scroll(function() {
			if (selfObj.css("display") != "none") {
				$.windowCenter(selfObj);
				$(bgDiv).css( {
					width : $(document).width(),
					height : $(document).height()
				});
			}
		});
	}

	$.windowCenter = function(obj) {
		var windowWidth = $.support.opacity ? window.innerWidth
				: document.documentElement.clientWidth;
		var windowHeight = $.support.opacity ? window.innerHeight
				: document.documentElement.clientHeight;
		var objWidth = obj.width();
		var objHeight = obj.height();
		var objTop = tooltipTop + $.getBodyScrollTop();
		var objLeft = (windowWidth - objWidth) / 2;
		obj.css( {
			position : "absolute",
			display : "block",
			"z-index" : 1000,
			top : objTop,
			left : objLeft
		});
	}

	$.checkRequire = function(value) {
		var reg = /.+/;
		return reg.test($.trim(value));
	}

	$.minLength = function(value, length, isByte) {
		var strLength = $.trim(value).length;
		if (isByte)
			strLength = $.getStringLength(value);

		return strLength >= length;
	};

	$.maxLength = function(value, length, isByte) {
		var strLength = $.trim(value).length;
		if (isByte)
			strLength = $.getStringLength(value);

		return strLength <= length;
	};

	$.rangeLength = function(value, minLength, maxLength, isByte) {
		var strLength = $.trim(value).length;
		if (isByte)
			strLength = $.getStringLength(value);

		return strLength >= minLength && strLength <= maxLength;
	}

	$.checkMobilePhone = function(value) {
		return /^(13\d{9}|18\d{9}|15\d{9})$/i.test($.trim(value));
	}

	$.checkPhone = function(val) {
		var flag = 0;
		val = $.trim(val);
		var num = ".0123456789/-()";
		for ( var i = 0; i < (val.length); i++) {
			tmp = val.substring(i, i + 1);
			if (num.indexOf(tmp) < 0)
				flag++;
		}
		if (flag > 0)
			return true;
		else
			return false;
	}

	$.checkEmail = function(val) {
		var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
		return reg.test(val);
	};

	$.checkUrl = function(val) {
		var reg = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
		return reg.test(val);
	};

	$.checkCurrency = function(val) {
		var reg = /^\d+(\.\d+)?$/;
		return reg.test(val);
	};

	$.checkNumber = function(val) {
		var reg = /^\d+$/;
		return reg.test(val);
	};

	$.checkInteger = function(val) {
		var reg = /^[-\+]?\d+$/;
		return reg.test(val);
	};

	$.checkDouble = function(val) {
		var reg = /^[-\+]?\d+(\.\d+)?$/;
		return reg.test(val);
	};

	$.checkEnglish = function(val) {
		var reg = /^[A-Za-z]+$/;
		return reg.test(val);
	};

	$.checkQQMsn = function(val) {
		var reg = /^[1-9]*[1-9][0-9]*$|^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
		return reg.test(val);
	};

})(jQuery);

jQuery(function($) {
	$(".search-input").focus(function() {
		if ($.trim(this.value) == this.getAttribute("empty"))
			this.value = "";
		$(this).removeClass("inactive");

	}).blur(
			function() {
				if ($.trim(this.value) == ""
						|| $.trim(this.value) == this.getAttribute("empty")) {
					this.value = this.getAttribute("empty");
					$(this).addClass("inactive");
				}
			});

	if ($("#sysmsg-error") != "none" || $("#sysmsg-success") != "none") {
		var hideErr = function() {
			$("#sysmsg-box").slideUp(300);
		};

		errHideTimeOut = setTimeout(hideErr, 5000);

		$("#sysmsg-box .close").one("click", function() {
			$("#sysmsg-box").hide();
		});
	}
});

function shoucang(obj) {
	

	var top=$(obj).children("a").offset().top;
	var left=$(obj).children("a").offset().left;
	var goodsid=$(obj).parent(".dop").parent(".deal").attr("attr");
	$(".pings").show();
	$(".pings").css("top",top-52+"px");
	$(".pings").css("left",left-30+"px");
	$(".pings span").eq(0).attr("gsid",goodsid);
	$(".pings span").eq(1).attr("gsid",goodsid);
	$(".pings span").eq(0).next("input").val("1");	

	/*var top = $(obj).children("a").offset().top;
	var left = $(obj).children("a").offset().left;
	var goodsid=$(obj).attr("attr");
	var divp=$(obj).parent("div").siblings("span [class=\"ping\"]");

	divp.show();
	divp.css("top", top - 52 + "px");
	divp.css("left", left - 30 + "px");
	divp.children("span").eq(0).attr("gsid", goodsid);
	divp.children("span").eq(1).attr("gsid", goodsid);
	divp.children("span").eq(0).next("input").val("1");
	*/
	
}


function sh_time(obj) {

	objn = $(obj).children("div").show();

}
function hi_time(obj) {

	objn = $(obj).children("div").hide();

}

function hd() {

	$(".pings").hide();

}

function AddFavorite(sURL, sTitle) {
	try {
		window.external.addFavorite(sURL, sTitle);
	} catch (e) {
		try {
			window.sidebar.addPanel(sTitle, sURL, "");
		} catch (e) {
			alert("加入收藏失败，请使用Ctrl+D进行添加");
		}
	}
}

function homePage(obj, val) {
	try {
		obj.style.behavior = 'url(#default#homepage)';
		obj.setHomePage(val);
	} catch (e) {
		if (window.netscape) {
			try {
				netscape.security.PrivilegeManager
						.enablePrivilege("UniversalXPConnect");
			} catch (e) {
				alert("此操作被浏览器拒绝！");
			}

			var prefs = Components.classes['@mozilla.org/preferences-service;1']
					.getService(Components.interfaces.nsIPrefBranch);
			prefs.setCharPref('browser.startup.homepage', val);
		}
	}
}

