(function($) {
	$.fn.FeatureBox = function(options) {
		return this.each(function(options) {
			var e = this;
			var curId = "";
			var showId = "";
			var autoNext=0;
			var onShow = false;
			
			var options = $.extend({
				autoNextDelay : 10000,
				fadeOutSpeed: 400,
				fadeInSpeed:800,
				slideInSpeed:600,
				slideInEasing:"easeOutExpo"
			},options);
			
			var show = function(id) {
				clearAutoNext();
				onShow = true;
				
				var txt = $($(id).find(".item_text").get(0));
				var img  = $($(id).find(".item_image").get(0));
				
				showId = id;
				//$(curId).stop();
				$(curId).animate({opacity:"hide"}, options.fadeOutSpeed, function() {
						txt.css({display:"none",opacity:1});
						img.css({left:"200px"});
				
						$(curId).hide();
						$(curId).css({opacity:1});
						
						$(id).show();
						img.animate({left:0}, options.slideInSpeed ,options.slideInEasing);
						txt.fadeIn(options.fadeInSpeed);
						
						//get next and show it's title
						curId = id;
						n = getNext();
						$(".next_title",e).html($(n).attr("title"));
						
						onShow = false;
						
						//set next delay
						setAutoNext();
					});
				
				
			};
			
			var getNext = function() {
				//get current <a>
				curA = $(".nav",e).find("a[href='"+ curId +"']")[0];
				if (curA) {
					
					//get next <li>
					var li = $(curA).parent().next();
					var n;
					if (li.length>0) {
						//if any
						n = $("a",li);
					} else {
						//reach end, start from first
						n = $(".nav",e).find("a:first");
					}
				}
				return n;
			};
			
			var runAutoNext = function() {
				$(".nav",e).find("a").removeClass("active");
				
				n = getNext();
				$(n).addClass("active");
				href = $(n).attr("href");
				show(href);
			};
			
			var clearAutoNext = function() {
				if (autoNext) {
					window.clearTimeout(autoNext);
				}
				autoNext = 0;
			};
			
			var setAutoNext = function() {
				clearAutoNext();
				autoNext = window.setTimeout(runAutoNext, options.autoNextDelay);
			};
			
			var init = function() {
				
				$(".nav",e).find("a").each(function(){
					var href = $(this).attr("href");
					$href=$(href);
					
					$href.hide();
					
					$(this).click(function(){
						this.blur();
						
						$(".nav",e).find("a").removeClass("active");
						$(this).addClass("active");
						show(href);
						return false;
					});
					
				});
				
				$(".nav",e).find("a:first").addClass("active");
				curId = $(".nav",e).find("a:first").attr("href");
				$(curId).show();
				
				n = getNext();
				$(".next_title",e).html($(n).attr("title"));
			};
			
			init();
			setAutoNext();
		});
	}
})(jQuery);
//-

$(document).ready(function() {
	//initialize when document ready
	
	//options:
		//autoNextDelay: delay for next slide if user didn't move it manually
		//fadeOutSpeed: animation speed to fade out old slide
		//fadeInSpeed: animation speed to fade in text of new slide
		//slideInSpeed: animation speed to slide in image of new slide
		//slideInEasing: slide in easing animation, complete list of available easing can be found on easing.js + jQuery's built-in: 'linear' and 'swing'
	$("#featurebox").FeatureBox({
		autoNextDelay : 10000,
		fadeOutSpeed: 400,		
		fadeInSpeed:800,
		slideInSpeed:600,
		slideInEasing:"easeOutExpo"
	});
});
