(function($) {
	//
	// plugin definition
	//
	$.fn.xmlmenu = function(options) {
		var opts = $.extend({}, $.fn.xmlmenu.defaults, options);
		
		return this.each(function() {
			var $this = $(this);
			var containerCount = 0;
			var hoverCounter = 0;
			
			var itemPath = [];
			
		
			//var $header = $this.find("div:first");
			var $content = $this;
			
			var items = []; 

				
			$.post(opts.xml,opts.postVariables,function(r){
				processData(r);
				
				var $item = $content
						.data("level",0)
						.data("parents",[])
						.data("posInArray",0)
						.data("children",items[0].children)
						.click(function(){
								var lnk = $(this).find("a").attr("href");
								if (lnk!="") window.open(lnk,"_self");
							})
						.hover(function(){
							hoverCounter++;
							showSubMenu($(this));
						},function(){
						 	hoverCounter--;
						 	hoverOutHandler();
						});
					
					if (items[0].children.length>0)
						$item.addClass("xmlmenu-content-item-children");								
			});
			
			function showSubMenu($me){
				var level = $me.data("level");
				var children = $me.data("children");
				var offset = $me.offset();
				var width = $me.width();
				
				
				while (itemPath.length > level){
					itemPath.pop();
				}
				itemPath[level] =$me .data("posInArray");
				
				
				if ($me.is('.xmlmenu-content-item')){
					$me.parent().find(".xmlmenu-content-item").removeClass("xmlmenu-content-item-active");
					$me.addClass("xmlmenu-content-item-active");
				} else {
					$me.parent().find(".xmlmenu-container-item").removeClass("xmlmenu-container-item-active");
					$me.addClass("xmlmenu-container-item-active");
				}
				
				updateContainers(level+1,(children.length>0));
				
				
				if (children.length>0){
				
					var $activeCont = $('body').find(".xmlmenu-container:last");	
										
					if (level==0)
						var offtop=offset.top; else var offtop=offset.top-1;
										
					$activeCont.html("");
							
					for (i=0;i<children.length;i++){
						var $activeItem = $activeCont.append(markupMenuItem(children[i],"xmlmenu-container-item")).children(".xmlmenu-container-item:last");
						
						$activeItem.data("level",level+1)
							.data("children",children[i].children)
							.data("posInArray",i)
							.click(function(){
								var lnk = $(this).find("a").attr("href");
								if (lnk!="") window.open(lnk,"_self");
							})
							.hover(function(e){
								showSubMenu($(this));
							},function(){
							});
						
						if (i+1<children.length)
							$activeItem.addClass("xmlmenu-container-item-border");
							//$activeCont.append("<div class='xmlmenu-container-divider'>");
					}
					
					var height = $activeCont.height(), width = $activeCont.width(), top, left;
					
					if (level == 0) {
						top = offtop+$me.height();
						left = offset.left;
					} else {
						top = offtop;
						left = offset.left-width-2;
					}
					
					
					
					// console.log(level);
					
					$activeCont.css({top:top, left:left});
					if(top<0)
						top=0;

				}
			}
			
			function updateContainers (level,children) { //add or remove
				var countIs = $('body').find(".xmlmenu-container").length;
				
				if (children){
					var countShouldBe = level;
				} else {
					var countShouldBe = level-1;
				}
				
				
				if (countIs<countShouldBe){
					$('body').append("<div class='xmlmenu-container'>").find(".xmlmenu-container:last")
							.hover(function(){
								hoverCounter++;
							},function(){
								hoverCounter--;
								hoverOutHandler();
							});
				} else if (countIs>countShouldBe) {
					$(".xmlmenu-container:gt("+(countShouldBe)+")").remove();
					$(".xmlmenu-container:eq("+(countShouldBe)+")").remove();
				}
			}
			
			function hoverOutHandler(){
				$this.queue("fx",[]);
				$this.stop();
				$this.delay(100).queue("",function () {
					if (hoverCounter==0){
						itemPath = [];
						$(".xmlmenu-content-item").removeClass("xmlmenu-content-item-active");
						updateContainers(1,false);
					}		
					$(this).dequeue();
				});
			}
			
			
			function markupMenuItem(item,cssClass){
				var markup;
				markup = "<div class='"+cssClass+"'>";
								
				if (item.link!=""){
					markup+="<a href='"+item.link+"'>"+item.title+"</a>";
				} else {
					markup+=item.title;
				}
				
				if (item.icon!="") {
					markup+="<div class='xmlmenu-content-icon'><img src='"+item.icon+"'></div>";
				}
				
				markup+="</div>";
				return markup;
			}
			
			function processData(r){
				var $r=$(r);				
				
				items = recursiveProcess($r.children("item"));
				
				function recursiveProcess(itembranch){
					var arr = [];
					var children = itembranch;
					
					children.each(function(){
						var obj = {};
						obj.title=$(this).attr("title");
						obj.link=$(this).attr("link") || "";
						obj.icon=$(this).attr("icon") || "";
						if ($(this).children("item").length>0)
							obj.children = recursiveProcess($(this).children("item")); else obj.children={};
						arr.push(obj);
					});
										
					return arr;
				}
				
			}

			
		});
	};
	
	//
	// private functions
	//

	//
	// public functions
	//
	
	//
	// plugin defaults
	//
	$.fn.xmlmenu.defaults = {
		url: "",
		postVariables:{}
	};
})(jQuery);