
/**
 * 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 + '">'            						
									+'\n        	<div id="' + this.id + '_wHeader" class="title_dialogue" style="height: 41px;">'
									+'\n            	<div class="font_Verdana fs14 ml15 pt8 mr10 white clear;">'
									+'\n                	<b id="' + this.id + '_wTitle" style="float:left">Info</b>'
									+'\n					<span style="float:right">'
									+'\n                		<a id="' + this.id + '_wBtnHide"><img src="/images/ui/ico_down_dia.png" border="0" /></a>&nbsp;&nbsp;'
									+'\n                		<a id="' + this.id + '_wBtnClose"><img src="/images/ui/ico_close_dia.png" border="0" /></a>'
									+'\n					</span>'
									+'\n                </div>'
									+'\n            </div>'
									+'\n            <div id="' + this.id + '_wBody" class="white">'
									+'\n            </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(yOffset)
    {
        // --- Finding Message Box Screen Position --- //
        var leftPos  = (HTMLElement.getBrowserWidth() - HTMLElement.getWidth(this.id)) / 2;
        
        var scrOfY = 0;
	    if( typeof( window.pageYOffset ) == 'number' ) {
	      //Netscape compliant
	      scrOfY = window.pageYOffset;
	    } else if( document.body && document.body.scrollTop ) {
	      //DOM compliant
	      scrOfY = document.body.scrollTop;
	    } else if( document.documentElement && document.documentElement.scrollTop ) {
	      //IE6 standards compliant mode
	      scrOfY = document.documentElement.scrollTop;
	    }
        
        var topPos   = (HTMLElement.getBrowserHeight() / 2) - 200 + scrOfY + parseInt(yOffset);        
        
        // --- 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" ><div id="' + this.id + '_wAlertMessageBody" >' + alertMessage + '</div><div ><center><input id="' + this.id + '_wBtnAClose" type="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(0);
        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 (messageType, isConfirm, geturl)
    {
    	this.geturl	=(geturl!='undefined')?geturl:'';
    	this.isConfirm	=(isConfirm!='undefined')?isConfirm:'false';
    	this.messageType	=(messageType)?messageType:'info';
        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;
    }
    
    
    /**
     * Add message into the MessageBox
     *
     * @return  void
     */
    PHP2Controls.MessageBox.prototype.setYOffset = function (yOffset)
    {
        this.yOffset = yOffset;
    }
    

    /**
     * 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,messageType, isConfirm, geturl)
    {
    	messagesCount = this.messages.length;        
        if (messagesCount)
        {
        	strConfirm ='';
        	if(this.isConfirm)
        	{
        		strConfirm ='<a id="' + this.id + '_wBtnOk"><img src="/images/ui/btn_ok.png" border="0" /></a> ';
        		strConfirm +='<a id="' + this.id + '_wBtnAClose"><img src="/images/ui/btn_cancel.png" border="0" /></a>';
        	}
        	else
        	{
        		strConfirm = '<a id="' + this.id + '_wBtnAClose"><img src="/images/ui/btn_ok.png" border="0" /></a>';
        	}
        	
        	// --- Filling MessageBox Body --- //            
        	var messageboxBody='<div id="' + this.id + '_wMessageBoxBody" class="border" style="background-color:#FFFFFF;">'
                    +'\n<div id="' + this.id + '_wMessageBoxMessageBody" class="ml15 mr15 mt15 mb15 border">'
                        +'\n<div class="ml10 mt10 mr10">'
                            +'\n<div class="float_l"><img src="/images/ui/{{messageTypeImage}}"/></div>'
                            +'\n<div class="float_l text_topic ml10" style="width: 315px;">'
                                +'\n{{message}}'
                            +'\n</div>'
                        +'\n</div>'
                        +'\n<br class="clear" />'
                        
                        +'\n<div align="center" class="mb15 pt5">'
                            + strConfirm
                        +'\n</div>'
                    +'\n</div>'
                +'\n</div>';
            var message='';
        	if(messagesCount==1)
        	{
        		message  += this.messages[0];
        	}
        	else
        	{
	            for (i = 0; i < messagesCount; i++)
	            {
	                message  += '<br/>'+ (i + 1) + '.&nbsp;' + this.messages[i];
	            }
            }            
        	
            messageTypeImage=(this.messageType=="error")?"ico_error.png":"ico_info.png ";
            messageboxBody=messageboxBody.replace('{{messageTypeImage}}',messageTypeImage);
            messageboxBody=messageboxBody.replace('{{message}}',message);
            this.wTitle = document.getElementById(this.id + '_wTitle'); 
            if(this.wTitle) this.wTitle.innerHTML = (this.messageType=="error")?'Error' : 'Info'; 
                     
            this.aWindowObject.wBody.innerHTML = messageboxBody;
            			
            // --- Displaying MessageBox Window --- //
            this.aWindowObject.display();
            this.aWindowObject.show();
            
            if(this.isConfirm)
            {
            	str = this.geturl ;
            	 // --- Set MessageBox btnClose onclick Handler --- //
                this.wBtnOk                  = document.getElementById(this.id + '_wBtnOk');
                this.wBtnOk.currentWinObject  = this.aWindowObject;
                this.wBtnOk.onclick           = function(){
                	if (str == "confirmDeleteMedia")
                	{	             		
                		this.currentWinObject.close();
                		deleteMedia();
                	}
                	else if (str == "infor")
                	{
                		this.currentWinObject.close();
                		alert('close');
                	}
                	else
                	{
                		this.currentWinObject.close();
                		window.location = str;
                	}
                };
            }
            // --- 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 --- //
            if (!this.yOffset)
            	this.yOffset = 0;
            this.aWindowObject.initScreenPosition(this.yOffset);
            this.aWindowObject.initDragAndDrop();
        }        
    }   
