$(document).ready( function(){
	// ----------------------------------------------------
	// Tab navigation
	// ----------------------------------------------------
	$('ul#tabs li a').click( function(){ // Navigate tabs
        var targetOffset = $('h1#header').offset().top;
        $('html,body').animate({scrollTop: targetOffset}, 'slow');
        $('ul#tabs li').removeClass('selected'); // Remove all highlighting from progress bar
        $(this).parent().addClass('selected'); // Mark this tab as 'selected'
        var target_tab = $(this).attr('href'); // Get the desired tab id
        $('div#content > div').hide(); // Hide all the tabs
        $(target_tab).show(); // ... and show the target one
    });

   // Navigate to the right tab if the URL contains an anchor
	if( document.location.hash.length > 0 ){
        $('ul#tabs li a[href="' + document.location.hash + '"]').click();
    } else {
        $('ul#tabs li:first a').click();
    }

	// ----------------------------------------------------
	// Choose your film
	// ----------------------------------------------------
	// Version list and 'cancel versions'
	$('a.cancel_versions').parent().hide();
	$('div.versions').hide();
	
	$('a.show_versions').click( function(){
		$(this).parent().parent().parent().siblings('div.versions').fadeIn();
		$(this).parent().hide();
		$(this).parent().parent().parent().parent().siblings().hide()
		$(this).parent().siblings(':has(a.cancel_versions)').show();
	});
	$('a.cancel_versions').click( function(){
		$(this).parent().parent().parent().siblings('div.versions').hide();
		$(this).parent().hide();
		$(this).parent().parent().parent().parent().siblings().fadeIn()
		$(this).parent().siblings(':has(a.show_versions)').show();
	});

	// Version info and 'cancel info'
	$('a.cancel_information').parent().hide();
	$('dl.version_information').hide();

	$('a.show_information').click( function(){
		$(this).parent().parent().siblings('dl.version_information').fadeIn();
		$(this).parent().hide();
		$(this).parent().siblings(':has(a.cancel_information)').show();
		return false;
	});
	$('a.cancel_information').click( function(){
		$(this).parent().parent().siblings('dl.version_information').fadeOut();
		$(this).parent().hide();
		$(this).parent().siblings(':has(a.show_information)').show();
		return false;
	});

	// Country selection and 'cancel that'
	$('a.cancel_version').parent().hide();
	$('p#country_choice').hide();

	$('a.select_version').click( function(){
		$(this).parent().parent().parent().parent().siblings().hide();
		$(this).parent().hide();
		$(this).parent().siblings(':has(a.cancel_version)').show()
	});

	$('a.cancel_version').click( function(){
		$(this).parent().parent().parent().parent().siblings().show();
		$(this).parent().hide();
		$(this).parent().siblings(':has(a.select_version)').show()
	});

	// ----------------------------------------------------
	// Book speaker?
	// ----------------------------------------------------
	$('dl#speakers dt a').click( function(){
		$(this).parent().next('dd').slideToggle();
		return false;
	});
	$('dl#speakers dd').hide();

	// ----------------------------------------------------
	// Calculate some profits
	// ----------------------------------------------------
	$('input#id_ticket_price').keyup( function(){
		var total_fee = eval( $('div#cost span').html() )
		var audience = eval( $('span#audience').html() )
		var price = $(this).val()
		
		if (!isNaN(price)) {

			var profit = Math.round( ( ( audience * price ) - total_fee )*Math.pow( 10,2 ) )/Math.pow( 10,2 );
	
			if( ( ( profit+'' ).match( '[.]' ) ) && ( ( profit+'' ).split( '.' )[1].length == 1 ) ) {
				profit += '0';
			}
			if( parseFloat(profit) < 0 ) {
				$('#profit').addClass('in_the_red');
			}
			else {
				$('#profit').removeClass('in_the_red');	
			}
	
			$('#profit span').html( profit );
		}
		else {
			alert('Please enter a number for your ticket price e.g. 10.50');
		}
	});
	// ... and if the user refreshes the page for some reason
	$('input#id_ticket_price').show( function(){
		var total_fee = eval( $('div#cost span').html() )
		var audience = eval( $('span#audience').html() )
		var break_even_price = Math.round(      ( total_fee / audience )*Math.pow( 10,2 )    )  /  Math.pow( 10,2 );
		
		if( ( ( break_even_price+'' ).match( '[.]' ) ) && ( ( break_even_price+'' ).split( '.' )[1].length == 1 ) ) {
			break_even_price += '0';
		}

		$('input#id_ticket_price').val( break_even_price );
		$('#profit span').html( '0' );
	});

	// ----------------------------------------------------
	// 'Add screening' form
	// ----------------------------------------------------
	if( jQuery().date_input )
		$('.date_select').date_input();
	
	// $("#id_screening_time").timepicker();
	
	$("#id_screening_time").mask("99:99");
	
	$('form#add_extra_screening').css({ "position":"absolute", "left":"-9999px" });
	$('a#show_screening_form').click( function(){
		$('form#add_extra_screening').css({ "position":"static", "left":"0" });
		$(this).fadeOut();
		return false;
	});
	
	// ----------------------------------------------------
	// Form help tooltips
	// ----------------------------------------------------
	$("div.form_help").hide().css({
		'background':'#fff',
		'color':'#DF5296',
		'font-weight':'bold',
		'border':'1px solid #C1C1C1',
		'position':'absolute',
		'top':'-3em',
		'left':'-2em',
		'width':'250px',
		'padding':'0.5em',
		'z-index':'10'
	});
	$("a.form_help").hover(function() {
		var pos = $(this).position();
		$(this.hash).css({'top':pos.top-6,'left':pos.left+16}).show();
	}, function() {
		$(this.hash).hide();
	});
	
	// ----------------------------------------------------
	// Form beautification
	// ----------------------------------------------------
	
	if (!$.browser.msie || $.browser.version.substr(0,1) != "6") {
		$("form:not(.notransform)").jqTransform();
	}
	
});
