$(document).ready(function() {
	var sort=$().getParmByUrl("OrderSort");
	if(sort != undefined && sort !="")
	{
		$("#ddlSort").attr("value",sort);
	}
	else
	{
		$("#ddlSort").attr("value","1");
	}
	
	// 设置上一页、下一页信息
	var curpage = parseInt($("#curpage").text());
	var totalpage =  parseInt($("#totalpage").text());
//	var catID=$().getParmByUrl("CatID") == undefined ? "" : "CatID="+$().getParmByUrl("CatID")+"&";
	var catID=cID == "" ? "" : "CatID="+cID+"&";
	
	var display=$().getParmByUrl("display") == undefined ? "" : "display="+$().getParmByUrl("display")+"&";
	var orderSort=$().getParmByUrl("OrderSort") == undefined ? "" : "OrderSort="+$().getParmByUrl("OrderSort")+"&";	
	if(curpage==1)
	{
		if(curpage!=totalpage && totalpage!=0)
		{
			$(".prepage").html(prePage);
			curpage = curpage+1;
			var page = "page="+curpage+"&";
			var urlPara = catID;
			urlPara += page;
			urlPara += display;
			urlPara += orderSort;
			if(urlPara.substring(urlPara.length-1,urlPara.length) == "&")
			{
				urlPara = urlPara.substring(0,urlPara.length-1);
			}
			$(".nextpage").html("<a href='" + baseUrl + urlPara + "'>"+nextPage +"</a>");

		}
		else
		{
			$(".prepage").html(prePage);
			$(".nextpage").html(nextPage);
		}
	}
	else if(curpage!=1 && curpage!=totalpage)
	{
		var curpage1=curpage-1
		var curpage2=curpage+1
		var page1 = "page="+curpage1+"&";
		var page2 = "page="+curpage2+"&";
		var urlPara1 = catID;
		urlPara1 += page1;
		urlPara1 += display;
		urlPara1 += orderSort;
		var urlPara2 = catID;
		urlPara2 += page2;
		urlPara2 += display;
		urlPara2 += orderSort;
		if(urlPara1.substring(urlPara1.length-1,urlPara1.length) == "&")
		{
			urlPara1 = urlPara1.substring(0,urlPara1.length-1);
			
		}
		if(urlPara2.substring(urlPara2.length-1,urlPara2.length) == "&")
		{
			urlPara2 = urlPara2.substring(0,urlPara2.length-1);
		}
		$(".prepage").html("<a href='" + baseUrl + urlPara1 + "'>"+prePage +"</a>");
		$(".nextpage").html("<a href='" + baseUrl + urlPara2 + "'>"+nextPage +"</a>");
	}
	else
	{
		$(".nextpage").html(nextPage);
		curpage = curpage-1;
		var page = "page="+curpage+"&";
		var urlPara = catID;
		urlPara += page;
		urlPara += display;
		urlPara += orderSort;
		if(urlPara.substring(urlPara.length-1,urlPara.length) == "&")
		{
			urlPara = urlPara.substring(0,urlPara.length-1);
		}
		$(".prepage").html("<a href='" + baseUrl + urlPara + "'>"+prePage +"</a>");
	}
	
	//给ddlSort控件绑定onChange事件
	$("#ddlSort").bind("change",function(){
		var urlPara="";
		var page=$().getParmByUrl("page") == undefined ? "" : "page="+$().getParmByUrl("page")+"&";	
		var display=$().getParmByUrl("display") == undefined ? "" : "display="+$().getParmByUrl("display")+"&";
		urlPara += catID;
		urlPara += page;
		urlPara += display;
		window.location.href=baseUrl+urlPara+"OrderSort="+$("#ddlSort").val();
	});
	//给页码文本框绑定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
                {
					var urlPara="";
					var display=$().getParmByUrl("display");
					var orderSort=$().getParmByUrl("OrderSort");
					urlPara += catID;
					urlPara += "page="+pageNum +"&";
					urlPara += display == undefined ? "" : "display="+display+"&";
					urlPara += orderSort == undefined ? "" : "OrderSort="+orderSort+"&";
					if(urlPara.substring(urlPara.length-1,urlPara.length) == "&")
					{
						urlPara = urlPara.substring(0,urlPara.length-1);
					}
					window.location.href=baseUrl+urlPara;
                }
            }
            else
            {  
                alert("输入错误！");
                $("#gopage").attr("value","");
                return false;
            }
        }
        else
        {
            alert("请输入要跳转到的页面");
           $("#gopage").attr("value","");
            return false;
        }
	});
	//给checkbox绑定onclick事件
	$(".cbCompare").bind("click",function(){
		$cb = $(this); 
		var n = 0;
		var m = 0;
		var rec = new Array(1);
		if($cb.is(":checked"))
		{
			rec[0] = $cb.offset().top;
			rec[1] = $cb.offset().left;
			m = 1;
		}
		$(".cbCompare").each(function(){
			$cb1 = $(this);
			if($cb1.is(":checked"))
			{
				if(m==0)
				{
					rec[0] = $cb1.offset().top;
					rec[1] = $cb1.offset().left;
				}  
				$("#btnCompare").css("top",rec[0]+20);                             
				$("#btnCompare").css("left",rec[1]);
				n++;
			}
		});
		if(n>0)
		{
			$("#btnCompare").addClass("compare_show");
		}
		else
		{ 
			$("#btnCompare").removeClass("compare_show");
		}
	});
	//给对比按钮绑定onclick事件
	$("#doCompare").bind("click",function(){
		var n = 0;
        var ids = "";
		$(".cbCompare").each(function(){
			if($(this).is(":checked"))
			{
				if(n==0)
				{
					ids = ids + "?method=compare&pids=" + $(this).val();
				}
				else
				{
					ids = ids + "&pids=" + $(this).val();
				}
				n++;
			}
		});
		if(n>0)
		{
			//window.open("Compare.aspx" + ids,"Compare","width=1000,height=850,scrollbars=yes,top=0,left=0,resizable=yes");
			targetUrl=compareUrl + ids;
			newwin=window.open("","Compare","status,scrollbars,resizable");
			if(document.all){
				newwin.moveTo(0,0)
				newwin.resizeTo(screen.width,screen.height)
			}
			newwin.location=targetUrl;
		}
	});
});