// JavaScript Document
/*
options:
'type': 'sequence' or 'random'
'timeout': 5000
'speed': 1000
*/

(function($) {
    $.fn.fadeinout = function(options) {
        return this.each(function() {
			$('div', this).css({
				'opacity': 0.0  
			})
			$('div.active', this).css({
				'opacity': 1.0  
			})
			$.fadeinout(this, options);
        });
    };

    $.fadeinout = function(container, options) {
        var settings = {
            'type':             'sequence',
            'timeout':          5000,
            'speed':          1000
        };
		
        if (options) {
            $.extend(settings, options);
		}
		
		var $active = $('div.active', container);

		if ( $active.length == 0 ) $active = $('div.last', container);

		// use this to pull the divs in the order they appear in the markup
		var $next =  $active.next().length ? $active.next()
			: $('div:first', container);
			
		if (settings.type == 'random') {
			// uncomment below to pull the divs randomly
			 var $sibs  = $active.siblings();
			 var rndNum = Math.floor(Math.random() * $sibs.length );
		 	 var $next  = $( $sibs[ rndNum ] );
		}


		$active.addClass('last-active');
		
		$active.css({opacity: 1.0})
			.delay((settings.speed/10))
			.animate({opacity: 0.0}, settings.speed, function() {
				
			});
	
		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, settings.speed, function() {
				$active.removeClass('active last-active');
			});


		setTimeout((function() {
			$.fadeinout(container, options);
		}), settings.timeout);

		
    };
})(jQuery);
