//Class will apply a dropdown for given parent/dropdown elements
//(The actual type of element is irrelevant)
var currentMenuObj = new Array( );
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.scaleDone = false;
    this.alphaDone = false;

    this.slideDirection = 'down';//down or right
    this.navigation_depth = 0;

    this.startAlpha = 1;
    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 = 0;

    //careful with speed, each menu thinks at this interval.
    this.speed = 70;


    this.widthIncrement = 64;
    this.heightIncrement = 20;
    this.alphaIncrement = .08;
}

bgzDropDown.prototype = {

init: function(parentObj, menuObj, slideDirection, eventType){
    this.parentObj = parentObj;
    this.menuObj = menuObj;
    //alert(parentObj.id);
    //alert(menuObj.id);

    if(!this.parentObj || !this.menuObj){ return; }

    this.slideDirection = slideDirection;

    if(this.parentObj.parentNode.id.match(/lm_sub_\d+_\d+/)){
        idA = this.parentObj.parentNode.id.split('_');
        this.navigation_depth = idA[3];
        //alert(parentObj.id + ' ' + this.navigation_depth);
    }

    this.parentObj.vars = this;
    if(eventType != null && eventType == 'click'){
        this.parentObj.onclick = mOpen;
    }
    else{
        this.parentObj.onmouseover = mOpen;
        this.parentObj.onmouseout = mClose;
    }

    //set it in motion
    bgzDropThink(this.parentObj.id);

}

}

function mOpen(e){
    var depth = this.vars.navigation_depth;
    if(this.vars.slideDirection == 'down'){
        if(currentMenuObj[depth] && currentMenuObj[depth].id != this.id){
            currentMenuObj[depth].vars.menuObj.style.zIndex = this.vars.zIndexClose;
            currentMenuObj[depth].vars.cDirection = -1;

            t_parent_id = currentMenuObj[depth].id;
            for(var i = depth; i < 10; i++){
                if(!currentMenuObj[i]){ continue; }

                //alert('t_parent_id: ' + t_parent_id);
                //alert('currentMenuObj.parent.id: ' + currentMenuObj[i].parentNode.parentNode.id);
                //alert('main id: ' + currentMenuObj[depth].id);
                //alert('-------------');

                //if(currentMenuObj[i].parentNode.parentNode.id == t_parent_id && currentMenuObj[i].parentNode.parentNode.id != currentMenuObj[depth].id){
                    //alert('here');
                    currentMenuObj[i].vars.menuObj.style.zIndex = this.vars.zIndexClose;
                    currentMenuObj[i].vars.cDirection = -1;
                    t_parent_id == currentMenuObj[i].id;

                    var nodes = currentMenuObj[i].getElementsByTagName("*");
                    if(window.ActiveXObject){
                        nodes = currentMenuObj[i].all;
                    }

                    for(var j = 0; j < nodes.length; j++){
                        var node = nodes[j];
                        if(node.vars && node.vars.menuObj){
                            node.vars.menuObj.style.zIndex = this.vars.zIndexClose;
                            node.vars.cDirection = -1;
                        }
                    }
                //}
            }
        }

        currentMenuObj[depth] = this;
        currentMenuObj[depth].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;
    this.vars.cDirection = -1;

    return false;
}

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;
        }

        if(vars.slideDirection == 'right'){
            if(curWidth == ''){
                curWidth = 0;
            }

            nextWidth = curWidth + vars.cDirection * vars.widthIncrement;

            if(vars.cDirection > 0){
                if(nextWidth >= vars.menuWidth){
                    vars.menuObj.style.width = vars.menuWidth + "px";
                    vars.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)";
                    }
                    vars.scaleDone = true;
                }
                else{
                    vars.menuObj.style.width = nextWidth + "px";
                }
            }
        }
        else if(vars.slideDirection == 'down'){
            if(curHeight == ''){
                curHeight = 0;
            }

            nextHeight = curHeight + vars.cDirection * vars.heightIncrement;

            //alert(objID + ' -> ' + 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";
                        vars.menuObj.style.height = 'auto';
                        vars.scaleDone = true;
                    //}
                    //else{
                    //    vars.menuObj.style.height = nextHeight + "px";
                    //}
                //}
                //else if(nextHeight >= vars.menuHeight){
                //else{
                    //fallback to something that should be supported
                //    vars.menuObj.style.height = vars.menuHeight + "px";
                //    vars.scaleDone = true;
                //}
                //else{
                //    vars.menuObj.style.height = nextHeight + "px";
                //}
            }
            else{
                //if(vars.menuObj.clientHeight && vars.menuObj.scrollHeight){
                //    if(nextHeight <= 1){
                        vars.menuObj.style.height = "0px";
                        vars.scaleDone = true;
                //    }
                //    else{
                //        vars.menuObj.style.height = nextHeight + "px";
                //    }
                //}
                //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) + ")";
                }

                vars.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)";
                }

                vars.alphaDone = true;
            }
            else{
                vars.menuObj.style.opacity = nextAlpha;
                if(document.all){
                    vars.menuObj.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + parseInt(nextAlpha * 100) + ")";
                }
            }
        }

        if(vars.alphaDone && vars.scaleDone){
            if(vars.cDirection < 0){
                vars.menuObj.style.display = 'none';
            }

            vars.cDirection = 0;
            vars.alphaDone = false;
            vars.scaleDone = false;
        }
    }
    else if(vars.cDirection == 0 && vars.slideDirection == 'down' && vars.menuObj.style.height != vars.menuObj.scrollHeight + 'px'){
        //make sure this is exactly correct all of the time
        vars.menuObj.style.height = vars.menuObj.scrollHeight + "px";
    }

    setTimeout("bgzDropThink('" + objID + "')", vars.speed);
}