////////////////////////////////////////////////////////////////////////////
// common
// //////////////////////////////////////////////////////////////////////////

function doJsonCall(jsonUrl) {
	$.ajax( {
		url :jsonUrl,
		async :true,
		dataType :"script",
		scriptCharset :"utf-8",
		cache :true
	});
}

// ////////////////////////////////////////////////////////////////////////////
// primary and secondary navigation
// //////////////////////////////////////////////////////////////////////////

function doNavigation(path, data) {
	if ((typeof (path) == "undefined") || (path.length == 0) || (data.length == 0)) {
		return;
	}
	// primary navigation
	var ul = $("#mainNavigation");
	//var ul = $("<ul />");
	ul.empty();

	var activeHubPage;

	for (var i = 0; i < data.length; i++) {
		var node = data[i];
		var li = renderPrimaryNavigationLi(node);
		if ((path.length > 1) && (path[1].href == node.href)) {
			activeHubPage = node;
			li.attr("class", "active");
		}

		var renderSubNodes = node.children;
		if (!renderSubNodes) {
			continue;
		}
		var subul = $("<ul/>");
		subul.css("display", "none");

		for ( var j = 0; j < node.children.length; j++) {
			subul.append(renderPrimaryNavigationLi(node.children[j]));
		}
		li.append(subul);
		addHoverIntentEffect(li);
		ul.append(li);
	}

	// secondary navigation
	if (typeof (activeHubPage) != "undefined") {
		ul = $("#secondaryNavigation");
		ul.empty();
		for (i = 0; i < activeHubPage.children.length; i++) {
			node = activeHubPage.children[i];
			li = renderSecondaryNavigationLi(node, ((path.length > 2) && (path[2].href == node.href)));
			li.appendTo(ul);
		}
	}

	// breadcrumbs
	var breadcrumbsHtml = "";
	for (i = 0; i < path.length; i++) {
		if (i == path.length - 1) {
			breadcrumbsHtml += path[i].name;
		} else {
			breadcrumbsHtml += "<a href=" + path[i].href + ">" + path[i].name + "</a> / ";
		}
	}
	$("#breadcrumbs").html(breadcrumbsHtml);

	// fix breadcrumbs size on home page
	if (path.length == 1) {
		$("#breadcrumbs").attr("style", "font-size: 11px");
	}
	

}

function addHoverIntentEffect(li) {
	li.unbind();
	li.hoverIntent({
		sensitivity :1,
		interval :100,
		over: function() {
			$(this).children("ul").slideDown({
				duration: 300,
				easing: "easeOutCirc"
			});
		},
	timeout: 100,
	out: function() {
		$(this).children("ul").slideUp({
			duration: 100,
			easing: "easeOutCirc"
		});
	}
	});
}

function renderPrimaryNavigationLi(node) {
	var li = $('<li/>');
	li.html('<a href="' + node.href + '">' + node.name + '</a>');
	return li;
}

function renderSecondaryNavigationLi(node, active) {
	var a = $("<a/>");
	a.attr("href", node.href)
	a.html(node.name);
	if (active) {
		a.attr("class", "active");
	}

	var li = $("<li/>");
	li.append(a);
	return li;
}

// ////////////////////////////////////////////////////////////////////////////
// other sites
// //////////////////////////////////////////////////////////////////////////

function doOtherSites(data) {
	var ul = $('<ul id="inOtherSites"/>');
	$.each(data, function(i, item) {
		var li = $('<li/>');
		var a = $('<a/>');
		a.css('background-image', 'url("' + item.image + '")');
		a.css('background-position', 'left');
		a.css('background-repeat', 'no-repeat');
		a.attr('href', item.href);
		a.attr('title', item.name + ' - ' + item.summary);
		var strong = $('<strong/>');
		strong.text(item.name);
		strong.appendTo(a);
		a.append('<br />' + item.summary);
		a.appendTo(li);
		li.appendTo(ul);
	});

	if (ul) {
		$('#inOtherSites').replaceWith(ul);
	}
}

// ////////////////////////////////////////////////////////////////////////////
// languages
// //////////////////////////////////////////////////////////////////////////

function doLanguages(data) {
	var countries = $('#popup div.body div.countries');
	countries.empty();
	var div = $("<div />");
	div.css("border", "none");
	for (var i = 0; i < data.length; i++) {
		var a = $("<a />");
		a.attr("href", data[i].link)
		a.html(data[i].name);
		countries.append(a);
		countries.append($("<br />"));
	}
}
