﻿/***
Filename:       nisa-yui-extensions.js
Date:           02/11/2007
Author:         James Condliffe
Description:    Creates the Nisa namespace
                NisaPanel - extends the YUI Panel object
                NisaTreeView - extends the YUI TreeView object
***/


// Create an empty object to act as a namespace
var Nisa = function(){};

// Constants for the Nisa object
Nisa.WEBSERVICE_URI = "webservice/webservice.asmx/";

// Internal properties
Nisa.memberId;


(function()
{
    /**
    * NisaPanel is an implementation of the YUI Panel which adds resizing
    * and other functionality.
    *
    * @namespace Nisa
    * @class NisaPanel
    * @extends YAHOO.widget.Panel
    * @constructor
    * @param {String} el The element ID representing the Panel <em>OR</em>
    * @param {HTMLElement} el The element representing the Panel
    * @param {Object} userConfig The configuration object literal containing 
    * the configuration that should be set for this Panel. See configuration 
    * documentation for more details.
    */
    Nisa.NisaPanel = function(el, userConfig)
    {
	    if (arguments.length > 0)
	    {
		    Nisa.NisaPanel.superclass.constructor.call(this, el, userConfig);
	    }
    }

    // Constants for the resizing functionality
    Nisa.NisaPanel.CSS_PANEL_NISA = "NisaPanel";
    Nisa.NisaPanel.CSS_RESIZE_HANDLE = "resizehandle";

    // Internal properties
    Nisa.NisaPanel.moduleID;


    // NisaPanel inherits from Panel
    YAHOO.extend(Nisa.NisaPanel, YAHOO.widget.Panel, {

        initDefaultConfig: function()
        {
            Nisa.NisaPanel.superclass.initDefaultConfig.call(this);
            
            // Configuration property: True if the panel can be resized. Defaults to true.
            this.cfg.addProperty("resizeable", {value: true});
            
            // Configuration property: ID of an alternative close widget
            this.cfg.addProperty("closeButtonID", {value: ""});
            
            // Configuration property: Cancel webservice notifications
            this.cfg.addProperty("notifyWebservice", {value: true});
        },

        init: function(el, userConfig)
        {
            Nisa.NisaPanel.superclass.init.call(this, el);
         
            var Dom = YAHOO.util.Dom,
                Event = YAHOO.util.Event,
                oInnerElement = this.innerElement,
                oResizeHandle = document.createElement("DIV"),
                sResizeHandleId = this.id + "_resizehandle";

            oResizeHandle.id = sResizeHandleId;
            oResizeHandle.className = Nisa.NisaPanel.CSS_RESIZE_HANDLE;
        
            Dom.addClass(oInnerElement, Nisa.NisaPanel.CSS_PANEL_NISA);
        
            this.resizeHandle = oResizeHandle;
        
            function initResizeFunctionality()
            {
                // If the NisaPanel is resizable
                if(this.cfg.getProperty("resizeable") == true)
                {
                    var me = this,
                        oHeader = this.header,
                        oBody = this.body,
                        oFooter = this.footer,
                        nStartWidth,
                        nStartHeight,
                        aStartPos,
                        nBodyBorderTopWidth,
                        nBodyBorderBottomWidth,
                        nBodyTopPadding,
                        nBodyBottomPadding,
                        nBodyOffset;    
            
                    oInnerElement.appendChild(oResizeHandle);        
                    this.ddResize = new YAHOO.util.DragDrop(sResizeHandleId, this.id);
                    this.ddResize.setHandleElId(sResizeHandleId);

                    this.ddResize.onMouseDown = function(e)
                    {        
                        nStartWidth = oInnerElement.offsetWidth;
                        nStartHeight = oInnerElement.offsetHeight;
            
                        if (YAHOO.env.ua.ie && document.compatMode == "BackCompat") 
                        {
                            nBodyOffset = 0;
                        }
                        else
                        {       
                            nBodyBorderTopWidth = parseInt(Dom.getStyle(oBody, "borderTopWidth"), 10),
                            nBodyBorderBottomWidth = parseInt(Dom.getStyle(oBody, "borderBottomWidth"), 10),
                            nBodyTopPadding = parseInt(Dom.getStyle(oBody, "paddingTop"), 10),
                            nBodyBottomPadding = parseInt(Dom.getStyle(oBody, "paddingBottom"), 10),                        
                            nBodyOffset = nBodyBorderTopWidth + nBodyBorderBottomWidth + nBodyTopPadding + nBodyBottomPadding;                    
                        }
            
                        me.cfg.setProperty("width", nStartWidth + "px");
                        aStartPos = [Event.getPageX(e), Event.getPageY(e)];        
                    };
                    
                    this.ddResize.onDrag = function(e) 
                    {
                        var aNewPos = [Event.getPageX(e), Event.getPageY(e)],                    
                            nOffsetX = aNewPos[0] - aStartPos[0],
                            nOffsetY = aNewPos[1] - aStartPos[1],
                            nNewWidth = Math.max(nStartWidth + nOffsetX, 10),
                            nNewHeight = Math.max(nStartHeight + nOffsetY, 10),
                            nBodyHeight = (nNewHeight - (oFooter.offsetHeight + oHeader.offsetHeight + nBodyOffset));
            
                        me.cfg.setProperty("width", nNewWidth + "px");
            
                        if (nBodyHeight < 0)
                        {        
                            nBodyHeight = 0;        
                        }
            
                        oBody.style.height =  nBodyHeight + "px";        
                    };
                    
                    this.ddResize.onMouseUp = function(e)
                    {
                        me.panelStateChangeUpdater("resize",{});
                    }                
                
                }
            
            }
        
            function onBeforeShow() 
            {    
               initResizeFunctionality.call(this);
               this.unsubscribe("beforeShow", onBeforeShow);
            }    
        
            function onBeforeRender() 
            {    
                if (!this.footer) 
                {    
                    this.setFooter("");    
                }
        
                if (this.cfg.getProperty("visible"))
                {
                    initResizeFunctionality.call(this);
                }
                else
                {
                    this.subscribe("beforeShow", onBeforeShow);
                }
                
                this.unsubscribe("beforeRender", onBeforeRender);
                
            }
            
            if(this.cfg.getProperty("resizeable") == true)
            {
                this.subscribe("beforeRender", onBeforeRender);
            }            
            
            // Determine the panel's module id and store it in the relevant property
            this.moduleID = parseInt(this.id.substr(5));
            
            
            
            // Apply the user-supplied configuration properties
            if (userConfig)
            {
                this.cfg.applyConfig(userConfig, true);
            }            
            
            // Hook up any custom close button
            var customClose = YAHOO.util.Dom.get(this.cfg.getProperty("closeButtonID"));
            if(customClose)
            {
                YAHOO.util.Event.addListener(customClose, "click", this.hide, this, true);
            }
            
            // Subscribe the webservice if necessary
            if(this.cfg.getProperty("notifyWebservice"))
            {
                this.subscribe("move", this.panelStateChangeUpdater);
                this.subscribe("hide", this.panelStateChangeUpdater);
            }

        },
        
        /***
        Updates the backend with the panel's new state
        ***/
        panelStateChangeUpdater: function(e,o)
        {
            // If the Nisa panels are still initialising, cancel execution
            if(Nisa.initialisingPanels)
            {
                return;
            }  
                
            try
            {
                switch(e)
                {                  
                    case "move":
                    case "resize":

                        var operation = "UpdateModuleLocation";
                        var xPos = parseInt(this.cfg.getProperty("x"));
                        var yPos = parseInt(this.cfg.getProperty("y"));
                        var width = parseInt(YAHOO.util.Dom.getStyle(this.element.firstChild,"width"));
                        var height = parseInt(YAHOO.util.Dom.getStyle(this.body,"height"));
                        var z = parseInt(this.cfg.getProperty("zIndex"));

                        var originalXPos;
                        var originalYPos;                                  
                                  
                        var postdata = "MemberId=" + Nisa.memberId + "&ModuleId=" + this.moduleID + 
                            "&X=" + xPos + "&Y=" + yPos + "&Z=" + z + "&Width=" + width + "&Height=" + height;
                            
                        YAHOO.util.Connect.asyncRequest("POST", Nisa.WEBSERVICE_URI + operation, null, postdata);
                        
                        break;
                        
                    case "hide":
                        
                        var operation = "CloseModuleLocation";
                        var postdata = "MemberId=" + Nisa.memberId + "&ModuleId=" + this.moduleID;

                        YAHOO.util.Connect.asyncRequest("POST", Nisa.WEBSERVICE_URI + operation, null, postdata);
                        
                        break;
                        
                    default:
                        console.log(e);
                       
                }
            }
            catch(e)
            {
                console.log(e);
            }    
            
        }

    });
    
})();