
$(document).ready(function() {
  
  //open all links tagged 'external' in a new window
  $('a[rel="external"], #countries li a').click(function() {
      window.open($(this).attr('href'));
      return false;
  });
  
  //homepage view toggle
  $('#book_room_h, #view_resv_h').click(function() {
    if($(this).hasClass('cur')) {} else {
      $(this).parent().toggleClass('resv');
      $('.cur').removeClass('cur');
      $(this).addClass('cur');
      $('.home_tab:visible').hide();
      $('#tab_'+$(this).attr('rel')).show();
    }
  });
  

  //date picker in booking boxes
  $('#check_in').datepicker({
    minDate: 0,
    maxDate: 357,
    // beforeShow: customRange,     
    speed: "fast",    
    showOn: "both", 
    buttonImage: "../../../common/images/ico_cal.png", 
    buttonImageOnly: true,
    changeMonth: false, 
    changeYear: false,
    closeAtTop: false,
    numberOfMonths: 2
  });
  
   $('#check_out').datepicker({
    minDate: 1,
    maxDate: 357,
    // beforeShow: customRange,     
    speed: "fast",    
    showOn: "both", 
    buttonImage: "../../../common/images/ico_cal.png", 
    buttonImageOnly: true,
    changeMonth: false, 
    changeYear: false,
    closeAtTop: false,
    numberOfMonths: 2
  });
  
  //sorting actions for hotel listing pages
  var sortSlideSpeed = 500;
  $('#sorting_ca p.push a').click(function() {
    if ($('#sorting_ca p.push').children().hasClass('sort_open')) {
      if ($(this).hasClass('sort_open')) {
        $('#sort_amen,#sort_map').slideUp(sortSlideSpeed%2);
        $('.sort_open').removeClass('sort_open');
      } else {
        $('#sort_amen,#sort_map').slideUp(300);
        $('.sort_open').removeClass('sort_open');
        $('#'+$(this).attr('rel')).show().hide().css({margin: 0}).slideDown(sortSlideSpeed);
		  if ($('#'+$(this).attr('rel')).attr('id') == 'sort_map'){
		  	loadVEAPI();
		  }
        $(this).addClass('sort_open');
      }
    } else {
      $('#'+$(this).attr('rel')).show().hide().css({margin: 0}).slideDown(sortSlideSpeed);
	  if ($('#'+$(this).attr('rel')).attr('id') == 'sort_map'){
	  	loadVEAPI();
	  }
      $(this).addClass('sort_open');      
    }
    return false;
  });
  
  ////////////////////////////// Added by Simon
  //sorting actions for hotels lists in hotel listing pages
  $('#filter_results').click(function() {
	  var filterString = "";
	  filterString = document.getElementById('biz_center').checked? filterString+"BUSC;|":filterString;
	  filterString = document.getElementById('exercise').checked? filterString+"EXRM;|":filterString;
	  filterString = document.getElementById('breakfast').checked? filterString+"CONT;|":filterString;
	  filterString = document.getElementById('parking').checked? filterString+"PARK;|":filterString;
	  filterString = document.getElementById('wifi').checked? filterString+"WIFI;|":filterString;
	  filterString = document.getElementById('pool').checked? filterString+"POOL;|":filterString;
	  filterString = document.getElementById('pets').checked? filterString+"PETS;|":filterString;

	  var arrFilterStr = filterString.split("|");
	  var hotelList = $('#hotelList').children();
	  for (hotelListIndex=0; hotelListIndex<hotelList.length; hotelListIndex++)
	  {
		  var amentities = getAmenities($(hotelList[hotelListIndex]).attr('id'));
		  if (match(amentities, arrFilterStr))
		  {
			  if ($(hotelList[hotelListIndex]).hasClass('sort_close'))
			  {
				$(hotelList[hotelListIndex]).show().hide().css({margin: 0}).slideDown(sortSlideSpeed);
				$(hotelList[hotelListIndex]).removeClass('sort_close');
			  }
		  }else{
			  if (!$(hotelList[hotelListIndex]).hasClass('sort_close'))
			  {
				$(hotelList[hotelListIndex]).slideUp(sortSlideSpeed%2);
				$(hotelList[hotelListIndex]).addClass('sort_close');
			  }
		  }
	  }
  });

  function getAmenities(hotelID){
	  return amenitiesMap[hotelID];
  }

  function match(amenity, filterStr){
	  if (!amenity || amenity.length == 0)
	  {
		  return false;
	  }
	  for (filterStrIndex=0; filterStrIndex<filterStr.length; filterStrIndex++)
	  {
		  if (amenity.indexOf(filterStr[filterStrIndex]) == -1)
		  {
			  return false;
		  }
	  }
	  return true;
  }
  //////////////////////////////
  
  //language dropdown
  $('#cur_lang').click(function() {
    $(this).next().toggle();
    $(this).toggleClass('open');
    return false;
  });

 
  //setup for gallery zoom plugin
  setupZoom();  
  
  //gallery slider
  //-----------------------------------------------------------------------
  var imgSize = $('#gallery_slider img').size();
  var imgRemainder = imgSize-4;  
  $('#gallery_prev').hide();
  $('#gallery_slider').attr('rel', imgSize);  
  $('#gallery_next').click(function() {
    $('#gallery_prev').show();      
    curRel = $('#gallery_slider').attr('rel')-1;
    $('#gallery_slider').attr('rel', curRel);
    $('#gallery_slider').animate({ left: '-=69px' }, 350);      
    if($('#gallery_slider').attr('rel') == imgSize-imgRemainder) {
      $(this).hide();
    }
    return false;
  });
  $('#gallery_prev').click(function() {
    $('#gallery_next').show();
    curRel = $('#gallery_slider').attr('rel');
    curRel++;
    $('#gallery_slider').attr('rel', curRel);
    $('#gallery_slider').animate({ left: '+=69px' }, 350);      
    if($('#gallery_slider').attr('rel') == imgSize) {
      $(this).hide();
    }    
    return false;
  });  
  
  
  //gallery hover
  $('#gallery .main_img, #gallery_slider a').hover(function() {
    $(this).append('<span class="enlarge" title="Click to enlarge"></span>');
  }, function() {
    $(this).children('span').remove();
  });
  
  
  //Price slider
  //-----------------------------------------------------------------------
  
  //hide arrows on all price list boxes unless there are more than 7 dates
  $('.price_list').each(function() {
    if($(this).children().find('th').size() <= 7) {
      $(this).children('.price_forward').hide();
    }
  });
  $('.price_slider').each(function() {
    var totalTh = $(this).find('th').size();
    $(this).attr('rel', totalTh);
  });
 
  //forward event start
  $('.price_forward').bind('hotelForwardEvent', function() {
    var slider = $(this).siblings('.price_mask').children('.price_slider');
    var thSize = $(this).siblings('.price_mask').find('th').size();
    var priceRemainder = thSize-7;    
    $(this).next('.price_back').show();        
    curRel_price = slider.attr('rel')-1;
    slider.attr('rel', curRel_price);
    slider.animate({ left: '-=58px' }, 350);       
    if(slider.attr('rel') == thSize-priceRemainder) {
      $(this).hide();
    }
    return false;
  });
  
  var thhotel = $('.price_forward').siblings('.price_mask').find('th');
  var h_size = $('.price_forward').siblings('.price_mask').find('th').size();
  var availableDayCol = 0;
  while ($(thhotel[availableDayCol]).hasClass("ex_date") && availableDayCol < h_size){
  	availableDayCol++;
  }
  //when init the page, move the available date to the second column of the table
  var i = 0;
  while(i < (availableDayCol-1)){
  	$('.price_forward').trigger('hotelForwardEvent');  
  	i++;
  }
  
  $('.price_forward').click(function() {
    var slider = $(this).siblings('.price_mask').children('.price_slider');
    var thSize = $(this).siblings('.price_mask').find('th').size();
    var priceRemainder = thSize-7;    
    $(this).next('.price_back').show();        
    curRel_price = slider.attr('rel')-1;
    slider.attr('rel', curRel_price);
    slider.animate({ left: '-=58px' }, 350);       
    if(slider.attr('rel') == thSize-priceRemainder) {
      $(this).hide();
    }
    return false;
  });
  //forward event end
  
  $('.price_back').click(function() {
    var slider = $(this).siblings('.price_mask').children('.price_slider');
    var thSize = $(this).siblings('.price_mask').find('th').size();
    $(this).prev('.price_forward').show();
    curRel_price = slider.attr('rel');
    curRel_price++;
    slider.attr('rel', curRel_price);
    slider.animate({ left: '+=58px' }, 350);
    if(slider.attr('rel') == thSize) {
      $(this).hide();
    }    
    return false;
  });  
 
 
  //Room detail price slider
  //-----------------------------------------------------------------------
  
  var priceSize = $('.room_slider:first li').size();
  if ($('.table_top').hasClass('mult_room')) {
    var priceRemainder = priceSize-5;
  } else {
    var priceRemainder = priceSize-7;    
  }
  var firstSlider = $('.room_slider:first');
  $('#room_back').hide();
  firstSlider.attr('rel', priceSize);  

  $('#room_forward').bind('roomForwardEvent', function() {
    $('#room_back').show();      
    curRel = firstSlider.attr('rel')-1;
    firstSlider.attr('rel', curRel);
    $('.room_slider').animate({ left: '-=58px' }, 350);      
    if(firstSlider.attr('rel') == priceSize-priceRemainder) {
      $(this).hide();
   }
    return false;
  });

  var throom = $('.room_slider:first li');
  var r_size = $('.room_slider:first li').size();
  availableDayCol = 0;
  while (($(throom[availableDayCol]).hasClass("ex_date") 
  		|| $(throom[availableDayCol]).hasClass("first_ex_date"))
  		&& availableDayCol < r_size){
  	availableDayCol++;
  }
   
  //when init the page, move the available date to the second column of the table
  var i = 0;
  while(i < (availableDayCol-1)){
  	$('#room_forward').trigger('roomForwardEvent');  
  	i++;
  }
  
  $('#room_forward').click(function() {
    $('#room_back').show();      
    curRel = firstSlider.attr('rel')-1;
    firstSlider.attr('rel', curRel);
    $('.room_slider').animate({ left: '-=58px' }, 350);      
    if(firstSlider.attr('rel') == priceSize-priceRemainder) {
      $(this).hide();
    }
    return false;
  });
  $('#room_back').click(function() {
    $('#room_forward').show();
    curRel = firstSlider.attr('rel');
    curRel++;
    firstSlider.attr('rel', curRel);
    $('.room_slider').animate({ left: '+=58px' }, 350);      
    if(firstSlider.attr('rel') == priceSize) {
      $(this).hide();
    }    
    return false;
  });
  
  
  
  //mult select form
  $('.room_wrap').children('select').change(function() {
    if($(this).val() == 0) {
      $(this).parent().siblings("div:visible").hide();
    } else {
      $(this).parent().siblings("div:hidden").show();      
    }
    
  });
 
  $('.add_pack').click(function() {
  	//if a package is selected, remove it first and then add again
	if ($('#pack_added').attr('style').toLowerCase().indexOf('none') == -1){
	    $('#pack_added').fadeOut(300);
		$('.success').fadeOut(300);
	}
    $(this).parent().parent().children('.success').fadeIn(300, function(){
      $('#pack_added').fadeIn(300);
    });
    return true;
  });
  $('#remove_pack').click(function() {
    $('#pack_added').fadeOut(300, function(){
      $('.success').fadeOut(300);
    });
    return true;
  });
  //-----------------------------------
  
});


//Selects range for date picker
function customRange(input) { 
    return {
      minDate: (input.id == "check_out" ? $("#check_in").datepicker("getDate") : null), 
      maxDate: (input.id == "check_in" ? $("#check_out").datepicker("getDate") : null)
    }; 
}