//////////////////////////////////////
// DYNAMIC MENU
///////////////////////////////////////

// Menu Object Constructor
function vMenu(whichdiv,id,ts){
    this.id = id;
    this.moving = 0;
    this.speed = 1;
    this.direction = -1;
    this.timeout = null;
    this.nextMenu = -1;
    
    this.timeoutSpeed = (ts) ? ts : 50;

    this.htmlElementId = whichdiv;
    this.htmlElement = document.getElementById(whichdiv);
    this.tabElement = document.getElementById("menutab"+id);
    
    this.limit = this.htmlElement.offsetHeight;
    this.vHeight = this.htmlElement.offsetHeight;
    
    this.initMenu = function(dir){
        if (dir) this.setDirection(dir);
        this.openClose();
    }
        
    this.speedUp = function(){ this.speed = this.speed + 2; }
    
    this.setDirection = function(updown){
        this.direction = (this.direction > 0) ? (updown * -1) : updown;
	    this.speed = 1;
    }
    
    this.getHeight = function(){ return parseInt(this.htmlElement.offsetHeight); }
    
    this.setLimit = function(inLimit){ this.limit = inLimit; }
    
    this.doGrow = function(){
        this.setHeight(this.direction * this.speed);
        this.setHeightHTML();
        this.speedUp();

        if (this.vHeight == 1 || this.vHeight == this.limit) return false;
        return true;
    }

    this.setHeight = function(change){
        if (this.vHeight + change < 2 && change < 0) this.vHeight = 1;
        else if (this.vHeight + change > this.limit && change > 0) this.vHeight = this.limit;
        else this.vHeight = this.vHeight + change;
    }

    this.setHeightHTML = function(){
        this.htmlElement.style.height = this.vHeight + 'px';

        if (this.vHeight > 1) this.htmlElement.style.display = 'block';
        else this.htmlElement.style.display = 'none';
    }

    this.openClose = function(dir,inNextMenu){
        if (inNextMenu > -1) this.nextMenu = inNextMenu;
        if (dir){
            this.direction = dir;
            this.tabElement.className = 'menutabup';
        }

        if(this.doGrow()){
            this.timeout = setTimeout("vMenuArray["+this.id+"].openClose()",this.timeoutSpeed);
            return;
        }
        
        if (this.nextMenu != -1) setTimeout("vMenuArray["+this.nextMenu+"].openClose(1)",this.timeoutSpeed);
        this.nextMenu = -1;
        if (this.direction < 0){
            this.tabElement.className = 'menutabdown';
            vClosed++;
            if (vClosed == vMenuArray.length) moveVObject('menuposition','loadingmenu');
        }
        clearTimeout(this.timeout);
        return;
    }
}

function moveVObject(object,loading){
    var element = document.getElementById(object);
    element.style.position = "absolute";
    element.style.top = "auto";
    element.style.left = "auto";
    if (loading) document.getElementById(loading).style.display = "none";
}

function findOpenVMenu(){
    for (m = 0; m < vMenuArray.length; m++){ if (vMenuArray[m].getHeight() > 2) return m; }
    return -1;
}

function growVMenu(menuId){
    var currentMenu = findOpenVMenu();
    if (menuId == currentMenu) vMenuArray[menuId].openClose(-1);
    else if (currentMenu != -1) vMenuArray[currentMenu].openClose(-1,menuId);
    else vMenuArray[menuId].openClose(1);
}