/**
 Template Name: Udvidet Javascript
 */

jQuery.fn.cycle = function(options) {
	
	var current = 0;
	var count = 0;
	
	this.options = $.extend({ fx: 'fade', duration: 5, items: 'li' }, options);
	
	this.init = function() {
		var self = this;
		
		// if the current is 0 check to see if the are any visible items, else we show the first item before we do anything else
		if (current == 0 && $(self.selector + " .active_cycle_item").length == 0 ) { $(self.selector + " " + self.options.items + ":eq(0)").show().addClass('active_cycle_item'); }
		
		// the count
		count = $(self.selector + " " + self.options.items).length - 1;
		
		console.log();
		
		this.start();
	}
	
	this.start = function() {
		var self = this;
		
		if (current == count) {
			current = -1;
		}
		
		var timer = setTimeout(function() {
			// fade out the current items
			$(self.selector + " .active_cycle_item").fadeOut('normal', function() {		
				$(this).removeClass('active_cycle_item');
				$(self.selector + " " + self.options.items + ":eq(" + (current+1) + ")").fadeIn('slow', function(){
					$(this).addClass('active_cycle_item');
					current++;
					self.start();
				});
			});
		}, ((self.options.duration *1000 ) + 500) );
	}
	
	this.init();
	
}
