jQuery(function() {
	//
	var INTERVAL   = 5000;
	var RANDOM     = true;
	var featured   = jQuery( 'ul#featured-slider' );
	
	var items      = featured.children();
	var range      = items.length, cycle = null;
	if( range < 2 ) return;

	var navigation = jQuery( '<li/>' ).addClass( 'caption' );
	var loader     = jQuery( '<li/>' ).addClass( 'loader' );
	var timeoutID  = 0;
	
	cycle = {
		
		range      : range,
		target     : items.first(),
		index      : 0,
		value      : 0,
		locked     : false,
		paused     : false,
		
		// http://en.wikipedia.org/wiki/Fisher-Yates_shuffle
		shuffle: function() {
			var i = cycle.range, j, current;
			while( i > 0 ) {
				j = Math.floor( Math.random() * i-- );
                current  = cycle[i];
                cycle[i] = cycle[j];
                cycle[j] = current;
			}
		},
		
		setTimer: function() {
			if( !cycle.locked ) {
				clearTimeout( timeoutID );
				timeoutID = setTimeout( cycle.init, INTERVAL );
			}
		},
		
		pause: function() {
			cycle.paused = +( new Date );
			cycle.setTimer();
			
			prevLink.stop().animate({opacity: 1});
			nextLink.stop().animate({opacity: 1});
			navigation.stop().animate({opacity: 1});
		},
		
		play : function() {
			if( +( new Date ) - cycle.paused > INTERVAL ) cycle.setTimer();
			cycle.paused = false;
			
			prevLink.stop().animate({opacity: 0});
			nextLink.stop().animate({opacity: 0});
			navigation.stop().animate({opacity: 0});
		},
		
		next : function() {
			cycle.goTo( cycle.value + 1 );
		},

		prev : function() {
			cycle.goTo( cycle.value - 1 );
		},
		
		init : function() {
			if( !cycle.paused ) {
				cycle.goTo( cycle.index + 1, RANDOM );
			}
		},

		goTo : function( index, rand ) {
			if( cycle.locked || ( !rand && index == cycle.value ) ) {
				return false;
			}
			if( index < 0 ) {
				rand && cycle.shuffle();
				index = cycle.range - 1;
			}
			else if( index >= cycle.range ) {
				rand && cycle.shuffle();
				index = 0;
			}
			if( rand && cycle.value == cycle[ index ] ) {
				return cycle.goTo( index + 1, rand );
			}
			cycle.index = index;
			cycle.value = rand ? cycle[ index ]: index;
			cycle.locked = true;
			//loaded[ cycle.value ]? cycle.target.fadeOut( loaded ): loader.fadeIn( load );
			loader.fadeIn( load );
			return true;
		}
	
	};
	
	function load() {
		cycle.target.hide();
		navigation.children().removeClass( 'active' ).eq( cycle.value ).addClass( 'active' );
		cycle.target = items.eq( cycle.value ).show();
		loader.fadeOut(function() {
			cycle.locked = false;
			cycle.setTimer();
		});
	}

	while( range-- ) cycle[ range ] = range;
	var prevLink = jQuery( '<li/>' ).addClass( 'prev' ).click( cycle.prev ).css({opacity: 0});
	var nextLink = jQuery( '<li/>' ).addClass( 'next' ).click( cycle.next ).css({opacity: 0});
	items.each(function( i, item ) {
		item = jQuery( item ).hide().find( '>img' );
		var text = item.attr( 'title' );
		var link = jQuery( '<a/>' );
		navigation.append( link );
		if( text ) {
			item.attr( 'title', '' );
			link.attr( 'title', text );
		}
		link.click(function( event ) {
			event.preventDefault();
			cycle.goTo( i );
		});
	});

	featured.append( navigation, prevLink, nextLink, loader ).hover( cycle.pause, cycle.play );
	cycle.goTo( cycle.range, RANDOM );

});


