﻿//----------------------------------------------------------
// Copyright (C) Textrix Solutions Inc. 2008
//----------------------------------------------------------
// PageMenuBar.js

/// <reference name="MicrosoftAjax.debug.js" />
/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />

Type.registerNamespace('Textrix.Cms.UI');

Textrix.Cms.UI.MenuBarBehavior = function(){    
    this._MenuBarElementID = null;
    this._WrapperElementID = null;
    this._ToolsZoneElementID = null;
    
    this._isIE = false;
    this._isIE6 = false;
    this._isFF = false;
    this._menuBarElement = null;
    this._wrapperElement = null;
    this._toolsZoneElement = null;
    this._toolsZonePosition = "Left";
    this._resizeHandler = null;
    this._isMenuScriptLoaded = false;
}
Textrix.Cms.UI.MenuBarBehavior.prototype = {
    initialize : function(){
        this._isIE = (Sys.Browser.agent == Sys.Browser.InternetExplorer);
        this._isIE6 = (Sys.Browser.agent == Sys.Browser.InternetExplorer && Sys.Browser.version < 7);
        this._isFF = (Sys.Browser.agent == Sys.Browser.Firefox);
        this._menuBarElement = $get(this._MenuBarElementID);
        this._wrapperElement = $get(this._WrapperElementID);
        this._toolsZoneElement = $get(this._ToolsZoneElementID);
        this._resizeHandler = Function.createDelegate(this, this.onLayoutChanged);
        this._isMenuScriptLoaded = false;
        
        // Hook-up the "resize" event to re-sizes and position
        // the UI element.
        $addHandler(window, 'resize', this._resizeHandler);
        //$addHandler(this._wrapperElement, 'scroll', this._resizeHandler);
        
        // This also needs to be done at the end of every
        // AJAX request.
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(this._resizeHandler);
        
        // The following removes the scrollbars in IE.
        // Note: This doesn't work when done within
        // the "onLayoutChanged" method.
        if(this._isIE){
            document.body.style.overflow = 'hidden';
            document.getElementsByTagName("html")[0].style.overflow = 'hidden';            
        }
        
        var outerWrapper = $get('outerWrapper');
        if(typeof(outerWrapper) == 'undefined' || outerWrapper == null){
            outerWrapper = document.createElement('div');
            var innerWrapper = this._wrapperElement;
            
            innerWrapper.parentNode.replaceChild(outerWrapper, this._wrapperElement);
            outerWrapper.appendChild(innerWrapper);
            outerWrapper.setAttribute('id', 'outerWrapper');
        }
        this._wrapperElement = outerWrapper;
        
        this.loadMenuScript();
        this.onLayoutChanged();
    },
    loadMenuScript : function(){
        if (this._isIE6 && (! this._isMenuScriptLoaded)){
            var item, itemList, anchors, lists;
            var menu = this._menuBarElement;
            itemList = menu.getElementsByTagName('li');
            
            for (var i=0; i<itemList.length; i++){
                item = itemList[i];
                lists = item.getElementsByTagName('ul');
                
                if (lists && lists.length){
                    item.UL = lists[0];
                    anchors = item.getElementsByTagName('a');
                    
                    if (anchors && anchors.length){
                        item.A = anchors[0];
                    }
                    
                    item.onmouseover = function(){
                        this.UL.style.visibility = 'visible';
                        this.UL.className += ' hoverUL';
                    };
                    
                    item.onmouseout = function(){
                        this.UL.style.visibility = 'hidden';
                        this.UL.className = this.UL.className.replace(/ hoverUL/, '');
                    };
                }
            }
            this._isMenuScriptLoaded = true;
        }
    },
    onLayoutChanged : function(e){
        var clientBounds = this._getClientBounds();
        var menuBounds = new Sys.UI.Bounds(0, 0, 0, 0);
        var toolsBounds = new Sys.UI.Bounds(0, 0, 0, 0);
                       
        if (this._menuBarElement){
            // The following causes an 'Unspecified Error' and
            // the work-around is to wrap the statement inside a try/catch
            // to suppress the error message. The error is generated from
            // within the Microsoft.Ajax library.  Although it throws an
            // error the page functions as expected.  This only happens
            // within IE 7.
            try {
                menuBounds = Sys.UI.DomElement.getBounds(this._menuBarElement);
            } catch(err) {}
            
            this._menuBarElement.style.position = 'absolute';
            this._menuBarElement.style.top = '0px';
            this._menuBarElement.style.left = '0px';
            this._menuBarElement.style.zIndex = '999999';
            
            if (this._toolsZoneElement){
                // See above.
                try {
                toolsBounds = Sys.UI.DomElement.getBounds(this._toolsZoneElement);
                } catch(err) {}
                
                this._toolsZoneElement.style.display = 'block';
                this._toolsZoneElement.style.position = 'absolute';
                if (this._toolsZonePosition == 'Left'){
                    this._toolsZoneElement.style.left = '0px';
                }else{    
                    this._toolsZoneElement.style.right = '0px';
                }
                this._toolsZoneElement.style.top = menuBounds.height + 'px';
                this._toolsZoneElement.style.width = toolsBounds.width + 'px';
                this._toolsZoneElement.style.height = (clientBounds.height - menuBounds.height) + 'px';
                this._toolsZoneElement.style.zIndex = '999998';
                this._toolsZoneElement.style.overflow = 'auto';
            }
            
            this._wrapperElement.style.display = 'block';
            this._wrapperElement.style.position = 'absolute';
            this._wrapperElement.style.top = menuBounds.height + 'px';
            
            if (this._toolsZonePosition == 'Left'){                
                this._wrapperElement.style.left = toolsBounds.width + 'px';
            }else{
                this._wrapperElement.style.left = '0px';
            }
            
            this._wrapperElement.style.width = (clientBounds.width - toolsBounds.width) + 'px';
            this._wrapperElement.style.height = (clientBounds.height - menuBounds.height) + 'px';
            this._wrapperElement.style.overflow = 'scroll';
        }
    },
    dispose : function(){},
    get_MenuBarElementID : function() {
        return this._MenuBarElementID;
    },
    set_MenuBarElementID : function(value){
        if (this._MenuBarElementID != value){
            this._MenuBarElementID = value;
        }
    },
    get_WrapperElementID : function(){
        return this._WrapperElementID;
    },
    set_WrapperElementID : function(value){
        if (this._WrapperElementID != value){
            this._WrapperElementID = value;
        }
    },
    get_ToolsZoneElementID : function(){
        return this._ToolsZonePanelID;
    },
    set_ToolsZoneElementID : function(value){
        if (this._ToolsZoneElementID != value){
            this._ToolsZoneElementID = value;
        }
    },
    get_ToolsZonePosition : function(){
        return this._toolsZonePosition;
    },
    set_ToolsZonePosition : function(value){
        if (this._toolsZonePosition != value){
            this._toolsZonePosition = value;
        }
    },    
    _getClientBounds : function(){
        var clientWidth;
        var clientHeight;
        if (!window.innerWidth){
            if (!document.documentElement.clientWidth == 0){
                clientWidth = document.documentElement.clientWidth;
                clientHeight = document.documentElement.clientHeight;
            }else{
                clientWidth = document.body.clientWidth;
			    clientHeight = document.body.clientHeight;
            }
        }else{
		    clientWidth = window.innerWidth;
		    clientHeight = window.innerHeight;
	    }
        return new Sys.UI.Bounds(0, 0, clientWidth, clientHeight);
    }
}

Textrix.Cms.UI.MenuBarBehavior.registerClass('Textrix.Cms.UI.MenuBarBehavior');

Sys.Application.notifyScriptLoaded();

Textrix.Cms.UI.MenuBarBehavior.Init = function(menuBarElementID, wrapperElementID, toolsZonePanelID, toolsZonePosition){
    var menuBehavior = new Textrix.Cms.UI.MenuBarBehavior();
    menuBehavior.set_MenuBarElementID(menuBarElementID);
    menuBehavior.set_WrapperElementID(wrapperElementID);
    menuBehavior.set_ToolsZoneElementID(toolsZonePanelID);
    menuBehavior.set_ToolsZonePosition(toolsZonePosition);
    menuBehavior.initialize();
}

//----------------------------------------------------------
// Add A Web Part Dialog
//----------------------------------------------------------
var catalogDialogID;
var catalogDialog;
var catalogDialogPostBackRef;
function InitWebPartCatalogDialog(dialogID){
    catalogDialogID = dialogID;
    catalogDialog = $find(dialogID);
}
function showWebPartCatalogDialog(ref){
    catalogDialogPostBackRef = ref;
    catalogDialog.show();
}
function webPartCatalogDialogCallBack(sender, typeName){
    eval(catalogDialogPostBackRef.replace('[[WEBPART]]', typeName));
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();