// varibles that are accessed across multiple functions
mainLogo = null; // main logo object, all other parts of page relate to this
aboutButton = gameButton = contributeButton = contactButton = null; // objects of the main menu's buttons
subMenuDiv = mainBodyDiv = null; // the divs for the parts of the screen
buttonOrder = null; // an array of what order the buttons should be in
subButtonOrder = null; // an array of the order in which the sub menu buttons display
screenMoving = null;

// called when the page loads, sets the possitions of the main menu buttons
function initMainButtons()
{
    // get the handel to the logo, this is the top of the work space
    mainLogo = document.getElementById("mainLogo");

    // get the handels to the buttons of the main menu
    aboutButton = document.getElementById("aboutButton");
    gameButton = document.getElementById("gameButton");
    contributeButton = document.getElementById("contributeButton");
    contactButton = document.getElementById("contactButton");

    // get the display div's
    subMenuDiv = document.getElementById("subMenuDiv");
    mainBodyDiv = document.getElementById("mainBodyDiv");

    // the order in which all the buttons should line up
    buttonOrder = new Array("about", "game", "contact");

    // sub menu starts on about menu and has about us as the top option
    subButtonOrder = new Array("aboutUs", "aboutPolicy");

    // we have to move the buttons into place
    continueMoverMainButton();
}

// called when a main menu button is clicked on
function hitMainButton(_button, _subButton)
{
    clearTimeout(screenMoving);

    // setup the default sub menu order
    switch (_button)
    {
        case "about":
            subButtonOrder = new Array("aboutUs", "aboutPolicy");
            break;
        case "game":
            subButtonOrder = new Array("gameAbout", "gameBuy", "gameScreenShots", "gameVideo");
            break;
        case "contact":
            subButtonOrder = new Array("contactEmail", "contactMail");
            break;
        default:
            return;
    }

    // if there has been passed a sub menu
    if (_subButton != null)
    {
           // loop through the order array an re-insert the requested button as the first one
        hitVal = false;
        for (var i=subButtonOrder.length-1; i>=1; i--)
        {
            if (subButtonOrder[i] == _subButton || hitVal)
            {
                subButtonOrder[i] = subButtonOrder[i-1];
                hitVal = true;
            }
        }

        subButtonOrder[0] = _subButton; // top button will be this one now
    }
    
    // hitting the main menu main button resets the sub menu to defaults
    if (buttonOrder[0] == _button)
    {
        continueSubMenuButton();
        return;
    }

    // divs shouldn't be shown yet
    subMenuDiv.style.width = "0px";
    subMenuDiv.style.height = "0px";
    mainBodyDiv.style.width = "0px";
    mainBodyDiv.style.height = "0px";

    // loop through the order array an re-insert the requested button as the first one
    var hitVal = false;
    for (i=buttonOrder.length-1; i>=1; i--)
    {
        if (buttonOrder[i] == _button || hitVal)
        {
            buttonOrder[i] = buttonOrder[i-1];
            hitVal = true;
        }
    }

    buttonOrder[0] = _button; // leftmost button becomes this one
    screenMoving = true;    // screens are moving around
    continueMoverMainButton(); // buttons start moving
}

// called periodically untill the buttons have been rearranged after a button is pressed
function continueMoverMainButton()
{
    var hOffset = 20, vSpacing = 15, moveVal = 0.25, lockSize=5;

    // get the possitions of all the buttons and the main logo
    var mainLogoPos = findPos(mainLogo);

    // move buttons so there tops are aligned with the bottom of the logo
    document.getElementById("aboutButton").style.top = mainLogoPos.bot+hOffset;
    gameButton.style.top = mainLogoPos.bot+hOffset;
    contactButton.style.top = mainLogoPos.bot+hOffset;

    // get dimentions and possitions of buttons
    var aboutButtonPos = findPos(aboutButton);
    var gameButtonPos = findPos(gameButton);
    var contactButtonPos = findPos(contactButton);

    var leftPoint = mainLogoPos.left;
    var allSet = true;

    // loop list of where buttons should place on the menu bar
    for (var i=0; i<buttonOrder.length; i++)
    {
        switch(buttonOrder[i])
        {
            case "about":
                // is button effectivly finished moving
                if (aboutButtonPos.left<(leftPoint+lockSize) && aboutButtonPos.left>(leftPoint-lockSize))
                    aboutButton.style.left = leftPoint+"px";
                else
                {
                    // move button and show that there are still moves to make
                    aboutButton.style.left = (aboutButtonPos.left+((leftPoint-aboutButtonPos.left)*moveVal))+"px";
                    allSet = false;
                }

                // the next button needs to line to the left of this one
                leftPoint += aboutButtonPos.width+vSpacing;
                break;
            case "game":
                // is button effectivly finished moving
                if (gameButtonPos.left<(leftPoint+lockSize) && gameButtonPos.left>(leftPoint-lockSize))
                    gameButton.style.left = leftPoint+"px";
                else
                {
                    // move button and show that there are still moves to make
                    gameButton.style.left = (gameButtonPos.left+((leftPoint-gameButtonPos.left)*moveVal))+"px";
                    allSet = false;
                }

                // the next button needs to line to the left of this one
                leftPoint += gameButtonPos.width+vSpacing;
                break;
            case "contact":
                // is button effectivly finished moving
                if (contactButtonPos.left<(leftPoint+lockSize) && contactButtonPos.left>(leftPoint-lockSize))
                    contactButton.style.left = leftPoint+"px";
                else
                {
                    // move button and show that there are still moves to make
                    contactButton.style.left = (contactButtonPos.left+((leftPoint-contactButtonPos.left)*moveVal))+"px";
                    allSet = false;
                }

                // the next button needs to line to the left of this one
                leftPoint += contactButtonPos.width+vSpacing;
                break;
        }
    }

    if (allSet)
        startSubMenuDisplay();
    else
        screenMoving = setTimeout("continueMoverMainButton();", 40);   // continue moving buttons
}
