﻿$(document).ready(function(){
    $(".cctop2d").hover(function(){
        var offsettop=$(this).find("li:first").offset().top-1;
        var width=$(this).find("li:first").width();
        var height=$(this).find("li:first").height();
        var offsetleft;
//        if(offsettop>1000)
//        {
//        offsetleft=$(this).find("li:first").offset().left-1;
//        }
//        else
//        {
//            offsetleft=$(this).find("li:first").offset().left;
//        }
        offsetleft=$(this).find("li:first").offset().left+1;
       $(this).find("li:last").css({display:"block",position:"absolute",left:(offsetleft-1)+"px",top:(offsettop+height)+"px",background:"#FFF"});
       
    },function(){
       $(this).find("li:last").css("display","none");
    });
    var count=-1;
    $("#keywordTxt").keyup(function(event){//搜索框事件
    if(event.keyCode==13)//
    {
        if(jQuery.trim($(this).val()).length<=0)return;
        var classid=$(this).parent().parent().parent().prev().find("ul:first").find("li:first").data("id");
        go($(this).val(),classid);
    }
    if(jQuery.trim($(this).val()).length>0)
    {
            var li=$(this).parent().parent().find("li:last");
        if(event.keyCode!=40&&event.keyCode!=38)
        {
            var offsettop=$(this).offset().top;
            var width=$(this).width();
            var height=$(this).height();
            var offsetleft;
            var txt=$(this);
            if(offsettop>1000)
            {
            offsetleft=$(this).offset().left-1;
            offsettop=offsettop-1;
            }
            else
            {
                offsetleft=$(this).offset().left;
            }
            var a=new Ajax();
            a.PageUrl="/AjaxWeb/indexajax.aspx";
            a.Method="post";
            a.PostString="keyword="+$(this).val();
            a.FunLodings=function()
            {
                //$(this).append("<img src='/images/webicon/loading.gif'/>");
            }
            a.FunReturn=function(result)
            {
                if(result!=0)
                {
                $(li).html(result);
                $(li).css({display:"block",position:"absolute",left:(offsetleft-5)+"px",top:(offsettop+height+4)+"px",background:"#FFF",'z-index':"222"});
                //下拉选项变换样式
                $(".index-t5").hover(function(){$(this).css({background:"#F1F0F4",color:"#D00D16",cursor:"pointer"});},function(){
                   $(this).css({background:"#FFF",color:"#525252",cursor:"pointer"}); 
                });
                $(".index-t5").click(function(){//下拉选项点击事件
                    txt.val($(this).html());
                    $(li).html("");
                    count=-1;
                });
               // txt.blur(function(){$(this).parent().parent().find("li:last").html("");count=-1; });文本框失去焦点事件
                }
                else
                {
                    $(li).html("").css("display","none");
                    count=-1;
                }
            }
            a.OnReadyStateChange();
        }
        if(jQuery.trim($(li).html()).length>0&&(event.keyCode==40||event.keyCode==38))
        {
            if(event.keyCode==40)//下
            {
                count++;
                if(count==$(".index-t5").length){count=0;}
                $(".index-t5").css({background:"#FFF",color:"#525252",cursor:"pointer"});
                $(".index-t5").eq(count).css({background:"#F1F0F4",color:"#D00D16"});
            }
            else if(event.keyCode==38)//上
            {
                if(count==0||count==-1)
                {
                    count=$(".index-t5").length;
                }
                count--;
                 $(".index-t5").css({background:"#FFF",color:"#525252",cursor:"pointer"});
                $(".index-t5").eq(count).css({background:"#F1F0F4",color:"#D00D16"});
            }
            $(this).val($(".index-t5").eq(count).html())
        }
     }
     else
     {
        $(this).parent().parent().find("li:last").html("");count=-1; 
     }
    });
    var blurecount=0;
    $("#keywordTxt").click(function(){if(location.href.indexOf("search")>=0)return;
        if(blurecount==0)$("#keywordTxt").val("");
        blurecount++;
    })
    $(document).click(function(evt)
    {
        var tget = evt ? evt.target : event.srcElement
        var name = tget.name;
        if( name == ""||typeof(name)=="undefined" )
        {
            name = tget.tagName;//alert(name);
        }
        if( name !="showLi"&&name!="keywordTxt")
        {
            $("#keywordTxt").parent().parent().find("li:last").html("");count=-1; 
        }
    });
    $(".index-t2").hover(function(){
        $(this).css({background:"#F1F0F4",color:"#D00D16",cursor:"pointer"});
    },function(){
    $(this).css({background:"#FFF",color:"#525252"});
        
    })
    $("img[name='searchBut']").click(function(){
        var div=$(this).parent().parent().parent();
        var classid=div.find("ul:first").find("li:first").data("id");
        go(jQuery.trim(div.find("input").val()),classid);
    });
    
    $(".cctop9").hover(function(){//头部选项卡鼠标悬停事件
       if(!($(this).find(".topclass").is(":visible")))
       {
        var offsetleft=$(this).offset().left;
        var offsettop=$(this).offset().top;
        var height=$(this).height();
       $(this).removeClass().addClass("cctop10");
        $(this).find(".topclass").css({left:offsetleft+"px",top:height+offsettop-1+"px"});
        $(this).find(".topclass").slideDown("fast");
        }
    },function(){
    $(this).removeClass().addClass("cctop9");
        $(this).find(".topclass").slideUp(10);
    });
    $(".topicon3").hover(function(){//我的动力鼠标事件
    if(!($(this).find(".ccti1").is(":visible"))){
        $(this).find(".ccti1").css({position:"absolute",left:$(this).offset().left+"px",top:($(this).offset().top+$(this).height())+"px",background:"#fff"});
        $(this).find(".ccti1").slideDown("fast");
        $(this).removeClass().addClass("topmy1");
        }
    },function(){
         $(this).find(".ccti1").slideUp("fast");
        $(this).removeClass().addClass("topicon3");
    });
     $(".cctop8c").hover(function(){//购物车鼠标事件
        if(!($(this).find(".ti3").is(":visible"))){ 
        $(this).find(".ti3").css({position:"absolute",left:($(this).offset().left-(window.screen.width==1024?249:247)+$(this).width())+"px",top:($(this).offset().top+$(this).height()+1)+"px",background:"#fff"});
        $(this).find(".ti3").slideDown("fast");
        $(this).removeClass().addClass("cctop8cc");
        topcart();
        }
    },function(){
         $(this).find(".ti3").slideUp("fast");
        $(this).removeClass().addClass("cctop8c");
    });
    cartproductcount();
    $("#sp_exit").click(function(){
    exituser();
});
});
function clickClass(obj,id)
{
    $($(obj).parent().parent().parent().parent().find("li:first")).html(obj.innerHTML);
    $($(obj).parent().parent().parent().parent().find("li:first")).data("id",id);
}

function close(obj)
{
    $($(".index-tt1").parent()).html("");
}
function go(word,classid)
{   if(word==""||word.length<=0){alert("请填写需要搜索的关键字!");return;}
    if(classid!=null&&classid!=0)
    {
        location.href="/product/search.aspx?classid="+classid+"&key="+urlencode(word)+"&page=1";
    }
    else
    {
        location.href="/product/search.aspx?key="+urlencode(word)+"&page=1";
    }
}

function urlencode(text)   
{   
    text = escape(text.toString()).replace(/\+/g, "%2B");   
    var matches = text.match(/(%([0-9A-F]{2}))/gi);   
    if (matches)   
    {   
        for (var matchid = 0; matchid < matches.length; matchid++)   
        {   
            var code = matches[matchid].substring(1,3);   
            if (parseInt(code, 16) >= 128)   
            {   
                text = text.replace(matches[matchid], '%u00' + code);   
            }   
        }   
    }   
    text = text.replace('%25', '%u0025');   
    return text;   
}
//购物车收起
function topcartshow(type,id)
{
    if(type==0){//普通商品
        if($("#topprodiv_"+id).is(":visible")){
            $("#topprodiv_"+id).hide("slow");
            $("#topproimg_"+id).attr("src","/images/webicon/shop9.gif");
        }else{
            $("#topprodiv_"+id).show("slow");
            $("#topproimg_"+id).attr("src","/images/webicon/shop8.gif");
        }
    }else{//套餐商品
        if($("#toptcdiv_"+id).is(":visible")){
            $("#toptcdiv_"+id).hide("slow");
            $("#toptcimg_"+id).attr("src","/images/webicon/shop9.gif");
        }else{
            $("#toptcdiv_"+id).show("slow");
            $("#toptcimg_"+id).attr("src","/images/webicon/shop8.gif");
        }
    }
}
//显示头部购物车
function topcart()
{
    var a=new Ajax();
    var a=new Ajax();
    a.PageUrl="/AjaxWeb/ShowCart.aspx";
    a.Method="post";
    a.PostString="show=1";
    a.FunLodings=function()
    {
        
    }
    a.FunReturn=function(info)
    {
        $("#div_topcart").html(info);
        $("#sp_cartprocount").html($("#hid_buycount").val());
    }
    a.OnReadyStateChange();
}
//购物车中商品个数
function cartproductcount(){
    var a=new Ajax();
    a.PageUrl="/AjaxWeb/ShowCart.aspx";
    a.Method="post";
    a.PostString="show=0";
    a.FunLodings=function()
    {
        $("#sp_cartprocount").html("-");
    }
    a.FunReturn=function(count)
    {
        $("#sp_cartprocount").html(count);
    }
    a.OnReadyStateChange();
}



function exituser(){//安全退出
    var a=new Ajax();
    a.PageUrl="/AjaxWeb/UserLogin.aspx";
    a.Method="post";
    a.PostString="type=3";
    a.FunLodings=function()
    { 
        
    }
    a.FunReturn=function(result)
    {
        if(result=="1")
        {
            alert("退出成功!");
            location.href=location.href;
        }
        else
        {
            alert("退出失败");
        }
    }
    a.FunLodingsClose=function()
    {

    }
    a.OnReadyStateChange();
}