//Class will apply a dropdown for given parent/dropdown elements
//(The actual type of element is irrelevant)
var currentMenuObj = null;
var currentSubMenuObj = null;
var bgzDDEnabled = true;

bgzDropDown = function( ){
    //back reference to anchor object
    this.parentObj = null;
    this.menuObj = null;

    this.isDropped = false;
    this.cDirection = 0;//current direction - 0 (inactive), -1 (closing), 1 (opening)
    this.hoverState = 0;//0 (not hovering), 1 (hovering)

    this.slideDirection = 'down';//down or right

    this.startAlpha = .4;
    this.endAlpha = 1;//tweak with a combination of css and js

    this.menuWidth = 250;//match this to the style sheet (only used on slide right menus).
    this.menuHeight = 200;//if it hits 200, it switches the height to auto then stops

    this.zIndexClose = 1000;
    this.zIndexOpen= 2000;

    this.delay = 400;

    //careful with speed, each menu thinks at this interval.
    this.speed = 50;


    this.widthIncrement = 64;
    this.heightIncrement = 48;
    this.alphaIncrement = .08;
}

bgzDropDown.prototype = {

init: function(parentObj, menuObj, slideDirection){
    this.parentObj = parentObj;
    this.menuObj = menuObj;

    if(!this.parentObj || !this.menuObj){ return; }

    this.slideDirection = slideDirection;

    this.parentObj.vars = this;
    this.parentObj.onmouseover = mOpen;
    this.parentObj.onmouseout = mClose;

    //set it in motion
    bgzDropThink(this.parentObj.id);

}

}

function mOpen(e){
    if(this.vars.slideDirection == 'down'){
        if(currentMenuObj){
            currentMenuObj.vars.menuObj.style.zIndex = this.vars.zIndexClose;
            currentMenuObj.vars.cDirection = -1;
        }

        currentMenuObj = this;
        currentMenuObj.vars.menuObj.style.zIndex = this.vars.zIndexOpen;
    }
    else{
        if(currentSubMenuObj){
            currentSubMenuObj.vars.menuObj.style.zIndex = this.vars.zIndexClose;
            currentSubMenuObj.vars.cDirection = -1;
        }

        currentSubMenuObj = this;
        currentSubMenuObj.vars.menuObj.style.zIndex = this.vars.zIndexOpen;
    }

    this.vars.cDirection = 1;
    this.vars.hoverState = true;

    return false;
}

function mClose(e){
    //dont set the direction right away, give it a little time.
    this.vars.hoverState = false;
    setTimeout("mSetDrop('" + this.id + "')", this.vars.delay);

    return false;
}

function mSetDrop(objID){
    var obj = document.getElementById(objID);
    var vars = obj.vars;

    if(vars){
        if(!vars.hoverState){
            vars.cDirection = -1;
        }
    }
}

function bgzDropThink(objID){
    var obj = document.getElementById(objID);
    var vars = obj.vars;

    if(vars.cDirection != 0){
        if(vars.menuObj.style.display != 'block'){
            vars.menuObj.style.display = 'block';
            vars.menuObj.style.opacity = 0;
            if(vars.slideDirection == 'right'){
                vars.menuObj.style.width = '0px';
            }
            else if(vars.slideDirection == 'down'){
                vars.menuObj.style.height = '0px';
            }
        }


        var nextWidth;
        var nextHeight;
        var curWidth = parseInt(vars.menuObj.style.width);
        var curHeight = parseInt(vars.menuObj.style.height);

        var nextAlpha;
        var curAlpha = parseFloat(vars.menuObj.style.opacity);

        if(!curAlpha){
            curAlpha = vars.startAlpha;
        }

        var scaleDone = false;
        var alphaDone = false;

        if(vars.slideDirection == 'right'){
            if(curWidth == ''){
                curWidth = 0;
            }

            nextWidth = curWidth + vars.cDirection * vars.widthIncrement;

            //alert(curWidth + ' - ' + vars.cDirection + ' - ' + vars.widthIncrement + ' - ');

            if(vars.cDirection > 0){
                if(nextWidth >= vars.menuWidth){
                    vars.menuObj.style.width = vars.menuWidth + "px";
                    scaleDone = true;
                }
                else{
                    vars.menuObj.style.width = nextWidth + "px";
                }
            }
            else{
                if(nextWidth <= 0){
                    vars.menuObj.style.width = "0px";
                    vars.menuObj.style.opacity = 0;
                    vars.menuObj.style.display = 'none';

                    if(document.all){
                        vars.menuObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
                    }
                    scaleDone = true;
                }
                else{
                    vars.menuObj.style.width = nextWidth + "px";
                }
            }
        }
        else if(vars.slideDirection == 'down'){
            if(curHeight == ''){
                curHeight = 0;
            }

            nextHeight = curHeight + vars.cDirection * vars.heightIncrement;

            if(vars.cDirection > 0){
                if(vars.menuObj.clientHeight && vars.menuObj.scrollHeight){
                    if(nextHeight >= vars.menuObj.scrollHeight){
                        //fallback to something that should be supported
                        vars.menuObj.style.height = vars.menuObj.scrollHeight + "px";
                        scaleDone = true;
                    }
                    else{
                        vars.menuObj.style.height = nextHeight + "px";
                    }
                }
                else if(nextHeight >= vars.menuHeight){
                    //fallback to something that should be supported
                    vars.menuObj.style.height = vars.menuHeight + "px";
                    scaleDone = true;
                }
                else{
                    vars.menuObj.style.height = nextHeight + "px";
                }
            }
            else{
                if(nextHeight <= 0){
                    vars.menuObj.style.height = "1px";

                    if(!window.opera){
                        //weird opera rendering bug happens when it runs this.
                        vars.menuObj.style.opacity = 1;
                        vars.menuObj.style.display = 'none';
                    }

                    if(window.ActiveXObject){
                        vars.menuObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
                    }
                    scaleDone = true;
                }
                else{
                    vars.menuObj.style.height = nextHeight + "px";
                }
            }
        }

        nextAlpha = curAlpha + vars.cDirection * vars.alphaIncrement;

        if(vars.cDirection > 0){
            if(nextAlpha >= vars.endAlpha){
                vars.menuObj.style.opacity = vars.endAlpha;
                if(document.all){
                    vars.menuObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + parseInt(vars.endAlpha * 100) + ")";
                }

                alphaDone = true;
            }
            else{
                vars.menuObj.style.opacity = nextAlpha;
                if(document.all){
                    vars.menuObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + parseInt(nextAlpha * 100) + ")";
                }
            }
        }
        else{
            if(nextAlpha <= vars.startAlpha){
                vars.menuObj.style.opacity = vars.startAlpha;
                if(document.all){
                    vars.menuObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
                }

                alphaDone = true;
            }
            else{
                vars.menuObj.style.opacity = nextAlpha;
                if(document.all){
                    vars.menuObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + parseInt(nextAlpha * 100) + ")";
                }
            }
        }



        if(alphaDone && scaleDone){
            vars.cDirection = 0;
        }
    }

    setTimeout("bgzDropThink('" + objID + "')", vars.speed);
}