// jModal is written by James Brooks, 2009

$(document).ready(function() {
	// Select all links with the name "modal"
	$('a[name=modal]').click(function(e) {
		// Ignore the normal link behaviours
		e.preventDefault();
		
		// Get the href tag
		var id = $(this).attr('href');
	
		// Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		// Set height and width to mask and fill up the whole screen
		$('#mask').css({'width':maskWidth, 'height':maskHeight});
		
		// Transitions		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("fast",0.8);
	
		// Get the window height and width
		var winH = $(window).outerHeight('true');
		var winW = $(window).width();
              
		// Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		// Run the transition
		$(id).fadeIn(2000); 
	
	});
	
	// If the user clicks close
	$('.window .close').click(function (e) {
		// Ignore the normal link behaviours
		e.preventDefault();
		
		$('#mask').fadeOut(1000);	
		$('#mask').fadeTo("fast",0.8);
	//	$('#mask').hide();
		$('.window').hide();
		$('.cap').html('<img src="images/loader.gif" alt="Loading..." />');
	});		
	
	// If the mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
		$('.cap').html('<img src="images/loader.gif" alt="Loading..." />');
	});	
	
	// If the user presses ESC
	$(document).keydown(function(k) {
		if(k.which == 27) {
			$('#mask').hide();
			$('.window').hide();
			$('.cap').html('<img src="images/loader.gif" alt="Loading..." />');
		}
	});

});
