/**
 * RejGallery
 * Simple gallery plugin that fades in images taken from link hrefs
 * V0.1 - Luke Robinson
 */
 
$.fn.rejGallery = function(settings) {
	
	// Defaults
	var settings = jQuery.extend(
	{
		'anchorWrap'	: 	null,
		'imageWrap'		:	null,
		'height'		:	340,
		'width'			:	321,
		'onComplete'	:	null
	},settings);			

	// Declarations
	var primaryImage = settings.imageWrap.find('img');

	// Init
	settings.imageWrap.css('position','relative');
	
	// Anchor click function
	$('a',settings.anchorWrap).click(function(){
		
		// Remove existing temporary image
		$("#temp_image").remove();
		
		// Create string for new temporary image
		var imageToLoad = 	'<img id="temp_image" style="display:none;position:absolute;z-index:999;top:0;left:0;" '+
							'src="' + $(this).attr('href') + '" '+
							'width="' + settings.width + '" '+
							'height="' + settings.height + '" />';
		
		// Insert temporary image into DOM
		primaryImage.before(imageToLoad);
	
		// Attach a load function to temporary image so we know when we can show it
		$("#temp_image").load(function(){
				
				var tempImage = $(this);
				
				tempImage.fadeIn('fast',function(){
					
					// Faded in, set the temp image as primary image
					var tempImageURL = tempImage.attr('src');
					primaryImage.attr('src',tempImageURL);
					
					// Call our complete event function if one exists
					if($.isFunction(settings.onComplete)) {
						settings.onComplete(primaryImage);
					}
																							   
				});
									   
		});
		
		return false;
			   
	});
	
	return $(this);

};