var overlayHandler = {
	init: function( $pictureLinks )
	{
		var parent = this;
		$pictureLinks.click( function( event ) {overlayHandler.show( event, parent, $(this) );}); //passes reference to parent
		return;
	},
	
	show: function( event, parent, $object )
	{
		//loads and sets the image element	
		var $picture = $('<img src="' + $object.attr('href') + '" alt="' + $object.attr('title') + '" />' )
			.load(function() //fire when image is loaded
			{ 
				var $overlayBg			= $('<div id="overlay-bg"></div>')
																.css({
																	'position': 'fixed',
																	'top': '0px',
																	'left': '0px',
																	'width': '100%',
																	'height': '100%',
																	'background': '#000',
																	'z-index': '1000',
																	'opacity': '0.5'
																})
																.click( parent.closeOverlay )
																.prependTo('body');

				var $overlayContent	= $('<div id="overlay-content"></div>')
																.append( $picture )
																.insertAfter( $overlayBg )
																.click( parent.closeOverlay );
				

				$overlayContent.css({
					'position': 'absolute',
					'left': ( ($(window).width() / 2) - ($picture.width() / 2) ) + 'px',
					'top': ( ($(window).height() / 2) - ($picture.height() / 2) ) + 'px',
					'width': $picture.width() + 'px',
					'height': $picture.height() + 'px',
					'z-index': '1500'
				})
				
				//moves content on scroll / resize
				parent.moveContent( $overlayContent );
				$(window).bind( 'scroll resize', function() {parent.moveContent( $overlayContent );} );
				
				/* IE 6 reports height as < windows height event though set to 100% when position fixed,
					 ergo; we set it to absolute and moves it on scroll & resize!
				*/
				if( $overlayBg.height() < $(window).height() )
				{
					$overlayBg.add( $overlayContent ).css( 'position', 'absolute' );
					parent.setOverlaySize( $overlayBg );		
					$(window).bind( 'scroll resize', function() {parent.setOverlaySize( $overlayBg );} );
				}
			});
					
		event.preventDefault(); //prevents the link from opening
		return;
	},
	
	closeOverlay: function() 
	{
		$('#overlay-bg').remove(); //removes overlay
		$('#overlay-content').remove();
		$(window).unbind('scroll resize', overlayHandler.setOverlaySize); //cleans up
		return;	
	},
	
	setOverlaySize: function()
	{
		$.each( arguments, function() {
			$(this).css({
				'top': $(window).scrollTop() + 'px',
				'height': $(window).height() + 'px'
			});	
		});
	},
	
	moveContent: function()
	{
		$.each( arguments, function() {	
			$(this).css({
				'left': ( ($(window).width() / 2) - ($(this).width() / 2) ) + $(window).scrollLeft() + 'px',
				'top': ( ($(window).height() / 2) - ($(this).height() / 2) ) + $(window).scrollTop() + 'px'
			});
		});
	}
};