/* @Project: JavaScript TreeMenu  @Version: 1.0 (Stable) @Date: 28.03.2007 */
/* check if object exists */
if(typeof oTreeMenu == "undefined") {
        var oTreeMenu = {};
}

/* tree menu */
oTreeMenu = {
        savedItems : [],
        /* register variable and settings */
        oRegister: function(oObj)
        {
            this.savedItems = [];
                /* 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_ = [];
                var     oElem = document.getElementById(oItem);
                if(oElem.getAttribute('id') == 'oTreeMenu') return;
                do
                {
                        if (oElem.getAttribute('id') == 'oTreeMenu') break;
                        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( true );
                
                return item_.join(":");
        },
                
        
         
        /* parse selected item */
        oParseTree: function(oId)
        {
            if (typeof(this.savedItems)=='undefined' || this.savedItems.length ==0) this.savedItems = [];
            var style_ = document.getElementById(this.oTreeItem[oId]).style.display;
                this.oShowHide(this.oTreeItem[oId]);
                var pId = this.oCloseChild(this.oTreeItem[oId]);
                if(style_ == 'none')
                {
                    this.savedItems.push(pId);
                    //this.SetCookie(pId);
                        //this.oSetCookie('oTreeMenuItem',this.oCloseChild(this.oTreeItem[oId]),this.oExpire);
                }
                else
                {
                    this.savedItems = ArrayHelper.Remove(this.savedItems, pId)                    
                    //this.DeleteCookie(pId);
                        //this.oSetCookie('oTreeMenuItem',this.oCloseChild(this.oTreeItem[oId],'close'),'close');
                }
                this.SetCookie(this.savedItems);
        },
        
        SetCookie : function(pId){
            this.oDate = new Date();
            this.oDate.setDate(this.oDate.getDate()+this.oExpire);
            if (typeof(this.savedItems) != "undefined" && this.savedItems.length != 0)
                document.cookie = "oTreeMenuItem =" + this.savedItems.join(",") + ( ( this.oExpire == null ) ? "" : "; expires=" + this.oDate.toGMTString() );
        },
        
        DeleteCookie : function ()
        {
           document.cookie = "oTreeMenuItem = null";
        },
        
        /* 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';
                                            document.getElementById(oItem).className = (style_ == 'none')?'':'open';
                                        }
                                        else if (this.isIE){
                                            document.all[oItem].style.display = (style_ == 'none')?'block':'none';
                                            document.getElementById(oItem).className = (style_ == 'none')?'':'open';
                                        }
                                    
                                }
                        }
                }
                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 + "oTreeMenuItem =" + 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 */
                
        getCookie : function(){
               if(document.cookie.length > 0)
                {
                        oStartPos = document.cookie.indexOf("oTreeMenuItem=");
                        if(oStartPos != -1)
                        {
                                oStartPos = oStartPos + 14 ;
                                oEnd = document.cookie.indexOf(";",oStartPos);
                                if(oEnd == -1)
                                        oEnd = document.cookie.length;
                                var items = unescape(document.cookie.substring(oStartPos,oEnd)).split(',');
                                return items;
                        }
                }
                return "";
        },
        
        /* re-render all items saved on cookie */
        oCookieItems: function()
        {
            this.Restore();
           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]);
                        }
                }       
        },
        
        Restore : function(){
            var items = this.getCookie();
            if (typeof(this.savedItems)=='undefined') this.savedItems = [];
            this.savedItems = items;
            for (var i=0; i<items.length;i++){
                tmp = items[i].split(":");
                var obj = document.getElementById(tmp[0]);
                if (typeof(obj) != "undefined" && obj != null) obj.style.display = "block";
                obj = document.getElementById(tmp[1]);
                if (typeof(obj) != "undefined" && obj != null) obj.style.display = "block";
            }
            this.SetCookie(this.savedItems);
        },

        /* for debug */ 
        oAlert: function(oStr)
        {
                alert(oStr);
        }
};

var ArrayHelper = {
       
    Remove : function(_arr, _item){
        var a = [];
        for (var i=0;i<_arr.length;i++){
            if (_arr[i]!=_item) a.push(_arr[i]);
        }
        return a;
    }
}
