//do not edit this function
function cell(szName, szURL, pSubmenus)
{
    this.szName = szName;
    this.szURL = szURL;
    this.pSubMenus = pSubmenus;
    this.pParentCell = null;
    this.bRoot = false;
    if (this.pSubMenus != null)
        for (var i = 0; i < this.pSubMenus.length; i++)
            this.pSubMenus[i].pParentCell = this;
    
    return this;
}

var szMenuName = "begrudgedMenu";       //the id of the div representing the menu
var bHorizontal = true;					// whether the root level menus are listed horizontally


//end editables
//going past this point at your own risk xD

var objDivSubMenus = Array();
var cSubMenus = 0;
var baseMenu = null;
var arrTimerIDs = Array();
var arrTimerNames = Array();
var bReady = false;

//creates a menu object
function menu(szName, arrMenus, bCreateDiv)
{
    this.id = cSubMenus++;
    this.szName = szName;
    
    //fill it with code
    this.html = "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" class=\"tblMenu\">";
    
    var bRoot = false;
    if (arrMenus == null) {
        arrMenus = navLinks;
        bRoot = true;
    }
    
    if (bRoot && bHorizontal)
    	this.html += "<tr class=\"trMenu\">";
    
    for (var i = 0; i < arrMenus.length; i++)
        this.html += writeCell(arrMenus[i], bRoot);
    
    if (bRoot && bHorizontal)
    	this.html += "</tr>\n";
        
    this.html += "</table>";
    
    if (bCreateDiv)
    {
        //this.html = "<div class=\"divMenu\" width=\"100%\">" + this.html + "</div>";
        this.elem = document.createElement("div");
        this.elem.setAttribute("class", "divMenu");
        this.elem.className = "divMenu";
        this.elem.style.position = "absolute";
        this.elem.id = "menu" + this.id;
        this.elem.innerHTML = this.html;
        document.body.appendChild(this.elem);
        if (this.elem.parentElement == null)    //another stupid thing about mozilla
            this.elem.parentElement = document.body;
        
        MozillaFix(arrMenus);
    }
    
    objDivSubMenus.push(this);
    
    return this;
}

//god dumb mozilla doesn't realize onmouseover... f3
function MozillaFix(arrCells)
{
    if (document.all)   //yay... ie compatible
        return;
    
        
    //noooooooooooooooooooooooooooooooooooooooo
    for (var i = 0; i < arrCells.length; i++)
    {
        //alert("fixing: " + arrCells[i].szName);
        getCellTD(arrCells[i].szName).onmouseover = overProcFF;
        getCellTD(arrCells[i].szName).onmouseout = outProcFF;
    }
}

function writeCell(objCell, bRoot)
{
	if (bRoot == null) bRoot = false;
	
	var tdClass = (!bHorizontal || !bRoot) ? "tdMenu" : "tdRootMenu";
	var ret = "<td class=\"" + tdClass + "\" id=\"" +
        objCell.szName.replace("\"", "\\\"") + "\" onclick=\"javascript:clickProc(this);\" onmouseover=\"javascript:overProc(this);\" onmouseout=\"javascript:outProc(this);\">" + 
        objCell.szName + "</td>";
	
	if (!bHorizontal || !bRoot)
		ret = "<tr class=\"trMenu\">" + ret + "</tr>";
	
	return ret + "\n";
}

/*obsolete
 * function writeMenu(pParentMenu)
{
    var pSubMenus;
    if (pParentMenu == null)
        pSubMenus = navLinks;
    else
        pSubMenus = pParentMenu.pSubMenus;
        
    var output = "";

    for (var i = 0; i < pSubMenus.length; i++)
        output += writeCell(pSubMenus[i]);
        
    return output;
}*/

function writeBaseMenu()
{
	for (key in navLinks)
		navLinks[key].bRoot = true;
	
    if (document.all)
        baseMenu = document.all[szMenuName];
    else if (document.getElementById)
        baseMenu = document.getElementById(szMenuName);
        
    if (baseMenu)
    {
        var objBaseMenu = new menu("begrudgedMenu");
        baseMenu.innerHTML = objBaseMenu.html;
        MozillaFix(navLinks);
    }
    else
        alert("The Div that the menu will be based on cannot be found, please check your navigation.php.");
}

function getCell(szName, pSearchIn)
{
    if (pSearchIn == null)
        pSearchIn = navLinks;
                
    for (var i = 0; i < pSearchIn.length; i++)
    {
        if (pSearchIn[i].szName == szName)
            return pSearchIn[i];
        if (pSearchIn[i].pSubMenus != null)
        {
            var tempObj = getCell(szName, pSearchIn[i].pSubMenus);
            if (tempObj != null)
                return tempObj;
        }
    }
    
    return null;
}

function getDivObj(objCell)
{
    for (var i = 0; i < objDivSubMenus.length; i++)
        if (objCell.szName == objDivSubMenus[i].szName)
            return i;
            
    return -1;
}

function calAbsOffset(objElem, szParam)
{
    var result = 0;
    
    do
    {
        //alert(objElem.tagName + ", " + objElem.className + ", " + objElem.style.position + ", " + szParam + ", " + objElem[szParam]);
        result += objElem[szParam];
        if (objElem.style.position.toLowerCase() == "absolute")
            break;
        objElem = objElem.offsetParent;
    } while (objElem && objElem.style &&
        objElem.tagName.toLowerCase() != "body");
        
    return result;
}

//create and display the submenu, objCell is a cell class,
//objCellTD is a html object (td, class=tdMenu)
function createSubMenus(objCell, objCellTD)
{
    var objSubMenu;
    
    var index = getDivObj(objCell);
    if (index == -1)
        objSubMenu = new menu(objCell.szName, objCell.pSubMenus, true, objCellTD);
    else
        objSubMenu = objDivSubMenus[index];
    
    objSubMenu.elem.style.position = "absolute";
    if (!bHorizontal || (objCellTD.className != "tdRootMenu" && objCellTD.className != "tdRootMenuHover")) {
	    objSubMenu.elem.style.left = (calAbsOffset(objCellTD, "offsetLeft") + objCellTD.offsetWidth - 10) + "px";
	    objSubMenu.elem.style.top = calAbsOffset(objCellTD, "offsetTop") + "px";
    } else {
    	objSubMenu.elem.style.left = calAbsOffset(objCellTD, "offsetLeft") +
    		(objCellTD.offsetWidth - objSubMenu.elem.offsetWidth) / 2 + "px";
    	objSubMenu.elem.style.top = calAbsOffset(objCellTD, "offsetTop") + objCellTD.offsetHeight + "px";
    }
    objSubMenu.elem.style.visibility = "visible";
    objSubMenu.elem.style.cursor = "default";
}

function getTimerIndex(szName)
{
    for (var i = 0; i < arrTimerNames.length; i++)
        if (szName == arrTimerNames[i])
            return i;
    return -1;
}

function validElement(elem)
{
	return elem.className == "tdMenu" || elem.className == "tdMenuHover" ||
		elem.className == "tdRootMenu" || elem.className == "tdRootMenuHover";
}

function overProc(srcElement)
{
    if (validElement(srcElement))
    {
        if (!bReady)
            return;
            
        var targObj = getCell(srcElement.id);
       
        //clears all the setTimeouts related to that and every parent cell
        var tempObj = targObj;
        while (tempObj)
        {
            if (tempObj.pSubMenus != null) for (var i = 0; i < lastTimerName.length; i++)
                if (lastTimerName[i] == tempObj.szName)
                {
                    clearTimeout(lastTimerID[i]);
                    lastTimerName.splice(i, 1);
                    lastTimerID.splice(i, 1);
                }
            tempObj = tempObj.pParentCell;
        }
        
        if (targObj != null)
        {
            if (targObj.pSubMenus != null)
                createSubMenus(targObj, srcElement);
            var szClass = targObj.bRoot ? "tdRootMenuHover" : "tdMenuHover";
            //set the cell to have the class tdMenuHover
            srcElement.setAttribute("class", szClass);
            srcElement.className = szClass;
        }
    }
}

function RemoveDiv(index)
{
    objDivSubMenus[index].elem.parentElement.removeChild(
        objDivSubMenus[index].elem);
    objDivSubMenus.splice(index, 1);
}

function getCellTD(szName)
{
    if (document.all)
        return document.all[szName];
    else
        return document.getElementById(szName);
}

function deselect(szName)
{
    var objCell = getCell(szName);
    var objCellTD = getCellTD(szName);
    
    var szClass = "tdMenu";
    if (objCell.bRoot)
    	szClass = "tdRootMenu";
    
    objCellTD.setAttribute("class", szClass);
    objCellTD.className = szClass;
    
    if (objCell.pSubMenus)
        hideMenu(szName);
}

function hideMenu(szName)
{
    var targObj = getCell(szName);
    var index = getDivObj(targObj);
    //if (index > -1 && objDivSubMenus[index].elem.style.visibility == "visible")
    //    objDivSubMenus[index].elem.style.visibility = "hidden";
    if (index > -1)
        RemoveDiv(index);
                        
    for (var i = 0; i < lastTimerName.length; i++)
        if (szName == lastTimerName)
        {
            lastTimerID.splice(i, 1);
            lastTimerName.splice(i, 1);
        }
}

var lastTimerID = Array();
var lastTimerName = Array();

function outProc(srcElement)
{
    if (validElement(srcElement))
    {
        if (!bReady)
            return;
            
        var objCell = getCell(srcElement.id);
        do
        {
            var timer = setTimeout("deselect('" + objCell.szName + "');", 200);
            lastTimerID.push(timer);
            lastTimerName.push(objCell.szName);
            objCell = objCell.pParentCell;
        } while (objCell != null);
    }
}

function clickProc(srcElement)
{
    if (validElement(srcElement) && srcElement.id != "")
    {
        var objCell = getCell(srcElement.id);
        window.location.href = objCell.szURL;
    }
}

function overProcFF(event)
{
    if (bReady)
        overProc(event.target);
}

function outProcFF(event)
{
    if (bReady)
        outProc(event.target);
}

function onLoad()
{
    bReady = true;
}

//add load events, but be compatible with other scripts
var curOnLoad = window.onload;
if (typeof curOnLoad != 'function')
    window.onload = onLoad;
else
{
    window.onload = function() {
        if (curOnLoad)
            curOnLoad();
        onLoad();
    };
}

function newImage()
{
    var elem;
    
    if (document.all)
        elem = document.all.asimg;
    else
        elem = document.getElementById("asimg");
        
    if (!elem)
        return;
    
    /*var lastSlash = window.location.href.lastIndexOf('/');
    if (lastSlash == -1)
        return;
        
    var fileName = window.location.href.substr(lastSlash + 1, 
        window.location.href.length - lastSlash - 1);
    var locGet = fileName.indexOf('?');
    if (locGet != -1)
        fileName = fileName.substr(0, locGet);*/
    
    elem.src = "http://begrudged.org/includes/as.inc.php/?action=getasimg&forcereload=" + (Math.random() * 65535);
}

function fontSizeChange(dFontSize)
{
	if (!bReady)
	{
		alert("Please allow the page to load fully.");
		return;
	}

	var elem;
	if (document.all)
		elem = document.all.fontSizeChange;
	else
		elem = document.getElementById("fontSizeChange");

	elem.value = dFontSize;
	submitFS();
}

function submitFS()
{
	if (!bReady)
	{
		alert("Please allow the page to load fully.");
		return;
	}

	setURL();
	var elem = document.getElementById("fontSizeSel");
	elem.submit();
}

function selTheme(theme)
{
	var elem;
	if (document.all)
		elem = document.all.b_skin;
	else
		elem = document.getElementById("b_skin");

	if (!elem)
		alert("Please allow the page to load fully.");

	elem.value = theme;
	submitFS();
}

function setURL()
{
	var elem;
	if (document.all)
		elem = document.all.refer;
	else
		elem = document.getElementById("refer");

	elem.value = window.location.href;
}