var hideDropdownsFlag = false;
var globalCurrentHoveredNavItem = -1;
var navigationTimer;

function hideDropdown()
{
	if (hideDropdownsFlag)
		$(".navigation li:not(li li) ul:first").slideUp();
}

function popupWindow(url, title, attr, top, left)
{
	var popup = window.open(url, title, attr);
	popup.moveTo(top, left);
	popup.document.body.style.padding = "0";
	popup.document.body.style.margin = "0";
	
	return false;
}

function showNavDropdown()
{
	$(".navigation li:not(li li):eq(" + globalCurrentHoveredNavItem + ")").find("ul:first").slideDown();
}

$(document).ready(function()
{
	/*$(".navigation:not(.index .navigation) li:not(li li)")
		.hover(function() { $(this).find("ul:first").slideDown(); }, function() { $(this).find("ul:first").slideUp(); });
		
	$(".index .navigation li:not(li li)")
		.hover(function() { $(this).find("ul:first").slideDown(); }, function() { $(this).find("ul:first").slideUp(); });
		
		<a href="img-6.gif">
			<img class="alignleft size-thumbnail wp-image-124" title="img-6" src="img-6-150x150.gif" alt="" width="150" height="150" />
		</a>
		
	*/
	
	$(".navigation li:not(li li)").each(function()
	{
		var thisUl = $(this).find("ul:first");
		var thisUlHTML = thisUl.html();
		var thisUlParent = thisUl.parent();
				
		thisUl.remove();
		thisUlParent.append(thisUlHTML);
	});
	
	$(".navigation ul li ul").each(function()
	{
		var thisUL = $(this);
		if (thisUL.find("li").length == 0)
			thisUL.remove();
	});
	
	$("a[href$='.png'], a[href$='.gif'], a[href$='.jpg']").each(function()
	{
		var thisLink = $(this);
		var thisImage = $(this).find("img:first");
		thisLink.click(function()
		{
			var baseURL = $("link[rel=index]").attr("href");
			var popup = window.open(baseURL + "/wp-content/themes/peter/image.php?image=" + thisLink.attr("href"), thisImage.attr("title"), "location=0,status=0,menubar=0,width=0,height=0");
			popup.moveTo(100, 100);
			
			return false;
		});
	});
	
	$(".navigation ul li:not(li li) ul").each(function()
	{
		$(this).find("li:last").addClass("bottom");
	});
	$(".footer ul li:last").addClass("last");
	
	if ($(".navigation li.news.selected").length > 0) $(".header").removeClass().addClass("header header-green");
	if ($(".navigation li.about-him.selected").length > 0) $(".header").removeClass().addClass("header header-orange");
	if ($(".navigation li.about-books.selected").length > 0) $(".header").removeClass().addClass("header header-red");
	if ($(".navigation li.interactive.selected").length > 0) $(".header").removeClass().addClass("header header-purple");
	if ($(".navigation li.author-visits.selected").length > 0) $(".header").removeClass().addClass("header header-blue");
	if ($(".navigation li.contact.selected").length > 0) $(".header").removeClass().addClass("header header-light-blue");
	
	$(".expandable").each(function()
	{
		var thisExp = $(this);
		$(this).find(".expandable-title").click(function()
		{
			thisExp.find(".expandable-content").slideToggle();
			return false;
		});
	});
		
	$(".navigation li:not(li li)").hover(
		function()
		{
			globalCurrentHoveredNavItem = $(".navigation li:not(li li)").index($(this));
			clearTimeout(navigationTimer);
			navigationTimer = setTimeout("showNavDropdown()", 500);
		},
		function()
		{
			clearTimeout(navigationTimer);
			$(".navigation li:not(li li):eq(" + globalCurrentHoveredNavItem + ")").find("ul:first").slideUp();
			globalCurrentHoveredNavItem = -1;
		}
	);
	
	$("a.dialogBox").each(function()
	{
		if ($(this).find(".alignleft").length > 0) $(this).addClass("alignleft");
		if ($(this).find(".alignright").length > 0) $(this).addClass("alignright");
	});
	
	$(".dialogBox").click(function()
	{
		var iFrame = $($(this).attr("href")).find(".iframe");
		var thisSrc = $($(this).attr("href")).find(".dialogBoxPopup-src").html();
		iFrame.attr("src", thisSrc);
		$($(this).attr("href")).show().dialog('open');
		$($(this).attr("href")).bind( "dialogbeforeclose", function(event, ui)
		{
			$(".layer").hide();
		});

		$(".layer").width($(document).width()).height($(document).height()).show();
		
		return false;
	});
	
	$(".layer").click(function()
	{
		$(".dialogBox").each(function()
		{
			$($(this).attr("href")).dialog('close');
		});
		return false;
	});
	
	$('.dialogBoxPopup').each(function()
	{
		var width = parseFloat($(this).find(".dialogBoxPopup-width").html());
		var height = parseFloat($(this).find(".dialogBoxPopup-height").html());
		
		if (width == 0 || !width) width = "auto";
		if (height == 0 || !height) height = "auto";
		if ($(this).find("iframe").length > 0) { width = "auto"; height = "auto"; }
		
		$(this).dialog({
			autoOpen: false,
			width: width,
			height: height,
			resizable: false
		});
	});
});
