// JavaScript Document
$(document).ready(function() {
   $('html').click(function() {
      $(".select-list").hide();
   });
   $('#enablePromo').click(function() {
      $(this).hide();
      $('#rateCodeEnt').show();
      $('#rateCodeEnt').focus();
      return false;
   });
   $('#bookbutton').click(function() {
      if (validateForm() == 0) {
         if($('#rateCodeEnt').val().length == 0) {
            $('#rateCode').attr("disabled", true);
         } else {
            $('#rateCode').removeAttr("disabled");
            $('#rateCode').val($('#rateCodeEnt').val());
         }
			if (validateSpecial() && $('#preview').attr('checked')) {
				$('#resform').attr('action', 'http://www.westgatereservations.com/westgate-resorts-search/');
			} else {
				$('#resform').attr('action', 'https://booking.ihotelier.com/istay/istay.jsp');
			}
         _gaq.push(['_linkByPost', this]);
         $('#resform').submit();
      }
         return false;
   });
   
	$("#select-destination ul li a").click(destinationSelected);
   
	$("#select-resort ul li a").click(resortSelected);
   
	$(".select-list a").click(function(event) {
      event.stopPropagation();
      $input_element = $(this).parent().parent().parent().prev().prev();
      $input_element.val(htmlDecode($(this).html()));
      $(".select-list").hide();
		validateSpecial();
      return false;
   });
   
   $(".wg-select").click(function(event) {
      event.stopPropagation();
      if($(this).next().is(":visible")) {
         $(".select-list").hide();
      } else {
        $(".select-list").hide();
        $(this).next().show();
      }
      return false;
   });
});    
//end document ready 
function destinationSelected() {
   $id = "group-" + $(this).attr('id').match(/\d+/) + "";
   $("#select-resort ul li").hide();
   $setLength = $("#select-resort ul li a[class*='" + $id + "']").length;
   $("#select-resort ul li a[class*='" + $id + "']").parent().show();
   if($setLength == 1) {
      $("#resort").val(htmlDecode($("#select-resort ul li a[class*='" + $id + "']").html()));
      $resid = $("#select-resort ul li a[class*='" + $id + "']").attr('id').match(/\d+/);
      $("#HotelID").val($resid);
   } else {
      $("#resort").val('Select Resort');
		$("#HotelID").val('000');
   }
	validateSpecial();
}

function resortSelected() {
   $id = $(this).attr('id').match(/\d+/);
   $("#HotelID").val($id);
	validateSpecial();
}
function htmlEncode(value){
   return $('<div/>').text(value).html();
}
function htmlDecode(value){
   return $('<div/>').html(value).text();
}

Date.format = 'mm/dd/yy'; 
$(function() {      
   $('.date-pick1').datePicker();
   $('.date-pick1').dpSetOffset(19, -77);
   $('.date-pick2').datePicker();
   $('.date-pick2').dpSetOffset(19, -184);
   $('#arrival-date').bind(
      'dpClosed',
      function(e, selectedDates) {
         var d = selectedDates[0];
         if (d) {
            d = new Date(d);
            $('#departure-date').dpSetStartDate(d.addDays(1).asString());
				$arrivalDate = parseDate($('#arrival-date').val());
	         $departureDate = parseDate($('#departure-date').val());
				$days =  daydiff($arrivalDate, $departureDate);
				if($days <= 0 || isNaN($days)) {
			      $('#departure-date').val("");
				}
				validateSpecial();
         }
      }
   );
   $('#departure-date').bind(
      'dpClosed',
      function(e, selectedDates)
      {
         var d = selectedDates[0];
         if (d) {
            d = new Date(d);
            //$('#arrival-date').dpSetEndDate(d.addDays(-1).asString());
				validateSpecial();
         }
      }
   );
});
   
function validateForm() {
   $errors = 0;
   $errorMessage = "";
   if($('#arrival-date').val().length == 0) {
      $errors++;
      $errorMessage += "Please select an arrival date.\n"
   }
   if($('#departure-date').val().length == 0) {
      $errors++;
      $errorMessage += "Please select a departure date.\n"
   }
   if($('#resort').val() == 'Select Resort') {
      $errors++;
      $errorMessage += "Please select a resort.\n"
   }
   if ($errors > 0) {
      alert($errorMessage);
   }
   return $errors;
}
function validateSpecial() {
	$isSpecial = 0;
	$resortArray = ['75709','75712','75716','75715','75699','75721','75711','75724','75703','75710'];
	$selectedResort = $("#HotelID").val();
	$arrivalDate = parseDate($('#arrival-date').val());
	$departureDate = parseDate($('#departure-date').val());
	$days =  daydiff($arrivalDate, $departureDate);
   $rooms = $("#rooms").val()
   //
	if($.inArray($selectedResort, $resortArray) > -1) {
      $isSpecial++;	
	}
	//
   if($rooms == 1) {
      $isSpecial++;	
	}
	if($days >= 4 && $days <= 7) {
	   $isSpecial++;	
	}
	if($isSpecial == 3) {
	   showSpecial();
		return true;
	} else {
	   hideSpecial();	
		return false;
	}
}
function showSpecial() {
   $(".item-resort-preview").delay(350).fadeIn();	
	$(".item-promo-code").animate({"top" : "273px"}, 'slow');
	$(".item-search").animate({"top" : "266px"}, 'slow');
	$(".item-more-special-offers").css({"border-top" : "solid 1px #CCC", "top" : "43px"});
	$(".booking-special-offers ul").animate({"top" : "44px", "height" : "0px"}, 'slow');
}
function hideSpecial() {
   $(".item-resort-preview").fadeOut('fast');
	$(".item-promo-code").animate({"top" : "229px"}, 'slow');
	$(".item-search").animate({"top" : "222px"}, 'slow');
	$(".booking-special-offers ul").animate({"top" : "0px", "height" : "48px"}, 'slow', function() {
		$(".item-more-special-offers").css({"border-top" : "", "top" : "44px"});
   });
}
function parseDate(str) {
   var mdy = str.split('/')
   return new Date('20' + mdy[2], mdy[0]-1, mdy[1]);
}

function daydiff(first, second) {
   return Math.floor((second-first)/(1000*60*60*24));
}
