(function($) {
	$.fn.myImageFader = function(options) {

	var defaults = {			
		hover: false,
		slides: '.slide',
		remote: '.remote', // clicker
		autoStart: 6000, // Set to positive number for auto start and interval time
		restart: 0, // Set to positive number for restart and restart time
		fadespeed: 300 // Speed of fade animation
		};

	this.each(function() {
		var $obj = $(this);
		var $o = $.extend(defaults, options);
		var $s = $($o.slides,$obj).length;
		var $c = 0;
		var $t = 0;
		
		if ($s > 1){
			$($o.slides,$obj).each(function(){
				$(this).css({display:'none'});
				$($o.slides,$obj).eq($c).show();
				var $id = $($o.slides,$obj).index(this);
				$($o.remote,$obj).find(".right").before("<div class='buttons middle' id='"+$id+"'></div>");
				});
		} else {
			$($o.remote,$obj).hide();
			}
			
		if ($s > 1) sliderIntervalID = setInterval(function(){fadeaway();}, $o.autoStart);
			
		$($o.slides,$obj).eq($c).show();
		$($o.remote,$obj).find("[id='"+$c+"']").addClass("current");
		
		$($obj).hover(function(){clearInterval(sliderIntervalID)},function(){sliderIntervalID = setInterval(function(){fadeaway();}, $o.autoStart)});

		$($o.remote,$obj).find(".left").click(function(){
			if ($t >= 1){
				$c = $t;
				$t = $c - 1;
				$($o.slides,$obj).eq($c).hide();
				$($o.remote,$obj).find("[id='"+$c+"']").removeClass("current");
				$($o.slides,$obj).eq($t).show();
				$($o.remote,$obj).find("[id='"+$t+"']").addClass("current");
				}
			});
		
		$($o.remote,$obj).find(".middle").click(function(){
			$c = $t;
			$t = parseInt($(this).attr("id"));
			$($o.slides,$obj).eq($c).hide();
			$($o.remote,$obj).find("[id='"+$c+"']").removeClass("current");
			$($o.slides,$obj).eq($t).show();
			$($o.remote,$obj).find("[id='"+$t+"']").addClass("current");
			});
		
		$($o.remote,$obj).find(".right").click(function(){
			if ($t < ($s-1)){
				$c = $t;
				$t = $c + 1;
				$($o.slides,$obj).eq($c).hide();
				$($o.remote,$obj).find("[id='"+$c+"']").removeClass("current");
				$($o.slides,$obj).eq($t).show();
				$($o.remote,$obj).find("[id='"+$t+"']").addClass("current");
				}
			});
		
		function fadeaway(){
			//clearInterval(sliderIntervalID);
			$c = $t;
			$t = $t + 1;
			if ($t >= ($s)) $t = 0;
			
			$($o.slides,$obj).eq($c).hide();
			$($o.remote,$obj).find("[id='"+$c+"']").removeClass("current");
			$($o.slides,$obj).eq($t).show();
			$($o.remote,$obj).find("[id='"+$t+"']").addClass("current");
			}
		
		//alert($s+":"+$o.slides);
		});
	};	
})(jQuery);
