$(function(){
//图片放大效果
  var zoomnum=3;
  var imgw=855;
  var imgh=855;
  $("#cursor").css("opacity",0.3);
  $("#smallpic").mouseover(function(){
          $("#cursor").show();
	  $("#bigpic").show();
	  $("#product_info").hide();
	}).mouseout(function(){
          $("#cursor").hide();
	  $("#bigpic").hide();
 	  $("#product_info").show();
	}).mousemove(function(e){
var dapeizengjiagaodu = 0;
var dapeizengjiagaodu2 = 0;
if($("#bigpic").height()>320){
   dapeizengjiagaodu = 75;
   dapeizengjiagaodu2 = 285;
   }
    var widthh=e.pageX-$(this).offset().left||e.originalEvent.layerX-$(this).offset().left||0;//
    var heightt=e.pageY-$(this).offset().top||e.originalEvent.layerY-$(this).offset().top||0;//
    var hhh = (232+dapeizengjiagaodu)-52;
    var h = heightt-52;if(h<0)h=0;if(h>hhh)h=hhh;
    var w= widthh-78;if(w<0)w=0;if(w>129)w=129;
      $("#cursor").css("margin-top",h+"px").css("margin-left",w+"px");
      $("#bigpic").css("background-position","-"+((w+78)*zoomnum-235+imgw)+"px -"+((h+52)*zoomnum-156+dapeizengjiagaodu2+imgh)+"px");
     
  });


//商品分类下拉效果
  $("div[id^=dalei_]").click(function(){
    var xiaolei=$(this).attr("id").replace("dalei","xiaolei");
    $("#"+xiaolei).toggle("slow");
  })
var locastr = location.href;
if(locastr.indexOf("?")>0){
  locastr=UrlDecode(locastr);
  locastr=locastr.split("?")[1];
  locastr=locastr.split("&");

  for(i=0;i<locastr.length;i++){
    if(locastr[i].indexOf("channel_name")>=0){
     locastr = locastr[i].split("=")[1]; 
     break;
    }
  }

switch(locastr){
  case "女装":
  locastr="nvzhuang";
  break;
  case "男装":
  locastr="nanzhuang";
  break;
  case "配饰":
  locastr="peishi";
  break;
  case "鞋品":
  locastr="xiepin";
  break;
  case "包包":
  locastr="baobao";
  break;
  case "化妆品":
  locastr="huazhuangpin";
  break;
  case "运动休闲":
  locastr="yundongxiuxian";
  break;
  case "玩具礼品":
  locastr="wanjvlipin";
  break;
  case "美食":
  locastr="meishi";
  break;
  case "其他":
  locastr="qita";
  break;
}
}else{
  locastr = locastr.split("/")[5];
}

$("div[name="+locastr+"]").show();
if(location.href.indexOf("ydxx=是运动休闲")>0){
$("div[name=yundongxiuxian]").show();
}
//
    var myurl = location.href;
    var pos = myurl.lastIndexOf('/');
    if(pos>0)
    {
        myurl = myurl.substring(pos);
    }
     $('#clock').jclock({ withDate: true, withWeek: true });
    if($.cookie("dnt")!=null && $.cookie("username")==null)
    {
        //location='/app/appLogin.aspx?next='+$.URLEncode(window.location.href);
       // return;
       $.ajax({
             url:'/app/appLogin.aspx?next='+$.URLEncode(window.location.href),
             type: 'GET',
             dataType: 'html',
             timeout: 20000,//超时时间设定
             success: function(data){
               showName();
             }
            }); 
    }
    if($.cookie("dnt")==null && $.cookie("username")!=null)
    {
        $.cookie("username", null);
        $.ajax({
             url:'/app/logout.aspx',
             type: 'GET',
             dataType: 'html',
             timeout: 20000,//超时时间设定
             success: function(data){
               showName();
             }
            }); 
        
    }
    
    showName();
        
    //取天气
    $.ajax({
     url:'/app/weather.ashx',
     type: 'GET',
     dataType: 'html',
     timeout: 20000,//超时时间设定
     success: function(data){
       $('#weatherSpan').html(data);  
     }
    }); 
});

function showName()
{

    if($.cookie("username")==null)
        {
            $('#userlogin').hide();
            $('#loginframe').show(); 
        }
    else
        {
        $('#loginframe').hide();
        $('#username').html($.cookie("username"));
        $('#userlogin').show();
        }
}

function loginBbs(){
var param ='UserBox='+$("#UserBox").attr("value")+'&PwdBox='+$("#PwdBox").attr("value");
$.ajax({
 url:'/app/login.aspx',
 type: 'POST',
 dataType: 'html',
 timeout: 20000,//超时时间设定
 data:param,//参数设置
 error: function(response,status){alert('错误，请稍后再试');},//错误处理，隐藏进度条
 success: function(data){
  //加载成功处理
  if(data=="1" && $.cookie("username")!=null)
  {
        $('#loginframe').hide();
        $('#username').html($.cookie("username"));
        $('#userlogin').show();
  }
  else
  {
    alert('登录错误!');
  }
  
 }
}); 
}

//收藏商品type=3 收藏店铺typeid=4 关注店铺type=5 
function addFavor(typeid, docid){
  var t="收藏";
  if(typeid=="5")t="关注";
  $.get("/app/Favorites.ashx?type="+typeid+"&docid="+docid, function(data){
    if(data==1){alert(t+"成功，谢谢！")}
    else if(data==2){alert("您已经"+t+"过了，谢谢！")}
    else{alert("请先登录，再来"+t+"，谢谢！")};
  });
}


//评分显示小星星和桃心
function star(e,docid){
  var i=0;
  var s=0;
  var m=$("#star"+e+" div");
  m.hover(function(i){
    i = m.index($(this));
    $("#star"+e+" div:lt("+(i+1)+")").css("background-position",(i<2?"center":"left"));
    $("#star"+e+" div:gt("+i+")").css("background-position","right");
	},function(){
	if(s==0){m.css("background-position","right");}
  }).click(function(){
	s=m.index($(this));
        if(e==0){
	  $.get("/app/rating.ashx?docid="+docid+"&favorite="+s, function(data){
            if(data==1){alert("评分成功，谢谢！")}
            else if(data==2){alert("您已经评过分了，谢谢！")}
            else{alert("请先登录再来评分，谢谢！")}
            });
        }else{
	  $.get("/app/rating.ashx?docid="+docid+"&desire="+s, function(data){
            if(data==1){alert("评分成功，谢谢！")}
            else if(data==2){alert("您已经评过分了，谢谢！")}
            else{alert("请先登录再来评分，谢谢！")}
            });
        }
  });
}
//显示评分
function getStar(docid){
$.get("/app/getRating.ashx?docid="+docid,function(data){
$("#favorite").html(data.split(";")[0]);
$("#desire").html(data.split(";")[1]);
});
}



function SetCookie(name,value) 
{ 
var Days = 30; //此 cookie 将被保存 30 天 
var exp = new Date(); //new Date("December 31, 9998"); 
exp.setTime(exp.getTime() + Days*24*60*60*1000); 
document.cookie = name + "="+ escape(value) +";path=/;domain=www.bjhwa.com;expires="+ exp.toGMTString();
} 
function GetCookie(name) 
{ 
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)")); 
if(arr != null) return unescape(arr[2]); return null; 
} 


//保存最后浏览过的产品id
function SetHistory(productid){
  var hpid=GetCookie("historyproductid");
  hpid=hpid+ ",,,,,,,,,,";
  hpid1=hpid.split(",");
  hpid="";
  for(i=0;i<5;i++){
    if(hpid1[i]!=productid){
      hpid+=hpid1[i]+","
    }
  }
hpid = productid+","+hpid;
  SetCookie("historyproductid",hpid);
}
//<img src="/images/pic12.gif" /><span>韩版优雅垫肩修身小西装</span>
$(function(){
  var hpid2 = GetCookie("historyproductid");
  hpid2+=",,,,,";
  var hpid = hpid2.split(",");
  for(ii=1;ii<5;ii++){
   getHidProduct(hpid[ii],ii-1);
  }
});

function getHidProduct(productid,hisid){
 $.get("/app/productInfo.ashx?docid="+productid, function(data){
if(data.length>10){
      var mydata = data.split(";");
$(".his").eq(hisid).html("<table style=\"float:left\"><tr><td class=\"border02\" height=\"213\" align=\"center\"><a href=\""+mydata[1]+"\"><img src=\"" + mydata[0] + "\" border=\"0\" /></a></td></tr><tr><td class=\"border02\" align=\"center\" height=\"23\" style=\"width:178px;margin-top:5px;background:url(/images/menu_bg10.gif);\"><a href=\"" + mydata[1] + "\">"+mydata[2]+"</a></td></tr></table>");

}
    });
}


//首页楼层展示
$(function(){
var m=$("li[class^=lczs_]");
m.css("cursor","pointer");
var n=$(".left_dpcx_loucengcontent_pic span[id]");
m.mouseover(function(){
  n.hide();
    $(".lczs_curr").attr("class","lczs_normal");
    $(this).addClass("lczs_curr");

    if(m.index($(this))==0){
$("#FB").show();
      n.eq(0).show()
      .mouseover(function(){$(this).show();})
      .mouseout(function(){
          $(this).hide();



          $("#FB").html($(this).html())
          $("#FB").show();
      });
    }else{
      n.eq(m.index($(this))).show()
      .mouseover(function(){$(this).show();})
      .mouseout(function(){
          $(this).hide();

          $("#FB").html($(this).html())
          $("#FB").show();
      });
    }

  //}
});
/*
  $(".left_dpcx_loucengcontent_pic span").click(function(){
    $(this).hide();
    $("#F5-7").show();
  });
  $(document).click(function(){
    $(".left_dpcx_loucengcontent_pic span").hide();
    $("#F5-7").show();
  });
  $(".left_dpcx_loucengcontent_pic").mouseout(function(){
    $(".left_dpcx_loucengcontent_pic span").hide();
    $("#F5-7").show();
  });*/
})



//首页分类查询小类的现实隐藏效果
$(function(){
var m=$(".middle_right_index_flcxlist li");
var n=$(".xiaofenlei");
m.mouseover(function(){
  n.hide();
  n.eq(m.index($(this))).show();
});
});


function UrlEncode(str)
{ 
     var ret=""; 
     var strSpecial="!\"#$%&()*+,/:;<=>?[]^`{|}~%"; var tt="";
     for(var i=0;i<str.length;i++)
     { 
         var chr = str.charAt(i); 
         var c=str2asc(chr); 
         tt += chr+":"+c+"n"; 
         if(parseInt("0x"+c) > 0x7f)
         { 
             ret+="%"+c.slice(0,2)+"%"+c.slice(-2); 
         }
         else
         { 
             if(chr==" ") 
                 ret+="+"; 
             else if(strSpecial.indexOf(chr)!=-1) 
                 ret+="%"+c.toString(16); 
             else 
                 ret+=chr; 
         } 
     } 
     return ret; 
} 

function UrlDecode(str){ 
     var ret=""; 
     for(var i=0;i<str.length;i++)
     { 
         var chr = str.charAt(i); 
         if(chr == "+")
         { 
             ret+=" "; 
         }
         else if(chr=="%")
         { 
             var asc = str.substring(i+1,i+3); 
             if(parseInt("0x"+asc)>0x7f)
             { 
                 ret+=asc2str(parseInt("0x"+asc+str.substring(i+4,i+6))); 
                 i+=5; 
             }
             else
             { 
                 ret+=asc2str(parseInt("0x"+asc)); 
                 i+=2; 
             } 
         }
         else
         { 
             ret+= chr; 
         } 
     } 
     return ret; 
} 

//限时抢购
function getQianggou(myid){
  $.get("/app/productnownum.ashx?productid="+myid,function(data){
    if(data==-1){
      alert("网络繁忙，请稍后刷新页面！")
    }else{
      $("#now_"+myid).html(data);
      $("#over_"+myid).html(Number($("#total_"+myid).html())-Number($("#now_"+myid).html()));
    }
  })
}
$(function(){
  $("input[id^=btn_]").click(function(){

    var myid=this.id.substr(4);
    var Address=$("#address_"+myid).val();
    if($("#now_"+myid).html()=="0"){
      alert("对不起，已经抢光了，欢迎下次再来抢购！");
    }else{

        $.get("/app/panicbuying.ashx?productid="+myid, function(data){


        if(data==0){
          alert("登录系统之后才能进行抢购!");
        }else if(data==1){
          alert("网络繁忙，请稍后重试。");
        }else if(data==2){
          alert("每人仅限抢购1次，该产品您已经成功抢购过！请到" + Address + "购买。");
        }else if(data==3){
          $("#now_"+myid).html($("#now_"+myid).html()-1);
          $("#over_"+myid).html(Number($("#over_"+myid).html())+1);
          alert("抢购成功！请到" + Address + "购买。");
        }else if(data==4){
          alert("对不起，已经抢光了，欢迎下次再来抢购！")
        }

      });
    }
  });
})


//祝福墙
$(function(){
  $.get("/app/ashx/lovewalltop.ashx?n=4",function(data){
    var allZF = data.split("|");
    for(i=0;i<allZF.length;i++){
      var t="<font color=\"#FF0000\">"+allZF[i+2]+"</font>送给<font color=\"#FF0000\">"+allZF[i+3]+"</font>的祝福:"+allZF[i];
      if(t.length>110)t=t.substr(0,108)+"..."
      $("#allzfq").html($("#allzfq").html()+"<li class=\"allzfq\" title=\""+allZF[i]+"\"><img src=\"\/images\/tubiao14.gif\" \/>"+t+" "+ allZF[i+1] +"<\/li>");
      i=i+3;
    }
  });
});

//链接颜色
$(function(){
  $("td>a").hover(function(){$(this).css("color","#FF0000")},function(){$(this).css("color","#000000")});
})




//单品页面的最IN单品和美丽课堂
function getProd(a,b){
  if(b==""){
    $("#aD"+a.substr(1)).parent().parent().hide();
  }
 
if(b.indexOf(",")>0){
  var allDanpin = b.split(",");
var ii = 0;
if(allDanpin.length>4){
  ii=4;
}else{
  ii=allDanpin.length
}

  for(i=0;i<ii;i++){
    $.get("/app/productInfo.ashx?docid="+allDanpin[i],function(data){
      var mydata = data.split(";");
if(mydata[2]!=null){
if(a=="danpin"){
      $("#aDanpin").html($("#aDanpin").html()+"<table style=\"float:left\" cellspacing=\"4\"><tr><td class=\"border02\" align=\"center\"><a href=\""+mydata[1]+"\"><img src=\"" + mydata[0] + "\" border=\"0\" /></a></td></tr><tr><td class=\"border02\" align=\"center\" height=\"23\" style=\"width:178px;margin-top:5px;background:url(/images/menu_bg10.gif);\"><a href=\"" + mydata[1] + "\">"+mydata[2]+"</a></td></tr></table>");
}else{
      $("#aDapei").html($("#aDapei").html()+"<table style=\"float:left\" cellspacing=\"4\"><tr><td class=\"border02\" align=\"center\"><a href=\""+mydata[1]+"\"><img src=\"" + mydata[0] + "\" border=\"0\" /></a></td></tr><tr><td class=\"border02\" align=\"center\" height=\"23\" style=\"width:178px;margin-top:5px;background:url(/images/menu_bg10.gif);\"><a href=\"" + mydata[1] + "\">"+mydata[2]+"</a></td></tr></table>");
}
}
    });
  }
}
 
}


//搜索
$(function(){
  $("#channel_name").change(function(){
    var opt="<option value=\"\"> 全部小类 </option>"
    if($("#channel_name").val()==""){
      $("#leibie").html(opt);
    }else{
      var xiaolei = getxiaolei($("#channel_name").val());
      xiaolei = xiaolei.split("|");

      for(i=0;i<xiaolei.length;i++){
        if(xiaolei[i]!=""){
          opt+="<option value=\""+xiaolei[i]+"\">"+xiaolei[i]+"</option>"
        }
      }
      $("#leibie").html(opt);
      $("#xl").width($("#leibie").width());
    }
  });
  $("#goSearch").click(function(){
    var s="";
    if($("#channel_name").val()!=""){s+="&channel_name="+$("#channel_name").val()}
    if($("#leibie").val()!=""){s+="&leibie="+$("#leibie").val()}
    if($("#keyword").val()!="关键字" && $("#keyword").val()!=""){s+="&title=" + $("#keyword").val()}
    //if(location.href.indexOf("id=")>0){
    //  location.href="/app/productlist.aspx?"+s.substr(1) + "&id="+location.href.split("id=")[1].split("&")[0];
    //}else{
      location.href="/app/productlist2.aspx?"+s.substr(1);
    //}
  });
})

//大类小类对应，根据大类获取小类
function getxiaolei(a){
switch(a){
  case "女装":
return "T恤|连衣裙|针织衫|衬衫|外套|卫衣|帽衫|风衣|运动服|礼服|打底裤|牛仔裤|休闲裤|正装裤|运动裤|文胸|吊带|家居服|内裤|袜子|婷美|爱乐芬|法罗/伊丝艾拉|童装|搭配|裙子|打底衫";
break;
  case "男装":
return "T恤|衬衫|针织衫|卫衣|帽衫|西服|棉服|夹克|风衣|运动服|牛仔裤|休闲裤|运动裤|西裤|家居服|内裤|袜子|童装|搭配|外套";
break;
  case "配饰":
return "项链|手链|戒指|耳饰|发饰|手镯|项坠|男表|女表|太阳镜|美瞳|眼镜架|Zippo|烟具|帽子|腰带|徽章|丝巾|围巾|领带/领结|手套|袖扣|挂饰|水晶|发带|腕带|帽子|手机壳|发夹";
break;
  case "鞋品":
return "男运动鞋|女运动鞋|男休闲鞋|女休闲鞋|高跟鞋|鱼嘴鞋|平底鞋|靴子|凉鞋|家居鞋|商务皮鞋";
break;
  case "包包":
return "双肩包|斜挎包|手提包|手包|腰包|钱包|箱包";
break;
  case "化妆品":
return "护肤|彩妆|香氛|身体护理|美妆工具";
break;
  case "运动休闲":
return "阿迪达斯/三叶草折扣店|李宁|匡威|安踏|卡玛|班尼路/S&K/I.P.ZONE|迪士尼运动|圣迪奥|罗宾汉|cachecache|Baby Fox|米奇";
break;
  case "玩具礼品":
return "LOMO|毛绒玩具|汽车配饰|盆栽|玩偶|魔术类|游戏周边|手机装饰|电脑配件|大头贴|创意贴纸|小家电|数码产品|杯具|文具";
break;
  case "美食":
return "美食|水吧";
break;
  case "其他":
return "家居用品|美发|美甲|屈臣氏|维络城|婚纱/写真摄影|游戏厅|手机|打火机|烟斗|手表";
break;
}
}

//价格显示红色
$(function(){
  $("td[background=/images/menu_bg10.gif] a").each(function(){
    if($(this).html().indexOf("￥")==0)$(this).css("color","#FF0000").html("<font color=\"#FF0000\">"+$(this).html()+"</font>");
  });
})

//淘宝贝左右箭头图片切换
$(function(){
  $("img[src$=_left.gif]").css("cursor","pointer").click(function(){
    var myppid=this.id.substr(0,2);
    $("#"+myppid+$("#"+myppid+"_currPicNum").val()).hide();
    $("#"+myppid+"_currPicNum").val(Number($("#"+myppid+"_currPicNum").val())-1);
    if($("#"+myppid+"_currPicNum").val() == 0) $("#"+myppid+"_currPicNum").val($("#"+myppid+"_totlePicNum").val());
    $("#"+myppid+$("#"+myppid+"_currPicNum").val()).fadeTo("slow",1);
  });
  $("img[src$=_right.gif]").css("cursor","pointer").click(function(){
    var myppid=this.id.substr(0,2);
    $("#"+myppid+$("#"+myppid+"_currPicNum").val()).hide();
    $("#"+myppid+"_currPicNum").val(Number($("#"+myppid+"_currPicNum").val())+1);
    if($("#"+myppid+"_currPicNum").val() == Number($("#"+myppid+"_totlePicNum").val()) + 1) $("#"+myppid+"_currPicNum").val(1);
    $("#"+myppid+$("#"+myppid+"_currPicNum").val()).fadeTo("slow",1);
  });
})

$(function(){
  if(location.href.indexOf("/index.shtml")<0){//如果不是店铺首页
    if($(".middle_right_title01").size()==0){//如果不是单品页面
      var m =$(".middle_right > div:nth-child(4) > table > tbody > tr > td > table").size();
      if(m<4){
        $(".middle_right > div:nth-child(4)").css("text-align","left");
        $(".middle_right > div:nth-child(4) > table > tbody > tr").eq(1).hide();
      }
      if(m==0){
        $(".middle_right > div:nth-child(3)").hide();
        $(".middle_right > div:nth-child(4)").hide();
      }
      var n =$(".middle_right > div:nth-child(7) > table > tbody > tr > td > table").size();
      if(n<4){
        $(".middle_right > div:nth-child(7)").css("text-align","left");
        $(".middle_right > div:nth-child(7) > table > tbody > tr").eq(1).hide();
      }
      if(n==0){
        $(".middle_right > div:nth-child(6)").hide();
        $(".middle_right > div:nth-child(7)").hide();
      }
    }else{//如果是单品页面，则不需要
    }
  }else{//如果是店铺首页，美丽课堂的数量小于4则左对齐，隐藏“更多”，如果一个也没有，则全部隐藏

    var m =$(".middle_right_dzdpxd > .middle_right_chanpinlist > table > tbody > tr > td > table").size();
    if(m<4){
        $(".middle_right_dzdpxd > .middle_right_chanpinlist").css("text-align","left").css("padding-left","20px").width(763);
        $(".middle_right_dzdpxd > .middle_right_chanpinlist tr").filter(":last").hide();
      }
      if(m==0){
//        if($(".middle_right_spxd").html().indexOf("更多")>0){
        $(".middle_right_dzdpxd").hide();
//        }
      }


//////////////////
    mm =$(".middle_right_spxq > .middle_right_chanpinlist > table > tbody > tr > td > table").size();
    if(mm<4){
        $(".middle_right_spxd > .middle_right_chanpinlist").css("text-align","left").css("padding-left","20px").width(763);
        $(".middle_right_spxd > .middle_right_chanpinlist tr").filter(":last").hide();
      }
      if(mm==0){
//        if($(".middle_right_spxd").html().indexOf("更多")>0){
        $(".middle_right_spxd").hide();
//        }
      }

  }

  $(function(){
   var tdnum = $("div[class=middle_right_chanpinlist] > table > tbody > tr > td > table > tbody > tr > td").size();
//alert(tdnum);
   if(tdnum<8){
     var tdmore=$("div[class=middle_right_chanpinlist] > table > tbody > tr").eq(1);
     if(tdmore.size()>0){
     if(tdmore.html().indexOf("更多")>0){tdmore.hide();}
     }
   }
  })
  $(function(){
  var meiliketanglist = $("#meiliketanlist table td>img");
  if(meiliketanglist.size()<4){
    $("#meiliketanlist").css("text-align","left").css("padding-left","20px").width(763);
    $("#meiliketanlist>table tr").eq(1).hide();
  }
  })
  $(function(){
  var danpinlist = $("#danpinlist table td>img");
  if(danpinlist.size()<4){
    $("#danpinlist").css("text-align","left").css("padding-left","20px").width(763);
    $("#danpinlist>table tr").eq(1).hide();
  }
  })

})

