/* 
   Metody jQuery na potrzeby menu dynamicznego
*/
var menuDynamic = new Object();
var menuExpandUrl = "";
var menuExpandNaviId = "";

menuDynamic.init = function() {
	initializeLevel1();
	initializeLevel2();
}

function initializeLevel1() {
	$("#menuDynamic > ul > li").hover(
		function() {
			$(this).children("a").css('background','#788CC7');
			if ($(this).children("div.m1").size() == 1) {
				if ($(this).children(".m1").children("ul").size() == 0) {
					expandMenu($(this).children("div.m1").attr("id"));
				}
				$(this).children("div.m1").show();
				resizeItem($(this).children("div.m1"));
			}
		},
		function() {
			$(this).children("div.m1").hide();
			$(this).children("a").not(".active").css('background','#ff6000');
		}
	).css('cursor', 'pointer');
}
function initializeLevel2() {
	$("#menuDynamic .m1 > ul > li").hover(
		function() {
			if ($(this).children("div.m2").size() == 1) {
				if ($(this).children("div.m2").children("ul").size() == 0) {
					expandMenu($(this).children(".m2").attr("id"));
				}
				var objWidth = $(this).parent().width();
				$(this).children("div.m2").css("left", objWidth);
				$(this).children("div.m2").show();
				resizeItem($(this).children("div.m2"));
			}
		},
		function() {
			$(this).children(".m2").hide();
		}
	).css('cursor', 'pointer');
}
function resizeItem(item) {
	if (item.children("ul").children('li').size() != 0) {
		var divWidth = item.width();
		item.children("ul").children('li').css('width',divWidth+'px');
	} else {
		setTimeout(function(){resizeItem(item);},100);
	}
}

function expandMenu(navigationId) {
	/* wyciągam tylko numer nawigacji, usuwam r z id */
	var navigationIdTrue = navigationId.substring(1);
	if (menuExpandNaviId == "") {
		var completeExpandUrl = menuExpandUrl + "?expandRoot=" + navigationIdTrue;
		menuExpandNaviId = navigationId;
		var request = new xmlHttpRequest("GET", completeExpandUrl, "false", "expandMenuResult", "defaultFailure");
		request.send();
	} else {
		setTimeout(function(){expandMenu(navigationId);},100);
	} 
	
}

function expandMenuResult(response) {
	var destinationObj = $("#"+menuExpandNaviId);
	destinationObj.html(response.responseText);
	if (destinationObj.attr("class") == "m1") {
		initializeLevel2();
	}
	menuExpandNaviId = "";
}
