﻿/// <reference path="../jquery-1.3.2-vsdoc.js" />
/************************************************************************/
/*                  首页，管理中心 顶部智能搜                      */
/************************************************************************/
(function($) {
    var orgText = "";
    var timeout = null;
    var _SELF = null;
    $.fn.IntelliSence = function(closeHtml, btn_searchHtml, searchTipHtml) {
        if ($(this).is("input:text")) {
            _SELF = this;
            $(closeHtml).click(function() { $(this).parent().hide(); });
            $(this).keyup(function(event) {
                var keyCode = parseInt(event.keyCode);  //alert(keyCode);

                if (keyCode === 40 || keyCode === 38 || keyCode === 13) {
                    if (itemsSelect(keyCode)) {
                        return;
                    }
                    if (keyCode === 13) {

                        if ($(this).val() != orgText) {
                            $(searchTipHtml).hide()
                            $(btn_searchHtml).click();
                        }
                        return;
                    }
                } //↑ 下 回车

                if (keyCode === 19 || keyCode === 27 || keyCode === 45 || keyCode === 144 || keyCode === 145)
                    return;
                if (keyCode >= 33 && keyCode <= 40)
                    return;
                if (keyCode >= 112 && keyCode <= 123)
                    return;
                var value = $(this).val();
                if (value === "" || value === null || value == orgText) {
                    return;
                }
                if (value.indexOf(",") != -1) {
                    return;
                }
                if (value.indexOf("'") != -1) {
                    return;
                }
                orgText = value;

                var type = $(this).attr("name");
                //$("#msg").html(value+type+"<img src=\"../Images/loading_s.gif\" />");
                var shopId = $(this).attr("sid");
                //alert(type);
                if (shopId > 0) {
                    getInfo(value, type, shopId);
                }
                else {
                    getInfo(value, type, 0);
                }
                orgText = "";
            });
        }
        function getInfo(keyWord, type, shopId) {

            var dataUrl = "className=BildingInfo&methodName=GetBuildingsByIntelliSence&parameter=" + encodeURI(keyWord); // + ",1,10,0,''",

            switch (type) {
                case "buiding":
                    dataUrl = "className=BildingInfo&methodName=GetBuildingsByIntelliSence&parameter=" + encodeURI(keyWord); // + ",1,10,0,''",
                    break;
                case "food":
                    dataUrl = "className=CompanyUser&methodName=SearchFoodIntelliSence&parameter=" + encodeURI(keyWord) + ",8";
                    break;
                case "shop":
                    dataUrl = "className=CompanyUser&methodName=SearchEnterpriseIntelliSence&parameter=" + encodeURI(keyWord) + ",8";
                    break;
                case "groupBuilding":
                    dataUrl = "className=BildingInfo&methodName=GetGroupBuildingsByIntelliSence&parameter=" + encodeURI(keyWord) + ",8";
                    break;
                case "groupShop":
                    dataUrl = "className=CompanyUser&methodName=SearchGroupEnterpriseIntelliSence&parameter=" + encodeURI(keyWord) + ",8";
                    break;
                case "shopSendBuiding":
                    dataUrl = "className=BildingInfo&methodName=GetEnterpriseSendArea&parameter=" + encodeURI(keyWord) + "," + shopId + "";
                    break;
                default:
                    return;
            }

            $(closeHtml).nextAll().remove(); //.after("<img src=\"../Images/loading_s.gif\" />");
            $.ajax({
                url: "/PageHandler.ashx",
                data: dataUrl,
                dataType: 'json',
                cache: false,
                success: function(data) {

                    $(searchTipHtml).show();
                    $(searchTipHtml).find("a,img").remove();
                    var text = "";
                    if (type == "buiding" || type == "groupBuilding" || type == "shopSendBuiding") {
                        if (data[0].length == 0) {
                            return
                        }
                        for (var i = 0; i < data[0].length; i++) {
                            name = data[0][i].BuildingName;
                            id = data[0][i].StoriedBuildingId;
                            text = text + "<a href=\"###\">" + name + "</a>";
                        }
                    }
                    else {
                        if (data.length == 0) {
                            return;
                        }
                        for (var i = 0; i < data.length; i++) {
                            text = text + "<a href=\"###\">" + data[i] + "</a>";
                        }
                    }

                    $(closeHtml).after(text);
                    $(searchTipHtml + " > a").click(function() {
                        $(_SELF).val($(this).html());
                        var keyWord = $(this).html();
                        $(this).parent().hide();
                        orgText = "";
                        $(btn_searchHtml).click();
                    });
                },
                error: function(data) {

                    // alert("智能搜 输入参数错误server error");
                }
            });
        }
        function itemsSelect(keyCode) {

            $(this).removeClass();
            var o = $(searchTipHtml);

            if (!$(o).is(":visible"))
                return false;
            if ($(o).find("a").length == 0)
                return false;

            var curObj = $(o).find("a[name='selected']");
            if ($(curObj).length > 0) {
                //enter

                if (keyCode === 13) {
                    $(curObj).click();
                    $(o).hide();
                    return true;
                }
                //up
                if (keyCode === 40) {
                    if ($(curObj).next().is("a"))
                        curObj = $(curObj).next();
                    else
                        curObj = $(o).find("a:first");
                }
                //down
                if (keyCode === 38) {
                    if ($(curObj).prev().is("a"))
                        curObj = $(curObj).prev();
                    else
                        curObj = $(o).find("a:last");
                }
            }
            else {
                curObj = $(o).find("a:first");
            }
            $(o).find("a").removeAttr("name").removeAttr("style");
            $(curObj).attr("name", "selected").attr("style", "background-color:#316AC5;color:#FFFFFF;text-decoration:none;");

            return false;
        }
    };
})(jQuery);
/************************************************************************/
/*                                                                      */
/************************************************************************/
jQuery.extend({
    BottomToolbarAction: function(toolbarId) {
        changePersentHeight(40);
        //        var changerSize = 50;
        //        var maxWidth = $("#" + toolbarId).width();
        //        changePosition(0, maxWidth, changerSize);

        //计算降落到底部需要移动的次数
        function calculateCount(item, bodyTop, maxWidth, changeHeight) {
            var bodyHeight = $(window).height();
            var lastHeight = parseInt(bodyHeight + bodyTop - item.position().top - item.height());

            var count = lastHeight / changeHeight;
            if (lastHeight % changeHeight != 0) {
                count += 1;
            }
            return parseInt(count);
        }
        function changePersentHeight(persont) {
            var item = $("#" + toolbarId);
            var poptimer;
            if (item[0]) {
                if (parseInt(persont) < 0) {
                    item.css("bottom", "0");
                    clearTimeout(poptimer);
                }
                else {
                    item.css("bottom", persont + "%");
                    poptimer = setTimeout(function() {
                        changePersentHeight(parseInt(persont) - 2);
                    }, 60);
                }
            }
        }
        //改变位置
        function changePosition(currentWidth, maxWidth, changeHeight) {
            var item = $("#" + toolbarId);
            var poptimer;
            if (item[0]) {
                var height = item.height() + 5;
                //滚动条的高度
                var bodyTop = 0;
                if (typeof window.pageYOffset != 'undefined') {
                    bodyTop = window.pageYOffset;
                }
                else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
                    bodyTop = document.documentElement.scrollTop;
                }
                else if (typeof document.body != 'undefined') {
                    bodyTop = document.body.scrollTop;
                }
                //窗体高度
                var bodyHeight = $(window).height();
                //窗体宽度
                var bodyWidth = $(window).width();
                //降落需要的次数
                var count = calculateCount(item, bodyTop, maxWidth, changeHeight);
                //每次降落需要变更的宽度
                var changeWidth;
                if (count == 0) {
                    changeWidth = maxWidth - currentWidth;
                }
                else {
                    changeWidth = (maxWidth - currentWidth) / count;
                }

                //left坐标
                var itemLeft = parseInt((bodyWidth - currentWidth) / 2);
                //top坐标
                var itemTop = parseInt(item.position().top + changerSize);
                //css样式
                var nextWidth = parseInt(currentWidth + changeWidth)
                var cssclass = { top: itemTop + "px", overflow: "hidden",
                    width: nextWidth + "px", left: itemLeft
                };

                item.css(cssclass);
                if (itemTop + height <= bodyHeight) {
                    poptimer = setTimeout(function() {
                        changePosition(nextWidth, maxWidth, changeHeight);
                    }, 1);
                }
                else {
                    clearTimeout(poptimer);
                    var cssclass = { position: "absolute", top: parseInt(bodyHeight - height) + "px",
                        width: maxWidth + "px", overflow: "hidden"
                    };
                    item.css(cssclass);
                }
            }
        }
    }
});
/*
*	提示消息
*/
var UnReadMessageBox = {};
//显示消息框
UnReadMessageBox.showMsgBox = function() {
    $("#msgContent").css("display", "block");
}
//隐藏消息框
UnReadMessageBox.hideMsgBox = function() {

    $("#msgContent").css("display", "none");
    $("#msgContent").mouseout(function() {
        $("#msgContent").css("display", "none");
    });

    $("#msgContent").mouseover(function() {
        $("#msgContent").css("display", "block");
    });
}
/*
*	弹出层
*/
$(document).ready(function() {
    var ishow = $("#isshowpop").val();
    if (ishow == "True") {
        //弹出层
        var normalHeight = $("#pop").height();
        $("#pop").height(0);
        changeHeight(normalHeight);

    }
    //$("body").addClass("skin_3"); 
});

var hidenTimeer;
function changeHeight(normalHeight) {
    var poptimer;

    var item = $("#pop");
    var height = item.height() + 5;
    var width = item.width();

    var bodyTop = 0;
    if (typeof window.pageYOffset != 'undefined') {
        bodyTop = window.pageYOffset;
    }
    else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
        bodyTop = document.documentElement.scrollTop;
    }
    else if (typeof document.body != 'undefined') {
        bodyTop = document.body.scrollTop;
    }

    var bodyHeight = $(window).height();
    var bodyWidth = $(window).width();
    var itemTop = bodyTop + bodyHeight - height + 5 + "px";
    var itemLeft = bodyWidth - width + "px";
    $("#pop").css({ position: "absolute", display: "inline", height: height, top: itemTop, left: itemLeft, overflow: "hidden" });
    if (normalHeight >= height) {
        poptimer = setTimeout("changeHeight(" + normalHeight + ")", 1);
    }
    else {
        clearTimeout(poptimer);
        hidenTimeer = setTimeout("hidePop()", 30000)
    }
}
function hidePop() {
    $("#pop").fadeOut("slow");
    clearTimeout(hidenTimeer);
}
function closePop() {
    $("#pop").hide();
}

$(this).scroll(function() {
    // 页面发生scroll事件时触发
    var bodyTop = 0;
    var bodyLeft = 0;
    if (typeof window.pageYOffset != 'undefined') {
        bodyTop = window.pageYOffset;
        bodyLeft = window.pageXOffset;
    }
    else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {
        bodyTop = document.documentElement.scrollTop;
        bodyLeft = document.documentElement.scrollLeft;
    }
    else if (typeof document.body != 'undefined') {
        bodyTop = document.body.scrollTop;
        bodyLeft = document.documentElement.scrollLeft;
    }

    var item = $("#pop");
    var height = item.height();
    var width = item.width();
    var bodyHeight = $(window).height();
    var bodyWidth = $(window).width();
    var itemTop = bodyTop + bodyHeight - height + 5 + "px";
    var itemLeft = bodyLeft + bodyWidth - width + "px";
    item.css("top", itemTop);
});

/*
*	聊天
*/
//对<a>进行处理，达到将页面在指定窗口打开的功能。

var cookiesDomain = window.location.host;
var ChatPath = 'http://' + cookiesDomain + '/Chat';
var chat = {};
chat.SW = {
    __Prefix: "CHAT_SW_",
    GetName: function(id) {
        return this.__Prefix + id;
    },
    Check: function(a, wHeight, wWidth) {//a，要进行处理的a元素。wHeight、wWidth如果需要控制窗口大小，请指定。
        if (!a.target) {
            a.target = Math.random();
        }
        var name = this.GetName(a.target);
        if (j.getcookie(name) != "opened") {
            j.setcookie(name, "opened", null, "/", cookiesDomain);
            if (wHeight && wWidth) {
                if (a.target) {
                    window.open(a.href, name, 'height=' + wHeight + ', width=' + wWidth + ', top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
                    return false;
                }
            }
        }
        return true;
    }
};
chat.T = {
    GetHiddenValue: function(_result, _name) {
        if (_result == null || typeof (_result) != "string")
            return "";
        if (_name == null || typeof (_name) != "string")
            return "";
        if (_result.length > _name.length + 8) {
            if (_result.indexOf("<!--" + _name + ":") != -1) {
                var startIndex = _result.indexOf("<!--" + _name + ":") + _name.length + 5;
                return _result.substring(startIndex, _result.indexOf("-->", startIndex));
            }

        }
        return "";
    }
}

