﻿
function dialog(title, content, width, height, cssName, ShowClose) {
    if (ShowClose == undefined)
        ShowClose = true;

    if ($("#floatBox .boxcontent").length == 0) {
        var temp_float = new String;
        temp_float = "<div id=\"floatBoxBg\"></div>";
        temp_float += "<div id=\"floatBox\" class=\"floatBox\">";
        temp_float += "<div class=\"title\"><h4></h4><span>" + (ShowClose ? "关闭" : "") + "</span></div>";
        temp_float += "<div class=\"boxcontent\"></div>";
        temp_float += "</div>";
        $("body").append(temp_float);
        $("#floatBox .boxcontent").css("height", (parseInt(height)-10) + "px");
        
      

        $("#floatBox").draggable({ 'cursor': 'move' });
        $("#floatBox").css("cursor", "move");
    }
    else {
        $("#floatBoxBg").height($(document).height() - 5);
         $("#floatBox .boxcontent").css("height", (parseInt(height)-10) + "px");
        $(".title span").html(ShowClose ? "关闭" : "");
    }

    $("#floatBox .title span").click(function () {
        $("#floatBox").hide();
        $("#floatBoxBg").hide();
    });


    $("#floatBox .title h4").html(title);

    contentType = content.substring(0, content.indexOf(":"));
    content = content.substring(content.indexOf(":") + 1, content.length);
    switch (contentType) {
        case "url":
            var content_array = content.split("?");
            $("#floatBox .boxcontent").ajaxStart(function () {
                $(this).html("loading...");
            });
            $.ajax({
                type: content_array[0],
                url: content_array[1],
                data: content_array[2],
                error: function () {
                    $("#floatBox .boxcontent").html("error...");
                },
                success: function (html) {
                    $("#floatBox .boxcontent").html(html);
                }
            });
            break;
        case "text":

            $("#floatBox .boxcontent").html(content);

            break;
        case "id":
            $("#floatBox .boxcontent").html($("#" + content + "").html());
            break;
        case "iframe":
            $("#floatBox .boxcontent").html("<iframe src=\"" + content + "\" width=\"100%\" height=\"" + (parseInt(height) - 10) + "px" + "\" scrolling=\"auto\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\"></iframe>"); break;
    }

    $("#floatBoxBg").height($(document).height());
    $("#floatBoxBg").show();
    $("#floatBox").css({ left: (($(window).width()) / 2 - (parseInt(width) / 2)) + "px", top: (($(window).height() - (height == "auto" ? 300 : parseInt(height))))/2 + $(document).scrollTop() + "px", width: width, height: height });
    $("#floatBox").show();

}


        
        Dialog=function(){}

        Dialog.Close = function () {
            $("#floatBoxBg").hide();
            $("#floatBox").hide();
            }

         Dialog.Alert=function( message,title,fn) {


             var m = "<div style='width:320px;height:60px;text-align:left;overflow:hidden'>" + message +
             "</div><div style='float:right'><input type='button' class='okbutton' value='确定' /></div>";
             dialog(title==null?"提示":title, "text:" + m, "350px", "150px", "text", false);
             $(".okbutton").click(function () { Dialog.Close(); if ($.isFunction(fn)) { fn(); }; });

         }

         Dialog.Confirm=function(title, message, fn) {
 
             var m = "<div style='width:320px;height:60px;text-align:left;overflow:hidden'>" + message + 
             "</div><div style='float:right'><input type='button' class='okbutton' value='确定' /><input type='button' class='cancelbutton' value='取消' /></div>";
             dialog(title, "text:" + m, "350px", "150px", "text",false);
             $(".okbutton").click(function () { if ($.isFunction(fn)) { fn(true); }; Dialog.Close(); });
             $(".cancelbutton").click(function () {  if($.isFunction(fn)){ fn(false);}; Dialog.Close(); });
         }

         Dialog.Pormpt=function(title, message, fn) { 
             var m = "<div style='width:320px;height:20px;text-align:left;overflow:hidden'>" + message + 
             "</div><div style='text-align:left'><input type='text' class='txtmessage' /></div><div style='float:right'><input type='button' class='okbutton' value='确定' /><input type='button' class='cancelbutton' value='取消' /></div>";
             dialog(title, "text:" + m, "350px", "150px", "text", false);
             $(".okbutton").click(function () { if ($.isFunction(fn)) { fn(true, $(".txtmessage").val()); }; Dialog.Close(); });
             $(".cancelbutton").click(function () { if ($.isFunction(fn)) { fn(false, null); }; Dialog.Close(); });
         }

         Dialog.Open = function (title, content, width, height, cssName, ShowClose) {
             dialog(title, content, width, height, cssName, ShowClose);
         }
   
