// JavaScript Document


function populate_booking_table(new_week) {
	var current_week = jQuery('#booking_current_week').val();
	var product_id = jQuery('#booking_product_id').val();
	var slot_positions = jQuery('#booking_slot_positions').val();
	var today = new Date();
	var this_location = location.protocol+'//'+location.host+location.pathname;
	//var this_location = location.href.replace(/#+$/g, '') ;

	jQuery.getJSON ((this_location + '?golfpress_ajax_call=get_week_slot_data&current_week='+current_week+'&new_week='+new_week+'&product_id='+product_id +'uniq='+today),
		
		function (data) {
		
			jQuery('#booking_inner td').attr('flyout','').not('.slot_disabled').removeClass();
			jQuery("#previous_button a").removeClass();
			jQuery('#booking_inner td div').css('width',0);
			jQuery('#booking_current_week').val(data.new_date);
			jQuery('#booking_navigation_week').html('Week beginning '+data.pretty_date);
			jQuery.each(data.days, function(i,this_day){
				//alert('#booking_day_'+i);
				//alert(this_day.slots_filled);
				jQuery('#booking_day_'+i+'_heading').html(this_day.pretty_time);
				jQuery('#booking_date_'+i).val(this_day.date);

					if (this_day.day_disabled == 'true') {
						jQuery('#booking_inner td[id^=booking_day_'+i+'_]').addClass('day_disabled');	
					}						


				jQuery.each(this_day.events, function(j,this_event){

					jQuery('#booking_day_'+i+'_'+this_event.begin_slot).addClass('event event_begin').attr('flyout','Event: ' + this_event.name + '<br />Please contact us for more information on this event');
					jQuery('#booking_day_'+i+'_'+this_event.end_slot).addClass('event event_end').attr('flyout','Event: ' + this_event.name + '<br />Please contact us for more information on this event');
					var first_index = jQuery('table#booking_inner>tbody>tr').index(jQuery('#booking_day_'+i+'_'+this_event.begin_slot).parent());
					var last_index = jQuery('table#booking_inner>tbody>tr').index(jQuery('#booking_day_'+i+'_'+this_event.end_slot).parent());
					for (f=first_index+1; f<last_index;f++) {
					jQuery('table#booking_inner>tbody>tr:eq('+f+')>td:eq('+i+')').addClass('event').attr('flyout','Event: ' + this_event.name + '<br />Please contact us for more information on this event');
					//addClass('event');
					}
					//alert(first_index + ' to ' + last_index);
					
					
				})

				jQuery.each(this_day.slots_filled, function(j,slot){
					//alert('#booking_day_'+i+'_'+slot.time);
					//alert(slot.fill);

					//jQuery('#booking_day_'+i+'_'+slot.time).addClass('filled_'+slot.fill);
					jQuery('#booking_day_'+i+'_'+slot.time).attr('fill',slot.fill);
					jQuery('#booking_day_'+i+'_'+slot.time+' div').css('width',((slot.fill/slot_positions)*100)+'%');
					if (slot.fill == slot_positions) {
						jQuery('#booking_day_'+i+'_'+slot.time).addClass('completly_full');
						slot_message = 'No more bookings available for this time.';
					} else {
						slot_message = slot_positions - slot.fill + ' free positions available.';
					}
					
					jQuery('#booking_day_'+i+'_'+slot.time).attr('flyout',jQuery('#booking_day_'+i+'_'+slot.time).attr('flyout') + '<br />' +slot_message);
				
				})


			var d = data.new_date.split('-');
			var last_cal_date = new Date();
			last_cal_date.setFullYear(d[0],d[1]-1,parseInt(d[2])-1);
			if (last_cal_date < today) {
				jQuery("#previous_button a").addClass('disabled');
			}
			})

				jQuery('#booking_inner td').not('.slot_disabled').bind("mouseenter",function(e){
					if (jQuery(this).attr('flyout').length > 0) {
					this_message = jQuery('#booking_heading th').eq(jQuery(this).parent().children().index(this)).html().replace('<br>','')+' at '+jQuery(this).parent().children().eq(0).html()+'</strong><br />'+jQuery(this).attr('flyout');	
					} else {
					this_message = jQuery('#booking_heading th').eq(jQuery(this).parent().children().index(this)).html().replace('<br>','')+' at '+jQuery(this).parent().children().eq(0).html()+'</strong><br />This time is available to book';	
					}
					jQuery(document.body).append('<div id="gp-tt-'+jQuery(this).attr('id')+'"><strong>'+this_message+'</div>');
					jQuery('#gp-tt-'+jQuery(this).attr('id')).css({padding:"1em", fontSize:"9pt", position:"absolute",top:(e.pageY+20)+'px', left:(e.pageX+20)+'px', background:"#EBDBB8", border:"1px solid #C89C39", color:"black",'text-align':'left'});
				}).bind("mouseleave",function(){
					jQuery('#gp-tt-'+jQuery(this).attr('id')).remove();
				});

		}
	);
	
	
}

jQuery(document).ready( function () {								  
	if(jQuery("#booking_table").length > 0) {
		jQuery('.booking_time_date_submit').attr('disabled','disabled');
		populate_booking_table('this');		
			jQuery('#booking_inner td').click(function () {
				if (!jQuery(this).hasClass('day_disabled') && !jQuery(this).hasClass('slot_disabled')) {
					if (!jQuery(this).attr('fill')) {
						var this_fill = 0;
					} else {
						var this_fill = parseInt(jQuery(this).attr('fill'));
					}
					var slot_positions = parseInt(jQuery('#booking_slot_positions').val());
					var this_book = 0;
					jQuery('#people_to_book select').each(function (i) {
						this_book += parseInt(jQuery(this).val());												
					});
					var slot_total = (this_fill + this_book);
					jQuery('#booking_inner .selected, #booking_heading th.selected').removeClass('selected');
					if (this_book == 0) {
						alert ('Please select the amount of people in your party.');
						jQuery('#selected_date').val('');
						jQuery('#selected_time').val('');
						jQuery('.booking_time_date_submit').attr('disabled','disabled');
					} else if (slot_total <= slot_positions) {
						jQuery(this).addClass('selected');
						
						var this_id = jQuery(this).attr('id');
						this_day = jQuery('#booking_date_'+this_id.split('_')[2]).val();
						jQuery('#booking_day_'+this_id.split('_')[2]+'_heading').addClass('selected');
						jQuery(this).parent().children('th').addClass('selected');
						this_time = (this_id.split('_')[3] + ':' + this_id.split('_')[4]);
						//alert(this_day);alert(this_time);
						jQuery('#selected_date').val(this_day);
						jQuery('#selected_time').val(this_time);
						jQuery('.booking_time_date_submit').attr('disabled','');
					} else if (this_fill == slot_positions) {
						alert ('The time you have selected has been completly booked out. We apoligise fo rthe convienence.');
						jQuery('#selected_date').val('');
						jQuery('#selected_time').val('');
						jQuery('.booking_time_date_submit').attr('disabled','disabled');
					} else {
						alert ('There are not enough free positions to cater for your party at the time selected. \n Extra players will be booked in the closest slots following this time');
						//jQuery('#selected_date').val('');
						//jQuery('#selected_time').val('');
						//jQuery('.booking_time_date_submit').attr('disabled','disabled');
						jQuery(this).addClass('selected');
						
						var this_id = jQuery(this).attr('id');
						this_day = jQuery('#booking_date_'+this_id.split('_')[2]).val();
						jQuery('#booking_day_'+this_id.split('_')[2]+'_heading').addClass('selected');
						jQuery(this).parent().children('th').addClass('selected');
						this_time = (this_id.split('_')[3] + ':' + this_id.split('_')[4]);
						//alert(this_day);alert(this_time);
						jQuery('#selected_date').val(this_day);
						jQuery('#selected_time').val(this_time);
						jQuery('.booking_time_date_submit').attr('disabled','');
					}
				}
			});
		jQuery('#people_to_book select').change(function () {
			if(jQuery('#booking_inner td.selected').length) {
				jQuery('#booking_inner td.selected').click();	
			}
		});
		jQuery("#previous_button a").click(function () {
			if (!jQuery(this).hasClass('disabled'))
			populate_booking_table('previous');
			return false;
		});
		jQuery("#next_button a").click(function () {
			populate_booking_table('next');
			return false;
		});
	}
jQuery(document.body).ajaxStart(function() {
   jQuery(document.body).append('<div id="gp-loading">Loading...</div>');
   jQuery('#gp-loading').css({padding:"1em", fontSize:"9pt", position:"fixed",
top:"-50px", right:"0", background:"#EBDBB8", border:"1px solid #C89C39", color:"black"})
   .animate({top:'0px'},1000);
}).ajaxStop(function() {
   jQuery('#gp-loading').animate({top:'-50px'},1000,function() {jQuery('#gp-loading').remove();});
});

});