$(document).ready(function(){
	
	//Navigation Drop Down Functions
	$('nav li:has(ul)').hover(
	  function(){
	    $(this).find('ul').fadeIn('fast');
	  },
	  function(){
	    $(this).find('ul').hide();
	  }
	);
	
	//User changes the check in date
	/*
	* Rule 1: 2 Night Minimum
	* Exception: 4 Night Minmimum between 4/16 - 5/6, 5/23 - 6/3 and 8/23 - 9/6
	* Exception: No nightly, weekly only from 6/25 - 8/6
	*/

	$('.mycal').change(function(){
		
		var d = new Date();
		var thisYear = d.getFullYear();	
		var selectedDate = $(this).val();
		var period = $("input[name='period']:checked").val();
		
		if(selectedDate < '06/25/'+thisYear || selectedDate > '08/06/'+thisYear){
			$('#pNightly').attr('disabled',false);
		}
		
		if(period == 'nightly'){					
			
			if(selectedDate >= '04/16/'+thisYear && selectedDate <= '05/06/'+thisYear){
				$("select[name='nights']").html("\<option>4</option>\<option>5</option>\<option>6</option>");
			}else if(selectedDate >= '05/23/'+thisYear && selectedDate <= '06/03/'+thisYear){
				$("select[name='nights']").html("\<option>4</option>\<option>5</option>\<option>6</option>");
			}else if(selectedDate >= '08/23/'+thisYear && selectedDate <= '09/06/'+thisYear){
				$("select[name='nights']").html("\<option>4</option>\<option>5</option>\<option>6</option>");
			}else if(selectedDate >= '06/25/'+thisYear && selectedDate <= '08/06/'+thisYear){
				$('#pNightly').attr('disabled',true);
				$('#pWeekly').attr('checked','checked');
				$("select[name='nights']").html("\<option>7</option>\<option>14</option>\<option>21</option>");
			}else{
				$("select[name='nights']").html("\<option>2</option>\<option>3</option>\<option>4</option>\<option>5</option>\<option>6</option>");
			}
		
		}else{  //period == Weekly
			
			if(selectedDate >= '06/25/'+thisYear && selectedDate <= '08/06/'+thisYear){
				$('#pNightly').attr('disabled',true);
				$('#pWeekly').attr('checked','checked');
				$("select[name='nights']").html("\<option>7</option>\<option>14</option>\<option>21</option>");
			}		
		
		}
		
		
		
	});

	
	
		
	//Jquery Datepicker Calendar for search forms
	var weekendFilter = function (date) {	
		
		return (date.getDay() != 6 && date.getDay() != 0) ? [false, ''] : [true, ''];
		
   	}
   
   function noFilter(date) {
     	
     	//June, July and August are always weekly!!!!
     	
     	if(date.getMonth() == 5){ //June
			if(date.getDate() >= 25){
				return (date.getDay() != 6 && date.getDay() != 0) ? [false, ''] : [true, ''];
			}else{
				return [true,''];
			}
		}else if(date.getMonth() == 6){ //July		
			return (date.getDay() != 6 && date.getDay() != 0) ? [false, ''] : [true, ''];
		}else if(date.getMonth() == 7){ //August
			if(date.getDate() <= 6){
				return (date.getDay() != 6 && date.getDay() != 0) ? [false, ''] : [true, ''];
			}else{
				return [true,''];
			}							
		}else{

			return [true, ''];
		}
   }
   
    //calendar used on _quick_search.cfm
	$('.mycal').datepicker({
      showOn: 'button',
      buttonImage: '/images/booking/cal.png',
      buttonImageOnly: true,
      beforeShowDay: weekendFilter,
      maxDate: "+1Y",
      minDate: "+1D"
   });  
   
   function formatDate(value){
    return value.getMonth()+1 + "/" + value.getDate() + "/" + value.getFullYear();
   }
   
   //User has clicked on the Period radio buttons on Quick Search
   $("input[name='period']").click(function(){
   		
   		var d = new Date();
		var thisYear = d.getFullYear();
   		var selectedDate = $('.mycal').val();
   		var period = $("input[name='period']:checked").val();
   		
   		//user selects 'nightly'
   		if(period == 'nightly'){
   			
   			$('#pNightly').attr('disabled',false);
   			   			
   			if(selectedDate >= '04/16/'+thisYear && selectedDate <= '05/06/'+thisYear){
				$("select[name='nights']").html("\<option>4</option>\<option>5</option>\<option>6</option>");
			}else if(selectedDate >= '05/23/'+thisYear && selectedDate <= '06/03/'+thisYear){
				$("select[name='nights']").html("\<option>4</option>\<option>5</option>\<option>6</option>");
			}else if(selectedDate >= '08/23/'+thisYear && selectedDate <= '09/06/'+thisYear){
				$("select[name='nights']").html("\<option>4</option>\<option>5</option>\<option>6</option>");
			}else{
				$("select[name='nights']").html("\<option>2</option>\<option>3</option>\<option>4</option>\<option>5</option>\<option>6</option>");
			}
   			
   			//run 'nofilter'
   			$(".mycal").datepicker('option', {beforeShowDay: noFilter});
   			
   			//set arrival date to tomorrow
   			var d = new Date();   			   			   			   			
   			//$(".mycal").val((d.getMonth()+1) + '/' + (d.getDate() + 1) + '/' + d.getFullYear());
   			if ($(".mycal").val()=='') {$(".mycal").val((d.getMonth()+1) + '/' + (d.getDate() + 1) + '/' + d.getFullYear());}

   			//show message
   			//alert('There is limited availability for nightly accommodations. Please call for special considerations.');
   			
   		}else{
   			
   			//update 'nights' drop down
   			$("select[name='nights']").html("\<option>7</option>\<option>14</option>\<option>21</option>");
   			
   			//run 'weekendfilter'
   			$(".mycal").datepicker('option', {beforeShowDay: weekendFilter});
   			
   			//set to next saturday
   			var nextSaturday = $("input[name='nextSaturdayHidden']").val();
   			$(".mycal").val(nextSaturday);
   			
   			if(selectedDate >= '06/25/'+thisYear && selectedDate <= '08/06/'+thisYear){
				$('#pNightly').attr('disabled',true);
			}
   		}
   });
	
	
	
	//Contact form validation
	$('#contactform').validate();
	
	//Contact form validation
	$('#offseasonform').validate();
	
	
	//Home page slider
	$('#myslider').cycle({fx:'fade'});
	
	
	//Home page featured jcarousel
	$('#mycarousel').jcarousel();
	
	
	
	
	
	
	
	//Quick Contact Hide/Show
	$('#quickcontact :input').focus(function () {
      $('#quickcontact').addClass('focused').find('.field').show();
   }).blur(function () {
      setTimeout(function () {
         if(!$('#quickcontact').is('.focused')){
            $('#quickcontact .field:gt(0)').hide();
         }
      }, 1000);
      $('#quickcontact').removeClass('focused');
   });
	
	
	
	//Quick Contact Form Validation
	$('#quickcontact').submit(function(){
		
		name = $('[name=qcname]').val();
		email = $('[name=qcemail]').val();
		email2 = $('[name=qcemail2]').val();
		
		if(email2 == ''){
		
			if(name == '' || email == '' || name == 'Name' || email == 'Email'){
				alert('Name and Email are required fields.');
				return false;
			}else{
				
				$.post('/submit/quickcontact', $('#quickcontact').serialize(),function(){
					$('#quickcontact').hide();
					$('#qcmessage').html('Thanks! We have received your information.');
				});
			  			
				return false;
		
			}
		
		}else{
			alert('You are spam go away.');return false;
		}
		
	});
	
	
	//Quick Contact onFocus clear
	$('[name=qcname]').focus(function(){
		if($(this).val()=='Name'){$(this).val('');}
	}).blur(function(){
		if($(this).val()==''){$(this).val('Name');}
	});
	
	$('[name=qcemail]').focus(function(){
		if($(this).val()=='Email'){$(this).val('');}
	}).blur(function(){
		if($(this).val()==''){$(this).val('Email');}
	});
	
	$('[name=qcphone]').focus(function(){
		if($(this).val()=='Phone'){$(this).val('');}
	}).blur(function(){
		if($(this).val()==''){$(this).val('Phone');}
	});
	
	$('[name=qczip]').focus(function(){
		if($(this).val()=='Zip'){$(this).val('');}
	}).blur(function(){
		if($(this).val()==''){$(this).val('Zip');}
	});
	
	$('[name=qccomments]').focus(function(){
		if($(this).val()=='Comments'){$(this).val('');}
	}).blur(function(){
		if($(this).val()==''){$(this).val('Comments');}
	});
	

		
	
	//Brochure Request Form Validation
	$('#brochureform').submit(function(){
		
		name = $('[name=brochurename]').val();
		email = $('[name=brochureemail]').val();
		address = $('[name=brochureaddress]').val();
		city = $('[name=brochurecity]').val();
		state = $('[name=brochurestate]').val();
		zip = $('[name=brochurezip]').val();
		email2 = $('[name=brochureemail2]').val();
		
		if(email2 == ''){
		
			if(name == '' || name == 'Name' || email == '' || email == 'Email' || address == '' || address == 'Address' || city == '' || city == 'City' || state == '' || state == 'State' || zip == '' || zip == 'Zip'){
				alert('All fields are required.');
				return false;
			}else{
				
				$.post('/submit/brochureform', $('#brochureform').serialize(),function(){
					$('#brochureform').hide();
					$('#brochuremessage').html('Thanks! We have received your information.');
				});
				
				return false;
			}
		
		}else{
			alert('You are spam go away.');return false;
		}
		
	});
	
	
	//Brochure Request Form Hide/Show
	$('#brochureform :input').focus(function () {
      $('#brochureform').addClass('focused').find('.field').show();
    }).blur(function () {
      setTimeout(function () {
         if(!$('#brochureform').is('.focused')){
            $('#brochureform .field:gt(1)').hide();
         }
      }, 1000);
      $('#brochureform').removeClass('focused');
   });
   
   
   	//Brochure Request Form onFocus clear
	$('[name=brochurename]').focus(function(){
		if($(this).val()=='Name'){$(this).val('');}
	}).blur(function(){
		if($(this).val()==''){$(this).val('Name');}
	});
	
	$('[name=brochureemail]').focus(function(){
		if($(this).val()=='Email'){$(this).val('');}
	}).blur(function(){
		if($(this).val()==''){$(this).val('Email');}
	});
	
	$('[name=brochureaddress]').focus(function(){
		if($(this).val()=='Address'){$(this).val('');}
	}).blur(function(){
		if($(this).val()==''){$(this).val('Address');}
	});
	
	$('[name=brochurecity]').focus(function(){
		if($(this).val()=='City'){$(this).val('');}
	}).blur(function(){
		if($(this).val()==''){$(this).val('City');}
	});
	
	$('[name=brochurestate]').focus(function(){
		if($(this).val()=='State'){$(this).val('');}
	}).blur(function(){
		if($(this).val()==''){$(this).val('State');}
	});
	
	$('[name=brochurezip]').focus(function(){
		if($(this).val()=='Zip'){$(this).val('');}
	}).blur(function(){
		if($(this).val()==''){$(this).val('Zip');}
	});
	
	
	
	//Book Now credit card same as contact info checkbox
	$('#ccSame').click(function(){
		
		if( $(this).attr('checked') == true ){
			
			$('input[name=billingname]').val($('input[name=firstname]').val() + ' ' + $('input[name=lastname]').val());
			$('input[name=billingaddress1]').val($('input[name=address1]').val());
			$('input[name=billingaddress2]').val($('input[name=address2]').val());
			$('input[name=billingcity]').val($('input[name=city]').val());
			$('select[name=billingstate]').val($('select[name=state]').val());
			$('input[name=billingzip]').val($('input[name=zip]').val());
			$('select[name=billingcountry]').val($('select[name=country]').val());
		}
		
	});	
	
	
});
