﻿getCookie = function(name) {
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]); return '';
}
setCookie = function(name, value, expires, path, domain, secure) {
    var liveDate = new Date();
    expires = liveDate.setTime(liveDate.getTime() + expires * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

getCookieOrign = function(name) {
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return arr[2]; return '';
}


Number.prototype.toDecimalString = function() {
    var f = (this + 0.00001).toString();
    var index = f.indexOf('.');
    return f.substring(0, index + 3);
}

jQuery(function() {
    $.fn.extend({
        FixIE6PNG: function() {
            if ($.browser.msie) {
                var ver = parseFloat($.browser.version);
                if (!isNaN(ver) && ver >= 7.0) {
                    return;
                }

                var backgroundImage = $(this).css("background-image");
                var srcImage = $(this).attr("src");
                if (backgroundImage && backgroundImage != 'none' && backgroundImage.match(/\.png/i) != null) {
                    var origUrl = backgroundImage.substring(5, backgroundImage.length - 2);
                    var blank = HoumartWeb.Fwk.Site.instance.getSitePath("Root") + 'Images/blank.gif';
                    var backgroundPosition = $(this).css("background-position");
                    $(this).css("background-image", "url(" + blank + ")");
                    if (backgroundPosition) {
                        $(this).css("background-position", backgroundPosition);
                    }
                    $(this).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + origUrl + "', sizingMethod='crop')");
                } else if (srcImage && srcImage.match(/\.png|\.jpg/i) != null) {
                    var blank = HoumartWeb.Fwk.Site.instance.getSitePath("Root") + 'Images/blank.gif';
                    var height = $(this).height();
                    var width = $(this).width();
                    $(this).attr("src", blank);
                    $(this).css("width", width + "px");
                    $(this).css("height", height + "px");
                    $(this).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + srcImage + "', sizingMethod='crop')");
                }
            }
        }
    });

    $.extend({
        alert: function(title, text, callback) {
            var dlg = $('<div></div>').attr("title", title).append($("<p></p>").html(text));
            var runCallback = function() {
                if (callback != undefined) {
                    if (typeof (callback) == 'function')
                        callback()
                }
            }
            dlg.dialog({
                autoOpen: false,
                draggable: true,
                resizable: false,
                modal: true,
                buttons: {
                    "确定": function() {
                        $(this).dialog("close");
                    }
                },
                close: function() {
                    runCallback();
                    $(this).remove();
                }
            });
            dlg.dialog("open");
        }
    });

    $.extend({
        confirm: function(title, text, confirmCallback, cancelCallback) {
            var dlg = $('<div></div>').attr("title", title).append($("<p></p>").html(text));

            var runConfirmCallback = function() {
                if (confirmCallback != undefined) {
                    if (typeof (confirmCallback) == 'function')
                        confirmCallback();
                }
            }
            var runCacelCallback = function() {
                if (cancelCallback != undefined) {
                    if (typeof (cancelCallback) == 'function')
                        cancelCallback();
                }
            }

            dlg.dialog({
                autoOpen: false,
                draggable: true,
                resizable: false,
                modal: true,
                closeTex: "hide",
                buttons: {
                    "确定": function() {
                        runConfirmCallback();
                        $(this).dialog("close");
                    },
                    "取消": function() {
                        runCacelCallback();
                        $(this).dialog("close");
                    }
                },
                close: function() {
                    $(this).remove();
                }
            });
            dlg.dialog("open");
        }
    });
});

Type.registerNamespace("HoumartWeb.Fwk");

HoumartWeb.Fwk.Site = function() {
    this._sitePathArray = [];
}
HoumartWeb.Fwk.Site.prototype = {
    registerSitePath: function(funKey, url) {
        this._sitePathArray[funKey] = url;
    },

    getSitePath: function(funKey) {
        return this._sitePathArray[funKey];
    },
    initialize: function() {
    },
    dispose: function() {
    }
}
HoumartWeb.Fwk.Site.serviceFailed = function(error) {
    $.alert("操作发生错误", error.get_message());
}

HoumartWeb.Fwk.Site.enter = function(obj) {
    evt = window.event;
    keyCode = evt.keyCode ? evt.keyCode : (evt.which ? evt.which : evt.charCode);
    if (keyCode == 13) {
        $(obj).click(); //搜索事件
        $("Form").submit(function() { return false; });
        return false;
    }
    else { return true; }
}

HoumartWeb.Fwk.Site.Search = function(obj) {
    var searchVal = $(obj).prev().find("input").val();
    if (searchVal == '' || searchVal == '搜索商品')
    { alert('请输入搜索内容'); return false; }
    window.location.href = $(obj).attr("action") + '?keyword=' + encodeURI(searchVal);
    return false;
}

HoumartWeb.Fwk.Site.UserArea = function() {
    $(".UserArea").hover(function() {
        var pt = $(this).find("SPAN").position();
        $("#divArea").css("left", pt.left);
        $("#divArea").css("top", pt.top + 15);
        $('#divArea').show();
    }, function() { $('#divArea').hide(); });
    $("#divArea LI").mouseover(function() { $(this).addClass("areaBack"); });
    $("#divArea LI").mouseout(function() { $(this).removeClass("areaBack"); });
    $("#divArea LI").bind("click", HoumartWeb.Fwk.Site.SetUserArea);
    //$(".UserArea").bind("mouseout", function() { $("#divArea").hide(); });
}
HoumartWeb.Fwk.Site.SetUserArea = function() {
    var pathUrl = HoumartWeb.Fwk.Site.instance.getSitePath("Root");
    pathUrl += "Users/UserArea.ashx?";
    var areaName = $(this).html();
    var param = "areaID=" + $(this).attr("value") + "&areaName=" + encodeURI(areaName) + "&ReturnUrl=" + encodeURI(window.location.href);
    window.location.href = pathUrl + param;
}


HoumartWeb.Fwk.Site.registerClass('HoumartWeb.Fwk.Site');
HoumartWeb.Fwk.Site.instance = new HoumartWeb.Fwk.Site();
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();


