﻿document.write('<script src="http://resources.uni2uni.com/uni2uni2.5/lib/jquery.cookie.min.js" type="text/javascript"></script>');
document.write('<script src="http://resources.uni2uni.com/uni2uni2.5/lib/jquery.autocomplete.pack.js" type="text/javascript"></script>');
document.write('<link href="http://resources.uni2uni.com/uni2uni2.5/lib/jquery.autocomplete.css" rel="stylesheet" type="text/css" />');
var KeyHistory = false;
$(document).ready(function() {
    //载入用户信息
    LoadWelcome();

    $("#unisearch").click(function() {
        Search();
    })

    $("#alogin").click(function() {
        var outurl = window.location.href;
        location.href = "http://user.juumoo.com/login.aspx?url=" + outurl;
    });

    $("#txtsearch").keyup(function(event) {
        var e = event || window.event;
        if (e.keyCode == 38 || e.keyCode == 40) {
            KeyHistory = true;
        }
        if (e.keyCode == 13) {
            if (!KeyHistory) {
                $("#unisearch").click();
            } else {
                KeyHistory = false;
            }
        }
    }).focus();

    KeepKeyWord();

//    $("#txtsearch").autocomplete("http://digital.juumoo.com/ajax/autocomplete.ashx?callback=?", {
//        minChars: 0,
//        width: 390,
//        matchContains: "word",
//        autoFill: false,
//        selectFirst: false,
//        selectOnly: true,
//        scroll: true,
//        scrollHeight: 300,
//        max: 20
//    });
})

function Search() {
    //var citycookie = $.cookie('CookieCity');
    var txtcity = 0;
    var txtkeyword = $.trim($("#txtsearch").val().replace("<", "").replace(">", ""));
    var dirname = window.location.href.toLowerCase().match(new RegExp("http://([a-zA-Z0-9]*).juumoo.com/"))[1];
    //if (dirname == "www" || dirname == "user" || dirname == "order" || dirname == "shop") {
    //    dirname = "info";
    //}
    if (txtkeyword == "") {
        alert("请输入要搜索的关键字!");
    }
    else {
        $.cookie('KeyWord', encodeURIComponent(txtkeyword), { expires: 7, path: '/', domain: 'juumoo.com' });
        location.href = "http://info.juumoo.com/" + txtcity + "-20-1-1-0-10000-1000-10000.shtml?keyword=" + encodeURIComponent(txtkeyword);
    }
}

function GetCookies(name) {
    var arr = document.cookie.match(new RegExp(name + "=([^&;]+)"));
    if (arr != null) { return decodeURI(arr[1]); }
    return "";
}

function LoadWelcome() {
    var outurl = encodeURIComponent(window.location.href);
    var UserName = GetCookies('Email');
    if (UserName == '') {
        UserName = GetCookies("Mobile");
    }
    if (UserName != '') {
        var welcome = '<li>[<a href="http://user.juumoo.com/logout.aspx?url=' + outurl + '" target="_self">退出</a>]</li><li class="welcome"><span>' + UserName + '</span>，欢迎光临联嘉网</li>';
        $("#ulwelcome").html(welcome);
    }
}

function KeepKeyWord() {
    var keywordcookie = $.cookie('KeyWord');
    if (keywordcookie != undefined && keywordcookie != "" && keywordcookie != null) {
        $("#txtsearch").val(decodeURIComponent(keywordcookie));
    }
}