function verifyTheDate(date) {
	var checkinDate = new Date($j('#checkinDate').val());
	var checkoutDate1 = new Date($j('#checkinDate').val());
	var checkoutDate2 = new Date($j('#checkinDate').val());
	var today = new Date(date);
	var checkinDay = checkinDate.getDay();
	
	if(checkinDay == 1) {
		if(today.equals(checkoutDate1.addDays(4)) || today.equals(checkoutDate2.addDays(7))) return true;
	} else if(checkinDay == 5){
		if(today.equals(checkoutDate1.addDays(3)) || today.equals(checkoutDate2.addDays(7))) return true;
	} else if(checkinDay == 6){
		if(today.equals(checkoutDate1.addDays(7))) return true;
	}
	return false;
}

function verifyTheDateMain(date) {
	var checkinDate = new Date($j('#checkinDateMain').val());
	var checkoutDate1 = new Date($j('#checkinDateMain').val());
	var checkoutDate2 = new Date($j('#checkinDateMain').val());
	var today = new Date(date);
	var checkinDay = checkinDate.getDay();
	
	if(checkinDay == 1) {
		if(today.equals(checkoutDate1.addDays(4)) || today.equals(checkoutDate2.addDays(7))) return true;
	} else if(checkinDay == 5){
		if(today.equals(checkoutDate1.addDays(3)) || today.equals(checkoutDate2.addDays(7))) return true;
	} else if(checkinDay == 6){
		if(today.equals(checkoutDate1.addDays(7))) return true;
	}
	return false;
}

function isDate(dateStr) {

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	
	if (matchArray == null) {
		return false;
	}
	
	month = matchArray[1]; // p@rse date into variables
	day = matchArray[3];
	year = matchArray[5];
	
	if (month < 1 || month > 12) { // check month range
		return false;
	}
	
	if (day < 1 || day > 31) {
		return false;
	}
	
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		return false;
	}
	
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			return false;
		}
	}
	return true; // date is valid
}


// Onload When Document Ready
$j(document).ready(function() {
  $j('a[rel=external]').attr('target', '_blank');
  $j('.equalize').equalHeights('true');
  $j("ul.resort li:not(:first,:last),ul.sales li:not(:first,:last),ul.groups li:not(:first,:last)").after("<li><a class=\"spacer\">&nbsp;</a></li>");
  //$j("#udm li ul").not(":last").not(":first").after("<li><a class=\"spacer\">&nbsp;</a></li>");
  $j('a.popup').live('click',function(){var data=$j(this).metadata();var name=(data.name==null)?'popup':data.name;w=screen.availWidth;h=screen.availHeight;var leftPos=(w-data.width)/2,topPos=(h-data.height)/2;window.open($j(this).attr('href'),name,'width='+data.width+',height='+data.height+',toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=yes,top='+topPos+',left='+leftPos);return false;});
  
  $j(function(){

      $j("ul.dropdown li").hover(function(){

          $j(this).addClass("hover");
          $j('ul:first',this).css('visibility', 'visible');

      }, function(){

          $j(this).removeClass("hover");
          $j('ul:first',this).css('visibility', 'hidden');

      });
  });

	
	if($j("#tabs").length) {
		$j("#tabs").tabs();
	}

	if($j(".myTab").length) {
		$j(".myTab").click(function(){
			setValues($j(this).attr("id"));
		});
	}
	
  $j(".theImage,.lifestyleBottom,.myTab,#tabThumbnails,.theCopy").hover(function(){
			clearHomeNavTimer();
    }, function(){
//			counter--;
//      setHomeNavTimer();
			setTimeout('setValues(counter);', 8000);

    }
  );

	setTimeout('setValues(2);', 8000);

  if ($j('#applicationForm').length) {
  	$j("#applicationForm").validate();
	}

  if ($j('#signupForm').length) {
  	$j("#signupForm").validate();
	}

  if ($j('#kitForm').length) {
  	$j("#kitForm").validate();
	}

  if ($j('#requestForm').length) {
  	$j("#requestForm").validate();
	}
	
  if ($j('#visitForm').length) {
  	$j("#visitForm").validate();
	}

  if ($j('#contactForm').length) {
  	$j("#contactForm").validate();
	}

	if($j("#checkinDate").length) {
		$j('#checkinDate').datepicker({
			showOn: 'both', 
			buttonImage: '../images/misc/calendar.png', 
			dateFormat: 'm/d/yy',
			minDate: new Date(2012, 3, 21),
			maxDate: new Date(2012, 11, 31),
			constrainInput: true,
			beforeShowDay: onlyMFS,
			onSelect: clearCheckout
		});
	
		$j('#checkoutDate').datepicker({
			showOn: 'both', 
			buttonImage: '../images/misc/calendar.png', 
			dateFormat: 'm/d/yy',
			constrainInput: true,
			beforeShowDay: onlyAfter
		});
	}

	if($j("#checkinDateMain").length) {
		$j('#checkinDateMain').datepicker({
			showOn: 'both', 
			buttonImage: '../images/misc/calendar.png', 
			dateFormat: 'm/d/yy',
			minDate: new Date(2012, 3, 21),
			maxDate: new Date(2012, 11, 31),
			constrainInput: true,
			beforeShowDay: onlyMFS,
			onSelect: clearCheckoutMain
		});
	
		$j('#checkoutDateMain').datepicker({
			showOn: 'both', 
			buttonImage: '../images/misc/calendar.png', 
			dateFormat: 'm/d/yy',
			constrainInput: true,
			beforeShowDay: onlyAfterMain
		});
	}


	if($j("#rvcheckinDate").length) {
		$j('#rvcheckinDate').datepicker({
			showOn: 'both', 
			buttonImage: '../images/misc/calendar.png', 
			dateFormat: 'm/d/yy',
			minDate: new Date(2012, 3, 21),
			maxDate: new Date(2012, 11, 31),
			constrainInput: true,
			beforeShowDay: onlyMFS,
			onSelect: rvclearCheckout
		});
	
		$j('#rvcheckoutDate').datepicker({
			showOn: 'both', 
			buttonImage: '../images/misc/calendar.png', 
			dateFormat: 'm/d/yy',
			constrainInput: true,
			beforeShowDay: rvonlyAfter
		});
	}

	if($j("#rvcheckinDateMain").length) {
		$j('#rvcheckinDateMain').datepicker({
			showOn: 'both', 
			buttonImage: '../images/misc/calendar.png', 
			dateFormat: 'm/d/yy',
			minDate: new Date(2012, 3, 21),
			maxDate: new Date(2012, 11, 31),
			constrainInput: true,
			beforeShowDay: onlyMFS,
			onSelect: rvclearCheckoutMain
		});
	
		$j('#rvcheckoutDateMain').datepicker({
			showOn: 'both', 
			buttonImage: '../images/misc/calendar.png', 
			dateFormat: 'm/d/yy',
			constrainInput: true,
			beforeShowDay: rvonlyAfterMain
		});
	}


	if($j("#actStartDate").length) {
		$j("#actStartDate").datepicker({
			showOn: 'button', 
			buttonImage: '../images/misc/calendar.png', 
			buttonImageOnly: true,
			onSelect: function(dateText, inst) {
				$j("#actEndDate").datepicker("option", "minDate", dateText);
				$j("#actEndDate").datepicker("option", "defaultDate", dateText);
			}

		});
	}
	
	if($j("#actEndDate").length) {
		$j("#actEndDate").datepicker({
			showOn: 'button', 
			buttonImage: '../images/misc/calendar.png', 
			buttonImageOnly: true
		});
	}

	if($j("#conDateforVisit").length) {
		$j('#conDateforVisit').datepicker({
			showOn: 'button',
			buttonImage: '../images/misc/calendar.png', 
			dateFormat: 'm/d/yy',
			buttonImageOnly: true,
			minDate: +1
		});
	}
	
  if ($j('#updateForm').length) {
		$j("#updateForm").validate({
			rules: {
				ownName: "required",
				ownSiteID: "required",
				ownEmail: {
					required: true,
					email: true
				},
				ownPasswordConfirm: {
					equalTo: "#ownPassword"
				}
			}
		});
	}

  if ($j('label.infield').length) {
    $j.getScript(
      '/javascript/jquery.infieldlabel.min.js',
      function() {
        $j('label.infield').inFieldLabels();
      }
    );
  }


$j("#send-to-friend-link").fancybox({
	'scrolling'		: 'no',
	'hideOnOverlayClick' : false,
	'hideOnContentClick' : false,
	'titleShow'		: false,
	'height' : 375,
	'width'  : 340,
	'type'   : 'iframe'
});

$j("a.showPriceBreakdown").fancybox({
	'scrolling'		: 'no',
	'hideOnOverlayClick' : false,
	'hideOnContentClick' : false,
	'frameHeight' : 225,
	'frameWidth'  : 400
});



	$j("#reservationTabs").submit(function(){
		if(!isDate($j("#checkinDate").val())){
			alert('Please select a valid checkin date before submitting.');
			$j("#checkintDate").select();
			return false;
		}
	
		if(!isDate($j("#checkoutDate").val())){
			alert('Please select a valid checkout date before submitting.');
			$j("#checkoutDate").select();
			return false;
		}
	
	
		if(!verifyTheDate($j("#checkoutDate").val())) {
			var checkinDate = new Date($j('#checkinDate').val());
			var checkoutDate = new Date($j('#checkoutDate').val());
			var checkoutWeek = checkinDate.clone().add({days:14})
			
			if(checkoutDate.compareTo(checkoutWeek) >= 0) {
				return true;
				// alert('Want to stay longer than a week? Contact a Sherkston Shores Team member today for the best discounts available 1-800-263-8121');
			} else {
				var checkinDay = checkinDate.getDay();
				var checkin3 = checkinDate.clone().addDays(3);					
				var checkin4 = checkinDate.clone().addDays(4);					
				var checkin7 = checkinDate.clone().addDays(7);					
	
				if(checkinDay == 1){
					if((!checkoutDate.equals(checkin4)) && (!checkoutDate.equals(checkin7))){
					  alert('You have selected an invalid checkout date.  Possible dates are: ' + checkin4.toString("dddd, MMMM dd") + ' or ' + checkin7.toString("dddd, MMMM dd") + '\n\nWant to stay longer than a week? Contact a Sherkston Shores Team member today for the best discounts available 1-800-263-8121');
					}
	
				} else if (checkinDay == 5) {
					if((!checkoutDate.equals(checkin3)) && (!checkoutDate.equals(checkin7))){
					  alert('You have selected an invalid checkout date.  Possible dates are: ' + checkin3.toString("dddd, MMMM dd") + ' or ' + checkin7.toString("dddd, MMMM dd") + '\n\nWant to stay longer than a week? Contact a Sherkston Shores Team member today for the best discounts available 1-800-263-8121');
					}
					
				} else if (checkinDay == 6){
					if(!checkoutDate.equals(checkin7)) {
					  alert('You have selected an invalid checkout date.  Possible dates are: ' + checkin7.toString("dddd, MMMM dd") + '\n\nWant to stay longer than a week? Contact a Sherkston Shores Team member today for the best discounts available 1-800-263-8121');
					}
				}
			}
		
			return false;
		}
	});			
	

	$j("#reserveMain").submit(function(){
		if(!isDate($j("#checkinDateMain").val())){
			alert('Please select a valid checkin date before submitting.');
			$j("#checkintDateMain").select();
			return false;
		}
	
		if(!isDate($j("#checkoutDateMain").val())){
			alert('Please select a valid checkout date before submitting.');
			$j("#checkoutDateMain").select();
			return false;
		}
	
		if(!verifyTheDateMain($j("#checkoutDateMain").val())) {
			var checkinDateMain = new Date($j('#checkinDateMain').val());
			var checkoutDateMain = new Date($j('#checkoutDateMain').val());
			var checkoutWeekMain = checkinDateMain.clone().add({days:8})
			
			if(checkoutDateMain.compareTo(checkoutWeekMain) >= 0) {
				alert('Want to stay longer than a week? Contact a Sherkston Shores Team member today for the best discounts available 1-800-263-8121');
			} else {
				var checkinDayMain = checkinDateMain.getDay();
				var checkin3Main = checkinDateMain.clone().addDays(3);					
				var checkin4Main = checkinDateMain.clone().addDays(4);					
				var checkin7Main = checkinDateMain.clone().addDays(7);					
	
				if(checkinDayMain == 1){
					if((!checkoutDateMain.equals(checkin4Main)) && (!checkoutDateMain.equals(checkin7Main))){
					  alert('You have selected an invalid checkout date.  Possible dates are: ' + checkin4Main.toString("dddd, MMMM dd") + ' or ' + checkin7Main.toString("dddd, MMMM dd") + '\n\nWant to stay longer than a week? Contact a Sherkston Shores Team member today for the best discounts available 1-800-263-8121');
					}
	
				} else if (checkinDayMain == 5) {
					if((!checkoutDateMain.equals(checkin3Main)) && (!checkoutDateMain.equals(checkin7Main))){
					  alert('You have selected an invalid checkout date.  Possible dates are: ' + checkin3Main.toString("dddd, MMMM dd") + ' or ' + checkin7Main.toString("dddd, MMMM dd") + '\n\nWant to stay longer than a week? Contact a Sherkston Shores Team member today for the best discounts available 1-800-263-8121');
					}
					
				} else if (checkinDayMain == 6){
					if(!checkoutDateMain.equals(checkin7Main)) {
					  alert('You have selected an invalid checkout date.  Possible dates are: ' + checkin7Main.toString("dddd, MMMM dd") + '\n\nWant to stay longer than a week? Contact a Sherkston Shores Team member today for the best discounts available 1-800-263-8121');
					}
				}
			}

			return false;
		}
	
	});			




  
/*  $j('.send-to-friend').click(function() {
    $j.blockUI.defaults.css = {};
    $j.blockUI({ message: $j('#friend_form'),showOverlay: false});
  });

  $j('.close').click(function(){
    $j.unblockUI();
    return false;
  });

	$j('#sendToFriend').click(function() { 
  	// update the block message 
  	$j.blockUI({ message: "Sending Message..." }); 

		$j.post('send-to-friend-proc.cfm', $j('#sendToFriendForm').serialize(), 
		  function(data){
         $j.unblockUI(); 
			});

  }); 
*/
  
  $j(".slider_container, .photo-box").hover(
    function () {
      $j(this).addClass("hover");
    },
    function () {
      $j(this).removeClass("hover");
    }
  );
  
  $j("#image_slider1").easySlider({
    controlsBefore:	'<div class="button_selector">',
    controlsAfter:	'</div>',	
    prevId: 'gal_prev1',
    nextId: 'gal_next1',
    speed: 600
  });
  
  $j("#image_slider2").easySlider({
    controlsBefore:	'<div class="button_selector">',
    controlsAfter:	'</div>',	
    prevId: 'gal_prev2',
    nextId: 'gal_next2',
    speed: 600
  });
  
  $j("#image_slider3").easySlider({
    controlsBefore:	'<div class="button_selector">',
    controlsAfter:	'</div>',	
    prevId: 'gal_prev3',
    nextId: 'gal_next3',
    speed: 600
  });
  
  $j("#image_slider4").easySlider({
    controlsBefore:	'<div class="button_selector">',
    controlsAfter:	'</div>',	
    prevId: 'gal_prev4',
    nextId: 'gal_next4',
    speed: 600
  });
  
	$j("#image_slider5").easySlider({
    controlsBefore:	'<div class="button_selector">',
    controlsAfter:	'</div>',	
    prevId: 'gal_prev5',
    nextId: 'gal_next5',
    speed: 600
  });

  $j("#image_slider6").easySlider({
    controlsBefore:	'<div class="button_selector">',
    controlsAfter:	'</div>',	
    prevId: 'gal_prev6',
    nextId: 'gal_next6',
    speed: 600
  });

  $j("#image_slider7").easySlider({
    controlsBefore:	'<div class="button_selector">',
    controlsAfter:	'</div>',	
    prevId: 'gal_prev7',
    nextId: 'gal_next7',
    speed: 600
  });

  $j("#image_slider8").easySlider({
    controlsBefore:	'<div class="button_selector">',
    controlsAfter:	'</div>',	
    prevId: 'gal_prev8',
    nextId: 'gal_next8',
    speed: 600
  });
  
  $j(".image_zoom").fancybox({
    'zoomSpeedIn':		300, 
    'zoomSpeedOut':	300, 
    'overlayShow':		false,
    'hideOnContentClick': true
  });

  $j(".info_lightbox").fancybox({
    'zoomSpeedIn':		300, 
    'zoomSpeedOut':	300, 
    'overlayShow':		false,
    'frameWidth': 750,
    'frameHeight': 420,
    'hideOnContentClick': false,
  });




  $j("a.fancy_group").fancybox({
    'hideOnContentClick': false
  });

  if ($j('.cycle_fade').size) {
    $j.getScript(
      '/javascript/jquery.cycle.min.js',
      function() {
        $j('.cycle_fade').cycle({
          fx: 'fade' 
        });
      }
    );
  }

  
});
