
//////////////////////////////////////
// DYNAMIC MENU
///////////////////////////////////////
////////////////////////
// Menu Object Constructor
////////////////////////
function Menu(whichdiv){
    this.speed = 1;
    this.limit = 0;
    
    ////////////////////////
    // Function to control the acceleration of the menu
    ////////////////////////
    function speedUp(){
	    this.speed = this.speed + 5;
    }
    
    ///////////////////////
    // Function to Change the direction and reset the speed
    ///////////////////////
    function setDirection(updown){
        if (this.direction > 0){
            this.direction = 0 - updown;
        }
        else {
            this.direction = updown;
        }
	    this.speed = 1;
    }
    
    ///////////////////////
    // Returns the height of the menu object
    ///////////////////////
    function getHeight(){
        return parseInt(document.getElementById(whichdiv).offsetHeight);
    }
    
    ////////////////////////
    // Used during intialization to find the maximum height of the menu
    ////////////////////////
    function setLimit(newlimit){
        if (newlimit > 0){
            this.limit = newlimit;
        }
        else {
            this.limit = this.getHeight;
        }
    }
    
    //////////////////////////
    // Set the height of the menu, then accelerate.
    ///////////////////////////
    function doGrow(){
        if (setHeight(whichdiv,this.direction * this.speed,this.limit)){
            this.speedUp();
            return true;
        }
        else {
            return false;
        }
    }
    
    this.setLimit = setLimit;
    this.speedUp = speedUp;
    this.setDirection = setDirection;
    this.getHeight = getHeight;
    this.doGrow = doGrow;
}

////////////////////////
// Set the height of the menu.
////////////////////////
function setHeight(div,change,limit){
    var thediv = document.getElementById(div);
    var oldheight = parseInt(thediv.offsetHeight);
    if (oldheight + change < 2){
        thediv.style.height = 1 + 'px';
        thediv.style.display = 'none';
        return false;
    }
    else if (oldheight + change > limit){
        thediv.style.height = limit + 'px';
        return false;
    }
    else {
        thediv.style.display = 'block';
        thediv.style.height = oldheight + change + 'px';
        return true;
    }
}

function hideMenu(div){
    var thediv = document.getElementById(div);
    thediv.style.height = '1px';
    thediv.style.display = 'none';
}

////////////////////
// Close the given menu
////////////////////
function closeMenu(whichDiv){
    document.getElementById(whichDiv).style.height = '0px';
    document.getElementById(whichDiv).style.display = 'none';
}

////////////////////////////////
// Checks to see whats open.
// This is probably the reason why sometimes more than one menu can open
////////////////////////////////
function findOpen(){
    for (m in menuArray){
        if (menuArray[m].getHeight() > 2){
            return m;
        }
    }
    return -1;
}

///////////////////////////
// These menu grow functions could be consolidated into 1, but I haven't done this.
// Makes sure that no other menu is moving before openning another one.
//////////////////////////
function menu1grow(){
    if (moving == 0 || moving == 1){
        if(menu1.doGrow()){
            moving = 1;
            menu1timeout = setTimeout("menu1grow();",50);
        }
        else {
            moving = 0;
            clearTimeout(menu1timeout);
        }
    }
    else {
        menu1timeout = setTimeout("menu1grow();",50);
    }
}

function menu2grow(){
    if (moving == 0 || moving == 2){
        if(menu2.doGrow()){
            moving = 2;
            menu2timeout = setTimeout("menu2grow();",50);
        }
        else {
            moving = 0;
            clearTimeout(menu2timeout);
        }
    }
    else {
        menu2timeout = setTimeout("menu2grow();",50);
    }
}

function menu3grow(){
    if (moving == 0 || moving == 3){
        if(menu3.doGrow()){
            moving = 3;
            menu3timeout = setTimeout("menu3grow();",50);
        }
        else {
            moving = 0;
            clearTimeout(menu3timeout);
        }
    }
    else {
        menu3timeout = setTimeout("menu3grow();",50);
    }
}

function menu4grow(){
    if (moving == 0 || moving == 4){
        if(menu4.doGrow()){
            moving = 4;
            menu4timeout = setTimeout("menu4grow();",50);
        }
        else {
            moving = 0;
            clearTimeout(menu4timeout);
        }
    }
    else {
        menu4timeout = setTimeout("menu4grow();",50);
    }
}

function menu5grow(){
    if (moving == 0 || moving == 5){
        if(menu5.doGrow()){
            moving = 5;
            menu5timeout = setTimeout("menu5grow();",50);
        }
        else {
            moving = 0;
            clearTimeout(menu5timeout);
        }
    }
    else {
        menu5timeout = setTimeout("menu5grow();",50);
    }
}


//////////////////////////////
// Closes any open menu items
// Then opens the menu clicked on
/////////////////////////////
function growMenu(nextMenu){
    createCookie('menu',nextMenu);
    
    var currentMenu = findOpen();
    
    if (nextMenu - currentMenu == 1){growNext(nextMenu);}
    else if (currentMenu == 0){menu1.setDirection(closeSpeed);menu1grow();growNext(nextMenu);}
    else if (currentMenu == 1){menu2.setDirection(closeSpeed);menu2grow();growNext(nextMenu);}
    else if (currentMenu == 2){menu3.setDirection(closeSpeed);menu3grow();growNext(nextMenu);}
    else if (currentMenu == 3){menu4.setDirection(closeSpeed);menu4grow();growNext(nextMenu);}
    else if (currentMenu == 4){menu5.setDirection(closeSpeed);menu5grow();growNext(nextMenu);}
    else {
        growNext(nextMenu);
    }
    
}


function growNext(nextMenu){
    if (nextMenu == 1){menu1.setDirection(openSpeed);menu1grow();}
    else if (nextMenu == 2){menu2.setDirection(openSpeed);menu2grow();}
    else if (nextMenu == 3){menu3.setDirection(openSpeed);menu3grow();}
    else if (nextMenu == 4){menu4.setDirection(openSpeed);menu4grow();}
    else if (nextMenu == 5){menu5.setDirection(openSpeed);menu5grow();}
}


function grown(nextMenu){
    if (nextMenu == 1){menu1.setDirection(openSpeed);menu1grown();}
    else if (nextMenu == 2){menu2.setDirection(openSpeed);menu2grown();}
    else if (nextMenu == 3){menu3.setDirection(openSpeed);menu3grown();}
    else if (nextMenu == 4){menu4.setDirection(openSpeed);menu4grown();}
    else if (nextMenu == 5){menu5.setDirection(openSpeed);menu5grown();}
}

function redirect(href)
{
    document.location.href = href;
}

function menu1grown(){
     document.getElementById('submenu1').style.height = menu1.limit+'px';
     document.getElementById('submenu1').style.display = "";
}

function menu2grown(){
     document.getElementById('submenu2').style.height = menu2.limit+'px';
     document.getElementById('submenu2').style.display = "";
}

function menu3grown(){
     document.getElementById('submenu3').style.height = menu3.limit+'px';
     document.getElementById('submenu3').style.display = "";
}

function menu4grown(){
     document.getElementById('submenu4').style.height = menu4.limit+'px';
     document.getElementById('submenu4').style.display = "";
}

function menu5grown(){
     document.getElementById('submenu5').style.height = menu5.limit+'px';
     document.getElementById('submenu5').style.display = "";
}


