////////////////////////////////////
//                                //
//  Created by Frank Mouthaan     //
//                                //
//  Version 1.0, 2009             //
//  Photo.Enlarge.js              //
//                                //
//  For enlarge photos in the     //
//  middle of the screen.         //
//                                //
////////////////////////////////////

$(document).ready(function() {


	$('.EnlargeMe').hover(function(event) {
		
		// Create the ENLARGED elements
		$("<div>").attr("class", "PhotoEnlarged").appendTo($("body"));
		$("<img>").attr('src', '').appendTo($(".PhotoEnlarged"));

		// Settings of the WINDOW
		var winH = $(window).height();
		var winW = $(window).width();
		var winO = $(window).scrollTop();

		// Find the source/link of the picture
		$('.PhotoEnlarged IMG').attr('src', $(this).attr('src'));

	  // Calculate the ENLARGED elements settings
		var fotH = $('.PhotoEnlarged').height();
		var fotW = $('.PhotoEnlarged').width();
		var fotO = $(this).offset();
		var fotoH = $(this).height();
		var fotoW = $(this).width();
		var top = ((winH / 2) + winO) - (fotH / 2);
		var left = (winW / 2) - (fotW / 2);

		// Create the HOVER elements
		if ( $(this).hasClass('LinkMe') ) {
			$("<a>").attr("class", "PhotoHover").appendTo($("body")).css({ 'top' : fotO.top + 'px', 'left' : fotO.left + 'px', 'height' : fotoH + 'px', 'width' : fotoW + 'px' });
			$('.PhotoHover').attr('href', $(this).parent().attr('href'));
		} else {
			$("<div>").attr("class", "PhotoHover").appendTo($("body")).css({ 'top' : fotO.top + 'px', 'left' : fotO.left + 'px', 'height' : fotoH + 'px', 'width' : fotoW + 'px' });
		}
		
	  // Give the ENLARGED elements there settings
		$('.PhotoEnlarged').css({ 'top' : top + 'px', 'left' : left + 'px'});
		$('.PhotoEnlarged').css({ 'opacity' : 0 }).show().animate({ 'opacity' : 1 }, 500);

	  // Remove all elements
		$(".PhotoHover").mouseleave(function() {
			$('.PhotoEnlarged').remove();
			$('.PhotoHover').remove();
		});

	}, function(event) {

	  // Remove all elements
    if ( !$(event.relatedTarget).is(".PhotoHover") ) {
			$('.PhotoEnlarged').remove();
			$('.PhotoHover').remove();
	  }

	});


});

