
mainMenu = {
	
	plusClass:	'plus',
	minusClass:	'minus',
	menuVisible: new Array,
	curentLocation: '',
	
	create: function ( rootListId ) {
		this.rootItem = document.getElementById ( rootListId ).childNodes[0];
		this.loadVisible();
		this.loadLocation();		
		this.goThrow ( this.rootItem );
	},
	
	goThrow: function ( ulItem ) {
		var rootChilds = ulItem.childNodes;
		for ( var i = 0; i < rootChilds.length; i++ ) {
			var child = rootChilds[i];
			var childChildNodes = child.childNodes;
			if ( childChildNodes.length > 1 ) {
				this.goThrow ( child.childNodes[1] );
				this.setSlice ( child );				
			}
			if ( this.getLocation ( child.childNodes[0].href ) == this.curentLocation ) {
				if ( child.parentNode.parentNode.id.indexOf('menuItem') >= 0 ) {
					this.show ( child.parentNode.parentNode );
				}
			}
		}
	},
	
	setSlice: function ( listItem ) {
		if ( this.menuVisible[listItem.id] == 1 ) {
			this.show ( listItem );
		} else {
			this.hide ( listItem );
		}
		this.addEvent(listItem);
	},

	clicked: function ( e ) {
		var evt = e || window.event;
		var srcElement = evt.srcElement ? evt.srcElement : evt.target;		
		mainMenu.stopEvent(evt);
		mainMenu.toggle ( srcElement );		
	},
	
	stopEvent: function ( evt ) {
        evt.cancelBubble = true;
        if (!evt.cancelBubble)
            evt.stopPropagation();
	},
	
	addEvent: function (sObject) {
		if ( typeof attachEvent != 'undefined' ) {
			sObject.attachEvent ( 'onclick', mainMenu.clicked, true ); 			
		} else {
			sObject.addEventListener ( 'click', mainMenu.clicked, true ); 
		}
	},
	
	hide: function ( element ) {
		if ( element.childNodes[1] ) {
			element.className = this.plusClass;
			element.childNodes[1].style.display = 'none';
			this.menuVisible[element.id] = 0;
			this.saveVisible();
		}
	},
	
	show: function ( element ) {
		if ( element.childNodes[1] ) {
			element.className = this.minusClass;
			element.childNodes[1].style.display = '';
			this.menuVisible[element.id] = 1;
			this.saveVisible();
		}
	},

	toggle: function ( element ) {
		var liElement = element;
		if ( element.href == undefined ) {
			if ( liElement.className == this.plusClass ) this.show ( liElement );
			else if ( liElement.className == this.minusClass ) this.hide ( liElement );
		} else {
			liElement = element.parentNode;
			this.show ( liElement );
		}
	},
	
	saveVisible: function ( ) {
		var cookieText = '';
		for ( var v in this.menuVisible ) {
			if ( this.menuVisible[v] == 1 ) cookieText += v+',';
		}
		cookieText = cookieText.replace ( /\,$/, '' );
		this.setCookie ( 'mainMenu', cookieText );
	},
	
	loadVisible: function ( ) {
		var cookieText = this.getCookie ( 'mainMenu' ) + '';
		var cookieTextArr = cookieText.split ( ',' );
		for ( var i = 0; i < cookieTextArr.length; i++ )
			if ( cookieTextArr[i] != '' && cookieTextArr[i] != 'undefined' ) 
				this.menuVisible[cookieTextArr[i]] = 1;
	},
	
	setCookie: function (name, value, expires, path, domain, secure) {
          document.cookie = name + "=" + escape(value) +
          ((expires) ? "; expires=" + expires : "") +
          ((path) ? "; path=" + path : "") +
          ((domain) ? "; domain=" + domain : "") +
          ((secure) ? "; secure" : "");
	},
	
	getCookie: function ( name ) {
		var cookie = " " + document.cookie;
		var search = " " + name + "=";
		var setStr = null;
		var offset = 0;
		var end = 0;
		if (cookie.length > 0) 
		{
			offset = cookie.indexOf(search);
			if (offset != -1) 
			{
				offset += search.length;
				end = cookie.indexOf(";", offset)
				if (end == -1) 
				{
					end = cookie.length;
				}
				setStr = unescape(cookie.substring(offset, end));
			}
		}
		return(setStr);
	},
	
	loadLocation: function ( ) {
		this.curentLocation = this.getLocation ( window.location +'' );		
	},
	
	getLocation: function ( loc ) {
		loc = loc + '';
		loc = loc.replace ( /\?.*$/, '' );
		loc = loc.replace ( /\/?$/, '' );
		return loc;
	}
}

