(function( $ ){
  $.fn.addImageFader = function(options) {
    
    return this.each(function(){
    	var settings = {
				'effect'         : 'fade',
				'images'				 : [],
				'start_image'		 : 0,
				'folder_prefix'  : '',
				'autoswitch'     : true,
				'autoswitch_duration': 10001
      };
      
      // If options exist, lets merge them
      // with our default settings
      if ( options ) { 
        $.extend( settings, options );
      }
      
      var current_image = settings.start_image;
      
      // initialize - set current, prev and next image
      var previmage = $('.headerslider-previmage', this);
      previmage.hide();
      // only fill the prev image with a source when there actually IS a previous image
      if(!current_image == 0){
      	previmage.attr('src', settings.folder_prefix + settings.images[current_image-1]);
      }
      
      var nextimage = $('.headerslider-nextimage', this)
      nextimage.hide().attr('src', settings.folder_prefix + settings.images[current_image+1]);
      
      var path = settings.folder_prefix + settings.images[settings.start_image];
      var mainimage = $('.headerslider-image');
      mainimage.attr('src', path);
      
      var btnNext = $('.headerslider-controls-prev', this);
      var btnPrev = $('.headerslider-controls-next', this);
      
      var enablePrevNext = function(){
      	btnNext.click(prevClickFunction);
      	btnPrev.click(nextClickFunction);
      }
      
      var disablePrevNext = function(){
				btnPrev.unbind('click');
				btnNext.unbind('click');
      }
      
      
      var prevClickFunction = function(){
      	// only do anything when this is not the first image of the series
      	if(!(current_image == 0) ){
      		var prevSrc = settings.folder_prefix + settings.images[current_image-1];
      		disablePrevNext();
      		previmage
      			.css('z-index', 15)
      			.fadeIn(500, function(){
      				current_image--;
      				// set previmage to mainimage
      				mainimage.attr('src', previmage.attr('src'));
      				// set mainimage to nextimage
      				nextimage.attr('src', mainimage.attr('src'));
      				// show mainimage
      				mainimage.show();
      				// hide nextimage
      				previmage.hide();
      				nextimage.hide();
      				enablePrevNext();
      		})
      		.attr('src', prevSrc);
      	}else{
      		// this is the first image, so set the current_image 
      		// to the last one and start over again :-)
      		current_image = settings.images.length;
      		prevClickFunction();
      	}
      }
      
      var nextClickFunction = function(){
      	// only happen when this is not the last image of the series
      	if(!((current_image + 1) > (settings.images.length-1))){
      		var nextSrc = settings.folder_prefix + settings.images[current_image+1];
      		// disable the next-prev-buttons:
      		disablePrevNext();
      		nextimage
      			.fadeIn(500, function(){
      				current_image++;
      				// set mainimage to previmage
      				previmage.attr('src', mainimage.attr('src'));
      				// set nextimage to mainimage
      				mainimage.attr('src', nextimage.attr('src'));
      				// show mainimage
      				mainimage.show();
      				// hide nextimage
      				nextimage.hide();
      				previmage.hide();
      				enablePrevNext();
      		})
      		.attr('src', nextSrc);
      	}else{
      		// so, it is the last image in the row:
      		// lets set the current_image to 0 so we can start again
      		current_image = -1;
      		nextClickFunction();
      	}
      }
      enablePrevNext();
      
      var autoClickButton = function(){
      		nextClickFunction();
      		setTimeout(autoClickButton, settings.autoswitch_duration);
      }
      
      if(settings.autoswitch){
      	setTimeout(autoClickButton, settings.autoswitch_duration);
      }
		});   
	};
})( jQuery );
