/* Global variables */

var windowWidth, 
	windowHeight;

/**
 * AJAX for smooth page loading without navigating away from the current
**/

function loadPage(pageUrl)
{
	$.ajax({
			async: false,
			url : pageUrl,
			success: function(response)
			{
				var newBody = "";

				/* Filters only new content */
				for (i = 0; i < response.length; i++)
				{
					if (response.substr(i, 27) == '<div id="contentContainer">')
					{
						newBody = response.substr(i+27);

						break;
					}
				}

				for (i = 0; i < newBody.length; i++)
				{
					if (newBody.substr(i, 30) == '</div><!--/contentContainer-->')
					{

						newBody = newBody.substr(0, i);

						break;
					}
				}

				/* Appends new content */
				$("#contentContainer").fadeOut(50, function()
				{
					$(this).empty().html(newBody);

					fixBrowserBugs();


					$(this).fadeIn(200);

					$("#panoramaBig img").load(function()
					{
						calcPanoramaMaxHeight();
						resizePanoramaTable();
						
						$("#panoramaBig img").unbind("load");
					});
				});
			}
	});
}

/**
 * Resizes the panoramic image table and table columns based on the panorama width 
**/

/*function resizePanoramaTable()
{
	var minTableWidth = 500;

	if ($("#photoPan").length > 0)
	{
		var panoramaWidth = $("#panoramaBig img").outerWidth();

		if (panoramaWidth >= minTableWidth)
		{
			$("#details").css("width", panoramaWidth);
		}
	}
}*/

/**
 * Calculates and sets the max height of the panorama
**/

function calcPanoramaMaxHeight()
{
	if ($("#photoPan").length > 0)
	{
		var strBuffer = "",
			tolerance = 60,
			maxHeight = $("#main").height() - $("#pagination").outerHeight(true) - $("table#details").outerHeight(true) - $(".panReferenceKeyword").outerHeight(true) - tolerance;

		$("#panoramaBig img").hide();
		$("#panoramaBig img").css("max-height", maxHeight);
		$("#panoramaBig img").show();
	}
}

/**
 * Vertically aligns the master wrapper 
**/ 

function handleResize()
{
	var newTop = 0;

	newTop = (windowHeight / 2) - ($("#wrapper").outerHeight(true) / 2)
	newTop = (newTop <= 0) ? 0 : newTop;

	$("#wrapper").css("margin-top", newTop);
}

/**
 * Window resize event. Gets the new width & height and aligns the containers based on that
**/

$(window).resize(function()
{	
	/* Gets the current dimensions of the window */
	windowWidth = $(window).width();
	windowHeight = $(window).height();
	
	/* Wrapper centering */
	handleResize();
});

function fixBrowserBugs()
{
	/* Chrome/Safari check bug fix */
	var userAgent = navigator.userAgent.toLowerCase(); 
	$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase()); 
	if ($.browser.chrome)
	{
		$.browser.safari = false;
	}
	
	/* IE6 no max-width and max-height fix */
	if ($(".content#gallery, .content#calendar, .content#searchResults").length > 0)
	{
		$("#photoGrid img").each(function()
		{
			if ($(this).width() > $(this).height())
			{
				$(this).addClass("horizontal");
			}
			else
			{
				$(this).addClass("vertical");
			}
		});
	}
	
	/* Navigation font smoothing fix in safari */
	if ($.browser.safari)
	{
		$("nav").addClass("safari");
	}
}

/**
 * Window load event. Triggers after the images are loaded, so the panoramic table width can be calculated
**/

$(window).load(function()
{
	//resizePanoramaTable();

	/* Fixes annoying browser bugs */
	fixBrowserBugs();
});

$(document).ready(function()
{
	/* Gets the current dimensions of the window */
	windowWidth = $(window).width();
	windowHeight = $(window).height();
	
	/* Wrapper centering */
	handleResize();
	
	calcPanoramaMaxHeight();
	
	/* IE 11 support left ( if then finally supports history.pushState ... ) */
	if (($.browser.msie && $.browser.version.substr(0,1) > 10) || (!$.browser.msie))
	{
		/* AJAX page loading */
		/*$("a.jspage").live("click", function(event)
		{
			var url = $(this).attr("href"),
				title = "Must see Bulgaria";

			if (($(this).attr("title") != null) && ($(this).attr("title") != ""))
			{
				title += " :: "+$(this).attr("title")
			}

			History.pushState(null, title, url);

			event.preventDefault();
			return false;
		});*/

		/* History handling */
		/*History.Adapter.bind(window, 'statechange', function()
		{
			var State = History.getState();

			loadPage(State.url);
		});*/
	}
	
	$('.majorKeyword a, .panReferenceKeyword a').click(function()
	{		
		$.ajax
			({	async: false,
				cache: false,
				type: "POST",
				url : "/ajax/",
				data: "action=save_see_more_ref&ref="+window.location.pathname				
			});		
	});
	
	if (($.browser.msie && $.browser.version.substr(0,1) >= 9) || (!$.browser.msie))
	{
		/* Hide & Show information events */

		$("a.hsBar#hide").live("click", function()
		{
			$("a.hsBar#hide").css("display", "none");
			$("a.hsBar#show").css("display", "block");

			$(".content").stop().slideUp();
			
			return false;
		});

		$("a.hsBar#show").live("click", function()
		{
			$("a.hsBar#show").css("display", "none");
			$("a.hsBar#hide").css("display", "block");
			
			$(".content").stop().slideDown();
			
			return false;
		});
	}
	else
	{
		$("a.hsBar#hide").live("click", function()
		{
			$("a.hsBar#hide").css("display", "none");
			$("a.hsBar#show").css("display", "block");
			
			$(".content").hide();
			
			return false;
		});
		
		$("a.hsBar#show").live("click", function()
		{
			$("a.hsBar#show").css("display", "none");
			$("a.hsBar#hide").css("display", "block");
			
			$(".content").show();
			
			return false;
		});
	}
});

