(function($) { jQuery.noConflict();

	var teetime_info = {
			course_guid:"",
			date: "", 
			time: "", 
			confirmation_id: "", 
			receipt_id: "",
			member_guid: "",
			golfer_1: "",
			golfer_2: "",
			golfer_3: "",
			golfer_4: "",
			golfer_5: ""
		};
	
	var MGProShopCourses = new Array(); 


	MGBookTeeTimesUI = {

		//----------------------------------------------------------------------
		//
		//----------------------------------------------------------------------
		ShowPanel: function(id)
		{
			jQuery('#welcome').hide();
			jQuery('#step_1').removeClass("current");
			
			jQuery('#when').hide();
			jQuery('#step_2').removeClass("current");
			
			jQuery('#confirm').hide();
			jQuery('#step_3').removeClass("current");
			
			jQuery('#receipt').hide();
			
			
			
			switch(id)
			{
				case 'welcome':
					jQuery('#step_1').addClass("current");
				break;
				case 'when':
					jQuery('#step_2').addClass("current");
				break;
				case 'confirm':
					jQuery('#step_3').addClass("current");
				break;
			}
			
			jQuery('#' + id).show();
		},
		
		//----------------------------------------------------------------------
		//
		//----------------------------------------------------------------------
		GetCourses: function(proshop_guid)
		{
			jQuery.ajax(
			{
				url: "/ajax/get-courses",
				type: 'POST',
				cache: false,
				//dataType: 'html',
				data: {
					proshop_guid: proshop_guid
				},
				beforeSend: function(response, settings)
				{
					jQuery('#course_list').hide();
					jQuery('#course_list_working').show();
				},
				success: function(data) 
				{
					//alert('found teetimes: ' + data.teetimes.length );
					
					if( data.length > 0 )
					{
						MGProShopCourses = data;
						
						//---[ clear previous results ]---
						jQuery('#course_list').empty();
						
						//---[ loop through compiled tee sheet ]---
						for(i=0; i < data.length; i++ )
						{
							var course = MGProShopCourses[i];	
							
							var link = '<a href="javascript:MGBookTeeTimesUI.SelectCourse(\'' + course.course_guid + '\')" class="button">' + course.name + '</a>';
							
							jQuery('#course_list').append(link);
						}
					}
					
				},
				error: function(response, text_status, error)
				{
					alert(response.responseText, 'error');
				},
				complete: function(response, text_status)
				{
					jQuery('#course_list').show();
					jQuery('#course_list_working').hide();
				}
			});					
		},
		
		//----------------------------------------------------------------------
		//
		//----------------------------------------------------------------------
		SelectCourse: function(guid)
		{
			teetime_info.course_guid = guid;
			
			MGBookTeeTimesUI.RefreshTeeTimes();
			
			MGBookTeeTimesUI.ShowPanel('when');
		},
		
		
		//----------------------------------------------------------------------
		//
		//----------------------------------------------------------------------
		RefreshTeeTimes: function()
		{
			//---[ set current tee time date ]---
			teetime_info.date = jQuery('#teetime_date').val();
			
			jQuery.ajax(
			{
				url: "/ajax/get-available-teetimes",
				type: 'POST',
				cache: false,
				//dataType: 'html',
				data: {
					teetime_info: teetime_info
				},
				beforeSend: function(response, settings)
				{
					jQuery('#teetime_list').hide();
					jQuery('#teetime_list_working').show();
				},
				success: function(data) 
				{
					//alert('found teetimes: ' + data.teetimes.length );
					
					if( data.teetimes.length > 0 )
					{
						//---[ clear previous results ]---
						jQuery('#teetime_list').empty();
						
						//---[ loop through compiled tee sheet ]---
						for(i=0; i < data.teetimes.length; i++ )
						{
							var teetime = data.teetimes[i];
							
							var link = jQuery('<a href="javascript:MGBookTeeTimesUI.ConfirmTeeTime(\'' + teetime.time + '\');">' + teetime.display_time + '</a>');
							var div = jQuery('<div class="teetime available"></div>').html(link);
							
							
							if( !teetime.available )
							{
								jQuery(div).removeClass('available');
								jQuery(div).addClass('booked');
								
								jQuery(link).attr('href', 'javascript:void(0);');
							}
							
							//jQuery('{"display_time":"08:10am","time":"08:10:00","available":true,"is_marker":false');
							
							jQuery('#teetime_list').append(div);
						}
					}
					else
					{
						jQuery('#teetime_list').html('No tee times available at this time. You may wish to try calling the pro shop.');
					}
					
					
				},
				error: function(response, text_status, error)
				{
					alert(response.responseText, 'error');
				},
				complete: function(response, text_status)
				{
					jQuery('#teetime_list_working').hide();
					jQuery('#teetime_list').show();
				}
			});					
		},
		

		
		//----------------------------------------------------------------------
		//
		//----------------------------------------------------------------------
		ConfirmTeeTime: function(time)
		{
			teetime_info.time = time;
			
			//alert('we are confirming that golf starts at: ' + time );
			
			//[ populate the course details

			var course = undefined;
			
			//---[ loop through compiled tee sheet ]---
			for(i=0; i < MGProShopCourses.length; i++ )
			{
				if(  MGProShopCourses[i]['course_guid'] == teetime_info.course_guid )
					course = MGProShopCourses[i];	
			}			
			

			
			
			MGBookTeeTimesUI.ShowPanel('confirm');
			
			//---[ set form details ]---
			jQuery('#confirm_course').html(course.name);
			//jQuery('#confirm_course').html(teetime_info.course_guid);
			//jQuery('#confirm_teetime').html(teetime_info.date + ' &bull; ' + teetime_info.time );
			
			jQuery('#confirm_teetime_date').html(teetime_info.date);
			jQuery('#confirm_teetime_time').html(teetime_info.time);
		},
		
		//----------------------------------------------------------------------
		//
		//----------------------------------------------------------------------
		ShowAddedGolfers: function()
		{
			jQuery('#link_add_teetime_golfers').hide();
			jQuery('#add_teetime_golfers').slideDown();
		},	
		
		//----------------------------------------------------------------------
		//
		//----------------------------------------------------------------------
		BookTeeTime: function()
		{
			var course = undefined;
			
			//---[ loop through compiled tee sheet ]---
			for(i=0; i < MGProShopCourses.length; i++ )
			{
				if(  MGProShopCourses[i]['course_guid'] == teetime_info.course_guid )
					course = MGProShopCourses[i];	
			}				
			
			
			teetime_info.member_guid = jQuery('#book_teetimes_modal #member_guid').val();
			
			teetime_info.golfer_1 = jQuery('#book_teetimes_modal #golfer_1').val();
			teetime_info.golfer_2 = jQuery('#book_teetimes_modal #golfer_2').val();
			teetime_info.golfer_3 = jQuery('#book_teetimes_modal #golfer_3').val();
			teetime_info.golfer_4 = jQuery('#book_teetimes_modal #golfer_4').val();
			
			jQuery.ajax(
			{
				url: "/ajax/book-tee-time",
				type: 'POST',
				cache: false,
				data: {teetime_info:teetime_info},
				beforeSend: function(response, settings)
				{
					//---[ show ajax processing image ]----
					jQuery('#confirm_working').show();
				},
				success: function(json) 
				{
					//---[ we just booked ]---
					jQuery('#confirm_id').html( json );
					
					//---[ show new page ]---
					MGBookTeeTimesUI.ShowPanel('receipt');
				},
				error: function(response, text_status, error)
				{
					Process.error(response.responseText, 'error');
				},
				complete: function(response, text_status)
				{
					//---[ hide ajax processing image ]----
					jQuery('#confirm_working').hide();
				}
			});			
		}
		
		//[ end class methods
	}





jQuery(document).ready(function(){
	
	//var ws = jQuery.class('ws');
	
	var ajax = {
		//beforeSend: function(response, settings){
		//	alert('really did it now');
		//},
		//error: function(response, text_status, error)
		error: function(response)
		{
			alert('oh boy: ' + response.responseText);
		}
	};
	
	try
	{
		//var courses = WebService.call("GetCourses", {proshop_guid:"19cd5d4f-ae28-d523-24c9-3f92620e8c3c"} );
		
		//alert('response: ' + courses );
	}
	catch(e)
	{
		alert('caught ws exception: ' + e);
	}


});

})(jQuery);
