var nologin="<div>\u7528\u6237\u540D\uFF1A<input type=\"text\" id=\"uesrname\" />&nbsp;&nbsp;\u5BC6\u7801\uFF1A<input type=\"password\" id=\"password\" /></div><div><input type=\"submit\" class=\"login\" value=\"\" /><a href=\"/getpwd.html\">\u5FD8\u8BB0\u5BC6\u7801\uFF1F</a><a href=\"/reg.html\">\u514D\u8D39\u6CE8\u518C</a></div>";
var tcls="<a href=\"#\" id=\"lx\">&times;</a>";
var ctip="<span>\u4F01\u4E1A\u4F1A\u5458\u3001\u7AD9\u957F\u4F1A\u5458</span>\u7686\u53EF\u767B\u5F55";
$(document).ready(function(){
        $("#keyword").keypress(function(e){
               if(e.which=="13"){
                     if($('#keyword').val()==''){
                     alert("\u8BF7\u8F93\u5165\u8981\u641C\u7D22\u7684\u5185\u5BB9");
                     return false;
                     }else{$('#searchsub').click();}
                    }
        });
        $('#keyword').focus(function(){
            $(this).removeClass("slhlbg");
            $(this).val("");
        });
        $('#searchsub').click(function(){
                if($('#keywordb').val()==''){
                        alert("\u8BF7\u8F93\u5165\u8981\u641C\u7D22\u7684\u5185\u5BB9");
                        return false;
                } else{
                        setCookie("word","   "+$('#keyword').val());
                        $("#searchok").attr("action",'/search/'+$(".sel>span").attr("val")+'/?words='+urlencode($('#keyword').val()));
                        $("#searchok").submit();
                }
        })
        $(".sel>span").mouseover(function(){$(".select").show()});
        $(".select").mouseover(function(){$(".select").show()});
        $(".select").mouseout(function(){$(".select").hide()});
        $(".select >a").each(function(i){
            $(this).click(function(){
                $(".sel>span").html($(this).html());
                $(".sel>span").attr("val",$(this).attr("val"));
                $(".select>a").removeClass("hov");
                $(this).addClass("hov");
                $(".select").hide();
                setCookie("cate",$(this).attr("val"));
            });
        });
    $("body:not(.select)").click(function(){$(".select").hide()});
    $("#lx").live('click',function(){$(".tips").hide()});
    $("#password").live('keypress',function(e){if(e.which=="13") checkup()});
    $(".login").live('click',function(){checkup()});
    chklg();selected();
});
   var selected=function(){
        var valsel=getCookie("cate");
        var word=getCookie("word");
        if(valsel){
            $(".sel>span").html($(".select>a[val="+valsel+"]").html());
            $(".sel>span").attr("val",valsel);
            $(".select>a").removeClass("hov");
            $(".select>a[val="+valsel+"]").addClass("hov");
        }
        if(word) $("#keyword").val(unescape(word));
    }
    var checkup=function(){
            var lu=$("#uesrname").val();
            var lp=$("#password").val();
            if(!lu){$("#uesrname").focus();tipshow("\u8BF7\u8F93\u5165\u8D26\u53F7");return false;}
            if(!lp){$("#password").focus();tipshow("\u8BF7\u8F93\u5165\u5BC6\u7801\uFF01");return false;}
            $(".top_left").html("<img src=\"/images/index2/read.gif\" />");
            ajax(lu,lp);
    }
    var tipshow=function(t){
             t?t=t:t=ctip;
             t+=tcls;
             $(".tips").show();
             $(".tips").html(t);
    }
    var chklg=function(){
            var lu=getCookie("lu");var sid=getCookie("sid");var vip=getCookie("vip");
            if(lu){
                    sid?shopstr="<a target=\"_blank\" href=\"/shop/index/sid-"+sid+".shtml\">\u6211\u7684\u65FA\u94FA</a> |":shopstr="";
                    vip?comstr="<a target=\"_blank\" href=\"http://"+lu+".loooe.com\">\u6211\u7684\u4F01\u4E1A</a> |":comstr="";
                    var htm="<div>"+lu.toLowerCase()+" \u60A8\u597D\uFF0C\u6B22\u8FCE\u5149\u4E34<a href=\"/\">\u8DEF\u6613\u7F51</a>！ <a href=\"/user/\">\u6211\u7684\u8DEF\u6613</a> |"+shopstr+comstr+"<a href=\"/index/logout\" onclick=\"return cls();\">\u9000\u51FA</a></div>";
                    $(".top_left").html(htm);
                    $(".tips").hide();
            }else{
                    $(".top_left").html(nologin);tipshow(ctip);
            }
    }
    var ajax=function(lu,lp){
            $.ajax({
               type: "POST",
               url: "/index/loging/",
               data: "username="+lu.toLowerCase()+"&password="+lp,
               dataType: "json",
               success: function(val){
                            if(val[0]==0){
                                    $(".top_left").html(nologin);$("#uesrname").val(lu);$("#password").focus();tipshow('\u8D26\u53F7\u548C\u5BC6\u7801\u4E0D\u5339\u914D\uFF01');return false;
                            }else if(val[0]==-1){
                                    $(".top_left").html(nologin);$("#uesrname").focus();tipshow('\u7981\u6B62\u767B\u5F55\uFF01');return false;
                            }else if(val[0]==-2){
                                    $(".top_left").html(nologin);$("#uesrname").focus();tipshow('\u7528\u6237\u540D\u683C\u5F0F\u4E0D\u5BF9\uFF01');return false;
                            }else if(val[0]==-3){
                                    $(".top_left").html(nologin);$("#uesrname").val(lu);$("#password").focus();tipshow('\u5BC6\u7801\u683C\u5F0F\u4E0D\u5BF9\uFF01');return false;
                            }else if(val[0]==1){
                                    setCookie("lu",lu);setCookie("uid",val[1]);chklg();
                            }else{
                                    $(".top_left").html(nologin);tipshow('\u672A\u77E5\u9519\u8BEF\uFF01');return false;
                            }
               }
            });
    }
//加入收藏
var sURL='http://www.loooe.com';
var sTitle='\u8DEF\u6613\u7F51';
var pageURL='http://www.loooe.com';
var AddFavorite=function(sURL, sTitle) { //添加收藏
    try {
        window.external.addFavorite(sURL, sTitle);
    } catch (e) {
        try {
            window.sidebar.addPanel(sTitle, sURL, "");
        } catch (e) {
            alert("\u52A0\u5165\u6536\u85CF\u5931\u8D25\2C\u8BF7\u624B\u52A8\u6DFB\u52A0\uFF01");
        }
    }
}
var setHomepage=function(pageURL) { //设为首页
    if (document.all) {
        document.body.style.behavior='url(#default#homepage)';
        document.body.setHomePage(pageURL);
    }
    else if (window.sidebar) {
        if(window.netscape) {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e) {
                alert( "\u62B1\u6B49\uFF0C\u60A8\u6D4F\u89C8\u5668\u8BBE\u7F6E\u4E0D\u652F\u6301\u6B64\u529F\u80FD\uFF01" );
            }
        }
        var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
        prefs.setCharPref('browser.startup.homepage',pageURL);
    }
}
var sethome=function(){setHomepage(pageURL)}
var addfavr=function(){AddFavorite(sURL, sTitle)}
var addshopfavr=function(shopURL,shopTitle){shopURL=sURL+shopURL;	AddFavorite(shopURL,shopTitle)}
var urlencode=function(str){
    var hexStr = function(dec) { return '%' + dec.toString(16).toUpperCase(); };
    var ret = '',
            unreserved = /[\w.-]/; // A-Za-z0-9_.- // Tilde is not here for historical reasons; to preserve it, use rawurlencode instead
    str = (str+'').toString();

    for (var i = 0, dl = str.length; i < dl; i++) {
        var ch = str.charAt(i);
        if (unreserved.test(ch)) {
            ret += ch;
        }
        else {
            var code = str.charCodeAt(i);
            // Reserved assumed to be in UTF-8, as in PHP
            if (code === 32) {
                ret += '+'; // %20 in rawurlencode
            }
            else if (code < 128) { // 1 byte
                ret += hexStr(code);
            }
            else if (code >= 128 && code < 2048) { // 2 bytes
                ret += hexStr((code >> 6) | 0xC0);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 2048 && code < 65536) { // 3 bytes
                ret += hexStr((code >> 12) | 0xE0);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 65536) { // 4 bytes
                ret += hexStr((code >> 18) | 0xF0);
                ret += hexStr(((code >> 12) & 0x3F) | 0x80);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
        }
    }
    return ret;
}
