// Globals
var MENU_ITEM_MOUSEOVER_COLOR = 'white'
var MENU_BLOCK_WIDTH = 100
var _global_id_stack = 0
var _global_mouse_out_tigger = null
var _global_current = null

function mouseOverMenu(){ clearTimeout(_global_mouse_out_tigger) } 
function mouseOutMenu(o){
	_global_current = o
	_global_mouse_out_tigger = setTimeout('_mouseOutMenu()',1000)
}
function _mouseOutMenu(){
	_global_current.hideChildren()
	while(_global_current.parent){
		_global_current.hide()
		_global_current = _global_current.parent
	}
}

// Block object
function block(node){
	if(node){ // Use this node
		e1 = node
	}else{ // Create New Node
		var e1 = document.createElement('DIV')
		e1.style.position = 'absolute'
		e1.style.overflow = 'hidden'
		e1.id = '_node' + _global_id_stack++
		e1.style.width = MENU_BLOCK_WIDTH
		e1.style.top =  0
		e1.style.left =  0
		e1.style.visibility = 'hidden'
		e1.style.height = "auto";
		e1.className = 'menu_block'
	}
	e1._children = []
	e1._current = false

	e1.getYpos = function(){
		var t = 0
		o = this
		while(o.tagName != 'BODY'){
			t += o.offsetLeft
			o = o.parentNode
		}
		return t
	}

	e1.alignTop = function(o,offset){
		var t = 0
		
		while(o.tagName != 'BODY'){
			if(o.tagName.charAt(0) != 'T')
			t += o.offsetTop
			o = o.parentNode
		}
		var frame_bottom =  document.body.clientHeight+document.body.scrollTop
		if (frame_bottom < t + this.scrollHeight + 5)
			t = frame_bottom - this.scrollHeight - 5
		else t = t + (offset || 0)
		this.style.top = t 
	}
	e1.add = function(title,link,o){

		e2 = document.createElement('DIV')
		e2.className = 'menudiv'
		e2.style.position = 'relative'
		//e2.innerHTML = '<a href="'+(link || 'javascript:// no link')+'" class="menuItem">' + title +'</a>'
		var a = document.createElement('A')
		a.href = link || 'javascript:// no link'
		a.className = 'menuitem'
		a.innerHTML = title
		a.onmouseover = e2.onmouseover = function(){ mouseOverMenu() }
		e2.appendChild(a)
		this.appendChild(e2)
		if(o){
			arrow = document.createElement('DIV')
			arrow.innerHTML = '4'
			arrow.className = 'arrow'
			arrow.style.position = 'absolute'
			arrow.style.right = 0
			e2.appendChild(arrow)
		}
		
		if(o){
			this._children[this._children.length] = o
			o.parent = this
			var left = (this.style.position != '') ? left = parseInt(this.style.left) : this.getYpos()

			o.style.left = left + 141
			document.body.appendChild(o)
			e2._ref = o
		
			e2.onmouseover = function(){
				e = this
				while(e.className != 'menu_block' && e.id != 'menuDIV')
					e = e.parentNode
				e.hideChildren()
				this._ref.show()
				this._ref.alignTop(this,-1)
				e._current = this
				mouseOverMenu()
			}
		}else{
			e2.onmouseover = function(){
				//
				e = this
				while(e.className != 'menu_block' && e.id != 'menuDIV')
					e = e.parentNode
				e.hideChildren()
				e._current = this
				mouseOverMenu()
			}
		}
	}
	e1.hideChildren = function(){
		for(var i = 0; i< this._children.length; i++){
			if(this._children[i].style.visibility == 'visible'){
				var e = this._children[i]
				e.hide()
				//if(e._current) setDynMenuStyle(e)
				e._current = false
			}
		}

	}
	e1.hide = function(){
		this.hideChildren()
		this.style.visibility = 'hidden'
	}
	e1.show = function(){
		// Houston we have a problem
		this.fixHeight()
		this.style.visibility = 'visible'
	}

	e1.fixHeight = function(){
		//If menu height is bigger then screen height split menu
		if(document.body.clientHeight < this.scrollHeight){
			var h = 0
			for(var i = 0; i< this.children.length; i++){
				if(h + this.children[i].scrollHeight > document.body.clientHeight)
					break;
				h += this.children[i].scrollHeight
				this.children[i].style.width = this.style.width
			}
			h = 0
			
			var x = parseInt(this.style.top)
			var y = parseInt(this.style.left)
				
			for(; i< this.children.length; i++){
				var e2 = this.children[i]
				e2.style.position = 'absolute'
				e2.style.left = y //+ 100
				e2.style.width = this.style.width
				e2.className = 'menudiv'
				e2.style.top = x + h
				h+= e2.scrollHeight	
			}

			e2 = document.createElement('DIV')
			e2.innerHTML = ''
			e2.style.position = 'absolute'
			e2.style.overflow = 'hidden'
			e2.style.width = '1px'
			e2.style.height =  this.scrollHeight
			e2.style.top =  0
			e2.style.left =  parseInt(this.style.width) - 1
			//e2.style.backgroundColor = 'white'	
			
			this.appendChild(e2)

			this.style.width = parseInt(this.style.width)*2
			//this.style.backgroundColor = '#00557B'
		}
		return true
	}

	// EVENTS ////////////////////////////////
	e1.onselectstart = function(){ return false }
	e1.oncontextmenu = function(){ 
		event.cancelBubble = true
		return false 
	}
	e1.onmousedown = function(){
		this.hideChildren()
	}
	e1.onclick = function(){
		event.cancelBubble =true
	}
	e1.onmouseout = function(){
		mouseOutMenu(this)
	}
		e1.onmousemove = function(){ 
         mouseOverMenu()
    }
	/////////////////////////////////////////
	return e1
}

// Stop javscript errors
window.onerror = function(e){ status = ('javascript error suppressed :' + e) ; return true } 

