var bannersCache = new Array();

function rotate() {
	
	$activeBanner = $( 'ul.home-banners li.show' );
	$banners = $( 'ul.home-banners li' );

	// Find next one
	for( var i = 0; i < $banners.size(); i++ ) {
		
		if( $banners.eq(i).attr("id") == $activeBanner.attr( "id" ) ) {
			
			if( i + 1 < $banners.size() ) {
				$nextBanner = $banners.eq(i+1);
			} else {
				$nextBanner = $banners.eq(0);
			}
			
			$activeBanner.find( '.inner' ).fadeOut( "slow" ); //, function() {
			$nextBanner.find( '.inner' ).fadeIn( "slow" ); //, function() {
			
			$nextBanner.addClass( "show" );
			$activeBanner.removeClass( 'show' );
				//});
			//});
			
			break;
		}
	}

	setTimeout( "rotate()", 5000 );
}

function popup_(what) {
	var w = 800;
	var h = 520;
	var LeftPosition = (screen.width)  ? (screen.width-w)/2 : 0;
	var TopPosition  = (screen.height) ? (screen.height-h)/2 : 0;
	window.open(what,"PopupWindow","top="+TopPosition+",left="+LeftPosition+",menubar=0,resizable=1,statusbar=0,width="+w+",height="+h); 
}

$( function() {
	
	$( '.print' ).click( function() {
		
		popup_( '/bookings/print/' + $( this ).attr( "id" ) );
	});

	if( $( 'ul.home-banners li' ).size() > 0 ) {

		setTimeout( "rotate()", 5000 );
	}

	$( '#slots' ).change( function() {
		
		$( '#price' ).val( parseInt( $( this ).val() ) * parseFloat( $( '#slot-price' ).val() ) );
	});

	$( '.booking-table a' ).tooltip({
		showURL : false
	});
	
	$( '#bookings-table-switches .prev' ).click( function() {

		if( index == 0 ) {

			return false;
		} else if( index == 1 ) {

			$( this ).removeClass( 'active' ).removeClass( 'hover' );
		}

		$( '#bookings-table-switches .next' ).addClass( 'active' ).addClass( 'hover' );
		
		$.post( '/bookings', { action : 'prev_calendar_dates', start_date : first_dates[--index] }, function( r ) {

			eval( 'var response = ' + r );
			$( '#bookings-table' ).html( response.html_table );
			first_dates.push( response.first_date );
			last_dates.push( response.last_date );
		});

		return false;
	});

	$( '#bookings-table-switches .next' ).click( function() {
		
		if( index == 2 ) {

			return false;
		} else if( index == 1 ) {

			$( this ).removeClass( 'active' ).removeClass( 'hover' );
		}

		$( '#bookings-table-switches .prev' ).addClass( 'active' ).addClass( 'hover' );

		$.post( '/bookings', { action : 'next_calendar_dates', start_date : last_dates[index++], offset : false }, function( r ) {

			eval( 'var response = ' + r );
			$( '#bookings-table' ).html( response.html_table );
			first_dates.push( response.first_date );
			last_dates.push( response.last_date );
		});

		return false;
	});
})