var _activePanels = new Object();

function mrPanel() {
    this.id = null;
    this.style = null;
    this.expandMode = null;
    this.panelHeight = null;
    this.expandList = null;
    this.expandSpeed = 10;
    this.activate = function() {
                        _mrActivatePanel(this.id, this.style, this.expandMode, this.panelHeight, this.expandList,
                                          this.expandSpeed); } ;
}

function _mrExpandCollapse(e, sPanelId) {
    var o = (e ? e : window.event)[window.event ? 'srcElement' : 'target'].parentNode; //get LI node
    var nRate = 10;
    var sExpandId = null;
    var sCollapseId = null;

    if (_activePanels[sPanelId].rate) nRate = _activePanels[sPanelId].rate;

    if (o._expandMode.indexOf('single') != -1) {
        // oulook mode only a sigle group canbe expanded at one time, previous group must be collpased
        if (_activePanels[sPanelId].id == o.id) return;

        document.getElementById(_activePanels[sPanelId].id).className += ' collapse';
        sCollapseId = _activePanels[sPanelId].id;
        sExpandId = o.id;
            o.className = o.className.replace('collapse', '');
        
        //alert( 'Expand: ' + sExpandId + ' Collapse: ' + sCollapseId );
        mrSlideObj(sExpandId, sCollapseId, nRate)
        
    } else {
        if (o.className.indexOf('collapse') == -1) {
            o.className += ' collapse';
            sCollapseId = o.id;
        } else {
            o.className = o.className.replace('collapse', '');
            sExpandId = o.id;
        }

        mrSlideObj(sExpandId, sCollapseId, nRate)
    }

    _activePanels[sPanelId].id = o.id;

    o.firstChild.blur();
    return false;
}

var _groupId = 0;

function _mrActivatePanel(sPanelId, sClassName, sExpandMode, sPanelHeight, sExpandList, nRate) {
    sExpandMode = (sExpandMode
                      || 'multi').toLowerCase(); //'SingleGroup:AutoHeight single:auto; SingleGroup:Height single:100px;  Multi-Group automatically auto height
    sExpandList = (sExpandList || '0').toLowerCase();
    sExpandList = ';' + sExpandList + ';';

    if (sExpandMode == 'single') {
        if (sExpandList.indexOf('all') != -1 || sExpandList.split(";").length == 0) sExpandList = ";0;";
    }

    var bExpanded = false;
    //sPanelHeight = sPanelHeight || null;

    var oPanel = document.getElementById(sPanelId);
    var oUl = oPanel.getElementsByTagName("UL")[0];
    var l = oUl.childNodes.length;

    oPanel.className = sClassName
    oPanel.onselectstart = function() { return false; } ;
    var nGroupId=0;

    for (var i = 0; i < l; i++) {
        oLi = oUl.childNodes[i];

        if (oLi.nodeType == 1) {
            if (!oLi.id) oLi.id = 'g' + _groupId;

            oLi._expandMode = sExpandMode;
            var oChild = oLi.firstChild;
            oLi.className = 'group';
            var oGroup = oLi.getElementsByTagName('UL');

            if (oGroup) oGroup = oGroup[0];

            if (sExpandList != ';all;' && (bExpanded || sExpandMode.indexOf(
                                                            'single') != -1 && sExpandList.indexOf(
                                                                                   ';' + nGroupId + ';') == -1)) {
                oLi.className += ' collapse';
                oGroup.style.display = 'none';
            } else {
                 if ( ! _activePanels[sPanelId] )
                        _activePanels[sPanelId] = new Object();

                _activePanels[sPanelId].id = oLi.id;
                _activePanels[sPanelId].rate = nRate;
                bExpanded = true;
            }

            if (oChild.tagName) {
                oChild.className = 'header';
                oChild.onclick = function(event) { _mrExpandCollapse(event, sPanelId); } ;
            }
              nGroupId++;
            _groupId++;
        }

    //      alert( oLi.firstChild.innerHTML );  
    }

    if (sPanelHeight) {
        buttonHeight = 0;

        if (sPanelHeight == 'inherit') {
            document.getElementById(_activePanels[sPanelId].id).getElementsByTagName("UL")[0].style.display = 'none';
            
            oPanel.style.display = 'none';
            sPanelHeight = oPanel.parentNode.offsetHeight - oPanel.parentNode.scrollHeight;

           // alert( oPanel.parentNode.clientHeight + '  ' + oPanel.parentNode.offsetHeight + '  ' + oPanel.parentNode.scrollHeight);
        }

         oPanel.style.display = 'block';
               
         buttonHeight = oPanel.offsetHeight ;
        // alert( oPanel.scrollHeight );
     // alert( "PH: " +sPanelHeight + ' ParentTag:' + oPanel.parentNode.id + '  ButtonHeight:' + buttonHeight );

//        alert(sPanelHeight + ' ParentTag:' + oPanel.parentNode.id + '  ButtonHeight:' + buttonHeight);
        document.getElementById(_activePanels[sPanelId].id).getElementsByTagName("UL")[0].style.display = 'block';

        oPanel.style.height = sPanelHeight + 'px';


        var sGroupHeight = (parseInt(sPanelHeight) - buttonHeight) + 'px';
        //alert( sGroupHeight );
        oUls = oPanel.getElementsByTagName("UL");
        l = oUls.length;

        if (sExpandMode == 'single' && parseInt(sGroupHeight) > 50) {
            for (var i = 1; i < l; i++) {
                oUls[i].style.height = sGroupHeight;
                oUls[i].style.overflow = 'auto';
            }
        }
    } else oPanel.style.display = 'block';
        
}

function mrSlideObj(sExpandId, sCollapseId, nRate) {
    this.expandId = null;
    this.collapseId = null;

    if (sExpandId) this.expandId = document.getElementById(sExpandId).getElementsByTagName('UL')[0];

    if (sCollapseId) this.collapseId = document.getElementById(sCollapseId).getElementsByTagName('UL')[0];

    /* if opera or IE5 don't slide because of adverse affects */
    var bDontSlide = nRate == 10 || navigator.userAgent.indexOf('xOpera') != -1 || navigator.userAgent.indexOf('MSIE 5x') != -1;
    if (bDontSlide) {
        if (this.expandId) this.expandId.style.display = 'block';

        if (this.collapseId) this.collapseId.style.display = 'none';
        return;
    }

    this.interval = 100;
    this.rate = nRate || 4;

    if (this.expandId) {
        this.expandOverflow = this.expandId.style.overflow;
        this.expandId.style.overflow = 'hidden';
    }

    if (this.collapseId) {
        if (this.collapseId.style.height) this.collapseHeight = parseInt(this.collapseId.style.height);
        else this.collapseHeight = this.collapseId.offsetHeight;

        this.collapseOverflow = this.collapseId.style.overflow;
        this.collapseId.style.overflow = 'hidden';
    }

    //alert( this.rate );
    this._expandCollapse = function() {
        var percent;
        this.interval = this.interval - this.rate;

        if (this.interval > 5) {
            if (this.collapseId) {
                percent = this.interval / 100;
                this.collapseId.style.height = parseInt(this.collapseHeight * percent) + 1 + 'px';
            }

            if (this.expandId) {
                percent = (100 - this.interval) / 100;
                this.expandId.style.height = parseInt(this.expandHeight * percent) + 'px';
            }

            setTimeout(this._expandCollapse, 10);
        } else {
            if (this.expandId) {
                this.expandId.style.overflow = this.expandOverflow;
                this.expandId.style.height = this.expandHeight;
            }

            if (this.collapseId) {
                this.collapseId.style.overflow = this.collapseOverflow;
                this.collapseId.style.display = 'none';
                this.collapseId.style.height = this.collapseHeight;
            }
        }
    }

    if (this.expandId) {
        this.expandId.style.visibility = 'hidden';

        //alert( this.height );
        if (this.expandId.style.height) this.expandHeight = parseInt(this.expandId.style.height);
        else this.expandHeight = this.expandId.offsetHeight;

        this.expandId.style.visibility = 'visible';
        this.expandId.style.display = 'block';
    }

    this._expandCollapse();
}


