
$(document).ready(function()
{
	controller.init();
	
	$(window).hashchange(function()
	{
		var hash = location.hash.replace('#', '');
		
		if (hash != '')
		{
			window.location.hash = hash;
			
			controller.ajax(hash);
		}
	});
	
	$(window).hashchange();
	
	controller.popup.loading = $('.popup').html();
});

var controller = 
{
	ajax : function(url)
	{
		var ajax_url 	= url + '?ajax=1';
		
		var body 	= $('#body');
		var title 	= $('#title');
		var content = $('#content');
		
		title.fadeOut('fast');
		content.fadeOut('fast', function()
		{
			body.addClass('loading');
			
			$.ajax(
			{
				url 		: ajax_url,
				dataType 	: 'json',
				success 	: function(data)
				{
					var pageTitle 	= data.title;
					var pageContent = data.body;
					
					body.removeClass('loading');
					
					title.html(pageTitle);
					title.fadeIn('fast');
					
					content.html(pageContent);
					content.fadeIn('fast');
					
					if (pageTitle == false)
					{
						document.title = 'Cassettari.org';
					}
					else
					{
						document.title = pageTitle;
					}
					
					controller.init();
				},
				error 		: function (response)
				{
					/*$('#debug').html('');
					for(var prop in response)
					{
						$('#debug').html($('#debug').html() + '<br />' + prop + ': ' + response[prop]);
					}*/
					
					var pageTitle 	= '';
					var pageContent = '';
					
					switch (response.status)
					{
						case 404:
							pageContent = '<h3>Page Not Found</h3><p>Sorry, that page could not be found.</p>';
						break;
					}
					
					body.removeClass('loading');
					title.html(pageTitle);
					title.fadeIn('fast');
					content.html(pageContent);
					content.fadeIn('fast');
				}
			});
		});
	},
	init : function()
	{
		$('a.ajax').click(function(e)
		{
			window.location.hash = $(this).attr("href");
			
			e.preventDefault();
		});
		
		$('a[rel=external]').attr('target', '_blank');
	
		var window_height 	= $(window).height();
		var body_height 	= $('#content').outerHeight();
		
		if (body_height < window_height)
		{
			$('html').css('height', '100%');
			$('body').css('height', '100%');
		}
		else
		{
			$('html').css('height', 'auto');
			$('body').css('height', '100%');
		}
		
		$(".popup_show").click(function(e)
		{
			e.preventDefault();
			
			controller.popup.show(this.href);
		});
		
		$(".popup_hide").click(function(e)
		{
			e.preventDefault();
			
			controller.popup.hide();
		});
	},
	popup :
	{
		loading : null,
		image 	: null,
		visible : false,
		show 	: function(url)
		{
			if (!controller.popup.visible)
			{
				$(".popup").html(controller.popup.loading);
				$(".popup").css(
				{
					'width'  : '28px',
					'height' : '28px'
				});
				
				controller.popup.centre();
				
				$(".popup_background").css("opacity", "0.7");
				$(".popup_background").fadeIn("slow");
				$(".popup").fadeIn("slow");
				
				controller.popup.image = new Image();
				
				$(controller.popup.image).load(function()
				{
					controller.popup.adjust();
				});
				
				controller.popup.image.src = url;
				
				controller.popup.visible = true;
			}
		},
		adjust 	: function()
		{
			var image 	= $(controller.popup.image);
			
			image.hide();
				
			$(".popup").html(image);
			
			var h = image.height();
			var w = image.width();
			var y = ($(window).height() - h) / 2;
			var x = ($(window).width()  - w) / 2;
			
			$(".popup").animate(
			{
				width 	: w + 'px',
				height 	: h + 'px',
				top 	: y + 'px',
				left 	: x + 'px'
			},
			'slow',
			function()
			{
				image.fadeIn();
			});
		},
		hide 	: function()
		{
			if (controller.popup.visible)
			{
				$(".popup_background").fadeOut("slow");
				$(".popup").fadeOut("slow");
				
				controller.popup.visible = false;
			}
		},
		centre 	: function()
		{
			var popup = $('.popup');
			
			popup.css('top',  ($(window).height() - popup.outerHeight()) / 2 + 'px');
    		popup.css('left', ($(window).width()  - popup.outerWidth() ) / 2 + 'px');
		}
	}
};
