﻿/*
Javascript WebWindow control
*/

var WebWindow = Class.create();

WebWindow.prototype =
{
    // Constructor
    initialize : function(piTitle)
    {
        // Declare variable holding the TabControl's window title
        this.Title = '';
        // Set the window title if provided
        if((piTitle != null) && (!piTitle.blank()))
        {
            this.Title = piTitle;
        }
        
        // Declare style variables
        this.Width;
        this.Height;
        this.CenterScreen = false;
        this.MakeDraggable = false;
        this.zIndex;
        this.Class = '';
        
        // Declare boolean value indicating if the element has been completely processed
        this._elemComplete = false;
        
        // Declare variable holding the event handler function for the close event
        this.OnClose;
        
        // Declare variable holding the WebContainer
        this._htmlContainer;
        // Declare variable holding the titlebar container
        this._titleBarContainer;
        // Declare variable holding the content container
        this._contentContainer;
        
        // Call Build function to build the html structure
        this.Build();
    },
    
    Build : function()
    {
        try
        {
            // Build new main container node
            this._htmlContainer = Builder.node('div', { style: 'background-color:#FFFFFF;' });
            this._htmlContainer.className  = 'WebWindow';
            
            // Build new title bar node
            this._titleBarContainer = Builder.node('div');
            this._titleBarContainer.className  = 'WebWindowTitleBar';
            
            // Create left title part
            var titlePartLeft = Builder.node('div');
            titlePartLeft.className = 'TitlePartLeft';        
            
            // Create right title part
            var titlePartRight = Builder.node('div');
            titlePartRight.className = 'TitlePartRight';
            
            // Append title parts to title bar background container
            this._titleBarContainer.appendChild(titlePartLeft);
            this._titleBarContainer.appendChild(titlePartRight);
            
            
            // Build new content holder node
            this._contentContainer = Builder.node('div', { style: 'border:1px solid #666666;' });
            this._contentContainer.className  = 'WebWindowContent';
            
            // Append title bar and content containers to the main html container
            this._htmlContainer.appendChild(this._titleBarContainer);
            this._htmlContainer.appendChild(this._contentContainer);
            
            // Create close button
            var closeBtnContainer = Builder.node('div', { style: 'float:right;margin-top:2px;' });
            var closeBtn = Builder.node('img', { src: 'Template/SYSTEM/Image/closeBtn.gif' });
            
            closeBtnContainer.appendChild(closeBtn);
            Event.observe(closeBtn, 'click', this.Close.bindAsEventListener(this));
            
            // Append close button container to the title bar
            this._titleBarContainer.appendChild(closeBtnContainer);
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'RequestData', arguments, 'SITE', this.Id, e, false)
        }
    },
    
    AddTitle : function()
    {
        try
        {
            // Check if a title has been provided and add the title to the title bar
            if(!this.Title.blank())
            {
                var titleSpan = Builder.node('div', { style: 'float:left;' });
                titleSpan.className = 'WebWindowTitle';
                titleSpan.appendChild(document.createTextNode(this.Title));
                this._titleBarContainer.appendChild(titleSpan);
            }
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'AddTitle', arguments, 'WEBWINDOW', null, e, false)
        }
    },
    
    ApplyStyling : function()
    {
        try
        {
            // Set the display style property
            this._htmlContainer.style.display = 'none';
            // Append the window to the document
            document.body.appendChild(this._htmlContainer);
            
            // Check if a class name has been provided for the content area
            if((this.Class != null) && (!this.Class.blank()))
            {
                this._contentContainer.className = this.Class;
            }
            
            // Check if a width has been provided and set the style attriute accordingly
            if(this.Width != null)
            {
                this._htmlContainer.style.width = this.Width + 'px';
            }
            
            // Check if a height has been provided and set the style attriute accordingly
            if(this.Height != null)
            {
                this._htmlContainer.style.height = this.Height + 'px';
            }
            
            // Check if the CenterScreen option has been enabled and set the style attriutes accordingly
            if((this.CenterScreen != null) && (this.CenterScreen == true))
            {
                // Get the window's scroll offset
                var scrollOffset = Helpers.GetWindowScrollOffset();
                // Calculate the value for the margin-top property
                var topMargin = (($(this._htmlContainer).getHeight() / 2) * (-1)) + scrollOffset.top;
                // Calculate the value for the margin-left property
                var leftMargin = (($(this._htmlContainer).getWidth() / 2) * (-1));
                
                this._htmlContainer.style.top = ((Helpers.GetWindowSize().height / 2) - ($(this._htmlContainer).getHeight() / 2) - 5) + scrollOffset.top + 'px';
                this._htmlContainer.style.position = 'relative';
                
                this._htmlContainer.style.margin = '0 auto';
            }
            
            // Check if the zIndex property has been filled and set styling accordingly
            if(this.zIndex != null)
            {
                this._htmlContainer.style.zIndex = this.zIndex;
            }
            
            // Remove the window from the document
            this._htmlContainer.parentNode.removeChild(this._htmlContainer);
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'ApplyStyling', arguments, 'WEBWINDOW', null, e, false)
        }
    },
    
    EnableDragging : function()
    {
        try
        {
            // Check if the MakeDraggable option has been enabled and create scriptacolous draggable
            if((this.MakeDraggable != null) && (this.MakeDraggable == true))
            {
                new Draggable(this._htmlContainer,{ handle: this._titleBarContainer});
            }
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'EnableDragging', arguments, 'WEBWINDOW', null, e, false)
        }           
    },
    
    GetHtml : function()
    {
        try
        {
            if(this._elemComplete == false)
            {
                // Call AddTitle method to set the title of the window
                this.AddTitle();
                // Call ApplayStyling method to applay the provided style settings to the window
                this.ApplyStyling();
                // Enable dragging if activated
                this.EnableDragging();
                
                this._elemComplete = true;
            }
            
            return this._htmlContainer;
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'GetHtml', arguments, 'WEBWINDOW', null, e, false)
        }
    },
    
    appendChild : function(piElem)
    {
        try
        {
            // Check if an html node has been provided and append it to the content holder
            if(piElem != null)
            {
                this._contentContainer.appendChild(piElem);
                
            }
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'appendChild', arguments, 'WEBWINDOW', null, e, false)
        }
    },
    
    Close : function()
    {
        try
        {
            if(this.OnClose != null)
            {
                try
                {
                    this.OnClose();
                }
                catch(ex)
                {
                }
            }
            
            var parent = this._htmlContainer.parentNode
            if(parent != null)
            {
                var func = function()
                {
                    parent.removeChild(this._htmlContainer);
                }
                
                window.setTimeout(func.bind(this), 800);
                new Effect.Fade(this._htmlContainer, { from: 1.0, to: 0.0, duration: 0.8 });
            }
        }
        catch(e)
        {
            Logs.WriteException('EXCEPTION_GENERAL', 'Close', arguments, 'WEBWINDOW', null, e, false)
        }
    }
}