$( function()
{
	var isMobile = (screen.width <= 699),
		current;
		
	!isMobile
		&& $( ".slide button" ).hide();
	
	$( '.slide' ).each( function()
	{
		var	$this = $( this ),
			parent = $this.closest( "li" );
			
		$( 'ul', $this )
			.cycle(
			{
				prev: $( '.prev', parent ),
				next: $( '.next', parent ),
				timeout: 0,
				after: function( curr, t, opts )
				{
					var total = $( this ).closest( ".slide" ).siblings( "div.pagination" ).attr( "rel" );

					$( this ).closest( ".slide" )
						.height( $( this ).height() )
						.siblings( "div.pagination" )
							.find( "span" )
							.text( ( opts.currSlide + 1 ) + " of " + total );

					$( "video", curr ).length
						&& VideoJS.setupAllWhenReady();

					current = curr;

					$( "video", current ).length
						&& $( "video", current ).get( 0 ).pause();
				},
				fit: 1,
				height: 'fit',
				//fx: 'scrollLeft'
			})
			.parent().mousemove( function( e )
			{
				if( $( e.target ).is( "button" ) )
					return;

				if( $( e.target ).is( "div" ) )
				{
					$( "button", this ).hide()
					return;
				}

				var show = e.pageX - $( this ).offset().left < ( $( this ).width() / 2 ) ? ".prev" : ".next";

				$( "button", this )
					.filter( show ).stop( true, true ).fadeIn( "slow" ).end()
					.filter( show == ".prev" ? ".next" : ".prev" ).stop( true, true ).fadeOut( "slow" );
			})
			.mouseleave( function()
			{
				$( "button", this ).hide();
			});

		$( "ul li:first img", parent ).load( function()
		{
			$( this ).closest( ".slide" ).height( $( this ).height() );
		});
	});	
});

VideoJS.setupAllWhenReady();
