﻿/// <reference path="../Jquery1.3/jquery-1.3.2-vsdoc.js" />

//公告部分。
function GetSysAffice() {
    $.ajax({
        url: "/PageHandler.ashx",
        data: "className=ArticleInfo&methodName=GetNewsCommendList",
        dataType: 'json',
        cache: false,
        async: false,
        success: function(data) {
            var text = "";
            var domain = $("#domain1").val();
            if (domain != "" && domain != null && domain != "undefined") {
                domain = "http://" + domain;
            }
            if (data[0] != null) {
                $(data[0]).each(function(i, n) {
                    text += "<a href=\"" + domain + "/NewDetail/?nid=" + n.NewsId + "\" style=\"color:" + n.TitleSytle + "\">" + n.Title + "</a><br />";
                });
                text = text.substring(0, text.length - 6);
            }
            $("#AScrollorContainer").html("<div>" + text + "</div>");
        },
        error: function(data) {
            //alert(data.responseText);
        }
    });
    var AfficeCount = 6;
    if (AfficeCount > 1) {
        var as1 = new Masque.L.Scrollor($("#AScrollorContainer"), { Roll: true, AutoStop: true, StepCount: 25, Interval: 5000 });
        as1.Move();
    }
}

///热门搜索30/11/2009 DHL
WebUitility = {};
WebUitility.Cache = {};
WebUitility.HotKeyWord = {};
WebUitility.HotKeyWord.Config = { _Type: "shop" }
WebUitility.Cache.HotKeyWord = { _Food: null, _Shop: null, _FoodHtml: null, _ShopHtml: null };

WebUitility.HotKeyWord.GetHotKeyWords = function(type) {
    WebUitility.HotKeyWord.Config._Type = type;
    if (type == "food") {
        if (WebUitility.Cache.HotKeyWord._Food != null && WebUitility.Cache.HotKeyWord._FoodHtml != "") {
            //WebUitility.HotKeyWord.OnGetHotKeyWordsSuccess(WebUitility.Cache.HotKeyWord._Food);
            $("#hotSearch").html('热门搜索：' + WebUitility.Cache.HotKeyWord._FoodHtml);
            //$("a[name^='a_hotsearch_']").click(HotKeyWork_click);
            return;
        }
        type = 2;
    }
    else {
        if (WebUitility.Cache.HotKeyWord._Shop != null && WebUitility.Cache.HotKeyWord._ShopHtml != "") {
            //WebUitility.HotKeyWord.OnGetHotKeyWordsSuccess(WebUitility.Cache.HotKeyWord._Shop);
            $("#hotSearch").html('热门搜索：' + WebUitility.Cache.HotKeyWord._ShopHtml);
            //$("a[name^='a_hotsearch_']").click(HotKeyWork_click);
            return;
        }
        type = 1;
    }

    $.ajax({
        url: "/PageHandler.ashx?className=HotSearch&methodName=GetHotKeyWords&parameter=10," + type,
        dataType: 'json',
        cache: true,
        success: WebUitility.HotKeyWord.OnGetHotKeyWordsSuccess,
        error: function(data) {
        }
    });
}

WebUitility.HotKeyWord.OnGetHotKeyWordsSuccess = function(data) {
    var text = "";
    var domain = $("#domain").val();
    if (WebUitility.HotKeyWord.Config._Type == "food")//菜品
    {
        $(data).each(function(i, n) {
            text = text + "<a name='a_hotsearch_shop' class=\"t_s" + Math.floor(Math.random() * 7) + "\" href='http://" + domain + "/FoodList/?&t=1&fk=" + encodeURIComponent(n.KeyWord) + "'>" + n.KeyWord + "</a>";
        });
        WebUitility.Cache.HotKeyWord._Food = data;
        WebUitility.Cache.HotKeyWord._FoodHtml = text;
    }
    else {
        $(data).each(function(i, n) {
            text = text + "<a name='a_hotsearch_shop' class=\"t_s" + Math.floor(Math.random() * 7) + "\" href='http://" + domain + "/ShopList/?&t=1&sk=" + encodeURIComponent(n.KeyWord) + "'>" + n.KeyWord + "</a>";
        });
        WebUitility.Cache.HotKeyWord._Shop = data;
        WebUitility.Cache.HotKeyWord._ShopHtml = text;
    }
    $("#hotSearch").html('热门搜索：' + text);
    //$("a[name^='a_hotsearch_']").click(HotKeyWork_click);
}

//搜索控制
new Masque.L.Tabs("#searchTabs", { SelectedCSS: "current", Selected: function(tab, index) { $("#hSearchType").val(index); } });

function PageNavigation(data) {
    this._pageSize = data.pageSize || 10; ///每页显示项数
    this._itemsCount = data.totalRecord || 0; //总记录数
    this._currentIndex = data.currentIndex || 1; ///当前页索引///从1开始
    this._pageCount = data.pageCount || (this._itemsCount % this._pageSize == 0 ? Math.floor(this._itemsCount / this._pageSize) : Math.floor(this._itemsCount / this._pageSize) + 1); ///总页数
    this._currentIndexCss = data.currentIndexCss || "default"; //当前页样式
    this._showPageCount = data.showPageCount || 5;
}

PageNavigation.prototype = {
    getDescription: function() {
        return "<span>共有" + this._itemsCount + "条记录</span><span>每页" + this._pageSize + "条/共" + this._pageCount + "页</span><span>当前第" + this._currentIndex + "页</span>";
    },
    getPageNagition: function() {
        var length = this._pageCount || 0;
        if (length == 0 || this._currentIndex == 0)
            return "";
        var a_text = "";
        var pageMax = 0;
        var pageMin = 0;

        if (this._currentIndex > 0 && this._currentIndex <= 5) {
            pageMax = 5;
            pageMin = 1;
        }
        else if (this._currentIndex > 5 && this._currentIndex < length - 2) {
            pageMax = 1 * (this._currentIndex) + 2;
            pageMin = 1 * (this._currentIndex) - 2;
        }
        else {
            pageMax = length;
            pageMin = length - 5;
        }
        if (length < 5 && this._currentIndex < 5) {
            pageMax = length;
            pageMin = 1;
        }
        if (length > 2 && this._currentIndex != 1)
            a_text = a_text + "<span><a href=\"javascript:void(0);\" name='1'>首页</a></span>";
        if (length >= 2 && this._currentIndex > 1) {
            a_text = a_text + "<span><a href=\"javascript:void(0);\" name=\"" + (this._currentIndex * 1 - 1) + "\">上一页</a></span>";
        }

        for (var i = pageMin; i <= pageMax; i++) {
            if (this._currentIndex == i)
                a_text = a_text + "<span><a class=\"" + this._currentIndexCss + "\" name=\"" + i + "\" href=\"javascript:void(0);\"><span>" + i + "</span></a></span>";
            else
                a_text = a_text + "<span><a href=\"javascript:void(0);\" name=\"" + i + "\">" + i + "</a></span>";
        }

        if (pageMax < length && length != 1 && length >= 2) {
            a_text = a_text + "<span><a href=\"javascript:void(0);\" name=\'" + (this._currentIndex * 1 + 1) + "\'>下一页</a></span>";
            a_text = a_text + "<span><a href=\"javascript:void(0);\" name=\'" + length + "\'>尾页</a></span>";
        }
        return a_text;
    }
};

//城市列表

function catCityClick() {
    var cl = $("#openCityLayer");
    if (cl.css("display") == "none") {
        cl.show();
        $(this).children("span").text("↑");
    }
    else {
        cl.hide();
        $(this).children("span").text("↓");
    }
}

function GetOpenCityList() {
    //    $.ajax({
    //        url: "/PageHandler.ashx",
    //        data: "className=Area&methodName=GetOpenCityList",
    //        dataType: 'json',
    //        cache: false,
    //        async: false,
    //        success: function(data) {
    //            var text = "";
    //            var hotText = "";
    //            if (data != null) {
    //                if (data.length == 1) {
    //                    $("#spanChange").attr("style", "display:none");
    //                    return;
    //                }
    //                var currUrl = $("#openCityLayer").attr("rurl");
    //                $(data).each(function(i, n) {
    //                    if (n.IsHotCity) {
    //                        hotText += "<dd><a title=\"" + n.AreaName + "\" href=\"http://" + n.AppDomain + "/" + currUrl + "\">" + n.AreaName + "</a></dd>";
    //                    }
    //                    text += "<dd><a title=\"" + n.AreaName + "\" href=\"http://" + n.AppDomain + "/" + currUrl + "\">" + n.AreaName + "</a></dd>";
    //                });
    //            }
    //            if (hotText != "") {
    //                $("#hotcitydiv").attr("style", "display:inline;");
    //                $("#openHotCityList").html(hotText);
    //            }
    //            else {
    //                $("#hotcitydiv").attr("style", "display:none;");
    //            }

    //            $("#openCityList").html(text);
    //        },
    //        error: function(data) {
    //            //alert(data.responseText);
    //        }
    //    });
}

$().ready(function() {
    GetSysAffice();
    $("#catCity").click(catCityClick);

    $.ajax({
        url: "/PageHandler.ashx",
        data: "className=Utility&methodName=GetClassName&parameter=",
        dataType: 'json',
        success: function(data) {
            var isclose = $.cookie("isclose");
            if (isclose != "true") {
                $("body").addClass(data);
            }
            if (data == "newyear") {
                $("body").addClass("newyear_NO");
            }
        }
    });
    //关闭背景皮肤
    closeBG();

    $("#searchTabs").find("a").click(function() {
        var type = $(this).attr("name");
        $("#In_SearchKey").attr("name", type).val("");
        $("#In_SearchKey")[0].focus();
        $("#a_search").attr("name", type);
        $("#searchTabs").find("li").removeClass();
        $(this).parent().addClass("current");
        $("#searchTip").hide();

        WebUitility.HotKeyWord.GetHotKeyWords(type);
    });

    //智能输入    
    $("#In_SearchKey").IntelliSence("#head_search_close", "#a_search", "#searchTip");

    //搜索按钮
    $("#a_search").click(function() {

        var keyWord = $("#In_SearchKey").val();
        if (keyWord == "" || keyWord == null) {
            alert("搜索内容不能为空");
            return false;
        }

        var type = $("#In_SearchKey").attr("name");

        SetKeyWordSession(type, keyWord.replace('<', '').replace('>', ''));
        return false;
    });

    ///热门搜索关键字
    //  WebUitility.HotKeyWord.GetHotKeyWords(1);

    $("a[name^='a_hotsearch_']").click(HotKeyWork_click);

    //填入收藏夹
    function JSAddFavorite() {
        if (window.sidebar && "object" == typeof (window.sidebar) && "function" == typeof (window.sidebar.addPanel)) {
            //  firefox
            window.sidebar.addPanel(document.title, document.location.href, '');
        }
        else if (document.all && "object" == typeof (window.external)) {
            //  ie
            window.external.addFavorite(document.location.href, document.title);
        }
    }

    $("#a_addToFavor").click(function() {
        JSAddFavorite();
    });
    $("#a_addToFavors").click(function() {
        JSAddFavorite();
    });

    ///登录层初始化
    //    LoginLayer.Init();
    //    $("#btn_logins").click(function() {
    //        checkFrom();
    //    });
});

function SetCopyRightInfo() {
    event.returnValue = false;
    var t = document.selection.createRange().text;
    var s = "本文来源于";
    var index1 = Math.floor(Math.random() * (t.length));
    var index2 = Math.floor(Math.random() * (t.length));
    var min = Math.min(index1, index2);
    var max = Math.max(index1, index2);
    var begin = t.substr(0, min);
    var middle = t.substr(min, max - min);
    var end = t.substr(max);
    var str1 = "<a href=\" http://www.aiwaimai.com \"><img alt=\"爱外卖网\" src=\"http://qd.aiwaimai.com/images/img/logo.gif\" complete=\"complete\"/></a>";
    var str2 = "<a href=\" http://www.aiwaimai.com \">订餐就上爱外卖网！</a>";
    var newstr = begin + str1 + middle + str2 + end;

    clipboardData.setData('Text', newstr);
}

function HotKeyWork_click() {
    var keyWord = $(this).html();
    var type = $(this).attr("name");
    if (type == "a_hotsearch_food") {
        SetKeyWordSession("food", keyWord);
    }
    else {
        SetKeyWordSession("shop", keyWord);
    }
}

function SetKeyWordSession(type, keyWord) {
    var gtype = GetUrlParam("gtype");
    var price = GetUrlParam("price");
    var shopKey = GetUrlParam("sk");
    var areaName = GetUrlParam("area");
    var bulidKey = GetUrlParam("bk");
    var domain = $("#domain").val();

    $.ajax({
        url: "/PageHandler.ashx",
        data: "className=Utility&methodName=HotSearchHistory&parameter=" + encodeURI(keyWord) + "," + type,
        dataType: 'json'
    });

    var url = "";
    switch (type) {
        case "food":
            var bid = GetUrlParam("bid");
            url = "http://" + domain + "/FoodList/?t=1&fk=" + encodeURI(keyWord) + "";
            if (bid != "") {
                url += "&bid=" + bid + "";
            }
            break;
        case "shop":
            url = "http://" + domain + "/ShopList/?&t=1&sk=" + encodeURI(keyWord) + "&bk=" + bulidKey + "";
            break;
        case "buiding":
            url = "http://" + domain + "/ShopList/?&t=1&bk=" + encodeURI(keyWord) + "&sk=" + shopKey + "";
            break;
        default:
    }
    if (areaName != "") {
        url += "&area=" + encodeURI(areaName) + "";
    }
    if (gtype != "") {
        url += "&gtype=" + gtype + "";
    }
    if (price != "") {
        url += "&price=" + price + "";
    }
    window.location.href = url;
}

function closeBG() {
    $("#closenybg").click(function() {
        $("body").removeClass("newyear").addClass("newyear_NO");
        $.cookie("isclose", true);
    });
}

//function checkFrom() {
//    if (ValidateInput($('#txt_userName'), '请输入帐号') & ValidateInput($('#txt_pwd'), '11111') & ValidateInput($('#txt_checkcode'), '右侧=>')) {
//        UserLogin();
//    }
//    else {
//        $("#img_CheckCode").attr("src", "CheckCode.aspx?r=" + Math.random());
//    }
//}

//function UserLogin() {
//    var userType = 0;    
//    var url = '/Support/UserLogin.aspx';
//    var pars = 'UserName=' + encodeURI($("#txt_userName").val()) + '&Password=' + $("#txt_pwd").val() + '&CheckCode=' + $("#txt_checkcode").val() + '&UserType=' + userType;
//    $.ajax({
//        url: url,
//        data: pars,
//        dataType: 'text',
//        cache: false,
//        complete: showResponseLogin
//    });
//}
//var loginEval = null;

//function showResponseLogin(originalRequest) {
//    var result = originalRequest.responseText;
//    if (result == '0') {
//        ShowMsg('div_LoginMsg', "验证码错误", 'btn_logins');
//        ChangeValidateCode();
//    }
//    else if (result == "1") {
//        isLoginSuccess = true;
//        if (loginEval) {
//            $("#div_LoginLayer").dialog("close");
//            eval(loginEval);
//            loginEval = null;
//        }
//        var _href = window.location.href;
//        if (_href.indexOf('?') == -1)
//            window.location.href = _href + "?r=" + Math.floor(Math.random() * 1000);
//        else if (_href.indexOf('r') == -1)
//            window.location.href = _href + "&r=" + Math.floor(Math.random() * 1000);
//        else {
//            _href.replace(/r=\d+/g, Math.floor(Math.random() * 1000))
//            window.location.href = _href;
//        }

//        if (hyperlinkChat != null) {
//            chat.SW.Check(hyperlinkChat, 500, 348)
//            hyperlinkChat = null;
//        }
//    }
//    else if (result == "2") {
//        isLoginSuccess = false;
//        ShowMsg('div_LoginMsg', "用户名或密码错误，请重试。", 'btn_logins');
//        ChangeValidateCode();
//    }
//    else if (result == "4") {
//        isLoginSuccess = false;
//        ShowMsg('div_LoginMsg', "用户名不存在。", 'btn_logins');
//        ChangeValidateCode();
//    }
//    else {

//        isLoginSuccess = false;
//        ShowMsg('div_LoginMsg', "未知错误" + result, 'btn_logins');
//        ChangeValidateCode();
//    }
//}

/////登录层设置
//var LoginLayer = {}
//LoginLayer.T = {};

//LoginLayer.Init = function() {
//    LoginLayer.T = new j.overLayer({ color: "#000" });
//    $("#txt_checkcode").keydown(function(event) {
//        if (event.which || event.keyCode) {
//            if ((event.which == 13) || (event.keyCode == 13)) {
//                $("#btn_logins").click();
//                return false;
//            }
//        }
//        else {
//            return true
//        }
//    });
//    $("#div_LoginLayer").dialog({ autoOpen: false,
//        width: 406,
//        height: 246,
//        title: "登录",
//        resizable: false,
//        modal: true,
//        draggable: false,
//        close: function() {
//            ///////////////////////////取消登录或关闭层清空缓存///////////////////////////////////////
//            $.ajax({
//                url: "/PageHandler.ashx",
//                data: "className=Utility&methodName=ClearCaChe",
//                dataType: 'json',
//                cache: false,
//                success: function(data) {
//                },
//                error: function(data) {
//                    //alert(data);
//                }
//            });
//            LoginLayer.T.hide();
//        },
//        open: function() {
//            $.ajax({
//                url: "/CheckCode.aspx?id=" + Math.random(),
//                dataType: 'text',
//                cache: false,
//                success: function(data) {
//                    $("#img_CheckCode").attr("src", "/CheckCode.aspx?id=" + Math.random());
//                },
//                error: function(data) {
//                    $("#img_CheckCode").attr("src", "/CheckCode.aspx?id=" + Math.random());
//                }
//            });
//        }
//    });
//    
//    //初始化文本框的值
//    $("#txt_userName").val("");
//    $("#txt_pwd").val("");
//    $("#txt_checkcode").val("");
//    
//    //
//    $("#a_login").click(function() {
//        //显示
//        LoginLayer.Show();
//    });
//    ///取消
//    $("#btn_LogonCancel").click(function() {
//        $("#div_LoginLayer").dialog("close");
//    });
//    //验证码
//    $("#a_change_checkCode").click(function() { $("#img_CheckCode").attr("src", "../CheckCode.aspx?id=" + Math.random()) });
//}
//function ChangeValidateCode() {
//    $("#img_CheckCode").attr("src", "../CheckCode.aspx?id=" + Math.random());
//}

//LoginLayer.Show = function() {
//    LoginLayer.T.show();
//    //alert("a");
//    $("#div_LoginLayer").dialog("open");
//    //$("#a_change_checkCode").click();
//}

//var isLoginSuccess = false;
//var hyperlinkChat = null;
//function CheckIsLoginSuccess(aaaa) {
//    hyperlinkChat = aaaa;
//    if (isLoginSuccess && chat.SW.Check(aaaa, 500, 348)) {
//        return true;
//    }
//    else {
//        return false;
//    }
//}

//function CheckLogin() {
//    $.ajax({
//        url: "/PageHandler.ashx",
//        data: "className=UserBase&methodName=CheckLogin",
//        dataType: 'json',
//        cache: false,
//        async: false,
//        success: function(data) {
//            if (data == "False" || data == false) {
//                isLoginSuccess = false;
//                LoginLayer.Show();
//            }
//            else {
//                isLoginSuccess = true;
//            }
//        },
//        error: function(data) {
//            isLoginSuccess = false;
//        }
//    });
//}
