﻿/// <reference path="../K2046.js" />
/// <reference path="../Async.js" />
/// <reference path="../Func/Request.js" />
/// <reference path="../Func/AutoComplete.js" />
K2046.Header = {
    Attach: function() {
        var l = escape(document.referrer || document.URL);
        var url = "http://www19.53kf.com/company.php?arg=wd-qindeke&style=1&kflist=off&kf=&zdkf_type=1&language=cn&charset=gbk&lytype=0&referer=" + l + "&keyword=http%3A%2F%2Fwww.yomeier.com&tpl=pink";
        K2046.$("linkService").setAttribute("href", url);
        K2046.Users.GetLoginUser(function(result) {
            if (result.success) {
                K2046.SD("header_logined");
                K2046.$("header_username").innerHTML = result.NickName;
                K2046.SetHtml("header_sc_num", result.SCQuantity);
                K2046.Dom.Remove("header_un_login");
            }
            else if (result.SCQuantity) {
                K2046.SetHtml("header_sc_num", result.SCQuantity);
            }
        });
        K2046.Events.On("SearchType", "change", K2046.Header.LoadBrand);
        var elements = K2046.$Name("Keyword");
        if (elements && elements.length) {
            for (var x = 0; x < elements.length; x++) {
                if (elements[x].tagName && elements[x].tagName.toLowerCase() == "input") {
                    K2046.DefaultText.Attach(elements[x], { Default: "请输入您要搜索的关键字" });
                }
            }
        }
        K2046.Events.On("SearchForm", "submit", function() {
            var v = K2046.GetValue("Keyword");
            if (v == "请输入您要搜索的关键字") {
                K2046.SetValue("Keyword", "");
            }
        });
        K2046.Header.LoadBrand();

        var ac = K2046.AutoComplete.Attach({ el: "Keyword", url: "/Ajax/AjaxRequest.aspx?Action=LoadAutoComplete" });

        K2046.On(window, "load", function() {
            ac.WindowResize();
        });
    }

    , Beauty: {
        Brand: null
        , Price: ['50元及以下', '50元至100元', '100元至200元', '200元至300元', '300元至500元', '500元及以上']
    }

    , Mobile: {
        Brand: null
        , Price: ['500元及以下', '500元至1000元', '1000元至2000元', '2000元至3000元', '3000元至5000元', '5000元及以上']
    }

    , LoadBrand: function() {
        var t = parseInt(K2046.GetValue("SearchType"));
        K2046.$("SearchForm").action = "/" + (t ? "Beauty" : "Mobile") + "/Search.shtml";
        var brand = t ? K2046.Header.Beauty.Brand : K2046.Header.Mobile.Brand;
        if (brand) {
            K2046.Header.SetBrandOptions(brand);
            return;
        }
        K2046.Ajax.Post({
            url: "/Ajax/AjaxRequest.aspx?Action=" + (t ? "LoadBeautyBrand" : "LoadMobileBrand")
            , method: "post"
            , success: function(re) {
                if (t) {
                    K2046.Header.Beauty.Brand = re.Brand;
                } else {
                    K2046.Header.Mobile.Brand = re.Brand;
                }
                K2046.Header.SetBrandOptions(re.Brand);
            }
        });
        K2046.Header.SetPriceOptions(t ? K2046.Header.Beauty.Price : K2046.Header.Mobile.Price);
    }

    , SetBrandOptions: function(brand) {
        var s = K2046.$("BrandList");
        if (s && brand) {
            s.options.length = 1;
            for (var x = 0; x < brand.length; x++) {
                s.options[x + 1] = new Option(brand[x][1], brand[x][0]);
                if (brand[x][0] == K2046.Request.Query.Get("Brand")) {
                    s.options[x + 1].selected = "selected";
                }
            }
        }
    }

    , SetPriceOptions: function(price) {
        var s = K2046.$("PriceList");
        if (s && price) {
            s.options.length = 1;
            for (var x = 0; x < price.length; x++) {
                s.options[x + 1] = new Option(price[x], x + 1);
                if ((x + 1) == K2046.Request.Query.Get("Price")) {
                    s.options[x + 1].selected = "selected";
                }
            }
        }
    }
};

K2046.Header.Attach();