﻿/**
//    Description
//	    Returns a dialog box populated as instructed. 
//
//    Syntax
//        DialogBox has one javascript object parameter (p) required for instantiation.
//
//        This object has the following syntax:
//        {body:{type:type, src:src} [,id:id [,title:title [,h:h [,w:w [,l:l [,r:r [,t:t [,b:b [,buttons:buttons]]]]]]]]]
//
//        with these parts:
//        body        dialog body     Required. Object representing the body/message of the dialog.
//        id          dialog id       Optional. String representing the id of the dialog box.
//        title       dialog title    Optional. String displayed to the user as the dialog's title
//        h           height          Optional. Numeric value representing the total height (css) of the dialog box.
//        w           width           Optional. Numeric value representing the total width (css) of the dialog box.
//        l           left            Optional. Numeric value representing the left position (css) of the dialog box.
//        r           right           Optional. Numeric value representing the right position (css) of the dialog box.
//        t           top             Optional. Numeric value representing the top position (css) of the dialog box.
//        b           bottom          Optional. Numeric value representing the bottom position (css) of the dialog box.
//        buttons     buttons         Optional. Array of object(s) representing the buttons to display to the user for interaction.
//
//        the body object has these parts:
//        type        body type       Required. String value determining how to populate the dialog body. Accepts "iframe" | "html" (only).
//        src         body src        Required. URL for the source/location of the iframe, or valid formatted HTML.
//
//        the buttons objects have these parts:
//        id          button id       Required. String value representing the id of the button.
//        value       button text     Required. String that is displayed to user for interaction.
//        click       button function Required. Anonymous function object assigned to the button; fires when clicked.
//
//    Sample Usage:
//        var clickToSave = function() { alert("Saving..."); }
//        var clickToCancel = function() { alert("Cancelling..."); }
//        var clickToReset = function() { alert("Resetting..."); }
//        var h = 400;
//        var w = 400;
//        var p = {
//            "id":"sample",
//            "h":"400",
//            "w":"400",
//            "l":"100",
//            "t":"100",
//            "buttons":[
//                { "id":"btnSave", "value":"Save", "click":clickToSave },
//                { "id":"btnCancel", "value":"Cancel", "click":clickToCancel },
//                { "id":"btnReset", "value":"Reset", "click":clickToReset }
//            ],
//            "body":{ "type":"iframe", "src":"http://www.dot.state.oh.us" }
//        };
//        var d = new DialogBox(p);
**/
function DialogBox(params)
{
    //Ensure body is present.
    if (
        ((!params.hasOwnProperty("body")) || 
        (!params.body.hasOwnProperty("type")) || 
        (!params.body.hasOwnProperty("src"))) || 
        ((params.body === "") || 
        (params.body.type === "") || 
        (params.body.src === "") ||
        (params.body === "undefined") || 
        (params.body.type === "undefined") || ((params.body.type !== "html") && (params.body.type !== "iframe")) ||
        (params.body.src === "undefined"))
    ) { alert("Dialog will not display\n\nNo message to display."); return false; }

    //Check for id.
    if (
        (!params.hasOwnProperty("id")) ||
        (params.id === "")
    ){ params.id = "Message"; }

    //Check for title.
    if (
        (!params.hasOwnProperty("title")) ||
        (params.title === "")
    ){ params.title = "Message"; }

    //Set private variables.
    var base = "baseDialog";
    var p = "#panel_";
    var p2 = "panel_";
    var cPBC = "#cellPanelBodyContainer_";
    var cPBC2 = "cellPanelBodyContainer_";
    var dPB = "#divPanelBody_";
    var dPB2 = "divPanelBody_";
    var dBC = "#dialogButtonContainer_";
    var dBC2 = "dialogButtonContainer_";
    var t = "#td_";
    var t2 = "td_";
    var cc = "commandCell_";
    var sPT = "#spanPanelTitle_";
    var sPT2 = "spanPanelTitle_";
    var cF = "#closeFunction_";
    var cF2 = "closeFunction_";

    //Set public properties.
    this.id = params.id;
    this.title = params.title;
    this.panel = "#"+this.id;
    this.body = dPB+this.id;
    this.buttons = dBC+this.id;
    this.forceNoButtons = false;
    
    //See if user wants to create a dialog with no buttons
    if (params.forceNoButtons)
    {
        this.forceNoButtons = true;
    }

    //Remove duplicate dialog before  displaying.
    if ($(this.panel).length > 0) { $(this.panel).remove(); }

    //Clone dialog element and modify corresponding id's.
    $(p+base).clone().attr("id", this.id).insertBefore("#panel_baseDialog");
    $(cPBC+base, this.panel).attr("id", cPBC2+this.id);
    $(dPB+base, this.panel).attr("id", dPB2+this.id);
    $(t+base, this.panel).attr("id", t2+this.id);
    $(t+base+"Footer", this.panel).attr("id", t2+this.id+"Footer");
    $(sPT+base, t+this.id).attr("id", sPT2+this.id);
    $(t+cc+base, t+this.id).attr("id", t2+cc+this.id);
    $(cF+base, t+cc+this.id).attr("id", cF2+this.id);
    $(dBC+base, this.panel).attr("id", dBC2+this.id)

    //Set dialog title.
    $(sPT+this.id).text(this.title);

    //Set up draggability.
    $(t+this.id).css("cursor", "move");
    $(t+this.id+"Footer").css("cursor", "move");
    $(this.panel).draggable();

//    //Set up resizability.
//	$(this.panel).resizable({ 
//	    autohide: true, 
//	    minHeight: 75, 
//	    minWidth: 75,
//	    stop: function(){
//            $(cPBC+this.id).css("height", ($(p+this.id).height() - 44)+"px");
//            $(cPBC+this.id).css("width", ($(p+this.id).width() - 8)+"px");
//            $(dPB+this.id).css("height", $(cPBC+this.id).height()+"px");
//            $(dPB+this.id).css("width", $(cPBC+this.id).width()+"px");

//	        if ($("#frame_" + this.id).length > 0){
//                $("#frame_" + this.id).attr("height", $(parent).height()+"px");
//                $("#frame_" + this.id).attr("width", $(parent).width()+"px");
//	        }
//	    }
//	});

    //Set up the the dialog's absolute positioning values.
    this.h = ((params.hasOwnProperty("h") && (params.h !== "")) ? params.h : "300");    //height
    this.w = ((params.hasOwnProperty("w") && (params.w !== "")) ? params.w : "250");    //width
    this.t = ((params.hasOwnProperty("t") && (params.t !== "")) ? params.t+"px" : "");  //top
    this.l = ((params.hasOwnProperty("l") && (params.l !== "")) ? params.l+"px" : "");  //left
    this.r = ((params.hasOwnProperty("r") && (params.r !== "")) ? params.r+"px" : "");  //right
    this.b = ((params.hasOwnProperty("b") && (params.b !== "")) ? params.b+"px" : "");  //bottom

    //Center (vertically) if no vertical positioning information is sent.
    if ((this.t === "") && (this.b === ""))
    {
//        MICROSOFT'S CHANGE IN VE 6 DOES SOMETHING THAT BREAKS jQuery's $(document).height() FUNCTION
//        var dH = $(document).height();
        var dH = getH();
        var h = (Math.floor((dH / 2) - ((this.h) / 2)));
        this.t = h;
    }

    //Center (horizontally) if no horizontal positioning information is sent.
    if ((this.l === "") && (this.r === ""))
    {
/*
        MICROSOFT'S CHANGE IN VE 6 DOES SOMETHING THAT BREAKS jQuery's $(document).height() FUNCTION;
        jQuery's $(document).width() SEEMS TO FUNCTION NORMALLY.  IT SEEMS PRUDENT TO REMOVE IT AT THIS
        TIME TO UTILIZE THE SAME FUNCTIONALITY AS THAT TO DETERMINE HEIGHT, AS MS COULD BREAK THIS AS WELL.
*/
//        var dW = $(document).width();
        var dW = getW();
        var w = (Math.floor((dW / 2) - ((this.w) / 2)));
        this.l = w;
    }
 
    //Position and size the dialog box appropriately.
    if (this.t !== "") { $(this.panel).css("top", this.t); }
    if (this.l !== "") { $(this.panel).css("left", this.l); }
    if (this.r !== "") { $(this.panel).css("right", this.r); }
    if (this.b !== "") { $(this.panel).css("bottom", this.b); }

    $(this.body).css("height", (this.h - 44)+"px");
    $(this.body).css("width", (this.w - 8)+"px");
    $(this.body).css("padding", "0px");
    $(this.body).css("margin", "0px");
    $(this.panel).css("height", this.h+"px");
    $(this.panel).css("width", this.w+"px");
    $(this.panel).css("position", "absolute");
    $(this.panel).css("display", "block");

    if (params.body.type === "iframe")
    {
        //Add a new DOM element for the iframe.
        iFra = document.createElement("iframe");
        tmpSrc = document.createAttribute("src");
        tmpW = document.createAttribute("width");
        tmpH = document.createAttribute("height");
        tmpBorder = document.createAttribute("border");
        tmpFBorder = document.createAttribute("frameborder");
        tmpMW = document.createAttribute("marginwidth");
        tmpMH = document.createAttribute("marginheight");

        //Style the iframe and the dialog body.
        $(this.body).css("overflow", "hidden");
        $(this.body).css("overflowy", "hidden");
        $(this.body).css("text-align", "left");
        $(this.body).css("width", (this.w)+"px");
        $(iFra).attr("width", "100%");
        $(iFra).attr("height", "100%");
        $(iFra).attr("border", "0");
        $(iFra).attr("frameborder", "0");
        $(iFra).attr("marginwidth", "0");
        $(iFra).attr("marginheight", "0");
        $(iFra).css("margin", "0px");
        $(iFra).css("padding", "0px");

        //Load the iframe.
        $(iFra).attr("src", params.body.src);
        $(iFra).attr("name", "frame_" + this.id);
        $(iFra).attr("id", "frame_" + this.id);

        //Append the iframe to the dialog's DOM.
        $(this.body).append(iFra);
    }
    else
    {
        //Set the dialog's body to the passed (formatted) html string.
        $(this.body).html(params.body.src);
    }

    //Append the dialog's buttons.
    var btn = null;
    var tmpType = null;
    var tmpId = null;
    var tmpValue = null;
    var tmpOnClick = null;

    //If no buttons were specified, give a basic "OK" button that will close the dialog.
    //Unless the user is forcing a dialog box without buttons
    if (
        (!params.hasOwnProperty("buttons")) ||
        ((params.buttons === "") || (params.buttons === "undefined")) ||
        (params.buttons.length === 0)
    ){
        if (!this.forceNoButtons)
        {
            var tmpF = function(){ $("#"+params.id).remove(); };
            params.buttons = [{ "id":"btnOK", "value":"OK", "click":tmpF }];
        }
    }

    //Loop the button array and append a button object to the dialog DOM.
    for (var btnCount = 0; btnCount < params.buttons.length; btnCount++)
    {
        //Create button element and add.
        btn = document.createElement("input");
        tmpType = document.createAttribute("type");
        tmpId = document.createAttribute("id");
        tmpValue = document.createAttribute("value");
        
        //Bind the click event for the current button.
        $(btn).click(params.buttons[btnCount].click);

        //Set the current button's attributes.
        $(btn).attr("type", "button");
        $(btn).attr("id", params.buttons[btnCount].id);
        $(btn).attr("value", params.buttons[btnCount].value);

        //Style the current button.
        $(btn).css({
            "height":"18px",
            "width":"100px",
            "font":"400 10px verdana, tahoma, san-serif",
            "line-height":"0.75",
            "cursor":"pointer",
            "padding":"0px",
            "margin":"0px",
            "margin-left":"2px",
            "margin-right":"2px",
            "text-align":"center",
/*
            "color":"#FFFFFF",
            "background-color":"transparent",
            "border":"1px solid #FFFFFF",
*/
            "vertical-align":"middle"
        });

/*
        //Bind dynamic styles to the current button.
        $(btn).mouseover(function() { $(this).css("background-color", "#009900"); $(this).css("color", "#000000"); });
        $(btn).mouseout(function() { $(this).css("background-color", "transparent"); $(this).css("color", "#FFFFFF"); });
*/

        //Append the current button to the dialog DOM.
        $(this.buttons).append(btn);
    }
}
