$.fn.drag = function (box) {
    var _px = 0, py = 0, slider = this, maxX = 0;
    box = box || this;
    function __ondrag(event) {
        _px = event.pageX - box.offset().left;
        _py = event.pageY - box.offset().top;
        maxX = document.documentElement.clientWidth - box.width() - 2;
        $(document).mousemove(__draging).mouseup(__dragend);
        if (window.captureEvents) {
            window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
            event.preventDefault();
        }
        else slider[0].setCapture();
    }
    function __draging(event) {
        var x = event.pageX - _px,
		      y = event.pageY - _py;
        x = x < 0 ? 0 : x > maxX ? maxX : x;
        y = y < 0 ? 0 : y;
        box.css({
            left: x,
            top: y
        });
    }
    function __dragend(event) {
        $(document).unbind();
        if (window.captureEvents) window.releaseEvents(Event.MOUSEMOVE | Event.MOUSEUP);
        else slider[0].releaseCapture();
    }
    return this.mousedown(__ondrag).click(function (event) { event.stopPropagation(); });
};

function KS_window(settings) {
    var options = $.extend({
        id: "ks_window", width: 300, height: 200
    }, settings);
    var t = this;
    this.fuckIE6 = jQuery.browser.msie && jQuery.browser.version == "6.0" ? true : false;
    if (options.win instanceof (jQuery)) {
        this.win = options.win;
        this.tit = this.win.find("div.ks_win_tit");
        this.con = this.win.find("div.ks_win_con");
        this.closebtn = this.win.find("div.ks_win_closebtn");
    } else {
        this.win = $('<div id="' + options.id + '" class="ks_win" />')
	    .css({
	        "zIndex": "999",
	        "width": options.width,
	        "height": options.height //,"border" :"1px solid #827157"
	    });
        this.tit = $('<div class="ks_win_tit" />').appendTo(this.win);
        this.con = $('<div class="ks_win_con" />').appendTo(this.win);
        this.closebtn = $("<div class='ks_win_closebtn'>关闭</div>").appendTo(this.win);
        this.win.appendTo(document.body).hide();
    }
    this.bg = $("#ks_win_bg").hide();
    if (this.bg.length == 0) this.bg = $('<div id="ks_win_bg" />')
	      .css({
	          "zIndex": "998",
	          "backgroundColor": "#000",
	          "width": "100%",
	          "position": "absolute",
	          "top": 0,
	          "left": 0,
	          "opacity": .4,
	          "display": "none"
	      })
		  .appendTo(document.body);
    this.closebtn.click(function () { t.close(); });
    this.setContent(options.content || null);
    this.setTitle(options.title || null)
    this.tit.drag(this.win);
}
$.extend(KS_window.prototype, {
    open: function (showbg, content) {
        this.setContent(content);
        this.win.css({
            "left": (document.documentElement.clientWidth - this.win.width()) / 2,
            "top": (document.documentElement.clientHeight - this.win.height()) / 2 + (window.pageYOffset == undefined ? document.documentElement.scrollTop : window.pageYOffset)
        }).show();
        if (showbg) this.bg.show();
        if (this.fuckIE6) $("select").css("visibility", "hidden");
        this.bg.height(document.documentElement.scrollHeight);
    },
    close: function () {
        this.win.hide();
        this.bg.hide();
        if (this.fuckIE6) $("select").css("visibility", "visible");
    },
    setTitle: function (title) { if (title) this.tit.html(title); },
    setContent: function (content) { if (content) this.con.empty().html(content); }
});

/*
<div class="ks_win" id="Alert" style="width: 400px;">
<div class="ks_win_tit">提示消息</div>
<div class="ks_win_closebtn">关闭</div>
<div class="ks_win_con" style="padding:10px;text-align:center;line-height:150%;"></div>
<div style="padding:10px;text-align:center"><input name="" type="button" value=" 确定 " class="ks_win_okbtn" /> <input name="" type="button" value=" 取消 " class="ks_win_canclebtn" /></div>
</div>
*/

function trace() {
    var div = $('<div  style="padding:10px;text-align:center">');
    this.okbtn = $('<input type="button" value=" 确定 " style="width:80px;height:26px" />').appendTo(div);
    this.canclebtn = $('<input type="button" value=" 取消 " style="width:80px;height:26px" />').appendTo(div);
    div.appendTo(this.win);
    this.tit.html("信息提示");
    this.con.css({ "padding": "10px", "textAlign": "center", "lineHeight": "150%" });
}
trace.prototype = new KS_window({ height: 'auto' });
trace.prototype.confirm = function (msg, trueCallBack, falseCallBack) {
    var t = this;
    t.setContent(msg);
    t.open(true);
    t.okbtn.show().unbind();
    if (trueCallBack) t.okbtn.click(trueCallBack)
    t.okbtn.click(function () { t.close(); });

    t.canclebtn.show().unbind();
    if (falseCallBack) t.canclebtn.click(falseCallBack);
    t.canclebtn.click(function () { t.close(); });
    t.closebtn.unbind();
    if (falseCallBack) t.closebtn.click(falseCallBack);
    t.closebtn.click(function () { t.close(); });
}
trace.prototype.alert = function (msg, trueCallBack) {
    var t = this;
    t.setContent(msg);
    t.open(true);
    t.okbtn.show().unbind();
    if (trueCallBack) t.okbtn.click(trueCallBack)
    t.okbtn.click(function () { t.close(); });
    t.canclebtn.hide();

    t.closebtn.unbind();
    if (trueCallBack) t.closebtn.click(trueCallBack);
    t.closebtn.click(function () { t.close(); });
}
