var selectedItem = "";
var caching = new Hashtable();

function populateTree(node, locationId, themeId) {

	if (this.caching.containsKey(node)) {
		receiveTree(this.caching.get(node));

	} else {
		var params = "Action=GetTree&LocationId=" + locationId + "&Part=" + node + "&ThemeId=" + themeId;

		var ajax = new Ajax.Request(
			"getinfo.php",
			{
				method: "post",
				parameters: params,
				onSuccess: receiveTree,
				onFailure: function (response) { alert('There was a problem with the request (populateTree).' + response.responseText); }
			}
		);
	}

}

function receiveTree(request) {
	var xmldoc = request.responseXML;
	var treeNodes = xmldoc.firstChild;
	var name = treeNodes.nodeName + "Div";

	$(name).innerHTML = convertXMLToString(treeNodes);

	if (! this.caching.containsKey(treeNodes.nodeName)) {
		caching.put(treeNodes.nodeName, request);
	}
}

function treeNodeClick(node, locationId, themeId) {
	var display = $(node + locationId + 'Menu').style.display;

	if (display == "inline") {
		$(node + locationId + 'Menu').style.display = "None";
		$(node + locationId + "Point").src = "images/treemenu_main_icon_default.gif";

	} else {
		setDivBusy(node + locationId + "Div");
		populateTree(node, locationId, themeId);

		$(node + locationId + "Menu").style.display = "inline";
		$(node + locationId + "Point").src = "images/treemenu_main_icon_open.gif";
	}
}

function display(node, id, caller, locationId) {
	if ($("DefaultDiv")) {
		$("DefaultDiv").style.display = "none";
		$("ContentDiv").style.display = "block";
	}

	setDivBusy("ContentDiv");

	if (caching.containsKey(node + id)) {
		receiveContent(node, id, caching.get(node + id));

	} else {
		var params = "Action=GetContent&LocationId=" + locationId + "&Part=" + node + "&Id=" + id;
		var ajax = new Ajax.Request(
			"getinfo.php",
			{
				method: "post",
				parameters: params,
				onSuccess: function (response) { receiveContent(node, id, response); },
				onFailure: function (response) { alert('There was a problem with the request (display).' + response.responseText); }
			}
		);
	}

	if (selectedItem != "") {
		try {
			$(selectedItem).style.fontWeight = "";
		} catch(error) {
			// If selectedItem is hidden, because tree was closed, swallow error
		}
	}

	selectedItem = caller;
	$(selectedItem).style.fontWeight = "bold";
}

function receiveContent(node, id , request) {
	var result = request.responseText;
	$("ContentDiv").innerHTML = result;

	if (! this.caching.containsKey(node + id)) {
		this.caching.put(node + id, request);
	}
	
	result.evalScripts();
}

function getExcursions(groupId) {
	var display = $('Excursion-' + groupId + 'Menu').style.display;

	if (display == "inline") {
		$('Excursion-' + groupId + 'Menu').style.display = "none";
		$('Excursion-' + groupId + "Point").src = "images/treemenu_main_icon_default.gif";

	} else {
		// Retrieve the Excursions (from DB or caching)

		if (this.caching.containsKey("Excursion-" + groupId)) {
			receiveTree(this.caching.get("Excursion-" + groupId));

		} else {
			var params = "Action=GetTree&LocationId=" + locationId + "&Part=Excursion&Id=" + groupId;

			var ajax = new Ajax.Request(
				"getinfo.php",
				{
					method: "post",
					parameters: params,
					onSuccess: receiveTree,
					onFailure: function (response) { alert('There was a problem with the request (getExcursions).' + response.responseText); }
				}
			);
		}

		// Change Tree icon
		$('Excursion-' + groupId + 'Menu').style.display = "inline";
		$('Excursion-' + groupId + "Point").src = "images/treemenu_main_icon_open.gif";

	}
}

function getHotels(groupId) {
	var display = $('Hotel-' + groupId + 'Menu').style.display;

	if (display == "inline") {
		$('Hotel-' + groupId + 'Menu').style.display = "none";
		$('Hotel-' + groupId + "Point").src = "images/treemenu_main_icon_default.gif";

	} else {
		// Retrieve the Hotels (from DB or caching)

		if (this.caching.containsKey("Hotel-" + groupId)) {
			receiveTree(this.caching.get("Hotel-" + groupId));

		} else {
			var params = "Action=GetTree&LocationId=" + locationId + "&Part=Hotel&Id=" + groupId;

			var ajax = new Ajax.Request(
				"getinfo.php",
				{
					method: "post",
					parameters: params,
					onSuccess: receiveTree,
					onFailure: function (response) { alert('There was a problem with the request (getHotels).' + response.responseText); }
				}
			);
		}

		// Change Tree icon
		$('Hotel-' + groupId + 'Menu').style.display = "inline";
		$('Hotel-' + groupId + "Point").src = "images/treemenu_main_icon_open.gif";

	}
}

function setDivBusy(div) {
	$(div).innerHTML = '<img src="images/busy.gif" alt="Please wait..." />' ;
}

