/*
    Script:         ccMessageBox.js
    Author:         Timur Kovalev (timur@creativecodedesign.com, http://www.creativecodedesign.com)
    License:        LGPL
    Notes:          Displays a message box via DHTML
    Usage:          ccMessageBox.Show("MsgBoxTitle", "some <strong>html</strong> text", "Warning", 400, 100, null)                    
    Requirements:   include ccMessageBox.css and copy associated images into your projects
*/

var ccMessageBox = new function() {
    // Show:        creates a displays a message box with specified paramters
    // strTitle:    title of the message box
    // strText:     HTML text that should be displayed
    // strType:     string representing the message type: "Info" or "Error" or "Warning"
    // iWidth:      width in pixels
    // iHeight:     height in pixels
    // strOnClose:  any custom on-close handling (i.e. document.getElementById('email').focus(); )
    this.Show = function(strTitle, strText, strType, iWidth, iHeight, strOnClose) {
        var iScreenCenterX; 
        var iScreenCenterY; 
        var strDivID = new Date().getTime();                                                            // generate a random div id (so we have no id collisions in multi-msg box situations
        var strCloseScript = "document.getElementById('" + strDivID + "').style.visibility = 'hidden';"
            + strOnClose;                                                                               // generate the close command (simply hides the div)
        this.GetScreenCenter(this);                                                                     // populate browser-specific screen center coordinates
        var strStyle = "width: " + iWidth + "px; height: " + iHeight + "px;left: "
            + (this.iScreenCenterX - iWidth / 2) + "px;top: "                                                // place the box in
            + (this.iScreenCenterY - iHeight / 2) + "px;";                                                   // the middle of the client region
        var oPopupDiv = this.CreateDiv(strDivID, "ccPopup", strStyle, "");                              // create the outer DIV
        var oButtonDiv = this.CreateDiv(null, null, "position: absolute; top: "                         // create the close button
            + (iHeight - 30) + "px; left: " + (iWidth / 2 - 50) + "px;", "");                           // and position it 30 pixels from bottom
        var oButton = document.createElement('input');                                                  // creat the DIV that will host the button
        oButton.setAttribute("type", "button");
        oButton.setAttribute("value", "OK");
        oButton.setAttribute("class", "ccPopupButton");
        oButton.setAttribute("onclick", strCloseScript);
        oButtonDiv.appendChild(oButton);
        var oCloseXDiv = this.CreateDiv(null, "ccPopupClose", null, null);                              // add a 'X' button to title bars
        oCloseXDiv.setAttribute("onclick", strCloseScript);                                             // add the same onclick as the OK button
        var oTitleDiv = this.CreateDiv(null, "ccPopupTitle", null, strTitle);                           // add title
        var oIcondDiv = this.CreateDiv(null, "ccPopup" + strType + "Icon", null, "&nbsp");              // place the proper icon style
        var oMessageDiv = this.CreateDiv(null, "ccPopupMessage", null, strText);                        // add message

        oPopupDiv.appendChild(oButtonDiv);                                                              // add all elements
        oPopupDiv.appendChild(oCloseXDiv);
        oPopupDiv.appendChild(oTitleDiv);
        oPopupDiv.appendChild(oIcondDiv);
        oPopupDiv.appendChild(oMessageDiv);
        document.body.appendChild(oPopupDiv);
        oPopupDiv.style.visibility = 'visible';                                                         // make the outer DIV visible
    }

    // CreateDiv:   creates and returns a DIV object with specified attributes
    // strDivID:    the ID of the DIV being created or null if none
    // strDivClass: the class of the DIV
    // strDivStyle: any addional styles for the DIV
    // strDivHTML:  the inner HTML     
    this.CreateDiv = function(strDivID, strDivClass, strDivStyle, strDivHTML) {
        var oReturnObject = document.createElement('div');
        if (strDivID != null)
            oReturnObject.setAttribute('id', strDivID);
        if (strDivClass != null)
            oReturnObject.setAttribute('class', strDivClass);
        if (strDivStyle != null)
            oReturnObject.setAttribute('style', strDivStyle);
        if (strDivHTML)
            oReturnObject.innerHTML = strDivHTML;
        return oReturnObject;
    }

    // GetScreenCenter:     Code borrowed from http://www.webmasterworld.com/javascript/3647600.htm
    //                      Attempts to deremine the center of the visible browser area in an 
    //                      browser independ-ish fashion
    this.GetScreenCenter = function(oInstance) {
        var centerX, centerY;                                                               // get the client center
        if (window.innerHeight) {
            centerX = window.innerWidth;
            centerY = window.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) {
            centerX = document.documentElement.clientWidth;
            centerY = document.documentElement.clientHeight;
        } else if (document.body) {
            centerX = document.body.clientWidth;
            centerY = document.body.clientHeight;
        }                                                                                  
        var scrolledX, scrolledY;                                                           // get the scroll offset
        if (window.pageYoffset) {
            scrolledX = window.pageXoffset;
            scrolledY = window.pageYoffset;
        } else if (document.documentElement && document.documentElement.scrollTop) {
            scrolledX = document.documentElement.scrollLeft;
            scrolledY = document.documentElement.scrollTop;
        } else if (document.body) {
            scrolledX = document.body.scrollLeft;
            scrolledY = document.body.scrollTop;
        }
        oInstance.iScreenCenterX = (scrolledX + centerX / 2);
        oInstance.iScreenCenterY = (scrolledY + centerY / 2);
    };
}
