(function($){
	$.heroFader={};
	$.fn.heroFader=function(controlsSelector,pagesClass){
		var duration=400,sleep=8000;
			$.heroFader.elem=this,
			$.heroFader.controls=this.find(controlsSelector).hide(),
			$.heroFader.blocks=this.find(pagesClass);
		if($.heroFader.blocks.length<1){
			return this;
		}
		if($.heroFader.blocks.length<2){
			$.heroFader.blocks.eq(0).showHero(0);
			return this;
		}
		$.heroFader.blocks.each(function(i){
			var block_id=($(this).attr("id").length>0) ? $(this).attr("id") : "fader_block_"+i;
			$(this).attr("id",block_id);
			var class_name=(i==0) ? "first" : ""; 
			
			if(i==$.heroFader.blocks.length-1){
				class_name="last";
			}
			$.heroFader.controls.append('<a class="'+class_name+'" href="#'+block_id+'">&bull;</a>');
		});
		$.heroFader.controls.css({
			"margin-left":parseInt(-$.heroFader.controls.width()/2)
		}).show();
		
		$.heroFader.controls.find("a").click(function(){
			var target_url=$(this).attr("href").split("#")[1];
			$("#"+target_url).showHero(duration);
			return false;
		});
		var mouseOverFader=false;
		$.heroFader.elem.hover(function(){
			mouseOverFader=true;
		},function(){
			mouseOverFader=false;
		});
		$.heroFader.blocks.eq(0).showHero(0);
		setInterval(function(){
			if(mouseOverFader){
				return false;
			}
			var current=$.heroFader.blocks.filter(":visible"),
				nextHero=current.next();
			
			if(nextHero.length<1){
				nextHero=$.heroFader.blocks.eq(0);
			}
			nextHero.showHero(duration);
		},sleep);
		$.heroFader.controls.prepend('<a class="previous" href="#previous">Previous</a>');
		$.heroFader.controls.find("a.previous").click(function(){
			var current=$.heroFader.blocks.filter(":visible"),
				previousHero=current.prev();
			
			if(previousHero.hasClass("controls")){
				previousHero=$.heroFader.blocks.eq($.heroFader.blocks.length-1);
			}
			previousHero.showHero(duration);
			var target_url=$(this).attr("href").split("#")[1];
			$("#"+target_url).showHero(duration);
			return false;
		},sleep);
		
		$.heroFader.controls.append('<a class="next" href="#next">Next</a>');
		$.heroFader.controls.find("a.next").click(function(){
			var current=$.heroFader.blocks.filter(":visible"),
				nextHero=current.next();
			if(nextHero.length<1){
				nextHero=$.heroFader.blocks.eq(0);
			}
			nextHero.showHero(duration);
			
			var target_url=$(this).attr("href").split("#")[1];
			$("#"+target_url).showHero(duration);
			return false;
		},sleep);
		return this;
	};
	$.heroFader.currentlyAnimating=false;
	$.fn.showHero=function(duration){
		if($.heroFader.currentlyAnimating){
			return;
		}
		var current=$.heroFader.blocks.filter(":visible").css({"z-index":1});
		if(current[0]===this[0]){
			return;
		}
		$.heroFader.controls.find("a.active").removeClass("active");
		$.heroFader.controls.find("a[href$="+this.attr("id")+"]").addClass("active");
		$.heroFader.currentlyAnimating=true;
		this.css({opacity:0}).show().animate({opacity:1},duration,function(){
			$.heroFader.currentlyAnimating=false;
			current.css({opacity:0,"z-index":5}).hide();
		});
	};
})(jQuery);