if (typeof JKow == "undefined"){
	var JKow = {};
}

if (typeof JKow.UI == "undefined"){
	JKow.UI = {};
}

JKow.UI.Menu = {};
JKow.UI.MenuItem = {};


JKow.UI.MenuRenderer = function(){
	this.config = {};
	this.menu = null;
}

JKow.UI.MenuRendererPopup = function(menu, config){
	this._hideChildren = function(sender, id){
		var menuItem = sender.menu.findMenuItem(id);
		if (menuItem != null){
			var current_child =  menuItem.children;	
			while (current_child != null){
				current_child.elem.style.display = "none";
				current_child = current_child.next;
			}
			if (typeof this.config["menu-level-2-prefix"] == "string"){
				var e = document.getElementById(this.config['menu-level-2-prefix'] + '-' + id); 
				if (e != null){
					e.style.display = "none";
				}
				e = document.getElementById(id + '-tail');
				if (e != null){
				    e.style.display = "none";
				}
			}
			if (typeof this.onhidechildren == "function") this.onhidechildren(sender, id);
		}
	}
	this.isMouseOverChildren = function(sender, id){
		var result = false;
		var menuItem = sender.menu.findMenuItem(id);
		if (menuItem != null){
			var current_child =  menuItem.children;	
			while (current_child != null){
				if (current_child.states.ismouseover) {
					result = true;
					break;
				}
				current_child = current_child.next;
			}
		}
		return result;
	}
	this.hideAllChildren = function(sender){
		var c = sender.menu.menuItems;
		while (c != null){
			var cid = c.id;
			this._hideChildren(sender, cid);
			c = c.next;
		}
	}	

	this.hideChildren = function(sender, id){
		var t = setTimeout(
			function(){
				var menuItem = sender.menu.findMenuItem(id);
				if (!(menuItem.states.ismouseover || sender.isMouseOverChildren(sender, id))){
					sender._hideChildren(sender, id);
				} else {
					clearTimeout(t);	
				}
			},
			700	
		);
	}

	this.showChildren = function(sender, id){
		this.hideAllChildren(sender);
		var offset_x = null;
		var menuItem = sender.menu.findMenuItem(id);

		if (menuItem != null){
			var current_child =  menuItem.children;	
			if (typeof this.config["menu-level-2-prefix"] == "string"){
				var e = document.getElementById(this.config['menu-level-2-prefix'] + '-' + id); 
				if (e != null){
					e.style.display = "inline";
				}
				e = document.getElementById(id + '-tail');
				if (e != null){
				    e.style.display = "inline";
				}
			}
			while (current_child != null){
				current_child.elem.style.display = "inline";
				if (typeof menuItem.config["child-offset-x"] == "number"){ current_child.elem.style.left = menuItem.config['child-offset-x'].toString() + 'px'; }
				current_child = current_child.next;
			}
			if (typeof this.onshowchildren == "function") this.onshowchildren(sender, id);
		}
	}

	this.init = function(menu, config){ 
		var sender = this;
		this.menu = menu;
		this.config = config;
		var current = this.menu.menuItems;
		var child_count = 0;
		
		while (current != null){
		    child_count = 0;
			var current_child = current.children;
			while (current_child != null){
				current_child.elem.style.display = "none";
				/* set up events */
				var id = current_child.id;
				eval('JKow.Event.addEvent( "mouseout", current_child.elem, function() { sender.hideChildren(sender, "' + id + '"); }  )');
				current_child = current_child.next;
				++child_count;
			}
			var id = current.id;
			if (child_count > 0){
			    eval('JKow.Event.addEvent( "mouseover", current.elem, function() { sender.showChildren(sender, "' + id + '"); } )'); 
			    eval('JKow.Event.addEvent( "mouseout", current.elem, function() { sender.hideChildren(sender, "' + id + '"); }  )');
			}
			current = current.next;
		}
	}
	this.init(menu, config);
}
JKow.UI.MenuRendererPopup.prototype = new JKow.UI.MenuRenderer();

JKow.UI.Menu = function(config) {
	this.config = {};	
	this.menuItems = null;	
	this.elem;

	this.addMenuItems = function(menuItems){
	}	

	this._findMenuItem = function(parent, id){
		var current = parent;
		var result = null;
		while (current != null){
			if (current.id == id){
				result = current;
			} else {
				if (current.children != null){
					result = this._findMenuItem(current.children, id);
				}
			}
			if (result == null){
			       	current = current.next;
			} else {
				break;
			}
		}
		return result;
	}

	this.findMenuItem = function(id){
		var parent = this.menuItems;
		var result = null;
		if (parent != null){
			result = this._findMenuItem(parent, id);
		}
		return result;
			
	}

	this.addMenuItemToParent = function (parent_id, menuItem){
		var parent = this.findMenuItem(parent_id);
		if (parent != null){
			parent.addChild(menuItem);
		    if (menuItem != null && menuItem.elem != null){
		        eval('JKow.Event.addEvent( "click", menuItem.elem, function() { location.href="' + menuItem.value + '"; } )'); 		        
		    }
		}
	}

	this.addMenuItem = function(menuItem){
		var current = this.menuItems;
		if (current == null){
			this.menuItems = menuItem;
		} else {
			var previous = current;
			var lim = 50;
			while (current != null){
				previous = current;
				current = current.next;
			}
			previous.next = menuItem;
		}
		if (this.elem != null && menuItem.elem != null && menuItem.elem.parentNode == null){
			this.elem.appendChild(menuItem.elem);
		} 
		
		if (menuItem != null && menuItem.elem != null){
		    eval('JKow.Event.addEvent( "click", menuItem.elem, function() { location.href="' + menuItem.value + '"; } )'); 
		}
		
		var t = this;
		return this;
	}

	this.init = function(config){
		this.config = config;
		if (typeof config['elid'] == "undefined"){
			this.elem = document.createElement('div'); 
		} else {
			this.elem = document.getElementById(config['elid']);
		}
	}	
	this.init(config);
}

JKow.UI.MenuItem = function(id, caption, value, config){
	var gid = 0;
	this.config = {};
	this.elem = null;
	this.children = null;
	this.next = null; 
	this.id = null;
	this.caption = null;
	this.value = null;
	this.states = { 'ismouseover': false, 'ismouseout' : false };

	
	this.addChild = function(child){
		var current = this.children;
		if (child != null){
		    if (current == null){
			    this.children = child;
		    } else {
			    var previous = current;
			    while (current != null){
				    previous = current;
				    current = current.next;
			    }
			    previous.next = child;
		    }            
		}
	}

	this.init = function(id, caption, value, config){
		this.config = config;
		this.id = id;
		this.caption = caption;
		this.value = value;
		this.config = config;
		if (typeof config["elid"] == "undefined"){ config["elid"] = (JKow.UI.MenuItem.gid++).toString(); }
		if (typeof config["createElement"] == "undefined"){
			config["createElement"] = false;
		} 
		if (config["createElement"]){
			this.elem = document.createElement('div');
		}else {
			this.elem = document.getElementById(config["elid"]);
		}
		if (this.elem != null){
			if (typeof config["size"] != "undefined"){
				this.elem.style.width = config.size.w.toString() + 'px';
			       	this.elem.style.height = config.size.h.toString() + 'px';				}
			/*this.elem.innerHTML = caption; */
			this.elem.id = config["elid"];
			this.elem.name = config["elid"]; 
			this.elem.className = (typeof config["className"] != "string") ? (this.elem.className) : config["className"];
		
			var sender = this;
			JKow.Event.addEvent(
				"mouseover", this.elem, function() { 
					sender.states.ismouseover = true,
					sender.states.ismouseout = false; 
				}  
			);
			JKow.Event.addEvent(
				"mouseout", this.elem, function() { 
					sender.states.ismouseover = false,
					sender.states.ismouseout = true; 
				}  
			);	
		}
	}
	this.init(id, caption, value, config);
}
