(function($) {
	$.fn.homebb = function(options){
		var self = this;
		var defaults = {
			navContainer: $('#homebb-nav'),
			navItemHtml: '<li class=""><a href="javascript:;">&nbsp;</a></li>',
			activeNavSelector: null,
			fadeInTime: 1000, // time it takes for the next element to fade in.
			fadeOutTime: 1000, // time it takes for current element to fade out when moving to the next. (called in show())
			fadeInDelay: 200, // time to wait after show() is called before fading in the next element
			fadeOutDelay: 5000, // time between animations, used for interval in combination with fadeInTime and fadeInDelay
			currentIndex: 0
		}
		var fixedOpts = {
			list: $(this).find('ul'),
			items: $(this).find('ul li')
		}
		this.opts = $.extend({}, defaults, options, fixedOpts);
		
		this.cycle = null;
		this.lastActionEndsAt = 0;
		this.inTimer = null;
		
		var init = function(){
			var o = self.opts;
			o.items.hide();
			o.items.eq(o.currentIndex).show();
			writeNav();
			startCycle();
		}
		
		var writeNav = function(){
			var o = self.opts;
			var i = o.items.size();
			for(;i>0;i--){
				o.navContainer.append(o.navItemHtml);
			}
			o.navContainer.find('>:first').addClass('first');
			o.navContainer.find('>:last').addClass('last');
			var aindex = o.items.index(o.list.find('>:visible'));
			if(aindex >= 0) setActiveNavItem(o.navContainer.find('>*').eq(aindex));
			attachNavEvents();
		}
		var attachNavEvents = function(){
			var o = self.opts;
			o.navContainer.find('>*').click(function(){
				stopCycle();
				show(o.navContainer.find('>*').index($(this)));
				setTimeout(function(){ startCycle(); }, o.fadeInDelay+o.fadeInTime);
			});
		}
		
		
		var setActiveNavItem = function(item){
			var o = self.opts;
			
			if(o.activeNavSelector == null){
				o.navContainer.find('>*').removeClass('active');
				$(item).addClass('active');
			}
			else{
				o.navContainer.find('>*').find(o.activeNavSelector).removeClass('active');
				$(item).find(o.activeNavSelector).addClass('active');
			}
			
		}
		
		
		var startCycle = function(){
			var o = self.opts;
			self.cycle = setInterval(function(){ next(); }, o.fadeInTime+o.fadeInDelay+o.fadeOutDelay);
		}
		var stopCycle = function(){ self.cycle = clearInterval(self.cycle); }
		
		var getNextIndex = function(){
			var o = self.opts;
			return (o.currentIndex+1 > o.items.size()-1)? 0 : o.currentIndex+1;
		}
		var getPrevIndex = function(){
			var o = self.opts;
			return (o.currentIndex-1 < 0)?o.items.size()-1:o.currentIndex-1;	
		}
		
		var next = function(){ show(getNextIndex()); }
		var prev = function(){ show(getPrevIndex()); }
		
		var show = function(i){
			if(isActive()) return;
			
			
			var o = self.opts;
			
			self.lastActionEndsAt = new Date().getTime() +o.fadeInDelay+o.fadeInTime;
			var ci = o.currentIndex;
			o.currentIndex = i;
			
			o.items.eq(ci).stop(true,true).fadeOut(o.fadeOutTime);
			self.inTimer = clearTimeout(self.inTimer);
			self.inTimer = setTimeout(function(){ setActiveNavItem(o.navContainer.find('>*').eq(i));o.items.eq(i).stop(true,true).fadeIn(o.fadeInTime); }, o.fadeInDelay);
		}
		
		var isActive = function(){
			return self.lastActionEndsAt >= new Date().getTime();
		}		
		
		
		init();
		
	
	}
})( jQuery );

$(window).load(function(){ // alleen afbeeldingen
	$('.homebb-container').homebb({});
});
