// Hover over image gallery
// Ryan Robison

(function($) {

    $.fn.swap_gallery = function(options) {
        return this.each(function() {   
            $.image_gallery(this, options);
        });
    };
		
		//get the settings for the image_gallery
		
		/* 	
				opacity = level of opacity
				bgColor = background color of thumbnails container
				outTime = this is the time it takes to fadeOut the images
				inTime =  this is the time it takes to fadeIn the images
				cursor = select the cursor type you wish to have displayed
				clickable = Yes sets the thumbnails to have to be clicked in order to change the main image
				url = sets the url for a link on the main image based off of the thumnails name attribute.
		*/

    $.image_gallery = function(container, options) {
        var settings = {
        	'opacity':    			0,							
					'bgColor':					'#000',
					'outTime':					100,
					'inTime':						100,
					'cursor':						'default',
					'clickable':				'no',
					'url':							'no'
        };
				
				// extend the settings
				
				if (options) {
            $.extend(settings, options);
				}
				
				image_container = "#" + container.id;
				
				$(image_container).css({'background' : settings.bgColor});
				
				thumb_image = "#" + container.id + " img:a";
				
				if(settings.clickable == 'no') {				
					$(thumb_image).click(function(){
						return false;
					});
					
					$(thumb_image).mouseover(function(){
					$(this).parent().stop().animate({opacity: settings.opacity}, settings.outTime);
					$(this).css({'cursor' : settings.cursor});
					
					var image_source = $(this).parent().attr("href");
					
					$("#main_image").fadeOut(50, function() {
						$("#main_image").attr({ src: image_source }).fadeIn(100);
					});		
					
					return false;
					}); //end mouseover
				
					$(thumb_image).mouseout(function(){
						$(this).parent().stop().animate({opacity:1}, settings.inTime);
					}); //end mouseout
					
				} else {
										
					$(thumb_image).mouseover(function(){
						$(this).parent().stop().animate({opacity: settings.opacity}, settings.outTime);
						$(this).css({'cursor' : settings.cursor});
						return false;
					}); //end mouseover
					
					$(thumb_image).mouseout(function(){
						$(this).parent().stop().animate({opacity:1}, settings.inTime);
					}); //end mouseout
					
					$("#main_image").mouseover(function(){
						$(this).parent().stop().animate({opacity: settings.opacity}, settings.outTime);
						$(this).css({'cursor' : settings.cursor});
						return false;
					}); //end mouseover
					
					$("#main_image").mouseout(function(){
						$(this).parent().stop().animate({opacity:1}, settings.inTime);
					}); //end mouseout
					
					$(thumb_image).click(function(){
																				
						var image_source = $(this).parent().attr("href");
						
						if(settings.url == 'yes'){
							
							var image_url = $(this).parent().attr("name");
							
							$("#main_image").fadeOut(50, function() {														
								$("#main_image").attr({ src: image_source }).fadeIn(100);
								$(".location").attr({ href: image_url });
							}); //end fadeOut
							
						} else {
						
							$("#main_image").fadeOut(50, function() {														
								$("#main_image").attr({ src: image_source }).fadeIn(100);
								$(".location").attr({ href: image_url });
							}); //end fadeOut
							
						}
					
						return false;
						
					}); //end click					
					
				} //end settings
				
    };

})(jQuery);

/*
jQuery.preloadImages = function(){
  	for(var i = 0; i<arguments.length; i++){
    	jQuery("<img>").attr("src", arguments[i]);
  	}
}	

*/