MiniCMS.Menu = function( id ) {
	this.OpenMenu = null;
	this.Timer = null;
	this.IconLeft = true;
	this.Clicks = [];
	this.id = id;
	MiniCMS.Menus[id] = this;
};


MiniCMS.Menus = [];


MiniCMS.Menu.prototype = {
	
	DrawMenu: function( id, data ) {
		document.getElementById(this.id).innerHTML += this.GetMenuHTML( id, data );
	},
	
	
	GetMenuHTML: function( id, data ) {
		if( !data || !data.length )
			return;
 			
		var submenus = '',
		  str = '<div id="' + id + '" class=Menu><table cellpadding=0 cellspacing=0 border=0 class=MenuTable';
		str += ' onMouseOver="MiniCMS.Menus[\'' + this.id + '\'].SetTimer(true)"';
		str += ' onMouseOut="MiniCMS.Menus[\'' + this.id + '\'].SetTimer()">';
		
			for( var key in data ) {
				var
					title = data[key].shift(),
					link = data[key].length ? data[key].shift() : null,
					icon = data[key].length ? data[key].shift() : null,
					desc = data[key].length ? data[key].shift() : null,
					target = data[key].length ? data[key].shift() : null,
					children = data[key].length ? data[key] : null,
					classname = (['MenuItem', link?'clickable':'', children?'expandable':'']).join(' '),
					actions = link || children ? true : false;
				
				submenus += children ? this.GetMenuHTML( id + '_' + key, children ) : '';

				str += '<tr class="' + classname + '"';
				
				if( children ) {
					str += ' onmouseover="MiniCMS.Menus[\'' + this.id + '\'].OnMouseOver(this, \'' + id + '_' + key + '\')"';
					str += ' onmouseout="MiniCMS.Menus[\'' + this.id + '\'].OnMouseOut(this, \'' + id + '_' + key + '\')"';
				}
				str += ' onmouseover="MiniCMS.Menus[\'' + this.id + '\'].OnMouseOver(this, \'' + id + '\')"';
				str += ' onmouseout="MiniCMS.Menus[\'' + this.id + '\'].OnMouseOut(this, \'' + id + '\')"';
				if( link ) {
					if( 'javascript:' == link.substr(0, 11) )
						link = link.substr(0, 11);
					else link = "window.open('" + link + ( target ? "','" + target : '') + "')";
					str += ' onmousedown=""';
					str += ' onmouseup="' + link + '"';
				}
				
				str += '><td nowrap><div class=Left>' + (icon && this.IconLeft ? icon : '&nbsp;') + '</div></td>';
				str += '<td nowrap><div class=Title>' + title + '</div></td>';
				str += '<td nowrap><div class=Right>' + (icon && !this.IconLeft ? icon : '&nbsp;') + '</div></td></tr>';
			}
		
		return str += '</table>' + submenus + '</div>';
	},
	
	Close: function( stopmenu ) {
		var path = [this.OpenMenu.id], tmp = this.OpenMenu.id, last_i = -1;
		if( tmp != stopmenu )
			while( (last_i = tmp.lastIndexOf('_')) > -1 && this.id != (tmp = tmp.substr(0, last_i)) ) {
				path.push( tmp );
				if( tmp == stopmenu )
					break;
			}
			
		var node = null;
		while( node = document.getElementById(path.shift()) ) {
			// DEBUG MiniCMS.Debug( '<code style="color: blue">Close( ' + node.id + ' )</code>' );
			MiniCMS.ToggleDisplay( node, false );
			if( node.HoverItem ) {
				MiniCMS.Normal( node.HoverItem );
				node.HoverItem.Bound = null;
				node.HoverItem = null;
			}
		}
		if( stopmenu ) {
			var parent = stopmenu.substr( 0, stopmenu.lastIndexOf('_') );
			if( this.id != parent )
			  this.OpenMenu = document.getElementById(parent);
			else this.OpenMenu = null;
		} else this.OpenMenu = null;
	},
	
	
	Open: function( openmenu, menuitem ) {
		var parent = openmenu.substr( 0, openmenu.lastIndexOf('_') );
		if( (!this.OpenMenu && this.id == parent) || (this.OpenMenu && this.OpenMenu.id == parent) ) {
			// DEBUG MiniCMS.Debug( '<code style="color: blue">Open( ' + openmenu + ' )</code>' );
			this.OpenMenu = document.getElementById( openmenu );
			MiniCMS.ToggleDisplay( this.OpenMenu, true );
			this.OpenMenu.style.left = (menuitem.offsetLeft + menuitem.clientWidth) + 'px';
			menuitem = MiniCMS.FindParent( menuitem, 'className', /\bMenuItem\b/ );

			var top = menuitem.offsetTop, parent = menuitem.offsetParent;
			while(parent && parent.offsetTop) {
				top += parent.offsetTop;
				parent = parent.offsetParent;
			}
			if( document.documentElement.clientHeight + document.documentElement.scrollTop - this.OpenMenu.offsetHeight < top )
				top = document.documentElement.clientHeight + document.documentElement.scrollTop - this.OpenMenu.offsetHeight;
			this.OpenMenu.style.top = (top > 0 ? top : '0') + 'px';
			
			this.OpenMenu.HoverItem = menuitem;
			menuitem.Bound = true;
			MiniCMS.Hover( menuitem );
		} else ; // DEBUG MiniCMS.Debug( 'ERROR in Open - not a child' );
	},
	
	
	OnMouseOver: function( menuitem, openmenu ) {
		// DEBUG MiniCMS.Debug( 'OnMouseOver: ' + openmenu + (this.OpenMenu ? ' (' + this.OpenMenu.id + ')' : '') );
		this.SetTimer( true );
		
		node = MiniCMS.FindParent( menuitem, 'className', /\bMenuItem\b/ );
		if( node.className.match(/\bexpandable\b|\bclickable\b/) )
			MiniCMS.Hover( node );
		
		var	closepath = [], openpath = [], tmp, last_i;
		if( this.OpenMenu ) {
			closepath = [tmp = this.OpenMenu.id];
			while( (last_i = tmp.lastIndexOf('_')) > -1 && this.id != (tmp = tmp.substr(0, last_i)) )
				closepath.push( tmp );
			// DEBUG MiniCMS.Debug('<b><code style="color: red">C[' + closepath + '](' + closepath.length + ')</code></b>');
		}
		
		if( openmenu ) {
			openpath = [tmp = openmenu];
			while( (last_i = tmp.lastIndexOf('_')) > -1 && this.id != (tmp = tmp.substr(0, last_i)) )
				openpath.push( tmp );
			// DEBUG MiniCMS.Debug('<b><code style="color: green">O[' + openpath + '](' + openpath.length + ')</code></b>');
		}
		
		var c = null, o = null;
		while(closepath.length && openpath.length) {
			c = closepath.pop();
			o = openpath.pop();
			if( c != o ) {
				closepath.push( c );
				openpath.push( o );
				break;
			}
		}
		
		// DEBUG MiniCMS.Debug('<b><code style="color: red">C[' + closepath + '](' + closepath.length + ')</code></b>');
		// DEBUG MiniCMS.Debug('<b><code style="color: green">O[' + openpath + '](' + openpath.length + ')</code></b>');
		
		if( closepath.length )
			this.Close( closepath.pop() );
		if( openpath.length )
			this.Open( openpath.pop(), menuitem );
				
		if( this.OpenMenu ) {
			// DEBUG MiniCMS.Debug('<b><code style="color: black">OpenMenu[' + this.OpenMenu.id + ']</code></b>');
		}
				
	},


	OnMouseOut: function( menuitem, openmenu ) {
		// DEBUG MiniCMS.Debug( 'OnMouseOut: ' + openmenu + (this.OpenMenu ? ' (' + this.OpenMenu.id + ')' : '') );
		if( openmenu )
			this.SetTimer();
		node = MiniCMS.FindParent( menuitem, 'className', /\bMenuItem\b/ );
		if( !node.Bound )
			MiniCMS.Normal( node );
	},
	
	
	SetTimer: function( resettimer ) {
		clearTimeout( this.Timer );
		
		if( resettimer )
			return;
		
		if( resettimer == null ) {
			this.Timer = setTimeout( 'MiniCMS.Menus["' + this.id + '"].SetTimer(false)', 500 );
			return;
		}
			
		// DEBUG MiniCMS.Debug( 'Timeout: ' + (this.OpenMenu ? this.OpenMenu.id : 'no open menus') );
		this.Close();
	}
	
};