$(document).ready(function() {

	// Hide main popup
	$('#popup').hide();

	// Create popup mask
	jQuery('<div id="popup_mask" />').appendTo(document.body);

	// Select all popup links
	$('.popup_link').click(function(e) {

		// Cancel the link behavior
		e.preventDefault();

		// Set height and width to mask to fill up the whole screen
		$('#popup_mask').css({'width':$(window).width(), 'height':$(document).height()});
		
		// Mask transition effect
		$('#popup_mask').fadeTo(400, 0.8); 

		ChangeContent($('#' + $(this).attr('href').split('#')[1]));
	});

	// Hide popup contents
	$('.popup_content').hide();

	// If mask is clicked
	$('#popup_mask').click(function (e) {
		HidePopup();
	});

	// If a key is pressed
	$(document).keyup(function (e) {
		// Escape
		if ( e.keyCode == 27 ) {
			HidePopup();
		}
		// Left
		else if ( e.keyCode == 37 ) {
			$('#popup .previous').click();
		}
		// Right
		else if ( e.keyCode == 39 ) {
			$('#popup .next').click();
		}
	});


	function ChangeContent(popup_content)
	{
		// Get document body dimensions before setting new content
		var body_width = GetClientWidth();
		var body_height = GetClientHeight();

		// Set new HTML content to popup
		$('#popup').html($(popup_content).html());

		// Get popup image
		var img = $('#popup img:first');
		var ratio = parseInt($(img).css('width')) / parseInt($(img).css('height'));

		// Check if popup is larger than document body
		if ( $('#popup').width() > body_width )
		{
			$(img).attr('width', body_width - ($('#popup').width() - parseInt($(img).css('width'))) - 50);
			$(img).attr('height', parseInt($(img).css('width')) / ratio);
		}
		// Check if popup is higher than document body
		if ( $('#popup').height() > body_height )
		{
			$(img).attr('height', body_height - ($('#popup').height() - parseInt($(img).css('height'))) - 50);
			$(img).attr('width', parseInt($(img).css('height')) * ratio);
		}

		$('#popup').width(parseInt($(img).css('width')));

		// Centererize popup
		var top = $(window).height() / 2 - $('#popup').height() / 2;
		var left = $(window).width() / 2 - $('#popup').width() / 2;
		$('#popup').css('left', left > 0 ? left : 0);
		$('#popup').css('top',  top > 0 ? top : 0);

		// Position navigation
		$('#popup .nav, #popup .previous, #popup .next').css({'height':parseInt($(img).css('height'))});

		// If close button is clicked
		$('#popup .close').click(function (e) {
			// Cancel the link behavior
			e.preventDefault();
			HidePopup();
		});

		// If previous button is clicked
		$('#popup .previous').click(function (e) {
			// Cancel the link behavior
			e.preventDefault();
			ChangeContent($('#' + $(e.target).attr('href').split('#')[1]));
		});
		// If previous button 2 is clicked
		$('#popup .previous2').click(function (e) {
			$('#popup .previous').click();
		});

		// If next button is clicked
		$('#popup .next').click(function (e) {
			// Cancel the link behavior
			e.preventDefault();
			ChangeContent($('#' + $(e.target).attr('href').split('#')[1]));
		});
		// If next button 2 is clicked
		$('#popup .next2').click(function (e) {
			$('#popup .next').click();
		});

		// Navigation hover (background needs to be set here because of IE)
		$('#popup .previous, #popup .next').css({'background':'transparent url(/plugin/cms/template/peter_kammermann/images/blank.gif) no-repeat'});
		$('#popup .previous').hover(function () {
			$(this).css({'background':'url(/plugin/cms/template/peter_kammermann/images/previous.png) left center no-repeat'});
		}, function () {
			$(this).css({'background':'transparent url(/plugin/cms/template/peter_kammermann/images/blank.gif) no-repeat'});
		});
		$('#popup .next').hover(function () {
			$(this).css({'background':'url(/plugin/cms/template/peter_kammermann/images/next.png) right center no-repeat'});
		}, function () {
			$(this).css({'background':'transparent url(/plugin/cms/template/peter_kammermann/images/blank.gif) no-repeat'});
		});

		// todo: If popup is already visible morph to new content size
		if ( $('#popup').is(':visible') )
		{
		}
		else
		{
			$('#popup').fadeIn(400);
		}
	}


	function HidePopup()
	{
		$('#popup_mask').fadeOut(400);
		$('#popup').fadeOut(400);
	}


	function GetClientWidth()
	{
		var D = document;
		return Math.max(Math.max(D.body.scrollWidth, D.documentElement.scrollWidth), Math.max(D.body.offsetWidth, D.documentElement.offsetWidth), Math.max(D.body.clientWidth, D.documentElement.clientWidth));
	}


	function GetClientHeight()
	{
		var D = document;
		return Math.max(Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), Math.max(D.body.clientHeight, D.documentElement.clientHeight));
	}
});

