$(function(){
/*
	$('body').click(function()
	{
		// hide others
		$('.bookOptions').css('display', 'none');
	});
*/
	
	
	$("#content-inner #news_area a").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	true
	});

	
	$('.bookLink').click(function(event)
	{
		// hide others
		$('.bookOptions').css('display', 'none');
		
		// show this one
		var theID = $(this).attr('id');
		var newID = theID.replace(/launch_/,'Opt_');
		
		$('#' + newID).css('top', '0'+event.pageY+'px').css('display','inline').css('left', '0'+event.pageX+'px').show(500);
	
	});
	
	$('.bookLink2').click(function(event)
	{
		// hide others
		$('.bookOptions').css('display', 'none');
		
		// show this one
		var theID = $(this).attr('id');
		var newID = theID.replace(/launch_/,'Opt_');
		
		$('#' + newID).css('top', '0'+event.pageY+'px').css('display','inline').css('left', '0'+event.pageX+'px').show('fast');
	
	});
	
	$('.bookOptions').click(function()
	{
		return false;
	});

	
	$('.bookOptions a').click(function()
	{
		window.location = $(this).attr('href');
		return false;
	});


	$('#terms_link').click(function()
	{
		
		$('#terms_popup, #close_terms').css({'display':'block','opacity':'0'});
		$('#terms_popup, #close_terms').animate({opacity:1.0},500);
		
		return false;
	});

	$('#close_terms a').click(function()
	{
		$('#terms_popup, #close_terms').animate({opacity:0},500);
		setTimeout("$('#terms_popup, #close_terms').css({'display':'none','opacity':'0'});",500);		
		
		return false;
	});

	$('#terms_popup, #close_terms').css({'opacity':'0'});
	

	if($.browser.msie && $.browser.version == '6.0'){
		$('#terms_popup, #close_terms').css({'position':'absolute'});
	}
	
	$('#driver_dob_container select').each(function(){
		$(this).change(function(){
			var date = $('#driver_dob_year').val()+'-'+$('#driver_dob_month').val()+'-'+$('#driver_dob_day').val();
			$('#driver_dob').val(date);
		});
	});
	$('#extra_date_container select').each(function(){
		$(this).change(function(){
			var date = $('#extra_date_year').val()+'-'+$('#extra_date_month').val()+'-'+$('#extra_date_day').val();
			$('#extra_date').val(date);
		});
	});
	$('#waiver_date_container select').each(function(){
		$(this).change(function(){
			var date = $('#waiver_date_year').val()+'-'+$('#waiver_date_month').val()+'-'+$('#waiver_date_day').val();
			$('#waiver_date').val(date);
		});
	});
	
	initDateSelect();
		
	$('#payment_add').click(function(){
		
		$('#payment_form_container').slideToggle();
		
		return false;
	});
	
	$('input.paymethod_radio').click(function(){
		if($(this).val()=='card'){
			$('#card_details_container').css('display','block');
		}else{
			$('#card_details_container').css('display','none');
		}
	});
	
	$('#extra_select').change(function(){
		var extraId = $(this).children(":selected").val();
		var blQty = $(this).children(":selected").attr('title');
		if(blQty=='1'){
			$('#quantity_wrap').css('display','block');
			$('#extra_qty').removeAttr('disabled');
		}else{
			$('#quantity_wrap').css('display','none');
			$('#extra_qty').attr('disabled','disabled');
			$('#extra_qty').attr('value','');
		}
		spanWhiteBackground();
		
	});
	
	
	$("#extrasForm").submit(function() {
		if ($('#extra_qty').is(':disabled') == false) {
			var val = $('#extra_qty').val();
			if (val > 0) {
				return true;
			}
			else {
				alert ('Quantity must be entered for this extra.');
				return false;
			}
	    }
	});
	
	$('#user_link').click(function(){
		
		$('#user_form').slideToggle();
		return false;
	});
	
	$('#contact_driver').click(function(){
		if($(this).attr('checked')==true){
			$('#driver_form').css('display','block');
		}else{
			$('#driver_form').css('display','none');
		}
		spanWhiteBackground();
	});
	
	$('form.normalForm .required').after('<img src="images/star.gif" alt="required" class="requiredImage" />');
	
	$('#add_conviction').click(function(){
		
		var lastRow = $(this).parent().parent().prev();
		
		$(lastRow).clone().insertAfter(lastRow);
		
		var newLastRow = $(lastRow).next();
		var objDate = new Date();
		
		initDateSelect();	
		
		$('input.conviction_code',newLastRow).val('');
		$('select:eq(0)',newLastRow).val(objDate.getDate());
		$('select:eq(1)',newLastRow).val(objDate.getMonth()+1);
		$('select:eq(2)',newLastRow).val(objDate.getFullYear()).trigger('change');

		initRemoveConv();
		
		spanWhiteBackground();
		
		return false;
	});
	
	initRemoveConv();
	
	
	$('a.editExtra').click(function(){
		
		var id = $(this).attr('id').replace('editExtra','');
		
		$('#extraRow'+id+' td.edit_qty').hide();
		$('#edit_qty'+id).show();
		
		return false;
		
	});
	
	
});

function initRemoveConv(){
	
	$('a.remove_conviction').click(function(){
		
		if($('a.remove_conviction').length > 1)	{
			$(this).parent().parent().remove();
		}
		
		return false;
	});
}

function initDateSelect(){
	$('div.date_select_container select').each(function(){
		$(this).change(function(){
			var objCont = $(this).parent();
			
			var date = $('select.date_select_year',objCont).val()+'-'+$('select.date_select_month',objCont).val()+'-'+$('select.date_select_day',objCont).val();
			$('input.date_select_hidden',objCont).val(date);
		});
	});
}




