
var interval;

jQuery.fn.crossfade = function ( options ) { 
	var defaults = { 
		use_hover_images: false,
		autorotate: true
	};

	var opts = $.extend( defaults, options );

	this.each( function ( ) { 
		var $this = $( this );

		// Hide all images except the first
		$this.find( 'h1' ).css( 'display', 'none' );
		$this.find( 'li:first' ).css( 'opacity', 1 );
		$this.find( 'li:first' ).css( 'filter', 'alpha(opacity=100)' );
		$this.find( 'li:first h1' ).css( 'display', 'block' );

		// Add in our navigation images
		if ( opts.use_hover_images )
		{
			$this.append( '<img id="prev-image" src="' + opts.prev_image_url + '" alt="' + opts.prev_image_alt_text + '" />' );
			$this.append( '<img id="next-image" src="' + opts.next_image_url + '" alt="' + opts.next_image_alt_text + '" />' );
		}

		$this.hover( function ( ) { 
			setImageNav( );
		}, function ( ) { 
// 			$( '#prev-image, #next-image' ).hide( );
		} );

		if ( opts.autorotate )
			interval = setInterval( 'autoNextImage( )', 5000 );

		$( '#prev-image' ).click( function ( ) { 
			if ( opts.autorotate )
				clearInterval( interval );

			if ( !$( 'li.current' ).prev( 'li' ).html( ) )
				return false;

			$current = $this.find( 'li.current' );
			$current.prev( 'li' ).css( 'opacity', 0 ).addClass( 'current' ).animate( { opacity: 1 }, 500 );
			$current.prev( 'li' ).next( 'h1' ).css( 'display', 'block' );
			$current.removeClass( 'current' ).animate( { opacity: 0 }, 750 );
			setImageNav( );
		} );

		$( '#next-image' ).click( function ( ) { 
			if ( opts.autorotate )
				clearInterval( interval );

			if ( !$( 'li.current' ).next( 'li' ).html( ) )
				return false;

			$( '#slideshow-images h1' ).css( 'display', 'none' );

			$current = $this.find( 'li.current' );
			$current.next( 'li' ).css( 'opacity', 0 ).addClass( 'current' ).animate( { opacity: 1 }, 500 );
			$current.next( 'li' ).next( 'h1' ).css( 'display', 'block' );
			$current.removeClass( 'current' ).animate( { opacity: 0 }, 750 );
			setImageNav( );
		} );

		$( '.slideshow-thumbs li' ).click( function ( ) { 
			if ( opts.autorotate )
				clearInterval( interval );

			var this_idx = $( '.slideshow-thumbs li' ).index( this );
			var target_idx = $( '.slideshow-images li' ).get( this_idx );
			var $target = $( target_idx );

			if ( $target )
			{
				$current = $( 'ul.slideshow-images li.current' );
				$target.css( 'opacity', 0 ).addClass( 'current' ).animate( { opacity: 1 }, 500 );
				$current.removeClass( 'current' ).animate( { opacity: 0 }, 750 );
				setImageNav( );
			}

			$( 'ul.slideshow-thumbs li a.current' ).removeClass( 'current' );
			$( this ).find( 'a' ).addClass( 'current' );
			$target.find( 'h1' ).css( 'display', 'block' );
		} );


		$( '.slideshow-images li' ).click( function ( ) { 
			if ( 0 < $( '.slideshow-images li.current' ).find( 'a' ).length )
				document.location.href = $( '.slideshow-images li.current' ).find( 'a' ).attr( 'href' );
			return false;
		} );
	} );

	autoNextImage = function ( ) { 
		$current = $( 'li.current' );
		$( '.slideshow-images h1' ).css( 'display', 'none' );

		if ( $( 'li.current' ).next( 'li' ).html( ) )
		{
			$current.next( 'li' ).css( 'opacity', 0 ).addClass( 'current' ).animate( { opacity: 1 }, 500 );
		}
		else
		{
			$current.closest( 'ul' ).find( 'li:first' ).css( 'opacity', 0 ).addClass( 'current' ).animate( { opacity: 1 }, 500 );
			$current.closest( 'ul' ).find( 'li:first h1' ).css( 'display', 'block' );
		}

		$current.removeClass( 'current' ).animate( { opacity: 0 }, 750 );
		$current.next( 'li' ).find( 'h1' ).css( 'display', 'block' );
		setImageNav( );
	}

	setImageNav = function ( ) { 
		if ( !$( 'li.current' ).next( 'li' ).html( ) )
			$( '#next-image' ).addClass( 'inactive' );
		else
			$( '#next-image' ).removeClass( 'inactive' );

		if ( !$( 'li.current' ).prev( 'li' ).html( ) )
			$( '#prev-image' ).addClass( 'inactive' );
		else
			$( '#prev-image' ).removeClass( 'inactive' );

		var this_idx = $( '.slideshow-images li' ).index( $( '.slideshow-images li.current' ) );
		var target_idx = $( '.slideshow-thumbs li' ).get( this_idx );
		$( '.slideshow-thumbs li a.current' ).removeClass( 'current' );
		$( target_idx ).find( 'a' ).addClass( 'current' );
	}
};

