jQuery(function($){

 	$.fn.extend({ 

 		imageSlider2: function( options ){					
			
			// --- Defaults ---------------------

			var defaults = {
				timeout: 					7000,
				fadeSpeed: 					'slow',
				selectorOverlay: 			'',
				overlayHeight: 				140,
				overlayOpacity: 			'.8',
				overlayFadeSpeed: 			400,
				overlayEnabled: 			true
			}

			var options =  $.extend(defaults, options);

			// --- Main ---------------------

    		return this.each(function(){
				
				var $elem 	= $(this);
				var o 		= options;
				
				var contenedor 	= $(this);
				var slider 		= create_interval( o, $elem );
				
				if( o.overlayEnabled==true ){
					var overlay 	= $(o.selectorOverlay);
					overlay.css({ opacity : '0' }).find('.bg').css({ 'opacity' : o.overlayOpacity });
				}

				$elem
				.mouseenter( function(){
					
					slideshowFlag = true;
					
					if( o.overlayEnabled==true )					
						update_overlay( contenedor, overlay );
					
					setTimeout(function(){

						clearInterval( slider );
						
						if( o.overlayEnabled==true )
							show_overlay( o, overlay );

					}, 100);
					
				})
				.mouseleave( function(){
					
					slideshowFlag = false;

					setTimeout(function(){
						
						if( o.overlayEnabled==true )
							hide_overlay( o, overlay );
						
						clearInterval( slider );
						slider = create_interval( o, $elem );
						
					}, 100);
					
				});
				

    		}); // return end

			function slideSwitch( o, $elem ) {
			    var $active = $('img.active', $elem);

			    if ( $active.length == 0 ) $active = $('img:last', $elem);

			    // use this to pull the images in the order they appear in the markup
			    var $next =  $active.next('img').length ? $active.next('img') : $('img:first', $elem);
			
				$active.addClass('last-active');
			    $next.css({opacity: 0.0})
			        .addClass('active')
			        .animate({opacity: 1.0}, 1000, function() {
			            $active.removeClass('active last-active');
			        });
			}

			function create_interval( o, $elem ){
				// console.log('Interval created.');
				return setInterval( function(){
					slideSwitch( o, $elem );
				}, o.timeout );
			}

			function show_overlay( o, overlay ){
				// console.log('Interval created.');
				overlay.animate({ top : '322px', opacity : '1' }, o.overlayFadeSpeed);
			}
			function hide_overlay( o, overlay ){
				// console.log('Interval created.');
				overlay.animate({ top : '442px', opacity : '0' }, o.overlayFadeSpeed);
			}
			function update_overlay( contenedor, overlay ){
				var imagen 	= $('img.active', contenedor);
				var titulo 	= imagen.attr('alt');
				var texto 	= imagen.attr('rel');
				var link	= imagen.attr('link');
				
				overlay.find('h1').html( titulo );
				overlay.find('p').html( texto );
				
				if( link ){
					var a = overlay.find('.inner').find('a');
					if( a.size()==0 ){
						a = $('<a class="slide-link" href="" target="_self">Read More &raquo;</a>').appendTo( overlay.find('.inner') );
					}
					a.attr('href', link);
					
				} else {
					overlay.find('.inner').find('a').remove();
				}
			}

    	} // extend end

	}); // wrapper end

});