$(document).ready(function() {
	
	setWindowManager();
	setContactManager();
	
});

function setContactManager() {
	
	$('#contact form').submit(function(event) {
		
		event.preventDefault();
		
		var contentLocation 	= '/include/script/mailer.php';
		var dataString 			= $(this).serialize();
		
		$.ajax({
			type: 		'POST',
			url: 		contentLocation,
			dataType: 	'JSON',
			data: 		dataString,
			success: 	function(data) {
				
				alert(data.response);
				
				// TODO notify the user that the message was delivered
				
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				
				// TODO We should do some kind of error reporting on here
				
			}
		});
		
	});
	
}

function setWindowManager() {
	
	$('#events').undelegate('a', 'click');
	$('#events').delegate('a', 'click', function(event) {
		
		var dataString = $(this).attr('href');
		
		event.preventDefault();
		
		startWindowManager(dataString);
		windowManagerDefault();
		
	});
	
}

function startWindowManager(dataString) {
	
	$('#window').empty();
	$('#window').html('<img src="' + dataString + '" />');
	
	var documentWidth 		= $(document).width();
	var documentHeight 		= $(document).height();
	var viewportWidth 		= $(window).width();
    	var viewportHeight 		= $(window).height();
	var windowHeight 		= $('#window').height();
	var windowWidth 		= $('#window').width();
	var windowOffset 		= $('#window').offset();
	
	// Set the opacity background to the page document size
	
	$('#opacity').css({
		'width'		: documentWidth,
		'height'	: documentHeight
	});
	
	// Sets the window to the center of the page document
	
	$('#window').css({  
		'position'	: 'absolute',  
		'top'		: windowOffset.top + (viewportHeight/2) - (windowHeight/2),
		'left'		: (viewportWidth/2) - (windowWidth/2)
	});
	
	$('#opacity').fadeIn('slow');
	$('#window').fadeIn('slow');
	
}

function windowManagerDefault() {
	
	$('#opacity').click(function(event) {
		
		event.preventDefault();
		
		windowManagerClose();
		
	});
	
	$('#window img').click(function(event) {
		
		event.preventDefault();
		
		windowManagerClose();
		
	});
	
}

function windowManagerClose() {
	
	$('#opacity').fadeOut('slow');
	$('#window').fadeOut('slow');
	
}
