
/**
 * File contains JS Library for AWindow Control
 *
 * JavaScript  version 1
 * @category   JavaScript Libraries
 * @author     Eugene A. Kalosha <aristarch@zfort.net>
 * @copyright  (c) 2004-2006 by ZFort Group
 * @version    SVN: $Id: 206$
 * @link       http://www.zfort.net
 * @since      File available since Release 2.3.0
 */

if (typeof(PHP2Controls) == 'undefined') PHP2Controls = new Object();

    /**
     * PHP2Controls.hWindow is JS Window properties Object.
     *
     * @author   Eugene A. Kalosha <aristarch@zfort.net>
     * @version  $Id: awindow.js, v 2.3.0 2006/09/23 $
     * @access   public
     * @package  php2
     */
    PHP2Controls.hWindow = function(wHandle, wTitle, wCSSBaseClass, wContentElement, canClose)
    {
        this.wHandle          = ((wHandle) ? wHandle : 'aWindow');
        this.wTitleCaption    = ((wTitle) ? wTitle : 'System Messages');
        this.wCSSBaseClass    = ((wCSSBaseClass) ? wCSSBaseClass : 'aWindow');
        this.wContentElement  = ((wContentElement) ? wContentElement : null);
        this.canClose         = ((canClose) ? canClose : true);
    }

    /**
     * PHP2Controls.AWindow is JS Class for HTML Draggable Window.
     *
     * @author   Eugene A. Kalosha <aristarch@zfort.net>
     * @version  $Id: awindow.js, v 2.3.0 2006/09/23 $
     * @access   public
     * @package  php2
     */
    PHP2Controls.AWindow = function(hWindow)
    {
        /**
         * Unique Control ID/Handle
         *
         * @var  string
         */
        this.id               = hWindow.wHandle;
        this.wHandle          = hWindow.wHandle;
        this.wTitleCaption    = hWindow.wTitleCaption;
        this.wCSSBaseClass    = hWindow.wCSSBaseClass;
        this.wContentElement  = hWindow.wContentElement;
        this.canClose         = hWindow.canClose;

        if (!document.getElementById(this.id))
        {
            createWindowHtmlCode = '<div id="' + this.id + '" class="aWindow"><div id="' + this.id + '_wHeader" class="wHeader"><span id="' + this.id + '_wTitle" class="wHText">Window Title</span><span class="rHButtons"><div id="' + this.id + '_wBtnHide" class="wBtnHide">&nbsp;</div><div id="' + this.id + '_wBtnClose" class="wBtnClose">&nbsp;</div></span></div><div id="' + this.id + '_wBody" class="wBody"></div></div>';
            
            // --- For Mozilla "insertAdjacentHTML" must be prototyped as Method of HTMLElement Class --- //
            document.body.insertAdjacentHTML("afterBegin", createWindowHtmlCode);
        }
        
        // --- Creating Window elements --- //
        this.aWindow    = document.getElementById(this.id);
        this.wHeader    = document.getElementById(this.id + '_wHeader');
        this.wTitle     = document.getElementById(this.id + '_wTitle');
        this.wBtnHide   = document.getElementById(this.id + '_wBtnHide');
        this.wBtnClose  = document.getElementById(this.id + '_wBtnClose');
        this.wBody      = document.getElementById(this.id + '_wBody');
        
        // --- Set Base Window CSS Class --- //
        this.aWindow.className = this.wCSSBaseClass;
        
        // --- Set Event Handlers --- //
        this.wBtnClose.currentWinObject  = this;
        this.wBtnHide.currentWinObject   = this;
        this.wHeader.currentWinObject    = this;
        this.wBtnClose.onclick   = function(){this.currentWinObject.close();};
        this.wBtnHide.onclick    = function(){this.currentWinObject.hide();};
        this.wHeader.ondblclick  = function(){this.currentWinObject.hide();};
        this.wTitle.innerHTML    = this.wTitleCaption ? this.wTitleCaption : 'System Messages';
        if (this.wContentElement) this.attachContent(this.wContentElement);
        
        /**
         * Unique Protect Frame ID
         *
         * @var  string
         */
        this.protectFrameId = null;
    }

    /**
     * Closes Window
     *
     * @return  void
     */
    PHP2Controls.AWindow.prototype.close = function()
    {
        this.aWindow.style.display = 'none';
        if (this.protectFrameId && (document.all.item(this.protectFrameId) != null)) document.all.item(this.protectFrameId).style.display  = 'none';
        
        // --- Unset Drag Window Ability --- //
        HTMLWindow.unsetDraggable(this.aWindow, this.wHeader);
    }

    /**
     * Closes Window
     *
     * @return  void
     */
    PHP2Controls.AWindow.prototype.display = function()
    {
        this.aWindow.style.display = 'inline';
        
        // --- Reprotecting DIV --- //
        this.protectFrameId   = HTMLElement.protectIEDiv(this.id);
    }

    /**
     * Hides Window Body
     *
     * @return  void
     */
    PHP2Controls.AWindow.prototype.hide = function()
    {
        if (typeof(this.wBody) == undefined) return true;

        // --- Hiding Window Body --- //
        headerHeight = this.wHeader.offsetHeight;
        this.wBody.style.display = 'none';
        
        this.aWindow.oldHeight   = this.aWindow.height;
        this.wHeader.height      = headerHeight + 'px';
        this.aWindow.height      = headerHeight + 'px';
        
        this.wBtnHide.onclick    = function(){this.currentWinObject.show()};
        this.wHeader.ondblclick  = function(){this.currentWinObject.show();};

        // --- Reprotecting DIV --- //
        this.protectFrameId   = HTMLElement.protectIEDiv(this.id);
    }

    /**
     * Hides Window Body
     *
     * @return  void
     */
    PHP2Controls.AWindow.prototype.show = function()
    {
        if (typeof(this.wBody) == undefined) return true;

        if (typeof(this.aWindow.oldHeight) != "undefined") this.aWindow.height = this.aWindow.oldHeight;
        
        this.wBody.style.display = '';
        
        // --- Set on btnHide click Event Handler --- //
        this.wBtnHide.onclick    = function(){this.currentWinObject.hide()};
        this.wHeader.ondblclick  = function(){this.currentWinObject.hide();};
            
        // --- Reprotecting DIV --- //
        this.protectFrameId   = HTMLElement.protectIEDiv(this.id);
    }

    /**
     * Closes MessageBox Window
     *
     * @return  void
     */
    PHP2Controls.AWindow.prototype.initScreenPosition = function()
    {
        // --- Finding Message Box Screen Position --- //
        var leftPos  = (HTMLElement.getBrowserWidth() - HTMLElement.getWidth(this.id)) / 2;
        var topPos   = (HTMLElement.getBrowserHeight() / 2) - 200;

        // --- Setting Message Box Screen Position --- //
        this.aWindow.style.left  = ((leftPos > 0) ? leftPos : '') + "px";
        this.aWindow.style.top   = ((topPos > 0) ? topPos : '') + "px";

        // --- Protecting IE --- //
        this.protectFrameId = HTMLElement.protectIEDiv(this.id);
    }
    
    /**
     * Attach contents to the Window Body
     *
     * @return  void
     */
    PHP2Controls.AWindow.prototype.attachContent = function(contentsNodeId)
    {
        var contentsNode             = document.getElementById(contentsNodeId);
        
        if (contentsNode.parentNode == this.wBody) return true;
        
        this.wBody.innerHTML       = '';
        this.wBody.style.width     = contentsNode.offsetWidth  + 'px';
        this.wBody.style.height    = contentsNode.offsetHeight  + 'px';
        this.aWindow.style.height  = this.wBody.offsetHeight + this.wHeader.offsetHeight + 'px';
        this.aWindow.style.width   = this.wBody.offsetWidth + 'px';
        this.aWindow.style.left    = HTMLElement.findPosX(contentsNode) + 'px';
        this.aWindow.style.top     = HTMLElement.findPosY(contentsNode) + 'px';
        
        // alert(HTMLElement.findPosX(contentsNode) + " " + HTMLElement.findPosX(this.aWindow));
        contentsNode.style.padding = 0;
        contentsNode.style.margin  = 0;
        
        if (typeof(document.importNode) == "function")
        {
            var tmpNode = document.importNode(contentsNode, true);
            this.wBody.appendChild(tmpNode);
        }
        else
        {
            var nodeHTML = (contentsNode.xml || contentsNode.outerHTML);
            this.wBody.innerHTML = nodeHTML;
        }
    
        contentsNode.parentNode.removeChild(contentsNode);
        
          /*var contentsNode             = document.getElementById(contentsNodeId);
          contentsNode.style.width     = this.wBody.offsetWidth  + 'px';
          contentsNode.style.height    = this.wBody.offsetHeight + 'px';*/
    }


    /**
     * On Window Moved Event Handler
     *
     * @return  void
     */
    PHP2Controls.AWindow.prototype.onMove = function()
    {
        // --- Protecting IE --- //
        this.protectFrameId = HTMLElement.protectIEDiv(this.id);
    }

    /**
     * Sets Window As Draggable
     *
     * @return  void
     */
    PHP2Controls.AWindow.prototype.initDragAndDrop = function()
    {
        // --- Assining onMove Event --- //
        this.aWindow.onMove = this.onMove;

        // --- Starting Drag Object --- //
        HTMLWindow.setDraggable(this.aWindow, this.wHeader, null, false);
        // Drag.init(this.wHeader, this.aWindow);
    }
    
    /**
     * PHP2Controls.Alert is JS Class for HTML Draggable Alert Window.
     *
     * @author   Eugene A. Kalosha <aristarch@zfort.net>
     * @version  $Id: awindow.js, v 2.3.0 2006/09/23 $
     * @access   public
     * @package  php2
     */
    PHP2Controls.Alert = function (alertMessage)
    {
        this.id             = 'pageSystemAlert';
        this.hWindow        = new PHP2Controls.hWindow(this.id, 'System Messages', 'aAlertWindow', null, true);
        this.aWindowObject  = new PHP2Controls.AWindow(this.hWindow);
        
        var alertBody = '<div id="' + this.id + '_wAlertBody" class="wAlertBody"><div id="' + this.id + '_wAlertMessageBody" class="wAlertMessageBody">' + alertMessage + '</div><div class="wAlertFooter"><center><input id="' + this.id + '_wBtnAClose" type="button" class="button" value="Ok" /></center></div></div>';
        this.aWindowObject.wBody.innerHTML = alertBody;
        
        // --- Displaying Alert Window --- //
        this.aWindowObject.wHeader.style.cursor = 'move';
        this.aWindowObject.display();
        this.aWindowObject.show();
        
        this.wBtnAClose                   = document.getElementById(this.id + '_wBtnAClose');
        this.wBtnAClose.currentWinObject  = this.aWindowObject;
        this.wBtnAClose.onclick           = function(){this.currentWinObject.close()};
        
        // --- Initialising Screen Position --- //
        this.aWindowObject.initScreenPosition();
        this.aWindowObject.initDragAndDrop();
    }

    /**
     * PHP2Controls.MessageBox is JS Class for HTML Draggable MessageBox Window.
     *
     * @author   Eugene A. Kalosha <aristarch@zfort.net>
     * @version  $Id: awindow.js, v 2.3.0 2006/09/23 $
     * @access   public
     * @package  php2
     */
    PHP2Controls.MessageBox = function ()
    {
        this.id             = 'pageSystemMessageBox';
        this.hWindow        = new PHP2Controls.hWindow(this.id, 'System Messages', 'aMessageBoxWindow', null, true);
        this.aWindowObject  = new PHP2Controls.AWindow(this.hWindow);
        
        // --- Set MessageBox Window --- //
        this.aWindowObject.wHeader.style.cursor = 'move';
        
        /**
         * MessageBox messages Array
         */
        this.messages = new Array();
    }
    
    /**
     * Add message into the MessageBox
     *
     * @return  void
     */
    PHP2Controls.MessageBox.prototype.add = function (message)
    {
        this.messages[this.messages.length] = message;
    }

    /**
     * Delete All messages from the MessageBox
     *
     * @return  void
     */
    PHP2Controls.MessageBox.prototype.clear = function (message)
    {
        this.messages = new Array();
    }

    /**
     * Shows MessageBox
     *
     * @return  void
     */
    PHP2Controls.MessageBox.prototype.show = function (message)
    {
        messagesCount = this.messages.length;
        
        if (messagesCount)
        {
            // --- Filling MessageBox Body --- //
            var messageboxBody = '<div id="' + this.id + '_wMessageBoxBody" class="wMessageBoxBody"><div id="' + this.id + '_wMessageBoxMessageBody" class="wMessageBoxMessageBody">';
        
            for (i = 0; i < messagesCount; i++)
            {
                messageboxBody  += '<div class="wMessageBoxMessage"><span class="messageNumber">' + (i + 1) + '.</span><span class="messageBody">' + this.messages[i] + '</span></div>';
            }
            
            messageboxBody  += '</div><div class="wMessageBoxFooter"><center><input id="' + this.id + '_wBtnAClose" type="button" class="button" value="Ok" /></center></div></div>';
            this.aWindowObject.wBody.innerHTML = messageboxBody;
            
            // --- Displaying MessageBox Window --- //
            this.aWindowObject.display();
            this.aWindowObject.show();
        
            // --- Set MessageBox btnClose onclick Handler --- //
            this.wBtnAClose                   = document.getElementById(this.id + '_wBtnAClose');
            this.wBtnAClose.currentWinObject  = this.aWindowObject;
            this.wBtnAClose.onclick           = function(){this.currentWinObject.close()};
        
            // --- Initialising Screen Position --- //
            this.aWindowObject.initScreenPosition();
            this.aWindowObject.initDragAndDrop();
        }
    }
