/*
    Revision: $Rev: 113 $
    Date:     $Date: 2009-08-28 10:29:26 +0100 (Fri, 28 Aug 2009) $
    Author:   $Author: Business $
    
    Copyright © 2009, Datrim Web Design Ltd. All rights reserved.
*/

YAHOO.namespace("datrim");

YAHOO.datrim.PageLoad = function() {
    /**
     * Compares two or more number of number values and returns the largest. Takes any number of arguments, 
     * but they must all be numbers as no type-checking is performed. This may be added later.
     * 
     * returns the argument with the largest value or 0 if no arguments were passed.
     */
    function getLargestValue() {
        return Math.max.apply(Math, arguments);
    }

    var glossaryWindow;
    var popupWindow;
    
    function openGlossaryWindow(href, target, width, height) {
        if (glossaryWindow && !glossaryWindow.closed)
        {
            glossaryWindow.location = href;
        }
        else
        {
            glossaryWindow = window.open(href, target, 'resizable,width=' + width + ',height=' + height);
        }
        glossaryWindow.focus();
    }
    
    function openPopupWindow(href, target, width, height) {
        if (popupWindow && !popupWindow.closed)
        {
            popupWindow.location = href;
        }
        else
        {
            popupWindow = window.open(href, target, 'resizable,width=' + width + ',height=' + height);
        }
        popupWindow.focus();
    }
   

    /**
     * The returned object here will be assigned to YAHOO.datrim.PageLoad and its members will then be 
     * publicly available.
     */
    return {
        
        /**
         * Sets things up by telling the DOM to call the adjustPanels function when the page has loaded.
         * Also sets up listeners for the search form input control.
         * 
         */
        init: function() {
            // Assign onDOMReady handler.
            YAHOO.util.Event.onDOMReady(this.adjustPanels);
        },

        /**
         * Called when the document has loaded. Sets all panels to match the height of the tallest one. Also
         * sets the position of and displays the main panel's background image. Currently, this function can 
         * handle 2 or 3 panels.
         * 
         * returns none.
         */
        adjustPanels: function() {
            var dom  = YAHOO.util.Dom;
            var lang = YAHOO.lang;
            
            var pageLoad = YAHOO.datrim.PageLoad;
            
            var searchText = dom.get('search-txt');
            
            if (!lang.isNull(searchText)) {
                // Hijack the search form text input control.
                YAHOO.util.Event.addFocusListener(searchText, pageLoad.searchFocus);
                YAHOO.util.Event.addBlurListener(searchText, pageLoad.searchBlur);
            }

            // Add listeners for all glossary items on page.            
            dom.getElementsByClassName('glossaryItem', null, null, pageLoad.setGlossaryListener);
            
            // Add listeners for all popup items on page.            
            dom.getElementsByClassName('popupItem', null, null, pageLoad.setPopupListener);            
            
			var lowerPanel = dom.get('lower-image');
            
			if (lang.isNull(lowerPanel))
			{
            	var lPanel = dom.get('nav');
            	var cPanel = dom.get('bd');
            	var rPanel = dom.get('right-sidebar');
				
				// Set page min height to the left panel + 40px.
				var minPanelHeight = lPanel.offsetHeight + 40;
				if (minPanelHeight < 290) {
					minPanelHeight = 290;
				}
				var panelHeight  = 0;
				
				if (lang.isNull(rPanel)) {    
					panelHeight += getLargestValue(lPanel.offsetHeight, cPanel.offsetHeight);
				}
				else {
					panelHeight += getLargestValue(lPanel.offsetHeight, cPanel.offsetHeight, rPanel.offsetHeight);
				}
				
				if (panelHeight < minPanelHeight) {
					panelHeight = minPanelHeight;
				}
							
				// Set all panels to same height.            
				var heightString = panelHeight + 'px';
				dom.setStyle(lPanel, 'height', heightString);
				dom.setStyle(cPanel, 'height', heightString);
				
				if (!lang.isNull(rPanel)) {
					dom.setStyle(rPanel, 'height', heightString);
				}
				
				var ft = dom.get('footer');
				
				if (!lang.isNull(ft)) {
					// Display the background image at the bottom of the main panel.
					var bgPos = '0px ' + (panelHeight - 284) + 'px';
					dom.setStyle(cPanel, 'background', 'url(/_src/images/footer-winter.png) no-repeat bottom left');
					dom.setStyle(cPanel, 'backgroundPosition', bgPos);
				}
			}
        },
        
        searchFocus: function(e) {
            YAHOO.util.Event.preventDefault(e);
            
            if (this.value === 'Search') {
                this.value = '';
            }
        },
        
        searchBlur: function(e) {
            YAHOO.util.Event.preventDefault(e);
            
            if (this.value === '') {
                this.value = 'Search';
            }
        },
        
        /**
         * Called when a glossary link is clicked.
         * @param {Object} e - unused.
         */
        onGlossary: function(e) {
            YAHOO.util.Event.preventDefault(e);
            
            var href = this.href;
            
            if (!YAHOO.lang.isNull(href)) {
                openGlossaryWindow(href, 'glossary', 670, 510);
            }
        },
        
        /**
         * Called when a popup link is clicked.
         * @param {Object} e - unused.
         */
        onPopup: function(e) {
            YAHOO.util.Event.preventDefault(e);
            
            var href = this.href;
            
            if (!YAHOO.lang.isNull(href)) {
                var winHeight = YAHOO.util.Dom.getAttribute(this, 'popupHeight'); 
                
                if (!YAHOO.lang.isNull(winHeight)) {
                    winHeight = parseInt(winHeight, 10);
                } else {
                    winHeight = 460;
                }
                openPopupWindow(href, 'glossary', 520, winHeight);
            }
        },
        
        /**
         * Adds a listener for a glossary item.
         * @param {Object} glossaryItem glossary hyperlink element.
         */
        setGlossaryListener: function(glossaryItem) {
            YAHOO.util.Event.addListener(glossaryItem, 'click', YAHOO.datrim.PageLoad.onGlossary);
        },

        /**
         * Adds a listener for a popup item.
         * @param {Object} glossaryItem popup hyperlink element.
         */
        setPopupListener: function(popupItem) {
            YAHOO.util.Event.addListener(popupItem, 'click', YAHOO.datrim.PageLoad.onPopup);
        }
    };
}();

YAHOO.datrim.PageLoad.init();
