/*
 *  格式化字符串
 * */
$.format = function(source, params) {
	if ( arguments.length == 1 ) 
		return function() {
			var args = $.makeArray(arguments);
			args.unshift(source);
			return $.format.apply( this, args );
		};
	if ( arguments.length > 2 && params.constructor != Array  ) {
		params = $.makeArray(arguments).slice(1);
	}
	if ( params.constructor != Array ) {
		params = [ params ];
	}
	$.each(params, function(i, n) {
		source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
	});
	return source;
};
var CommonUnity={
		isIE6:function(){
			return (document.all) && ([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6);
		},
		checkCnChart:function(values){
	    	// 校验中文字符
			var iCheck = false;
	    	for(i=0; i<values.length; i++){
				if(values.charCodeAt(i) > 255){
					iCheck = true;
					return iCheck;
				 }
			}
	    	return iCheck;
		}
	};
$(function(){
	var searchAllDrop = $("#SearchAllDrop");
	var searchAllPanel = $("#SearchAllPanel");
	var searchAllItems = searchAllPanel.find("li");
	// 选择头部搜索类型
	searchAllDrop.click(function(){
		if(searchAllPanel.css("display") != "none")
		{
			searchAllPanel.hide();
		}else
		{
			// 设置默认选中类型样式
			var searchType = $("#hdnSearchType").val();
			searchAllItems.removeClass("Search_Hover");
			searchAllPanel.find("li[catalogid='"+searchType+"']").addClass("Search_Hover");
			// 显示下拉菜单项
			searchAllPanel.show();
		}
	});

	// 设置头部搜索类型下拉项鼠标悬停样式
	searchAllItems.hover(function(){
		searchAllItems.removeClass("Search_Hover");
		$(this).addClass("Search_Hover");
	},function(){
		$(this).removeClass("Search_Hover");
	});
	// 选择头部搜索类型下拉项
	searchAllItems.click(function(){
		searchAllDrop.children("label").html(HtmlEncode($(this).text()));
		searchAllPanel.hide();
		// 保存当前所选类型(0:Product；1:Seller)
		$("#hdnSearchType").val($(this).attr("catalogid"));
	});
	toggleTextTips($("#searchKey"),"All products","Font_Gray");
	var ToggelCategory=$("#ToggelCategory");
	var CategoryPanel=$("#CategoryPanel");
	var timerCategory =0;
	ToggelCategory.bind("mouseover",function(){
		clearTimeout(timerCategory);
		CategoryPanel.show();
	});
	ToggelCategory.bind("mouseout",function(){
		timerCategory = setTimeout(function(){CategoryPanel.hide()},30);
		//CategoryPanel.hide();
	});
	CategoryPanel.bind("mouseover",function(){
		clearTimeout(timerCategory);
		CategoryPanel.show();
	});
	CategoryPanel.bind("mouseout",function(){
		timerCategory = setTimeout(function(){CategoryPanel.hide()},30);
		//CategoryPanel.hide();
	});
	$("#CategoryPanel > li:not(:first-child)").hover(function(){
		$("#CategoryPanel > li").removeClass("Menu_Hover");
		$(this).addClass("Menu_Hover");	
		// 显示子级菜单
		var subCategoryObj = $(this).find(".Sub_Menu");
		if(subCategoryObj.length > 0)
		{
			toggleMenu(true,subCategoryObj);
		}
	},function(){
			toggleMenu(false,$(".Sub_Menu"));
			$("#CategoryPanel > li").removeClass("Menu_Hover");
	});
	$(document).click(function(event) {
		var _e = $(event.target);
		if (_e.attr("id") != "SearchAllDrop" && _e.parent("#SearchAllDrop").length<1) {
			searchAllPanel.hide();
		}
	});
	initButtonEven($(".Btn_Default"));
})
/*
 *  切换显示文本框内默认提示文字
 * */
function toggleTextTips(obj,tipsText,tipsStyle)
{
	obj.click(function(){
		var _e = $(this);
		var _value = $.trim(_e.val());
		if(_value=="" || _value==tipsText)
			_e.removeClass(tipsStyle).val("");
	});
	obj.blur(function(){
		var _e = $(this);
		var _value = $.trim(_e.val());
		if(_value=="" || _value==tipsText)
		_e.addClass(tipsStyle).val(tipsText);
	});
}
/*
 * 统一处理页面按钮触发鼠标事件时效果
 */
function initButtonEven(buttonEven)
{
    buttonEven.mouseover(function(){
    	$(this).attr("class","Btn_Hover");
    }).mouseout(function(){
    	$(this).attr("class","Btn_Default");
    }).mousedown(function(){
    	$(this).attr("class","Btn_Down");
    });
}
/**
 * 展开/收缩类别下拉菜单
 * */
var resizeFlag = false;
function toggleMenu(display,toggleEven)
{
	var e = toggleEven;
	var ie6 = CommonUnity.isIE6();
	if(display)
	{
		e.show();
		if(!resizeFlag)
		{
			resizeFlag = true;
			var itemObj = e.parent("li");
			var itemHeight = itemObj.height();
			var subHeight = e.height();
			var resizeTop = subHeight-itemHeight;
			if(resizeTop<=17)
			{
				e.css("margin-top","-"+resizeTop+"px");
			}
		}
		// IE6 显示Iframe遮罩层
		if(ie6){			
			if(e.next(".iframefade").length < 1)
			{
				var iframeHTML = '<iframe src="javascript:false;" class="iframefade" style="position:absolute;z-index:10;filter: alpha(opacity=0);opacity:0;margin-top:{0};margin-left:{1};width:{2}px;height:{3}px;"></iframe>';
				var fadeHeight = e.height();
				var fadeWidth = e.width();
				iframeHTML = $.format(iframeHTML,"-21px","180px",fadeWidth,fadeHeight);
				$(iframeHTML).insertAfter(e);
			}else{
				e.next(".iframefade").show();
			}
		}
	}else{
		e.hide();
		if(ie6){
			e.next(".iframefade").hide();
		}
	}
}
