// 
//  jquery.flatXMLMenu.js
//  Alco
//  
//  Created by Matthias Link on 2011-02-04.
//  Copyright 2011 Interactive Systems. All rights reserved.
// 


(function($) {
$.widget("ui.flatXMLMenu",{
	options: {
		ajaxUrl: "url/to/xml",
		ajaxParams: {},
		identifierParams: ["g1", "g2", "g3"], 		// array of params, which are used in the url to determine which page the user visits
		css: {
			levelClass: "menu-level",
			levels: [
				{cssClass: "menu-level-1", activeClass: "menu-level-1-active"},
				{cssClass: "menu-level-2", activeClass: "menu-level-2-active"},
				{cssClass: "menu-level-3", activeClass: "menu-level-3-active"}
			],
			titleClass: "menu-title",
			childrenClass: "menu-children"
		},
		autoexpand: true
	},
	privates: {
		xml: undefined
	},
	_init: function(){
		var widget = this, $element = $(widget.element);
		widget._requestXML();
	},
	_requestXML: function() {
		var widget = this;
		
		$.post(widget.options.xml, widget.options.ajaxParams,function(data) {
			widget._parseXML(data);
			widget._generateMenu();
			if (widget.options.autoexpand){
				var path = widget._getActivePath();
				widget.expandWithPath(path);
			}
		}, true);
	},
	_parseXML: function(data) {
		var widget = this, xml = $(data).children(), indexRecursive;		
		
		xml = widget._activateRecursive(xml);
		xml = widget._indexRecursive(xml);
		
		
		
		widget.privates.xml = xml;
	},
	_indexRecursive: function(xml) {
		var widget = this;
		
		xml.each(function(i) {
			var $this = $(this), children;
			$this.attr("index", i);
			
			children = $this.children();
			
			if (children.length > 0) {
				$this= widget._indexRecursive(children);
			}
		});
		
		return xml;
	},
	_activateRecursive: function(_xml){
		var widget = this, xml = _xml, item, url = widget._prepareURL();
		
		item = xml.find("item[link="+url+ "]:last");
		item.attr("active", "true").attr("activePage", "true");
		item.parentsUntil("menu").attr("active", "true");
				
		return xml;
	},
	_prepareURL: function() {
		var widget = this, location = window.location, filename = location.pathname.replace(/^.+\//,"") , query = location.search, url, newQuery, temp, params = [];
						
		temp = query.replace(/^\?/,"").split("&");
		temp.map(function(_value, _index) {
			var key = _value.replace(/\=.+$/,"");
			
			if (widget.options.identifierParams.contains(key)) {
				params.push(_value);
			}	
		});
		
		url = filename;
		if (params.length > 0){
			url += "?"+params.join("&");
		}
				
		return url;
	},
	_generateMenu: function() {
		var widget = this, $element = $(widget.element) ,$menu = $(widget.privates.xml), css = widget.options.css, markup = "", $markup;
		
		
		$element.html("");
		
		$menu.each(function() {
			$element.append(widget._createItem(this, 0));
		});
				
		
		$element.find("."+css.childrenClass).hide();
		$element.find("."+css.titleClass).click(function(e){
			var $this = $(this);
						
			$element.find("."+css.childrenClass).hide();
			$this.parentsUntil($element).children("."+css.childrenClass).show();
			$this.parent().children("."+css.childrenClass).show();
			
			if ($this.parent().children("."+css.childrenClass).length > 0){
				// e.preventDefault();
			}
		});		
	},
	_createItem: function(_xml, _level, _index) {
		var widget = this, markup = "", xml = $(_xml), title = xml.attr("title"), link = xml.attr("link"), active = (xml.attr("active")=="true"), css = widget.options.css;
				


		markup = "<div class='"+css.levelClass+" "+css.levels[_level].cssClass+((active) ? " "+css.levels[_level].activeClass : "")+"'>";
		markup+= "<div class='"+css.titleClass+"'>";
		markup+= (link != "") ? "<a href='"+xml.attr("link")+"'>"+xml.attr("title")+"</a>" : xml.attr("title") ;
		markup+="</div>";
		
		if (xml.children().length > 0){
			markup+= "<div class='"+css.childrenClass+"'>";
			xml.children().each(function() {
				markup+= widget._createItem(this,_level+1);
			});
			markup+= "</div>";
		}
		markup+="</div>";
		return markup;
	},
	_getActivePath: function() {
		var widget = this, path = [], url = widget._prepareURL, href = window.location.toString(), xml = widget.privates.xml, item;
		
		
		item = xml.find("item[activepage=true]:last");
		
		path.push(parseInt(item.attr("index")));
		
		item.parents("item[active=true]").each(function() {
			path.push(parseInt($(this).attr("index")));
		});
		
		return path.reverse();
	},
	expandWithPath: function(array) {
		var widget = this, $element  = $(widget.element), css = widget.options.css, $item, i;
		
		$element.find("."+css.childrenClass).hide();
		
		$item = $element;
		
		
		for (i in array){
			$item = $item.children("."+css.levelClass+":eq("+array[i]+")").show().children("."+css.childrenClass).show();
		}
		
			
	}
});
})(jQuery);

