$(document).ready(function() {
	$("#sort_price dl").mouseover(function(){
		$(this).addClass("hove");
	}).bind("mouseleave",function(){
		$(this).removeClass("hove");
		$(this).children("dd").each(function(){
			$(this).hide();
		});
	});
	$("#sort_price dd").hover(function(){
		$(this).css({background:"#FFF3BF"});
	},function(){
		$(this).css({background:"#ffffff"});
	});
	//GetBrands("1");
	$("#cid").change(function(){
		GetBrands($(this).val());
		GetProps($(this).val());
	});
	
	// 设置上一页、下一页信息
	var curpage = parseInt($("#curpage").text());
	var totalpage =  parseInt($("#totalpage").text());
	if(curpage==1){
		if(curpage!=totalpage && totalpage!=0){
			$(".prepage").html(prePage);
			curpage = curpage+1;
			$(".nextpage").html("<a href='"+baseUrl+"-k_"+key+"-c_"+cid+"-b_"+bid+"-m_"+lp+","+hp+"-p_"+p+"-"+curpage+"-"+display+"-"+sort+"."+extend+"'>"+nextPage +"</a>");
		}
		else{
			$(".prepage").html(prePage);
			$(".nextpage").html(nextPage);
		}
	}
	else if(curpage!=1 && curpage!=totalpage){
		var curpage1=curpage-1
		var curpage2=curpage+1
		$(".prepage").html("<a href='"+baseUrl+"-k_"+key+"-c_"+cid+"-b_"+bid+"-m_"+lp+","+hp+"-p_"+p+"-"+curpage1+"-"+display+"-"+sort+"."+extend+"'>"+prePage +"</a>");
		$(".nextpage").html("<a href='"+baseUrl+"-k_"+key+"-c_"+cid+"-b_"+bid+"-m_"+lp+","+hp+"-p_"+p+"-"+curpage2+"-"+display+"-"+sort+"."+extend+"'>"+nextPage +"</a>");
	}
	else{
		$(".nextpage").html(nextPage);
		curpage = curpage-1;
		$(".prepage").html("<a href='"+baseUrl+"-k_"+key+"-c_"+cid+"-b_"+bid+"-m_"+lp+","+hp+"-p_"+p+"-"+curpage+"-"+display+"-"+sort+"."+extend+"'>"+prePage +"</a>");
	}
	
	//给页码文本框绑定onkeyup事件
	$("#gopage").bind("keyup",function(){
		$("#gopage").attr({
			"value" : $("#gopage").val().replace(/\D/g,"")
		});
	});
	//给跳转按钮绑定onclick事件
	$("#go").bind("click",function(){
		var pageNum = $("#gopage").val();
		if(pageNum.replace(new RegExp("(^[\\s]*)|([\\s]*$)", "g"), "") != "" ){
            if(pageNum != "0"){
                if(pageNum > totalpage){
                    alert("输入的数字大于总页数");
                    $("#gopage").attr("value","");
                    return false;
                }else{
					window.location.href=baseUrl+"-k_"+key+"-c_"+cid+"-b_"+bid+"-m_"+lp+","+hp+"-p_"+p+"-"+pageNum+"-"+display+"-"+sort+"."+extend;
                }
            }
            else{  
                alert("输入错误！");
                $("#gopage").attr("value","");
                return false;
            }
        }
        else{
            alert("请输入要跳转到的页面");
            $("#gopage").attr("value","");
            return false;
        }
	});
	
});

function GetBrands(cid){
	 $.ajax({
        type:"get",
        dataType:"json",
        url:"search.aspx?NoCopyRight=1",
        data:"type=ajax&action=getbrands&cid="+escape(cid),
        cache:false,
        success:function(msg){
			$("#brand").html("");
            var data = msg.Brands;
            var flag = false;
            $.each(data,function(i,m){
				flag = true;
				$("<input type='checkbox' value='"+m.BrandID+"' name='bid' id='b"+m.BrandID+"'>").appendTo($("#brand"));
				$("<label for='b"+m.BrandID+"'>"+m.BrandName+"</label>").appendTo($("#brand"));				
            });
            if(flag){
				$(".brand_tr").show();
			}else{
				$(".brand_tr").hide();
			}
        },//end success
        error:function(XMLHttpRequest,textStatus){alert(textStatus+"_brand");}
    });//end ajax
};//end function

function GetProps(cid){
	 $.ajax({
        type:"get",
        dataType:"json",
        url:"search.aspx?NoCopyRight=1",
        data:"type=ajax&action=GetProps&cid="+escape(cid),
        cache:false,
        success:function(msg){
			$("#field").html("");
            var data = msg.props;
            var flag = false;
            $.each(data,function(i,n){
				if(n.type!=1){
					flag = true;
					var index=i+1;
					var type=null;
					$("<h4>"+n.name+":</h4>").appendTo($("#field"));
					if(n.type==2){
						type="radio";
					};
					if(n.type==3){					
						type="checkbox";
					};
					$.each(n.options,function(j,m){
						var no = j+1;
						$("<input type='"+type+"' value='"+no+"' name='p" + index +"' id='p"+index+"_"+no+"'>").appendTo($("#field"));
						$("<label for='p"+index+"_"+no+"'>"+m.value+"</label>").appendTo($("#field"));
					});
					$("<br/>").appendTo($("#field"));
				};		
            });
            if(flag){
				$(".field_tr").show();
			}else{
				$(".field_tr").hide();
			};
        },//end success
        error:function(XMLHttpRequest,textStatus){alert(textStatus);}
    });//end ajax
};//end function