﻿var ajaxWin = function(win) {
    if (win != undefined) {
        //窗口的属性
        this.callBackData = win.callBackData || null; //关闭窗口时执行的代码
        this.url = win.url;
        this.width = win.width || 0; //size的值通过url的参数width和height来获得
        this.height = win.height || 0;
        this.opacity = win.opacity || 50;
        this.title = win.title || "";
        this.content = win.content;
        this.redirect = win.redirect; 
        this.loadcallback = win.loadcallback||null; //窗口加载完成时执行代码
        this.dragable = win.dragable||true;
    }
};
ajaxWin.prototype.open = function() { 

    removeWindow();

    //打开遮罩
    $("body").append("<div id=\"WinMask\" class=\"mask\" style=\"position: absolute;background-color:#000;filter:alpha(opacity=" + this.opacity + ");-moz-opcacity:0." + this.opacity + ";opacity:0." + this.opacity + ";\"></div>");

    var bodyHeight = document.body.offsetHeight - document.body.scrollTop;
    var clientHeight = document.documentElement.clientHeight;
    var clientWidth = document.documentElement.clientWidth;
    //设置Mask高度
    if (bodyHeight > clientHeight)
        clientHeight = bodyHeight;
    else
        bodyHeight = clientHeight;
    $("#WinMask").height(bodyHeight + "px");

    //加载窗口
    if (this.url != undefined)
        loadWindow(this.url, this.title,this.width,this.height,this.loadcallback);
    else if (this.content != undefined)
        $("#WinContent").append(this.content);
    if(this.dragable){
        $('#WinFrame').Drags({
                    handler: '#WinHead'
                });
    }
}
//加载的内容，重新设置他的尺寸
//function loadWindow(url, title) {
function loadWindow(url, title,width,height,loadcallback) {
    title = title || "";
    width = width||"auto";
    height = height||"auto";
    if (url == undefined || url == "" || url == "null") {
        removeWindow();
    }
    //移除窗口
    $("div").remove("#WinFrame");
    //创建窗口
   
    $("body").append("<div id=\"WinFrame\" class=\"frame ajaxwin\" style=\"position: absolute;top:100px;filter:alpha(opacity=0);-moz-opcacity:0;opacity:0\">" +
                    "<div id=\"WinHead\" class=\"head\">" +
                        "<span class=\"btnclose\"><a onclick=\"javascript:removeWindow();\" style=\"cursor:pointer;\" class=\"clsbtn\" title=\"关闭\"></a></span>" +
                        "<span class=\"title\">" + title + "</span>" +
                   "</div>" +
                   "<div id=\"WinContent\" class=\"content\"\"></div>" +
                 "<div class=\"clear\"></div></div>");
    
    
    $("#WinContent").load(url, function() {
        setFrameSize("#WinFrame",width,height);
        setLink("#WinFrame");
        //如果是Ie6 隐藏所有<select>
        hideSelects();
        loadcallback&&loadcallback();
    });

}
function setFrameSize(frameId,width,height) {
    var fixWidth = width!="auto";
    var fixHeight = height!="auto";
    var bodyHeight = document.body.offsetHeight;
    var clientHeight = document.documentElement.clientHeight;
    var clientWidth = document.documentElement.clientWidth;
    $(frameId).css("height", "").css("width", "");

    //设置窗口的定位
    var frameWidth = width;
    var frameHeight = height;

    $(frameId + " .content").css({ "height":(fixHeight?(frameHeight - 50) + "px":"auto"), "overflow": "auto", "overflow-x": "hidden"});
    $(frameId).css("width",fixWidth?frameWidth+"px":frameWidth)
    var frameTop = parseInt((clientHeight - $(frameId).height()) / 2) + document.documentElement.scrollTop;

    if (frameTop <= 0)
        frameTop = 0;            
  //==========================================
  //modif  by ben  for  set the show  Frame's  min size  and  max  size
  //==========================================
        if($(frameId).width()<100&&!fixWidth)
            frameWidth=100;
        if($(frameId).width()>1000&&!fixWidth)
                  frameWidth=900;
 //======================= ==================
    var frameLeft = parseInt((clientWidth - $(frameId).width()) / 2);
    var frameRight = frameLeft;
    
    $(frameId).css({
        "top": frameTop + "px",
        "left": frameLeft + "px",
        //"width":fixWidth?frameWidth+"px":frameWidth,
        "filter": "",
        "-moz-opcacity": "",
        "opacity": ""
    });
    $("#WinHead").width($(frameId + " .content").width())
    //$(frameId).css("height", frameHeight + "px");
    //$(frameId + " .content").append("<div class=\"clear\"></div>").css("height", (frameHeight - 40) + "px");
}

ajaxWin.prototype.close = function() {
    //if (this.callBackData != undefined) {
    //        eval(this.onClose);
    //}
    removeWindow();
}
function removeWindow(callBackData) {
    if($("#WinFrame object").size()>0)
        $("#WinFrame object").remove();
    $("#WinMask").remove();
    $("#WinFrame").remove();
    showSelects();
}

function hideSelects() {
    //如果是Ie6 隐藏所有<select>
    if (window.navigator.appVersion.indexOf("MSIE 6") > 0) {

        $("body").append("<iframe class='coverFrame' src='about:blank' style='position:absolute'></iframe>");
        $(".coverFrame").css({
            "height": $("#WinFrame").height(),
            "width": $("#WinFrame").width(),
            "top": $("#WinFrame").css("top"),
            "left": $("#WinFrame").css("left"),
            "z-index": $("#WinFrame").css("z-index") - 1
        });
        $("#WinFrame").resize(function() {
            $(".coverFrame").css({
                "height": $(this).height(),
                "width": $(this).width()
            });
        });
        //$("select").hide();
    }
}

function showSelects() {
    //如果是Ie6 显示所有<select>
    if (window.navigator.appVersion.indexOf("MSIE 6") > 0) {
        $(".coverFrame").remove();
    }
}
//=========================================================================================================================
//=========================================================================================================================
//=========================================================================================================================

var ajaxMsg = function(msg) {
    if (msg != undefined) {
        //提示框的属性
        this.result = msg.result || 0; //0错误 1正确 2提示 3警告
        this.title = msg.title || ""; //提示框的标题
        this.redirect = msg.redirect;
        this.content = msg.content || ""; //窗口的内容 通过后台json数据获得
        this.callBackData = msg.callBackData;
    }
}
//创建一个win
ajaxMsg.prototype.open = function(parentElement) {
    //创建Mask遮罩层
    $("body").append("<div id=\"MsgMask\" class=\"mask\"></div>");
    //设置Mask高度
    var bodyheight = document.body.offsetHeight;
    var clientheight = document.documentElement.clientHeight;
    if (bodyheight > clientheight)
        clientheight = bodyheight;
    else
        bodyheight = clientheight;
    $("#MsgMask").height(bodyheight + "px");

    //创建窗口
    $("body").append("<div id=\"MsgFrame\" class=\"frame\"></div>");
    var frameId = "#MsgFrame";

    //不同的style 不同的frame  frame由mask来控制
    switch (this.result) {
        case 0:
            $(frameId).addClass("msg_error");
            break;
        case 1:
            $(frameId).addClass("msg_right");
            break;
        case 2:
            $(frameId).addClass("msg_tips");
            break;
        case 3:
            $(frameId).addClass("msg_warning");
            break;
        default:
            $(frameId).addClass("msg_default");
            break;
    }
    //创建内容div
    $(frameId).append("<div id=\"MsgContent\" class=\"content\">" + this.content + "</div>");

    //设置Mask的宽高
    if (this.width > 0)
        $(frameId).width(this.width);
    if (this.height > 0)
        $(frameId).height(this.height);

    //设置窗口的定位
    var frame_width = $(frameId).width();
    var frame_height = $(frameId).height();
    var css_top = parseInt((document.documentElement.clientHeight - frame_height) / 2) - 100;
    if (css_top <= 0)
        css_top = 80;
    var css_left = (document.documentElement.clientWidth - frame_width) / 2;
    var css_right = css_left;
    $(frameId).css("top", css_top + "px").css("left", css_left + "px").css("right", css_right + "px");
    hideSelects();
}

//显示方式
ajaxMsg.prototype.show = function(callBack) {

    if (this.result == undefined) {
        if (callBack != undefined)
            callBack(this.callBackData);
        return;
    }

    if (this.result == 1) {
        if (this.content != undefined && this.content != "") {
            //this.open();
            //setTimeout(this.close, 1000);
            alert(this.content);
        }
        if (callBack != undefined)
            callBack(this.callBackData);
    }
    else {
        //this.open();
        //setTimeout(this.close, 2000);
        alert(this.content);
    }
}

//关闭
ajaxMsg.prototype.close = function() {
    removeMsg();
}
function removeMsg() {
    $("div").remove("#MsgMask");
    $("div").remove("#MsgFrame");
    showSelects();
}


//删除一条数据
function deleteData(url,alertMsg) {
    var id = request.QueryString("id", url);
    if (confirm("确定要删除吗？")) {
        $.getJSON(
        url,
        function(data) {
            var msg = new ajaxMsg(data);
            if (data.result == 1) {
                //重新绑定下分页信息
                var table = $("#item_" + id).parent().parent();
                try {
                    var span = $(".recordcount", table);
                    var count = parseInt(span.html() || "0");
                    $(table).pageHtml(count - 1, parseInt($(table).attr("pagesize") || 10), parseInt($(table).attr("pageindex") || 1));
                } catch (e) { }

                //移除当前行
                $(document.getElementById("item_" + id).tagName).remove("#item_" + id);
                table.setRowsClass();
                msg.content = alertMsg || "";
            }
            //msg.open();
            //setTimeout(msg.close, 1000);
            if(msg.content!="")
                alert(msg.content);
            if (msg.url != undefined && msg.url != "") {
                location.href = msg.url;
            }
        });
    }
}

//对id节点下的所有的a 定义click事件
function setLink(id) {
    if (id == undefined)
        id = "body";
    else
        id = "#" + id;
    $(id + " a").each(function() {
        var href = $(this).attr("href");
        if (href == undefined)
            return true;
        if (href.indexOf("javascript") == -1 && href.indexOf("#") == -1) {
            $(this).unbind("click").click(function() {
                switch (this.target) {
                    case "_new": //弹出窗口
                        this.width = request.QueryString("width", href);
                        this.height = request.QueryString("height", href);
                        var win = new ajaxWin({
                            url: href,
                            height: this.height,
                            width: this.width,
                            title: this.title
                        });
                        win.open();
                        break;
                    case "_window": //弹窗本身load
                        loadWindow(href, this.title);
                        break;
                    case "_delete": //删除连接
                        deleteData(href);
                        break;
                    case "_main":
                        {
                            var _href = this.href;
                            $("#main").loadUrl(this.href);

                            break;
                        }
                    case "_blank":
                    case "_self":
                    case "_top":
                    case "":
                    case null:
                        return true;
                        break;
                    case undefined:
                    case "undefined":
                        href = "#";
                        return false;
                        break;
                    default:
                        if (this.target != "")
                            if ($("#" + this.target).length > 0) {
                                $("#" + this.target).loadUrl(href);
                        }
                        else {
                            this.target = "";
                            return true;
                        }
                        break;
                }
                return false;
            });
        }
        else
            return true;
    });
}


//扩展load
; (function($) {
    $.fn.loadUrl = function(url, callback) {
        var id = this.attr("id");
        var cb = function() {
            setLink(id);
            if (id == "main") {
                navi.ParseUrl();
            }
        };
        if (!callback) {
            callback = cb
        }
        else {
            callback = function() {
                cb();
                callback();
            }
        }
        this.load(url, callback);
    }
})(jQuery);


function loadUrl(url, id) {
    $("#" + id).loadUrl(url);
}

