/* @Project: JavaScript TreeMenu  @Version: 1.0 (Stable) @Date: 28.03.2007 */
/* check if object exists */
if(typeof oTreeMenu == "undefined") {
        var oTreeMenu = {};
}

/* tree menu */
oTreeMenu = {

        /* register variable and settings */
        oRegister: function(oObj)
        {
                /* detect browser */
                this.isOPERA = (navigator.userAgent.indexOf('Opera') >= 0) ? true : false;
                this.isIE    = (document.all && !this.isOPERA) ? true : false;
                this.isDOM   = (document.getElementById && !this.isIE) ? true : false;
                this.oExpire = 365;
        
                /* check variable */
                if(oObj instanceof Object)
                {
                        /* items array */
                        var oTreeItem = new Array();
                        
                        for (i in oObj)
                                oTreeItem[oObj[i].n] = oObj[i].name;
                        
                        /* registered new variable */
                        this.oTreeItem = oTreeItem;
                }
                else
                        this.oAlert('Error \'oRegister::oObj\' type: ' + typeof(oObj));
                
                /* set display none to all subitems */
                /* for opera browsers */
                if (this.isDOM || this.isIE)
                {
                        document.writeln('<style type="text/css">');
                        document.writeln('.SubItemRow \{ display: none; \}');
                        document.writeln('</style>');
                  
                }
        },
        
        /* test new method */
        oCloseChild: function(oItem,oClose)
        {
                var item_ = new Array();
                var     oElem = document.getElementById(oItem);
                
                if(oElem.getAttribute('id') == 'oTreeMenu') return
                do
                {
                        if(typeof(oElem.getAttribute('id')) == "string")
                        {
                                /* IE fix (IE detected `null` as string ) */
                                if(oElem.getAttribute('id') != "")
                                        item_.push(oElem.getAttribute('id'));
                        }
                        oElem = oElem.parentNode;
                }
                while(oElem.getAttribute('id') != 'oTreeMenu');
                
                return item_.join(":"); 
        },
                
        
         
        /* parse selected item */
        oParseTree: function(oId)
        {
                var style_ = document.getElementById(this.oTreeItem[oId]).style.display;
                if(style_ == 'none')
                {
                        this.oShowHide(this.oTreeItem[oId]);
                        this.oSetCookie('oTreeMenuItem',this.oCloseChild(this.oTreeItem[oId]),this.oExpire);
                }
                else
                {
                        this.oShowHide(this.oTreeItem[oId]);
                        this.oSetCookie('oTreeMenuItem',this.oCloseChild(this.oTreeItem[oId],'close'),'close');
                }
        },
        
        /* show/hide selected item */
        oShowHide: function(oItem)
        {
                if(oItem != "")
                {
                        if(oItem != 'undefined')
                        {
                                var style_ = document.getElementById(oItem).style.display;
                                if(style_ != 'null')
                                {
                                      
                                        if(this.isDOM)  document.getElementById(oItem).style.display = (style_ == 'none')?'block':'none';
                                        else if(this.isIE)  document.all[oItem].style.display = (style_ == 'none')?'block':'none';
                                    
                                }
                        }
                }
                else
                        this.oAlert('Error \'oShowItem::oItem\' is: ' + typeof(oItem)); 
        },
        
        /* save cookie variables */
        oSetCookie: function(oName,oValue,oExp)
        {
                /* set expired time */
                this.oDate = new Date();
                this.oDate.setDate(this.oDate.getDate()+this.oExpire);
                document.cookie = oName + "=" + escape(oValue) + ( ( this.oExpire == null ) ? "" : "; expires=" + this.oDate.toGMTString() );
                //debug: this.oAlert( oName + "=" + escape(this.oNewCookie.join(":")) + ( ( this.oExpire == null ) ? "" : "; expires=" + this.oDate.toGMTString() ));
        },
        
        /* get cookie name */
        oGetCookie: function(oName)
        {
                if(document.cookie.length > 0)
                {
                        oStartPos = document.cookie.indexOf(oName + "=");
                        if(oStartPos != -1)
                        {
                                oStartPos = oStartPos + oName.length + 1 ;
                                oEnd = document.cookie.indexOf(";",oStartPos);
                                if(oEnd == -1)
                                        oEnd = document.cookie.length;
                                
                                return unescape(document.cookie.substring(oStartPos,oEnd));
                        }
                }
                return "";
        },
        
        /* re-render all items saved on cookie */
        oCookieItems: function()
        {
          return false; // by VS
                if(this.oGetCookie('oTreeMenuItem') != 'false')
                {
                        this.oSavedItem = this.oGetCookie('oTreeMenuItem');
                        this.CookieObj = this.oSavedItem.split(":");
                        this.CookieObj.reverse();
                        
                        for(var i=0; i<this.CookieObj.length; i++)
                        {
                                if(this.CookieObj[i] != "")
                                        this.oShowHide(this.CookieObj[i]);
                        }
                }       
        },

        /* for debug */ 
        oAlert: function(oStr)
        {
                alert(oStr);
        }
};

