jQuery.fn.extend({
  slideRight: function() {
    return this.each(function() {
      jQuery(this).animate({width: 'show'});
    });
  },
  slideLeft: function() {
    return this.each(function() {
      jQuery(this).animate({width: 'hide'});
    });
  },
  slideToggleWidth: function() {
    return this.each(function() {
      var el = jQuery(this);
      if (el.css('display') == 'none') {
    	//el.css('overflow: hidden'); // doesn't fix problem of bottom scroll
        el.slideRight();
      } else {
        el.slideLeft();
      }
    });
  },
  toggleFade: function() {
	return this.each(function() {
		var el = jQuery(this);
		if (el.css('display') == 'none') {
	      el.fadeIn("normal");
	    } else {
	      el.fadeOut("fast");
	    }
	});
  }
});

function toggleLoading()
{
	var content = $('#loadingContent');
	var centreContent = $('#center_content');
	var loading = $('#loadingOverlay');
	var contentHeight = centreContent.height();
	
	loading.css({'height': contentHeight});
	loading.show();
	content.hide();
	$('html, body').animate({scrollTop:0}, 'slow');
	return false;
}


function toggleLoadingShort()
{
	var content = $('#loadingContent');
	var loading = $('#loadingOverlay');
	var contentHeight = content.height();
	loading.css({'height': /* contentHeight */ '120px'});
	loading.show();
	content.hide();
	return false;
}

function compareScroll() 
{
	$('html, body').animate({scrollTop:0}, 'slow');
	$('#hotel_compare_link').effect('highlight', {color: '#00A5C5'}, 8000);
	return false;
}


function bookInsert(hotel_slug, hotel_name)
{
	$('#autocomplete_booking_search_where').val(hotel_name);
	$('#booking_search_where').val(hotel_slug);
	return false;
}

function bookNow(hotel_slug, hotel_name)
{
	$('#autocomplete_booking_search_where').val(hotel_name);
	$('#booking_search_where').val(hotel_slug);
	$('html, body').animate({scrollTop:0}, 'slow');
	$('#carousel-inner').fadeTo('slow', 0.4);
	$('#carousel-overlay').fadeTo('slow', 1.0);
	return false;
}

function updatePrice(id, total, room, roomname)
{
	$('#'+id).html(total);
	
	$('#room_select_notice').hide();
	$('#room_select_ul').show();
	
	$('#price_select_notice').show();
	$('#price_select_ul').hide();
	$('#price_select_total').hide();
	
	$('#room_select_'+room).html(roomname);
	$('#room_select_'+room).show();
}

function updateRooms(num_rooms)
{
  for (var i = 1; i <= 5; i++)
  {
	if (i <= num_rooms)
	{
	  $('#room_'+i).show();
	}
	else
	{
	  $('#booking_form_room_'+i+'_adults').val('0');
	  $('#booking_form_room_'+i+'_children').val('0');
	  $('#room_'+i).hide();
	}
  }
}

jQuery(document).ready(function() {
  jQuery("#viewmore-activate").addClass('active');
  jQuery("#viewmore-contents").slideToggle('fast');  
  jQuery("#viewmore-contents").css('display: none');
  jQuery("#viewmore-activate").click(function() {
	jQuery("#viewmore-contents").slideToggle('fast');
	jQuery("#viewmore-activate").toggleClass('active');
	return false;
  });
});

// Booking form
jQuery(document).ready(function() {
	loadMap();
});
	
function loadMap() 
{
	
  jQuery("#hotellist-activate").addClass('active');
  // jQuery("#hotellist-hotels").css({'display': 'none'}); // .slideToggleWidth();  // Hidden by default
  if (jQuery('#booking-offer')) jQuery('#booking-offer').css({'display': 'block'});

  jQuery("#hotellist-activate").click(function() {

	if (jQuery("#hotellist-hotels").is(':hidden')) {
		pageTracker._trackEvent('search','hotel-list','open');
	}
	  
  	if (jQuery('#mapview-hotels').is(':visible')) {
  		jQuery("#mapview-hotels").slideToggleWidth();
  		jQuery("#mapview-activate").toggleClass('active');
  	}
		jQuery("#hotellist-hotels").slideToggleWidth();
		jQuery("#hotellist-activate").toggleClass('active');
	if (jQuery('#mapview-hotels').is(':hidden')) {
		if (jQuery('#booking-offer')) jQuery('#booking-offer').toggleFade();
  	}
		return false;
  });
  jQuery("#hotellist-close").click(function() {
		jQuery("#hotellist-hotels").slideToggleWidth();
		jQuery("#hotellist-activate").toggleClass('active');
		if (jQuery('#booking-offer')) jQuery('#booking-offer').toggleFade();
		return false;
  });
}

// Booking form (map view)
jQuery(document).ready(function() {
  jQuery("#mapview-activate").addClass('active');
  // jQuery("#mapview-hotels").css({'display': 'none'}); // .slideToggleWidth(); // Hidden by default
  jQuery('#mapview-activate').show(); // As hidden unless JS - map no worky without JS
  jQuery('#booking-offer').css('display: block');
  jQuery("#mapview-activate").click(function() {
	if (jQuery("#mapview-hotels").is(':hidden')) {
		pageTracker._trackEvent('search','map-list','open');
	}
	  
  	if (jQuery('#hotellist-hotels').is(':visible')) {
  		jQuery("#hotellist-hotels").slideToggleWidth();
  		jQuery("#hotellist-activate").toggleClass('active');
  	}
		jQuery("#mapview-hotels").slideToggleWidth();
		try { hMapInit(); } catch (err) { /* oops */ };
		jQuery("#mapview-activate").toggleClass('active');
	if (jQuery('#hotellist-hotels').is(':hidden')) {
  		jQuery('#booking-offer').toggleFade();
  	}	
		return false;
  });
  jQuery("#mapview-close").click(function() {
		jQuery("#mapview-hotels").slideToggleWidth();
		jQuery("#mapview-activate").toggleClass('active');
		jQuery('#booking-offer').toggleFade(); 
		return false;
  });
});


jQuery(document).ready(function() {
	$(".display-wrap").hide();
});


// All links with rel external on them, open in a new window
jQuery(document).ready(function () {
	jQuery("a[rel='external']").attr({'target': '_blank'});
    jQuery(".external").attr({'target': '_blank'});
});


jQuery(document).ready(function() {
	$('.popup_contact_link').colorbox({opacity:0.6});
	//$('.popup_login_link').colorbox(); // IA request to not load in popup
	$('.popup_newsletter_link').colorbox({opacity:0.6});
	$('.popup_myterms_link').colorbox({opacity:0.6});
	
	$('.hotel_contact_link').colorbox({opacity:0.6});
	$('.hotel_map_link').colorbox({opacity:0.6});
	$('.hotel_staf_link').colorbox({opacity:0.6});
	var tourHref = $('.hotel_360_link').attr('href') + '?popup=true';
	$('.hotel_360_link').colorbox({href: tourHref, opacity:0.6});
	$('.hotel_video_link').colorbox({iframe:true, innerWidth:753, innerHeight:397, opacity:0.6});
});


// perform JavaScript after the document is scriptable.
$(function() {
	// setup ul.tabs to work as tabs for each div directly under div.panes
	$("ul.tabs").tabs("div.panes > div.tab-box", { history: true });
});


$(function() {
  $(".scrollable").scrollable({speed: 1600});
  $(".items img").click(function() {
	// see if same thumb is being clicked
	if ($(this).hasClass("active")) { return; }
	// calclulate large image's URL based on the thumbnail URL 
	var url = $(this).attr("src").replace("/160x56/", "/1300x341/");
	// get handle to element that wraps the image and make it semi-transparent
	var wrap = $("#image_wrap").fadeTo("slow", 1);
	// the large image 
	var img = new Image();
	// call this function after it's loaded
	img.onload = function() {
		// make wrapper fully visible
		wrap.fadeTo("slow", 1);
		// change the image
		wrap.find("img").attr("src", url);
	};

	// begin loading the image
	img.src = url;

	// activate item
	$(".items img").removeClass("active");
	$(this).addClass("active");

  // when page loads simulate a "click" on the first image
  }).filter(":first").addClass('active');
});
 

$(function() {
	$('.items2 img').filter(':first').addClass('active');
	$('.scrollable2').pod1fade();
});


$(function() {
	// initialize scrollable
	$(".scrollable3").scrollable({speed: 800});
});

$(function() {
	$(".scrollable4").scrollable({circular: true});
});

$(function() {
	$(".scrollable5").scrollable({ circular: true, mousewheel: true }).navigator();
});

$(function() {
	$(".hotel_facilities img[title]").tooltip({ effect: 'slide', delay: 0});	
	$(".hotels-facilities img[title]").tooltip({ effect: 'slide', delay: 0});	
});


function equalHeight(group) {
	var tallest = 0;
	group.each(function() {
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}
$(document).ready(function() {
	equalHeight($(".fullwidth3col .box-center"));
	equalHeight($("#barcelo-homepage .threecolshome .box-center"));
	equalHeight($(".centercol3col .rnd-box .box-center .min-height"));
});

// remove non-numbers from credit card number field in booking engine.
jQuery(document).ready(function () {
	jQuery('#booking_payment_card_number').live('blur', function () { // the form is output after an ajax call.
		var cc_el = jQuery('#booking_payment_card_number');
		var cc_num = cc_el.val();
		var cc_num_clean = '';
		for (var i = 0, l = cc_num.length; i < l; i++) {
			if (/[0-9]/.test(cc_num.charAt(i))) 
				cc_num_clean += cc_num.charAt(i);
		}
		cc_el.val(cc_num_clean);
	});
});

