//http://malsup.com/jquery/cycle/start.html

$(document).ready(function() {
	init_menu();
});

function init_menu()
{
	$("#menu li:not(.active), .tabbar li:not(.active)").hover(function() {
		$(this).addClass("active");
	}, function(){
		$(this).removeClass("active");
	});
	
	$("#menu li, .tabbar ul:not([control]) li").click(function() {
		location.href = $(this).find("a").attr("href");
	});
	
	$(".tabbar ul[control] li a").click(function(event) {
		event.stopPropagation();
		event.preventDefault();
		$(this).parent().click();
	});
	
	$(".tabbar ul[control] li").click(function(event) {
		event.stopPropagation();
		event.preventDefault();
		var lis = $(this).parent().children();
		for(var i = 0; i < lis.length; i++)
		{
			if($(this).get(0) == lis.get(i))
				break;
		}
		lis.unbind('mouseenter');
		lis.unbind('mouseleave');
		lis.removeClass("active");
		$(this).addClass("active");
		//XXX this sucks but noone pays for nonsuckage here
		$(".tabbar li:not(.active)").hover(function() {
			$(this).addClass("active");
		}, function(){
			$(this).removeClass("active");
		});
		var control = $(this).parent().attr("control");
		$("div[controlledby=" + control + "]").each(function() {
			//XXX this is needed because hidden gmaps iframe contents are displayed incorrectly
			if($(this).children().get(0).tagName == "IFRAME")
			{
				$(this).children().eq(0).attr("src", $(this).children().eq(i).attr("src"));
			}
			else
			{
				$(this).children().hide();
				$(this).children().eq(i).show();
			}
		});
	});
}
