﻿/*拖动*/
Move = {
    moveObject: null,
    xoff: 0,
    yoff: 0,
    evtobj: null,
    Start: function() {
        document.onmousemove = function(evt) {
            if (Move.moveObject != null) {
                var x = ($.browser.msie) ? event.clientX + document.documentElement.scrollLeft : evt.pageX;
                var y = ($.browser.msie) ? event.clientY + document.documentElement.scrollTop : evt.pageY;
                Move.moveObject.style.top = y + Move.yoff + "px";
                Move.moveObject.style.left = x + Move.xoff + "px";
            } else {
                document.onmousemove = null;
            }
        }
    },
    Bind: function(JQ, For) {
        $("body").bind("mouseup", function() { Move.moveObject = null; });
        JQ.bind("mousedown", function(evt) {
            Move.xoff = For.offsetLeft - evt.pageX;
            Move.yoff = For.offsetTop - evt.pageY;
            Move.moveObject = For;
            Move.Start();
        });
        JQ.bind("mouseout", function() { this.style.cursor = "default"; });
        JQ.bind("drag", function() { return false; });
        JQ.bind("selectstart", function() { return false; });
    }
};

/*模态窗口*/
Win = {
    CallBack: null,
    Container: null,
    Result: false,
    Init: function() {
        Win.Container = $('<iframe class="Win_overlay" style="z-index:97;"></iframe><div class="Win_overlay"></div><div tabindex="0" id="win" oncontextmenu="return false"><div class="Head" id="winHead"><div class="Left"></div><div class="Ico"></div><div id="winTitle" class="Title"></div><div class="Right"></div><div id="winClose" class="Close"></div></div><div class="Mid"><div class="Left"></div><div id="winMidCenter" class="Center"></div><div class="Right"></div></div><div class="Foot"><div class="Left"></div><div class="Center"><button id="buttonOK">确定</button>　<button id="buttonCancel">取消</button></div><div class="Right"></div></div></div>');
        $("body").eq(0).append(Win.Container);
        Win.Container.hide();
        Move.Bind($("#win div.Head"), document.getElementById("win"));
        $("#winClose").bind("click", function() { Win.Result = false; Win.Close() }).hover(function() { $(this).addClass("CloseHover") }, function() { $(this).removeClass("CloseHover") });
        $("#buttonCancel").bind("click", function() { Win.Result = false; Win.Close() });
        $("#buttonOK").bind("click", function() { if (!Win.Result) Win.Result = true; Win.Close() });
    },
    Close: function() {
        //$("body,html").css("overflow", Win.overflow);
        Win.Container.hide();
        if (Win.CallBack != null) Win.CallBack(Win.Result);
        Win.CallBack = null;
    },
    Open: function(Title, Content, CallBack, Width, Height) {
        if ($("#win").length == 0) Win.Init();
        Win.Result = false;
        if (Width == undefined) Width = 400;
        if (Height == undefined) Height = 200;
        $("#win").css("top", document.documentElement.scrollTop + window.screen.availHeight / 2 - Height / 2).css("left", document.body.clientWidth / 2 - Width / 2);
        $("#winTitle").html(Title);
        $("#winMidCenter").html(Content);
        $("#winClose").show();
        $("#buttonCancel").html("取消").show();
        $("#buttonOK").html("确定").show();
        $("#winTitle").width(Width - 40);
        $("#winMidCenter").width(Width - 10);
        $("#win>div").width(Width);
        $("#win>div.Foot").width(Width - 2);
        $("#win>div.Mid>div,#win>div.Mid").height(Height - 67);
        $("#win>div.Foot>div.Center").width(Width - 22);
        $(":text", Win.Container).addClass("text");
        $(".Win_overlay").height($("body").height());
        //$("body,html").css("overflow", "hidden");
        Win.Container.show();
        $(":text", Win.Container).click(function() { $(this).addClass("text_hover") }).blur(function() { $(this).removeClass("text_hover") });
        if (CallBack != undefined) Win.CallBack = CallBack;
        if ($("#win textarea").length < 1) {
            $("#win")[0].onkeydown = function(evt) {
                evt = (evt) ? evt : ((window.event) ? window.event : "");
                var key = evt.keyCode ? evt.keyCode : evt.which;
                if (key == 13) {
                    $("#buttonOK").click();
                } else if (key == 27) {
                    $("#winClose").click();
                }
            }
        } else {
            $("#win")[0].onkeydown = null;
        }
    },
    Loding: function(Content) { Win.Open("请稍后", "<div style='margin:30px;text-align:left;text-indent:24px'>" + Content + "</div>"); $("#buttonCancel").hide(); },
    Alert: function(Content, Call) { Win.Open("系统提示", "<div style='margin:30px;text-align:left;text-indent:24px'>" + Content + "</div>", Call); $("#buttonCancel").hide(); },
    Confirm: function(Content, Call) { Win.Open("警告窗口", "<div style='margin:30px;text-align:left;text-indent:24px'>" + Content + "</div>", Call); },
    Input: function(Content, Call, Value) { Win.Open("输入窗口", "<div style='margin:30px;text-align:left;'>" + Content + "：<br><br><input style='width:250px;margin-left:50px' id='WinInput' /></div>", Call, 400, 200); if (Value != undefined) { $("#WinInput").val(Value); Win.Result = Value; }; $("#WinInput").bind("change", function() { Win.Result = $(this).val() }) },
    Input2: function(Content, Call, Value) { Win.Open(Content, "<textarea style='width:370px !important;width:360px;height:140px;margin:10px' id='WinInput' />", Call, 400, 240); if (Value != undefined) { $("#WinInput").val(Value); Win.Result = Value; }; $("#WinInput").bind("change", function() { Win.Result = $(this).val() }) },
    InputMoney: function(Content, Call, Value) { Win.Open("输入窗口", "<div style='margin:30px;text-align:left;'>" + Content + "：<select id='WinInput'><option value='0'>0</option><option value='1'>1</option><option value='5'>5</option><option value='15'>15</option><option value='50'>50</option><option value='100'>100</option></select></div>", Call, 400, 200); if (Value != undefined) { $("#WinInput").val(Value); Win.Result = Value; }; $("#WinInput").bind("change", function() { Win.Result = $(this).val() }) },
    Info: function(config) {
        if (!config.Title) config.Title = "信息窗口";
        if (!config.Width) config.Width = 400;
        if (!config.Height) config.Height = 200;
        $.get(config.Url, function(html) { Win.Open(config.Title, html, config.Call, config.Width, config.Height) });

    }
};

/*扩展*/
String.prototype.len = function() { return this.replace(/[^x00-xff]/g, "**").length; }
Date.prototype.format = function(format) {
    var o = { "M+": this.getMonth() + 1, "d+": this.getDate(), "h+": this.getHours(), "m+": this.getMinutes(), "s+": this.getSeconds(), "q+": Math.floor((this.getMonth() + 3) / 3), "S": this.getMilliseconds() }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}
