/*
 * Async Treeview 0.1 - Lazy-loading extension for Treeview
 * 
 * http://bassistance.de/jquery-plugins/jquery-plugin-treeview/
 *
 * Copyright (c) 2007 Jörn Zaefferer
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: $Id: jquery.treeview.async.js 1275 2011-06-13 08:51:51Z mkozlowski $
 *
 */
/*function mark(obj) {
	$("#leftMenu").find("li,p").css('font-weight', 'normal');
	$(obj).css('font-weight', 'bold');
}*/


;(function($) {

function load(settings, root, child, container) {
	//console.log('dsad', tree.expanded_all);
	
	$.ajax({
		type: "POST",
		url: settings.url,
		data: "class=menu&method=defaultMethod&id="+root+"&main="+settings.main+"&mid="+settings.id+(typeof(expanded_all) != 'undefined' && expanded_all == 'true' ? "&expanded_all=true" : ""),
		dataType: "json",
		cache: true,
		success: function(response) {
			//console.log(response);
			if (settings.main == 1) settings.main = 0; 
			//console.log(settings);
			if (root == 0 && typeof(response.content) != 'undefined' && typeof(response.content.menu[0]) != 'undefined' && response.content.menu[0].classes != 'separator_menu') $("div#optional_header_menu").show();
			/*for(var i=0; i< response.content.parentsIds.length; i++){
				alert(response.content.parentsIds[i]);
				$("#leftMenu").find("li#"+response.content.parentsIds[i]).find("span.important").css('font-weight', 'bold');
			}*/
			//$("#leftMenu").find("li").css('font-weight', 'normal');
			//$("#"+root).css('font-weight', 'bold');
			function createNode(parent) {
				var current = $("<li/>").attr("id", this.id || "").html("<span>" + this.text + "</span>").appendTo(parent);
				//console.log(this.classes);
				if (this.classes == 'separator_menu') {
					current.addClass(this.classes);
					//current.css({"background-image": "none", "font-weight": "bold", "padding": "4px 5px 0 5px"});
					//console.log(current.prev());
					if (!current.prev().hasClass("separator_menu")) current.prev().addClass("last");	
				}
				if (this.classes) {
					current.children("span").addClass(this.classes);
					//current.css('font-weight', 'normal');
					if (this.classes != 'separator_menu'){
						current.find("span").click(function(){
						
								$("ul#leftMenu").find("span,a").removeClass("my");
								addMy($(this));
								
						});
					}
					
					
				}
				if (this.expanded) {
					
					current.addClass("open");
				}
				if (this.hasChildren || this.children && this.children.length) {
					var branch = $("<ul/>").appendTo(current);
					if (this.hasChildren) {
						current.addClass("hasChildren ");
						createNode.call({
							text:"placeholder",
							id:"placeholder",
							children:[]
						}, branch);
						//branch.css('display', 'none');
					}
					if (this.children && this.children.length) {
						$.each(this.children, createNode, [branch])
					}
				}
			}
			if (typeof(response.content) != 'undefined'){
				$.each(response.content.menu, createNode, [child]);
			}
			
	        $(container).treeview({add: child});
	    	/*if (typeof(response.content.data) != 'undefined'){
				if (response.content.data != '<error>'){
					$("#content").html(response.content.data);
					border();
					$('#re_os').leviTip({sourceType: 'element', source: '#infoTipRe', hideSourceElement: true});
		 			$('#wpr_os').leviTip({sourceType: 'element', source: '#infoTipWpr', hideSourceElement: true});	
					bindContents();
					bindPath();
				}
					
			}*/
			
			
	    	$("#path").html(response.path);
			//jak w url wpisujemy jakis konkretny id lub content
			if (new_menu_id){
				//$("#leftMenu").empty();
				refresh(new_menu_id);
				new_menu_id = '';
			} else {
				
			}
				
			bindMenuItems();
			enableListHover();
				
			
			
			//bindPath();
			//getContents()
//			if (response.time){
//				alert(response.time);
//			}
			if (response.time){
				$("#czas_generowania").html(response.time);
			}
			
			if (response.error){
		    	/*$("body").prepend(response.error);
				$("div.info").slideDown('slow').fadeIn('slow');
				
				//przycisk do zamkniecia gornego paska "info"
		 		$(".close_info_bar").show();
				$(".close_info_bar").click(function(event){
				   event.preventDefault();
				   $(this.parentNode.parentNode).hide("slow");
				 });*/
				showError(response.error);
				bindNotificationCloseButton();
		    } 
	    }
	    
	    
	});
	
}

var proxied = $.fn.treeview;
$.fn.treeview = function(settings) {
	//alert('w glownej');
	if (!settings.url) {
		return proxied.apply(this, arguments);
	}
	var container = this;
	load(settings, "0", this, container);
	var userToggle = settings.toggle;
	return proxied.call(this, $.extend({}, settings, {
		collapsed: true,
		toggle: function() {
			var $this = $(this);
			if ($this.hasClass("hasChildren")) {
				var childList = $this.removeClass("hasChildren").find("ul");
				childList.empty();
				
				load(settings, this.id, childList, container);
				//if (this.id != 0) border();
			}
			
			if (userToggle) {
				userToggle.apply(this, arguments);
			}
		}
	}));
};

})(jQuery);
