// Comment to fix EY deploy bug

function showItemOnMap(lat, lng) {
	if(GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("sidebarMap"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(lat, lng), 15);
    var marker = new GMarker(new GLatLng(lat, lng));
    map.addOverlay(marker);
  }
};

$(document).ready(function(){
	
	
   // Javascript Destination Search Box in header of layout

   var label = $("div#destinationSearchForm label").text();
   $("div#destinationSearchForm input:first").val(label);
   $("div#destinationSearchForm input:first").focus(function() {
      $("div#destinationSearchForm input:first").val("");
   });
   
   	$('a.addToTrip').click(function(e){
		e.preventDefault();
		$(this).next().children('div.itemDateTime').fadeIn();
	});
	
   $('p.callToAction a').toggle(
   		function(e){
   		   e.preventDefault();
   		   $(this).parent().next().fadeIn();
   		   return false;
   		},
   		function(e){
   		   e.preventDefault();
   		   $(this).parent().next().fadeOut();
   		   return false;
   		}
	 );
	
   $('a.closeItemDateTime').click(function(e){
      e.preventDefault();
      $(this).parent().parent().parent().fadeOut();
      return false;
   });

	$('input.selectNewTrip').each(function(){
		if(this.checked == true){
			this.checked = false;
		}
	});

	$('input.selectExistingTrip').each(function(){
		if(this.checked == false){
			this.checked = true;
		}
	});
	
	$('input.selectNewTrip').click(function(){
		$(this).parent().next().fadeOut(function(){
			$(this).next().fadeIn();
		});
	});
	
	$('input.selectExistingTrip').click(function(){
		$(this).parent().next().next().fadeOut(function(){
			$(this).prev().fadeIn();
		});
	});

   // Origin Locator
   $("p#origin a.editMyLocation").click(function(e){
      e.preventDefault();
      $("span#editOrigin").fadeOut(function(){
         $("div#locationForm").fadeIn();
      });
      $("p#origin a.editMyLocation:first").css("opacity", "0.1");
   });

   $("div#locationForm a").click(function(e){
      e.preventDefault();
      $("div#locationForm").fadeOut(function(){
         $("span#editOrigin").fadeIn();
      });
      $("p#origin a.editMyLocation:first").css("opacity", "1");
   });

   //Sidebar menu
   $('ul#sidebarMenu li h4').toggle(
   function(e){
      e.preventDefault();
      $(this).next().hide();
   }
   ,
   function(e){
      e.preventDefault();
      $(this).next().show();
   }
);
	
   //DatePicker fields
	
	var activeCal = false;
	
  $('input.chooseDate').datePicker({clickInput:true}).click(function(){
		activeCal = true;
		$('a.cancel').fadeIn();
	});
	
	$('#startDate').bind(
  	'dpClosed',
  	function(e, selectedDates)
  	{
  		var d = selectedDates[0];
  		if (d) {
  			d = new Date(d);
  			$('#endDate').dpSetStartDate(d.addDays(1).asString());
  		}
  	}
  );
  $('#endDate').bind(
  	'dpClosed',
  	function(e, selectedDates)
  	{
  		var d = selectedDates[0];
  		if (d) {
  			d = new Date(d);
  			$('#startDate').dpSetEndDate(d.addDays(-1).asString());
  		}
  	}
  );
	
	
   // Add a destination process JS
	
   var destLabel = $('p.destField > label').text();
   $('p.destField > label').next().val(destLabel).focus(function(){
      $(this).val("");
   });

	
   // Add an Attraction Process
   var photoCounter = 1;
   $('#photoFields li:nth-child(2)').css('display','block');
   $('#photoFields li:first').css('display','block');
   $('a.addNewPhoto').click(function(e){
      e.preventDefault();
      $(this).parent().parent().hide().next().fadeIn();
      var caption = $(this).parent().parent().children('p:first').children('input').val();
      $('#photoFields li:first p#noneSelected').text('');
      if(caption == ''){
         caption = 'no file selected';
         $('#photoFields li:first').append('<p class="fileName" id="fileName-' + photoCounter + '">'+ caption + '</p>');
      }
      else{
         $('#photoFields li:first').append('<p class="fileName" id="fileName-' + photoCounter + '">'+ caption + ' <a class="remove">Remove</a></p>');
         $('a.remove').unbind('click');
         $('a.remove').bind('click', function(){
            var id = $(this).parent().attr("id");
            var position = id.split('-');
            position = position[1];
            position++;
            $(this).parent().parent().parent().children('li:nth-child('+position+')').empty();
            $(this).parent().fadeOut();
         });
      };
      photoCounter++;
      return false;
   });

	// Show search transition div
	
	$('form#searchHotels').submit(function(e) {
		$('div#container').fadeOut(function(){
			$('div#transition').fadeIn();
		});
	});
	
	if ($('a.lightbox').length > 0) {
		$('a.lightbox').fancybox();
	};
	
	// Change dates on available hotels
	
  
	var distance = 10;
	var time = 250;
	var hideDelay = 500;
	
	var hideDelayTimer = null;
	var beingShown = false;
	var visible = false;
	
	var trigger = $('a#datesOfStay', this);
	var popup = $('div#changeDatesOfStay', this);
	$([trigger.get(0), popup.get(0)]).mouseover(function(){
		if(hideDelayTimer) clearTimeout(hideDelayTimer);
		
		if(beingShown || visible) {
			return;
		} else{
			beingShown = true;
			popup.css({
				display: 'block',
				top: 45,
				left: 337
			}).animate({
				opacity: 1,
				top: '-=' + distance + 'px'
			}, function() {
				beingShown = false;
				visible = true;
			});
		}
	}).mouseout(function(){
		if(activeCal) {
			return;
		}
		else {
			if(hideDelayTimer) clearTimeout(hideDelayTimer);

			hideDelayTimer = setTimeout(function() {
				hideDelayTimer = null;
				popup.animate({
					opacity: 0,
					top: '-=' + distance + 'px'
				}, function(){
					visible = false;
					$(this).css('display', 'none');
				});
			}, hideDelay);
		}
	});
	
	
	
	
	// FrontPAge info popup
	
	$('div#popups').css('display', 'none');
	
	$('#infoPage').each(function () {
	    // options
	    var distance = 10;
	    var time = 250;
	    var hideDelay = 500;

	    var hideDelayTimer = null;

	    // tracker
	    var beingShown = false;
	    var shown = false;

	    var trigger = $('a#trig', this);
	    var popup = $('div#popups', this).css('opacity', 0);

	    // set the mouseover and mouseout on both element
	    $([trigger.get(0), popup.get(0)]).mouseover(function () {
	      // stops the hide event if we move from the trigger to the popup element
	      if (hideDelayTimer) clearTimeout(hideDelayTimer);

	      // don't trigger the animation again if we're being shown, or already visible
	      if (beingShown || shown) {
	        return;
	      } else {
	        beingShown = true;

	        // reset position of popup box
	        popup.css({
	          top: -80,
	          display: 'block' // brings the popup back in to view
	        })

	        // (we're using chaining on the popup) now animate it's opacity and position
	        .animate({
	          top: '-=' + distance + 'px',
	          opacity: 1
	        }, time, 'swing', function() {
	          // once the animation is complete, set the tracker variables
	          beingShown = false;
	          shown = true;
	        });
	      }
	    }).mouseout(function () {
	      // reset the timer if we get fired again - avoids double animations
	      if (hideDelayTimer) clearTimeout(hideDelayTimer);

	      // store the timer so that it can be cleared in the mouseover if required
	      hideDelayTimer = setTimeout(function () {
	        hideDelayTimer = null;
	        popup.animate({
	          top: '-=' + distance + 'px',
	          opacity: 0
	        }, time, 'swing', function () {
	          // once the animate is complete, set the tracker variables
	          shown = false;
	          // hide the popup entirely after the effect (opacity alone doesn't do the job)
	          popup.css('display', 'none');
	        });
	      }, hideDelay);
	    });
	  });
	
	
	$('ul#mostViewed').hide();
	
	$('div#topDestinations > h3 > a:first').click(function(e){
		e.preventDefault();
		$(this).addClass("selected").next().removeClass("selected");
		$('ul#bestRated').fadeOut(function(){
			$('ul#mostViewed').fadeIn();
		});
	});
	
	$('div#topDestinations > h3 > a:last').click(function(e){
		e.preventDefault();
		$(this).addClass("selected").prev().removeClass("selected");
		$('ul#mostViewed').fadeOut(function(){
			$('ul#bestRated').fadeIn();
		});
	});
	
	
	
	$("div#changeDatesOfStay a.cancel").click(function(){
		if(hideDelayTimer) clearTimeout(hideDelayTimer);

			popup.animate({
				opacity: 0,
				top: '-=' + distance + 'px'
			}, function(){
				visible = false;
				$(this).css('display', 'none');
			});
		$('a.cancel').fadeOut();
		activeCal = false;
	});
	
	
});
