// JavaScript Document
//awCMUsePanelMenu = true;
var navBarRef; //the navbar object
var subMenuRef;
var noOfTopMenus;
var hiddenMenuDiv;

function awCMPanelOnLoad() {
    // set references to save looking them up repeatedly
    navBarRef = document.getElementById('navigation');
    subMenuRef = document.getElementById('subMenu');

    // have not been able to set the margin value in css
    // so the variable awCMNavBarLeftMargin is setup in the script
    navBarRef.style.marginLeft = awCMNavBarLeftMargin;


    if (navBarRef.addEventListener) {
        navBarRef.addEventListener("mouseout", outBar, true);
        subMenuRef.addEventListener("mouseout", outBar, true);
    }
    else if (navBarRef.attachEvent) {
        navBarRef.attachEvent('onmouseout', outBar);
        subMenuRef.attachEvent('onmouseout', outBar);
    }

    hiddenMenuDiv = document.getElementById('awHiddenMenus');
    noOfTopMenus = hiddenMenuDiv.childNodes.length;
    //alert(noOfTopMenus);
}

function awCalcMenuDimensions() {

    // get the hidden table for each menu        
    if (hiddenMenuDiv == null) {
            hiddenMenuDiv = document.getElementById('awHiddenMenus');
        }
    if (noOfTopMenus == null) {
        noOfTopMenus = hiddenMenuDiv.childNodes.length;
    }
    for (i = 0; i < noOfTopMenus; i++) {
        var oneMenuTable = hiddenMenuDiv.childNodes[i].childNodes[0];
        awSubRows[i] = oneMenuTable.rows.length;
        awSubColumns[i] = oneMenuTable.rows[0].cells.length;
    }
}

function getPositionByName(oneDivName) {
    var oneDivObject = document.getElementById(oneDivName);
    var offset = getPosition(oneDivObject);
    return offset;
}

function getPosition(oneDivObject) {
    var e = oneDivObject;
    var offset = { x: 0, y: 0 };
    while (e) {
        offset.x += e.offsetLeft;
        offset.y += e.offsetTop;
        e = e.offsetParent;
    }

    if (document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft)) {
        
	if ( awCMUsePanelMenu == true ){
		offset.x -= document.documentElement.scrollLeft;
	 	offset.y -= document.documentElement.scrollTop;
	}
    }
    else if (document.body && (document.body.scrollTop || document.body.scrollLeft)) {
     
        if ( awCMUsePanelMenu == true ){
	       offset.x -= document.body.scrollLeft;
	       offset.y -= document.body.scrollTop;
	}
    }
    else if (window.pageXOffset || window.pageYOffset) {     
        if ( awCMUsePanelMenu == true ) {
        offset.x -= window.pageXOffset;
	offset.y -= window.pageYOffset;
	}
    }

    return offset;
}

function awCMPanelKillAll() {
    document.getElementById('subMenu').style.display = 'none';
    if (noOfTopMenus == null) {
        if (hiddenMenuDiv == null) {
            hiddenMenuDiv = document.getElementById('awHiddenMenus');
        }
        noOfTopMenus = hiddenMenuDiv.childNodes.length;
    }
    for (i = 0; i < noOfTopMenus; i++) {
        var thisTopMenu = document.getElementById('Menu' + i);

        thisTopMenu.childNodes[0].className = 'awCMPanelLi';
    }
}

function awCMPanelOver(listElement) {
    awCMPanelKillAll();
    // debug variables
    var debugCalc = '';

    // get the screen width
    var screenMaxWidth = getWidth();

    

    // get the menu number
    var menuNumber = listElement.id;
    menuNumber = menuNumber.replace('Menu', '');

    listElement.childNodes[0].className = 'awCMPanelLiOver';

    // calculate the left margin - should be aligned around the tab
    var tempMargin = 0;
    var listItemOffset = getPosition(listElement);
    var navBarOffset = getPositionByName('menuList');
    if (navBarRef == null) {
        navBarRef = document.getElementById('navigation');
    }

    tempMargin = listItemOffset.x - navBarOffset.x + parseInt(navBarRef.style.marginLeft);

    // display the subMenu (so we can calculate it's width)
    // check we have the refereence
    if (subMenuRef == null) {
        subMenuRef = document.getElementById('subMenu');
    }
    
    subMenuRef.style.display = 'block';
    
    // now display the hidden submenus for the selected menu
    subMenuRef.innerHTML = document.getElementById('awHiddenSub' + menuNumber).innerHTML;

    
    // move it back bay half the width of the subMenu - half the width of the topmenu (ish - slightly right for style)
    // uses awCMPanelGap from the script (based on DropMenu Gap setting)
    tempMargin -= (subMenuRef.offsetWidth - listElement.offsetWidth - awCMPanelGap) / 2;

    // ensure the margin is greater than zero 
    tempMargin = Math.max(0, tempMargin);

    //ensure it fits on the screen ie margin + suubmenu width < total width
    screenMaxWidth = parseInt(screenMaxWidth) - navBarOffset.x + parseInt(navBarRef.style.marginLeft) - subMenuRef.offsetWidth;
    while ((tempMargin > 0) && (tempMargin > screenMaxWidth)) {
        tempMargin -= 1;
    }
    if (isNaN(tempMargin)) tempMargin = 0;
    subMenuRef.style.marginLeft = tempMargin + "px";
}

function getWidth() {
    var x = 0;
    if (self.innerHeight) {
        x = self.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientHeight) {
        x = document.documentElement.clientWidth;
    }
    else if (document.body) {
        x = document.body.clientWidth;
    }
    return x;
}

function outBar(e) {

    var subMenuOffset = getPositionByName('subMenu');
    var navBarOffset = getPositionByName('menuList');

    // make IE do events the W3C way
    if (!e) { var e = window.event; }

    // get the id of the event raiser (target) 
    var targ;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;

    // test the left margin
    var testBounds = false;
    var thisOffset = subMenuOffset;
    if (subMenuRef == null) {
        subMenuRef = document.getElementById('subMenu');
    }

    var thisWidth = subMenuRef.offsetWidth; //subMenuOffset.x; // parseInt(subMenuRef.style.width);
    var thisHeight = subMenuRef.offsetHeight; // subMenuOffset.y; parseInt(subMenuRef.style.height);
    var inNavBar = false;

    // are we in the navbar?
    if (targ.id == 'menuList') {
        thisOffset = navBarOffset;
        if (navBarRef == null) {
            navBarRef = document.getElementById('navigation');
        }
        thisWidth = navBarRef.offsetWidth;
        inNavBar = true;
    }

    // are we off the left hand edge?
    if (e.clientX < thisOffset.x) {
        testBounds = true;
    }

    // are we off the rigth hand edge?
    if (e.clientX >= thisOffset.x + subMenuRef.offsetWidth)//thisWidth )
    {
        testBounds = true;
    }

    // are we off the top?
    if (e.clientY < navBarOffset.y) {
        testBounds = true;
    }

    // are we off the bottom
    if (e.clientY >= (subMenuOffset.y + thisHeight)) {
        testBounds = true;
    }

    // if we were in the navbar and below the navbar and not within the sub menu
    if (inNavBar && (e.clientY <= subMenuOffset.y) && ((e.clientX <= subMenuOffset.x) || (e.clientX >= subMenuOffset.x + thisHeight))) {
        testBounds = true;
    }

    if (testBounds == true) {
        awCMPanelKillAll();
    }
}

/* *********************************************************************************** */

/*********************************************************************
* Get an object, this function is cross browser
* *** Please do not remove this header. ***
* This code is working on my IE7, IE6, FireFox, Opera and Safari
* 
* Usage: 
* var object = get_object(element_id);
*
* @Author Hamid Alipour Codehead @ webmaster-forums.code-head.com		
**/
function get_object(id) {
    var object = null;
    if (document.layers) {
        object = document.layers[id];
    } else if (document.all) {
        object = document.all[id];
    } else if (document.getElementById) {
        object = document.getElementById(id);
    }
    return object;
}
/*********************************************************************/

/*********************************************************************
* No onMouseOut event if the mouse pointer hovers a child element 
* *** Please do not remove this header. ***
* This code is working on my IE7, IE6, FireFox, Opera and Safari
* 
* Usage: 
* <div onMouseOut="fixOnMouseOut(this, event, 'JavaScript Code');"> 
*		So many childs 
*	</div>
*
* @Author Hamid Alipour Codehead @ webmaster-forums.code-head.com		
**/
function is_child_of(parent, child) {
    if (child != null) {
        while (child.parentNode) {
            if ((child = child.parentNode) == parent) {
                return true;
            }
        }
    }
    return false;
}
function fixOnMouseOut(element, event, scriptName) {
    var current_mouse_target = null;
    if (event.toElement) {
        current_mouse_target = event.toElement;
    } else if (event.relatedTarget) {
        current_mouse_target = event.relatedTarget;
    }
    if (!is_child_of(element, current_mouse_target) && element != current_mouse_target) {
        //eval(JavaScript_code);
        runMouseOut(element, event, scriptName)
    }
}

function runMouseOut(element, event, scriptName) {
    switch (scriptName) {
        case 'awCMPane':
            awColumnMenuHide(element)
            break;
        case 'awCMPaneParent':
            //alert('Parent');
            var thisMenuId = element.id.replace('awCMWindowMenu', 'awCMLargeWindowMenu');
            thisMenuId = thisMenuId.replace('awCMPaneMenu', 'awCMLargeWindowMenu');
            var thisMenu = document.getElementById(thisMenuId);
            var toElement = event.relatedTarget || event.toElement;
            if (toElement == null) {
                awColumnMenuHide(thisMenu);
                break;
            }

            if (toElement.id != thisMenuId) {
                if (!is_child_of(thisMenu, toElement) && (thisMenu != toElement)) {
                    awColumnMenuHide(thisMenu);
                }
            }
            break;
    }
}




/* *********************************************************************************** */
function awColumnMenuHide(thisMenuObject) {
    thisMenuObject.style.display = 'none';
}

function awColumnPaneExpand(thisPaneMenu) {
    var thisMenuId = thisPaneMenu.id.replace('awCMPaneMenu', 'awCMWindowMenu');
    awColumnMenuExpand(document.getElementById(thisMenuId));
}

function awColumnMenuExpand(thisMenuObject, additionalXOffset, additionalYOffset) {

	if (additionalXOffset === undefined){
		additionalXOffset = 0;
	}
        if (additionalYOffset === undefined){
		additionalYOffset = 0;
	}	
    // get the paneNumber from the id

    var thisMenuId = thisMenuObject.id.replace('awCMLargeWindowMenu', '');

    thisMenuId = thisMenuId.replace('awCMWindowMenu', '');
    //alert(thisMenuId);
    //var windowPane = document.getElementById('awCMWindowMenu' + thisMenuId);	

    var largeWindowPane = document.getElementById('awCMLargeWindowMenu' + thisMenuId);
    //alert(largeWindowPane.id);

    // debug
    //alert(thisMenuObject.id);
    largeWindowPane.style.display = 'block';
    // end debug

    var windowPaneOffset = getPosition(thisMenuObject);

    //alert(windowPaneOffset.y);
    largeWindowPane.style.marginLeft = (windowPaneOffset.x + additionalXOffset) + 'px';
    largeWindowPane.style.marginTop = (windowPaneOffset.y + additionalYOffset - 2) + 'px';
    largeWindowPane.style.display = '';
}

