/*
Hack Name: Drop Down Menus with Header Link Control

Hack Author: GuldantheWarlock (GuldantheWarlock@xmbgarage.com)

Last Updated: September 10, 2006

Hack Description:
This hack will allow you to specify sub-links for your header links through the admin panel.
This hack will display all sub-links in a JavaScript drop down menu under the specified link.
This hack also includes the features for Header Link Controls Advanced:
 - This hack will provide controls for the number of header links per line via Admin Panel -> Settings. 
 - This hack will will provide on/off controls for the link images in Admin Panel -> Settings. 
 - This hack will allow you to enter a separator for the links in Admin Panel -> Settings.

Supported Version: XMB 1.9.5 Nexus

Notes:

This Hack uses come code from Header Links Control Advanced by Adam Clarke (http://www.xmbservices.com/) and myself

This hack is released under the GPL. You should have recieved a copy of it with this hack.

Please backup your files before installing this hack. Neither XMBGarage nor the author can be held 
responsible if your board stops functioning properly due to you installing this hack.
*/

var headerLinks = new Array();
var headerSubmenus = new Array();
var hideTimeout;
var hideDelay   = 300;

function registerLoadFunc(func) {
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        oldOnLoad = window.onload;
        window.onload = function() {
            oldOnLoad();
            func();
        }
    }
}

function $(el) {
	return document.getElementById(el);
}

function getElementByClassName(class_name, parentObj) {
	parentObj = (typeof parentObj != 'undefined') ? parentObj : document;
	for(var i=0; i<parentObj.elements.length; i++) {
		if (parentObj.elements[i].className == class_name) {
			return parentObj.elements[i];
		}
	}
}

function extractId(obj) {
	if(obj.tagName == 'A') {
		return obj.className.split('-')[2];
	} else {
		return obj.id.split('-')[2];
	}
}

function initializeMenuItems() {
    docLinks = document.getElementsByTagName('a');
    //Load appropraite anchors into headerLinks
    var j = 0;
    for(var i = 0;i<docLinks.length;i++) {
	    if (docLinks[i].className.indexOf('header-link') > -1) {
		    headerLinks[j++] = docLinks[i];
	    }
    }
    
    for(i = 0; i<headerLinks.length; i++) {
	    headerSubmenus[i] = $('sub-links-'+extractId(headerLinks[i]));
	    headerSubmenus[i].onmouseover = function() { clearTimeout(hideTimeout); };
	    headerSubmenus[i].onmouseout  = function() { delayHide(extractId(this)); };
	    headerLinks[i].onmouseover = function() { showMenu(this, extractId(this)); };
	    headerLinks[i].onmouseout  = function() { delayHide(extractId(this)); };
    }
}

function showMenu(obj, id) {
	hideAllMenus();
	menuDiv = $('sub-links-'+id);
	menuDiv.style.display = '';
	menuDiv.x = getPosOffset(obj, 'left');
	menuDiv.y = getPosOffset(obj, 'top');
	menuDiv.style.left = menuDiv.x - clearBrowserEdge(obj, 'rightedge')+'px';
	menuDiv.style.top  = menuDiv.y - clearBrowserEdge(obj, 'bottomedge') + obj.offsetHeight+'px';
}

function hideMenu(id) {
	document.getElementById('sub-links-'+id).style.display = 'none';
}

function hideAllMenus() {
	for(var i=0; i<headerSubmenus.length; i++) {
		headerSubmenus[i].style.display = 'none';
	}
}

function delayHide(id) {
	hideTimeout = setTimeout("hideMenu("+id+");", hideDelay);
}

function retrieveMenuObj(linkObj) {
	parts = linkObj.className.split('-');
	return document.getElementById('sub-links-'+parts[2]);
}

function getPosOffset(obj, offsetType) {
    var totaloffset = (offsetType == 'left') ? obj.offsetLeft : obj.offsetTop;
    var parentEl    = obj.offsetParent;
    while(parentEl != null) {
        totaloffset = (offsetType == 'left') ? totaloffset + parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
        parentEl    = parentEl.offsetParent;
    }
    
    return totaloffset;
}

function clearBrowserEdge(obj, edge){
    var offSet  = 0;
    var menuObj = retrieveMenuObj(obj);
    
    if (edge == 'rightedge') {
        var windowedge = (document.all && !window.opera) ? document.body.scrollLeft + document.body.clientWidth - 15 : window.pageXOffset + window.innerWidth - 15;
        menuObj.contentmeasure = menuObj.offsetWidth;
        if (windowedge - menuObj.x < menuObj.contentmeasure) {
            offSet = menuObj.contentmeasure - obj.offsetWidth;
        }
    } else {
        var topedge = (document.all && !window.opera) ? document.body.scrollTop : window.pageYOffset;
        var windowedge = (document.all && !window.opera) ? document.body.scrollTop + document.body.clientHeight - 15 : window.pageYOffset + window.innerHeight - 18;
        menuObj.contentmeasure = menuObj.offsetHeight;
        if (windowedge - menuObj.y < menuObj.contentmeasure) { //move up?
            offSet = menuObj.contentmeasure + obj.offsetHeight;
            if ((menuObj.y - topedge) < menuObj.contentmeasure) { //up no good either?
                offSet = menuObj.y + obj.offsetHeight - topedge;
            }
        }
    }
    
    return offSet;
}

function checkHLForm(message) {
	if (typeof headerLinkIDs == 'undefined' || headerLinkIDs.length == 0) {
		return true;
	}
	
	var message = (message.length) ? message : '';
	
	var delChecked = false;
	for(var i = 0;i<headerLinkIDs.length;i++) {
		headerLink = document.getElementById('delete'+headerLinkIDs[i]);
		if (headerLink.checked == true) {
			delChecked = true;
		}
	}
	
	return (delChecked == true) ? confirm(hlDelConfirm) : true;
}

registerLoadFunc(initializeMenuItems);