/**
 * Global Javascript
*/

function validate_email ( val )
{
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
	
	return re.test( val );
}

/**
 * jQuery Wrapper
 *
 * Allows the use of the dollar sign insted of jQuery when other
 * libraries or scripts are included and are using the dollar sign.
*/

(
	
	function( $, document, undefined )
	{
		// Start all javascript inside wrapper to allow $
		
		
		// Start on document ready
		
		$(document).ready(
			
			function()
			{
				// Start 
				
				$('#check_in').datepicker(
					{
						dateFormat: 'mm/dd/yy',
						defaultDate: 0
					}
				);
				
				$('#check_in').bind(
					
					'change',
					
					function()
					{
						//getter
						var defaultDate = $(this).datepicker( "getDate" );
						
						defaultDate.setDate(defaultDate.getDate()+1);
						
						$("#check_out").datepicker( "option", "defaultDate", defaultDate );
						
						var d = defaultDate.getDate();
						d = d + '';
						var m = defaultDate.getMonth();
						m = m + 1;
						m = m + '';
						
						if( m.length < 2 )
						{
							m = '0' + m;
						}
						if( d.length < 2 )
						{
							d = '0' + d;
						}
						
						var y = defaultDate.getFullYear();
						
						newDateString = m + '/' + d + '/' + y;
						
						$("#check_out").val( newDateString );
						
					}
					
				);
				
				$('#check_out').datepicker(
					{
						dateFormat: 'mm/dd/yy',
						defaultDate: +1
					}
				);
				
				
				
				$('#check_out').bind(
					
					'change',
					
					function()
					{
						// Get checkin Date
						var checkinDate = $("#check_in").datepicker( "getDate" );
						checkinDate.setDate(checkinDate.getDate()+1);
						var checkinTime = checkinDate.getTime();
						
						// Get checkout Date
						var checkoutDate = $("#check_out").datepicker( "getDate" );
						var checkoutTime = checkoutDate.getTime();
						
						// check to see if the date is right
						if( checkinTime >= checkoutTime ){
							$("#check_out").datepicker( "option", "defaultDate", checkinDate );
							
							var d = checkinDate.getDate();
							d = d + '';
							var m = checkinDate.getMonth();
							m = m + 1;
							m = m + '';

							if( m.length < 2 )
							{
								m = '0' + m;
							}
							if( d.length < 2 )
							{
								d = '0' + d;
							}

							var y = checkinDate.getFullYear();

							newDateString = m + '/' + d + '/' + y;

							$("#check_out").val( newDateString );
							
							alert('Please select a checkout date that is not before or on your checkin date.')
						}
					
						
						
						
					}
					
				);
				
				
				// TRACKING
				
				$('#reservations-book-now').bind(
					
					'click',
					
					function()
					{
						_gaq.push(['_trackEvent', 'Booking Engine', 'Look to Book', 'Widget']);
					}
					
				);
				
				$('#specials-book-now').bind(
					
					'click',
					
					function()
					{
						_gaq.push(['_trackEvent', 'Booking Engine', 'Look to Book', 'Specials Tag']);
					}
					
				);
				
				$('#book-your-room').bind(
					
					'click',
					
					function()
					{
						_gaq.push(['_trackEvent', 'Booking Engine', 'Look to Book', 'Footer']);
					}
					
				);
				
				/*
					//getter
					var defaultDate = $( ".selector" ).datepicker( "option", "defaultDate" );
					//setter
					$( ".selector" ).datepicker( "option", "defaultDate", +7 );
				*/
				
				// jQuery UI Date Picker
				$('a.calendar').bind(
					
					'click',
					
					function()
					{
						var e = $(this).attr('rel');
						
						$(e).focus();
						
						return false;
					}
					
				);
				
				// Inputs
				/*
				if ( ! Modernizr.input.placeholder )
				{
					$('input').each(
						
						function()
						{
							var ph = $(this).attr('placeholder'),
								v = $(this).val();
								
							if ( ph != '' && v == '' )
							{
								$(this).val( ph ).attr( 'data-placeholder',ph ).bind(
									
									{
										focus: function()
										{
											var v = $(this).val(),
												ph = $(this).attr('data-placeholder');
												
											if ( v == ph )
											{
												$(this).val('');
											}
										},
										
										blur: function()
										{
											var v = $(this).val(),
												ph = $(this).attr('data-placeholder');
												
											if ( v == '' )
											{
												$(this).val( ph );
											}
										}
									}
									
								);
							}
						}
					);
				}*/
				
				$('#home-page-hero').cycle();
				
				// Exclusive Email Offers
				
				$('#widget-exclusive-offers form').bind(
					
					'submit',
					
					function()
					{
						var $e = $(this).children('div').children('ul').children('li').children('.ginput_container').children('input');
						var email = $e.val();
						
						var placeholder = $e.attr('placeholder');
						var holder = '<div></div>';
						var options = {
							resizable: false,
							modal: true,
							show: 'drop'
						};
						
						if ( '' == email || placeholder == email || ! validate_email ( email ) )
						{
							var msg = '<p>Please enter in a valid email address.</p>';
							
							options.buttons = {
								"Ok, I'll Try Again": function() { $( this ).dialog( "close" ); },
								Cancel: function() { $( this ).dialog( "close" ); }
							};
							
							var $dialog_error = $(holder).html(msg).dialog(options);
							
							$dialog_error.dialog('open');
							
							return false;
						}
						else
						{
							return true;
							
							var msg = '<p>You have successfully joined the exclusive meail offers.</p>';
							
							options.buttons = {
								Close: function() { $( this ).dialog( "close" ); }
							};
							
							var $dialog_success = $(holder).html(msg).dialog(options);
							
							$dialog_success.dialog('open');
							
							$e.val('');
						}
					}
				);
				
				
				// Location Map
				if( $("#map-side-bar").length )
				{
					$(".map-location:first").click();
				}
			}
			
		);
	}
	
) ( jQuery, document );
