﻿    var timer="";
    var AjaxTemplate={
        //获取dom节点
        $:function(domId){
            return document.getElementById(domId);
        },
        decode:function(value){
           return decodeURIComponent(value);
        },
        encode:function(value){
           return encodeURIComponent(decodeURIComponent(value));
        },
        //排序调用的方法
        sort:function(obj){
            this.fulValue("rfOrderTxt",obj.options[obj.selectedIndex].value);     
            this.createUrl();
            this.getPageContent();
        },
        //获取ProductType选择的值
        selectProductType:function(){        
            var length=$("#RefineByProductType li a.selected").size();
            var values=" ";
            for(var i=0;i<length;i++){
                if(i==length-1)
                    values+=this.decode($("#RefineByProductType li a.selected").get(i).href.split("#")[1]);
                else{
                    values+=this.decode($("#RefineByProductType li a.selected").get(i).href.split("#")[1])+"|";
                }
            }
            return values;
        },
        //加载遮罩
        showCover:function(){
            $(".overlay").css("display","block");
            var offset=$("#productBox").offset();
            var height = $("#productBox").height();  
            $(".overlay").css("height",height+"px");
            $(".overlay").css("left",offset.left+"px");
            $(".overlay").css("top",offset.top+"px");
        },
        //加载成功隐藏遮罩
        hideCover:function(){
            $(".overlay").css("display","none");
        },
        //获取产品品牌选中的值
        selectBrand:function (){
            var length=$("#RefineByBrand li a.selected").size();
            var values=" ";
            for(var i=0;i<length;i++){
                if(i==length-1)
                    values+=this.decode($("#RefineByBrand li a.selected").get(i).href.split("#")[1]);
                else{
                    values+=this.decode($("#RefineByBrand li a.selected").get(i).href.split("#")[1])+"|";
                }
            }
            return values;
        },
        //获取选中的值*传递属性名称
        selectAttrValue:function(id){
            var length=$("#"+id+" li a.selected").size();
            var values=" ";
            for(var i=0;i<length;i++){
            //if(i==length-1)
                //   values+=$("#"+id+" li a.selected").get(i).href.split("#")[1];
            //else{
                values+=this.decode($("#"+id+" li a.selected").get(i).href.split("#")[1])+"|";
            //}
            }
            return values;
        },
        //绑定值
        fulValue:function(str,value){
            if(value)
                this.$(str).value =value;
            else
                return this.$(str).value;
        },
        //触发单击刷新事件
        attrClick:function (obj){
            if(jQuery.className.has( obj, "disabled" )){
                return;
            };
            var  state = jQuery.className.has( obj, "selected" );
            if(state){
                $(obj).get(0).className="unSelected"
            }else{
                $(obj).get(0).className="selected";
            }
            //rfColorTxt是所有属性值的总称
            this.fulValue("rfColorTxt",menu_getAttrValue());
            this.fulValue("rfBrandTxt",this.selectBrand());
            this.fulValue("rfPageSize","0");
            this.fulValue("rfPriceTypeTxt",this.selectProductType());
            this.fulValue("rfSpeTxt",menu_getSpecValue());
            this.createUrl();
            this.getPageContent();
            return false;
        },
        //单击分页显示数目
        pageClick:function(num){      
            this.fulValue("rfPageSize",num.toString());      
            this.createUrl();
            this.getPageContent();
            return false;
        },
        //创建URL,用来保存信息
        createUrl:function(){
            var urlstr =this.encode("urlproductAtt")+"="+this.encode(this.fulValue("rfColorTxt").replace(/^\s*|\s*$/g,""))+"&";
            urlstr +=this.encode("urlproductSpe")+"="+this.encode(this.fulValue("rfSpeTxt").replace(/^\s*|\s*$/g,""))+"&";
            urlstr +=this.encode("urlbrand")+"="+this.encode(this.fulValue("rfBrandTxt").replace(/^\s*|\s*$/g,""))+"&";
            urlstr +=this.encode("urlprice")+"="+this.encode(this.fulValue("rfPriceMinTxt").replace(/^\s*|\s*$/g,"")+"|" +this.fulValue("rfPriceMaxTxt").replace(/^\s*|\s*$/g,""))+"&";
            urlstr +=this.encode("urlpagesize")+"="+this.encode(this.fulValue("rfPageSize").replace(/^\s*|\s*$/g,""))+"&";
            urlstr +=this.encode("urlorder")+"="+this.encode(this.fulValue("rfOrderTxt").replace(/^\s*|\s*$/g,""))+"&";
            urlstr +=this.encode("CategoryId")+"="+ this.encode(this.fulValue(menu_catelogTxt).replace(/^\s*|\s*$/g,""))+"&";
            urlstr +="m="+(new Date()).getTime()+"&";
            urlstr +=this.encode("urlproductType")+"="+this.encode(this.fulValue("rfPriceTypeTxt").replace(/^\s*|\s*$/g,""))+"&";
            urlstr +=this.encode("urlpagenum")+"="+this.encode( this.fulValue("rfpageNum").replace(/^\s*|\s*$/g,""));
            window.location.hash=urlstr;          
        },
         //单击分页
        pageNumClick:function(num){
            this.fulValue("rfpageNum",num.toString());
            this.fulValue("rfPageSize","1");  
            this.createUrl();
            this.getPageContent();
            return false;
        },
        //从url上获取信息
        getUrlHashInfo:function(){
            var me=this;
            if(location.href.split("#").length==2){
                var hashInfo =location.href.split("#")[1];
                for(var a=0;a<hashInfo.split("&").length;a++){
                    var i=hashInfo.split("&")[a];
                    if(i.split("=").length==2){
                        switch (i.split("=")[0]){
                            case me.encode("urlproductAtt"):
                                var urlAtt =me.encode("|")+i.split("=")[1]+me.encode("|");
                                me.fulValue("rfColorTxt",me.decode(i.split("=")[1]));
                                $("#RefineByProductType li a").each(function(){
                                    var attHash =me.encode(this.href.split("#")[1]);
                                    if(urlAtt.indexOf(attHash)>-1){
                                        $(this).addClass("selected");
                                    }
                                });
                                $("#RefineByBrand li a").each(function(){
                                    var attHash =me.encode(this.href.split("#")[1]);
                                    if(urlAtt.indexOf(attHash)>-1){
                                        $(this).addClass("selected");
                                    }
                                });
                                $("#RefineByColor li a").each(function(){
                                    var attHash =me.encode(this.href.split("#")[1]);
                                    if(urlAtt.indexOf(attHash)>-1){
                                        $(this).attr("className","selected");
                                    }
                                });
                                break;
                            case me.encode("urlproductSpe"): 
                                var urlAtt =me.encode("|")+i.split("=")[1]+me.encode("|");
                                me.fulValue("rfSpeTxt",me.decode(i.split("=")[1]));

                                $("#RefineBySpe li a").each(function(){
                                    var attHash =me.encode(this.href.split("#")[1]);
                                    if(urlAtt.indexOf(attHash)>-1){
                                        $(this).attr("className","selected");
                                    }
                                });
                                break;
                            case me.encode("urlbrand"):
                                var urlAtt =me.encode("|")+i.split("=")[1]+me.encode("|");                        
                                me.fulValue("rfBrandTxt",me.decode(i.split("=")[1]));                                                  
                                $("#RefineByBrand li a").each(function(){
                                    var attHash =me.encode(this.href.split("#")[1]);
                                    if(urlAtt.indexOf(attHash)>-1){
                                        $(this).attr("className","selected");
                                    }
                                });
                                break;
                            case me.encode("urlprice"):{
                                //if(i.split("=")[1].split("|").length==2){
                                //  fulValue("rfPriceMaxTxt",i.split("=")[1].split("|")[1]);
                                //  fulValue("rfPriceMinTxt",i.split("=")[1].split("|")[0]);
                                //};
                                //break;
                            } 
                            case me.encode("urlorder"):{
                                me.fulValue("rfOrderTxt",me.decode(i.split("=")[1]));
                                break;
                            }
                            case me.encode("urlpagesize"):{
                                me.fulValue("rfPageSize",me.decode(i.split("=")[1])); 
                                break;
                            }
                            case me.encode("urlproductType"): {
                                me.fulValue("rfPriceTypeTxt",me.decode(i.split("=")[1]));
                                var urlType =me.encode("|")+i.split("=")[1]+me.encode("|");
                                $("#RefineByProductType li a").each(function(){
                                    var attHash =me.encode(me.decode(this.href.split("#")[1]));
                                    if(urlType.indexOf(attHash)>-1){
                                        $(this).attr("className","selected");
                                    }
                                });
                                break;
                            }
                            case me.encode("urlpagenum"):{
                                me.fulValue("rfpageNum",me.decode(i.split("=")[1])); 
                                break;
                            }
                        }
                    }
                }
            me.createUrl();
            me.getPageContent();
        }
        },
        //获取内容
        getPageContent:function(){
            var me=this;
            me.showCover(); 
            jQuery.ajax({
                url: "/OsShop/AjaxTemplateBySwn/AjaxPage/getRefineInfo.aspx?"+location.hash.split("#")[1],
                type: "GET",
                dataType: "html",
                complete: function(res, status){
                    var i=me.$(menu_contr_num).value;	   

                    for(var a =0;a<=i;a++){
                        for(var b=0;b< $("#refine_"+a+" li a").size();b++){
                            if(res.responseText.indexOf(me.decode("attr_"+$("#refine_"+a+" li a").get(b).href.split("#")[1]))==-1){
                                $("#refine_"+a+" li a").get(b).className="disabled";
                            }else{
                                var  state = jQuery.className.has( $("#refine_"+a+" li a").get(b), "selected" );
                                if(state){
                                    $("#refine_"+a+" li a").get(b).className="selected";
                                }else{
                                    $("#refine_"+a+" li a").get(b).className="unSelected";
                                }
                            };
                        };
                    }
                    var y=me.$(menu_spec_num).value;
                    for(var a =0;a<=y;a++){
                        for(var b=0;b< $("#render_"+a+" li a").size();b++){
                            if(res.responseText.indexOf(me.decode("spec_"+$("#render_"+a+" li a").get(b).href.split("#")[1]))==-1){
                                $("#render_"+a+" li a").get(b).className="disabled";
                            }else{
                                var  state = jQuery.className.has( $("#render_"+a+" li a").get(b), "selected" );

                                if(state){
                                    $("#render_"+a+" li a").get(b).className="selected";
                                }else{
                                    $("#render_"+a+" li a").get(b).className="unSelected";
                                }
                            };
                        };
                    }
                }});
            jQuery.ajax({
                url: "/OsShop/AjaxTemplateBySwn/AjaxPage/getProductInfo.aspx?"+location.hash.split("#")[1],
                type: "GET",
                dataType: "html",

                complete: function(res, status){
    
                                me.$("productBox").innerHTML =res.responseText.match(/<form(.|\s)*?>((.|\s)*?)<\/form>/)[2];
                                me.hideCover();
            }})
        }
    };  
    //加载完成初始化
    $(function(){
        if(menu_maxPrice>0){
            $("#slider").slider({
                animate:true,
                max:menu_maxPrice,
                min:menu_minPrice,
                range:true,
                values:[menu_minPrice,menu_maxPrice],
                orientation:'auto',
                slide:function(event, ui) {
                    AjaxTemplate.fulValue("rfPriceMaxTxt",ui.values[1]);
                    AjaxTemplate.fulValue("rfPriceMinTxt",ui.values[0]);
                    AjaxTemplate.$("userMin").innerHTML=ui.values[0];
                    AjaxTemplate.$("userMax").innerHTML=ui.values[1];
                    if(timer){
                        clearTimeout(timer);
                        timer= setTimeout(function(){
                            AjaxTemplate.createUrl();
                            AjaxTemplate.getPageContent();
                        },1000);
                    }else{
                        timer= setTimeout(function(){
                            AjaxTemplate.createUrl();
                            AjaxTemplate.getPageContent();
                        },1000);
                    }
                }});
         }
         AjaxTemplate.getUrlHashInfo();
         });
