
var totalSelectedToCompare = 0;

function startResultsHotelAction()
{
  new Effect.Opacity(ID_HOTEL_RESULTS_CONTAINER, {
    duration:0.3,
    from:1.0,
    to:0.3
  });
}
function endResultsHotelAction()
{
  new Effect.Opacity(ID_HOTEL_RESULTS_CONTAINER, {
    duration:0.5,
    from:0.3,
    to:1.0
  });
  Tooltip.init();
  showMappaGenerale();
}

function sortResultsHotel(keyHrm, sort) {
  startResultsHotelAction();
  var sortTypeDir = sort.split('|');
  var options = {
    onComplete:endResultsHotelAction,
    asynchronous:true,
    parameters: PARAM['KEYHRM'] + '=' + keyHrm + '&' + PARAM['SORTTYPE'] + '=' + sortTypeDir[0] + '&' + PARAM['SORTDIR'] + '=' + sortTypeDir[1]
  };
  var updater = new Ajax.Updater( ID_HOTEL_RESULTS_CONTAINER, URL_HOTEL__SORT, options);
}

function goToPageResultsHotel(keyHrm, page) {
  startResultsHotelAction();
  var options = {
    onComplete:endResultsHotelAction,
    asynchronous:true,
    parameters: PARAM['KEYHRM'] + '=' + keyHrm + '&' + PARAM['PAGE'] + '=' + page
  };
  var updater = new Ajax.Updater( ID_HOTEL_RESULTS_CONTAINER, URL_HOTEL__CHANGE_PAGE, options);  
}

function changePageSize(keyHrm, pageSize) {
  startResultsHotelAction();
  var options = {
    onComplete:endResultsHotelAction,
    asynchronous:true,
    parameters: PARAM['KEYHRM'] + '=' + keyHrm + '&' + PARAM['PAGE_SIZE'] + '=' + pageSize
  };
  var updater = new Ajax.Updater( ID_HOTEL_RESULTS_CONTAINER, URL_HOTEL__CHANGE_PAGE_SIZE, options);  
}

function filterResultsHotel(keyHrm, withKey1, withValue1, withKey2, withValue2) {
  if(disableAllFilterCallBack)
    return;
  totalSelectedToCompare = 0;
  startResultsHotelAction();
  var options = {
    onComplete:endResultsHotelAction,
    asynchronous:true,
    parameters: PARAM['KEYHRM'] + '=' + keyHrm + '&' + withKey1 + '=' + withValue1 + (withKey2 != null ? '&' + withKey2 + '=' + withValue2 : '')
  };
  var updater = new Ajax.Updater( ID_HOTEL_RESULTS_CONTAINER, URL_HOTEL__FILTER, options);  
}

function filterResultsHotelByStarRating(keyHrm, fromStars, toStars) {
  filterResultsHotel(keyHrm, PARAM['FROM_STAR_RATING'], fromStars, PARAM['TO_STAR_RATING'], toStars );
}

function filterResultsHotelByUserRating(keyHrm, fromUserRating, toUserRating) {
  filterResultsHotel(keyHrm, PARAM['FROM_USER_RATING'], fromUserRating, PARAM['TO_USER_RATING'], toUserRating );
}

function filterResultsHotelByPrice(keyHrm, fromPrice, toPrice) {
  filterResultsHotel(keyHrm, PARAM['FROM_PRICE'], fromPrice, PARAM['TO_PRICE'], toPrice );
}

function filterResultsHotelByDistance(keyHrm, fromDistance, toDistance) {
  filterResultsHotel(keyHrm, PARAM['FROM_DISTANCE'], fromDistance, PARAM['TO_DISTANCE'], toDistance );
}

function filterResultsHotelByCityCityZone(keyHrm, cityCityZone) {
  filterResultsHotel(keyHrm, PARAM['BY_CITY_CITYZONE'], cityCityZone);
}
function filterResultsHotelByHotelName(keyHrm, hotelName) {
  filterResultsHotel(keyHrm, PARAM['BY_HOTEL_NAME'], hotelName);
}

function filterResultsHotelSeTypeAgent(keyHrm, paymentTypeInHotel) {
  filterResultsHotel(keyHrm, PARAM['BY_SE_TYPE_AGENT'], paymentTypeInHotel);
}

function filterResultsHotelSeTypeMerchant(keyHrm, paymentTypeOnline) {
  filterResultsHotel(keyHrm, PARAM['BY_SE_TYPE_MERCHANT'], paymentTypeOnline);
}

function filterResultsHotelWithAccessForDisabled(keyHrm, withAccessForDisabled) {
  filterResultsHotel(keyHrm, PARAM['WITH_ACCESS_FOR_DISABLED'], withAccessForDisabled);
}
function filterResultsHotelWithFitnessCenter(keyHrm, withFitnessCenter) {
  filterResultsHotel(keyHrm, PARAM['WITH_FITNESS_CENTER'], withFitnessCenter);
}
function filterResultsHotelWithInternetServices(keyHrm, withInternetServices) {
  filterResultsHotel(keyHrm, PARAM['WITH_INTERNET_SERVICES'], withInternetServices);
}
function filterResultsHotelWithRestaurant(keyHrm, withRestaurant) {
  filterResultsHotel(keyHrm, PARAM['WITH_RESTAURANT'], withRestaurant);
}
function filterResultsHotelWithParking(keyHrm, withParking) {
  filterResultsHotel(keyHrm, PARAM['WITH_PARKING'], withParking);
}
function filterResultsHotelWithSwimmingPool(keyHrm, withSwimmingPool) {
  filterResultsHotel(keyHrm, PARAM['WITH_SWIMMING_POOL'], withSwimmingPool);
}
function filterResultsHotelWithPetsAccepted(keyHrm, withPetsAccepted) {
  filterResultsHotel(keyHrm, PARAM['WITH_PETS_ACCEPTED'], withPetsAccepted);
}function filterResultsHotelWithAirConditioned(keyHrm, withAirConditioned) {
  filterResultsHotel(keyHrm, PARAM['WITH_AIR_CONDITIONED'], withAirConditioned);
}

function selectToCompare(keyHrm, idWhr)
{
  var options = {
    onSuccess:function(t){
      if( t.responseText == '1' )
      {
        $('whr_'+idWhr+'_title').addClassName('selectedToCompare');
        totalSelectedToCompare++;
      }
      else
      {
        $('whr_'+idWhr+'_title').removeClassName('selectedToCompare');
        totalSelectedToCompare--;
      }
    },
    asynchronous:true,
    parameters: PARAM['KEYHRM'] + '=' + keyHrm + '&' + PARAM['ID_WHR'] + '=' + idWhr
  };
  new Ajax.Request(URL_HOTEL__SELECT_TO_COMPARE, options);
}
    
function checkCompare()
{
  if( totalSelectedToCompare < CONST['MIN_SELECTED_TO_COMPARE'])
  {
    alert(MESSAGES['message.selecthoteltocompare']);
    return false;
  }
  return true;
}
      
function printDistance(distance)
{
  if(distance<1000)
    return '' + distance + '&nbsp;' + MESSAGES['label.meters'];
  else
    return '' + distance/1000 + '&nbsp;' + MESSAGES['label.kilometers'];
}
      
/*-----------------------------------------------------------
Toggles element's display value
Input: any number of element id's
Output: none 
---------------------------------------------------------*/
function toggleDisp() {
  for (var i=0;i<arguments.length;i++){
    var d = $(arguments[i]);
    if (d.style.display == 'none')
      d.style.display = 'block';
    else
      d.style.display = 'none';
  }
}

function closeTab(idPrefix, suffix, currentNumTab, numTabs)
{
  var idTabContent = idPrefix + suffix + 'Content' + currentNumTab;
  //var idTabClose   = idPrefix + suffix + 'Close' + currentNumTab;
  var idTabs       = idPrefix + suffix + 'Tabs';
  Effect.toggle( idTabContent ,'blind',{
    duration:0.5
  });
  //$(idTabClose).style.visibility = 'hidden';
  for (var i=1; i<=numTabs; i++)
  {
    var tabHeader = $(idPrefix + suffix + 'Header' + i);
     
    if(tabHeader!=null)
      tabHeader.removeClassName('active');
  }  
  $(idTabs).removeClassName('tabsOpen');
}
function toggleTab(idPrefix, suffix, currentNumTab, numTabs, closeIfOpen) 
{
  closeIfOpen = closeIfOpen == null ? true : closeIfOpen;
  var idTabHeader  = idPrefix + suffix + 'Header' + currentNumTab;
  var idTabContent = idPrefix + suffix + 'Content' + currentNumTab;
  //var idTabClose   = idPrefix + suffix + 'Close' + currentNumTab;
  var idTabs       = idPrefix + suffix + 'Tabs';

  if ($(idTabContent).style.display == 'none')
  {
    var somethingOpen = false;
    for (var i=1; i<=numTabs; i++)
    {
      var tabHeader = $(idPrefix + suffix + 'Header' + i);
      if(!tabHeader)
        continue;
      tabHeader.removeClassName('active');
      var tabContent = $(idPrefix + suffix + 'Content' + i);
      if(tabContent.style.display != 'none')
      {
        //Effect.toggle(idTabContent,'blind',{duration:0.5, queue:{scope:'menus', limit: 3}});
        toggleDisp(idPrefix + suffix + 'Content' + i);
        //$(idPrefix + suffix + 'Close' + i).style.visibility = 'hidden';
        somethingOpen = true;
      }
    }    
    if(tabHeader)
      tabHeader.blur();
    var tabContent = $(idTabContent);
    //tabContent.style.marginTop = '2px';
    if (!somethingOpen)
    {
      Effect.toggle(idTabContent,'appear',{
        duration:0.8
      });
    }
    else
    {
      toggleDisp(idTabContent);
    }
    //$(idTabClose).style.visibility = 'visible';

    if($(idTabHeader) != null)
      $(idTabHeader).addClassName('active');
    
    if($(idTabs) != null)
      $(idTabs).addClassName('tabsOpen');
  }
  else if(closeIfOpen)
  {
    Effect.toggle(idTabContent,'blind',{
      duration:0.5
    });
    $(idTabHeader).removeClassName('active');
    $(idTabs).removeClassName('tabsOpen');    
  }
}
function showHotelMap(jsIdWhr, idWhr, keyHrm, latitude, longitude, hideControls)
{
  var mapContainer = $(jsIdWhr + '_map');
  if (mapContainer.style.display == 'none')
  {
    mapContainer.style.display = 'block';
    window.setTimeout('showHotelMap(\'' +  jsIdWhr +  '\', ' + idWhr + ',' + keyHrm +', \'' + latitude + '\', \'' + longitude + '\');', 250);
    return;
  }
  var hotelMap = new MapManager(jsIdWhr + '_map', hideControls);
  hotelMap.show(latitude, longitude);
  var marker = hotelMap.addNewMarker( latitude, longitude );
  var urlParams = PARAM['KEYHRM'] + '=' + keyHrm + '&' + PARAM['ID_WHR'] + '=' + idWhr;
  hotelMap.setMarkerAjaxContent(marker, URL_HOTEL_MAP_MARKER_CONTENT, urlParams);  
}

function showHotelImageGallery(jsIdWhr, idWhr, keyHrm)
{
  var idImageGalleryContainer = jsIdWhr + '_slideShowGalleryContainer';
  var spinnerContainerObj = $(idImageGalleryContainer).firstDescendant();
  if( spinnerContainerObj!=null && $(spinnerContainerObj).hasClassName('spinnerContainer') )
  {
    var options = {
      //onComplete:getCurrentImageGallery,
      asynchronous:true,
      parameters: PARAM['KEYHRM'] + '=' + keyHrm + '&' + PARAM['ID_WHR'] + '=' + idWhr
    };
    new Ajax.Updater( idImageGalleryContainer, URL_HOTEL_IMAGE_GALLERY_CONTENT, options);  
  }
}

function showHotelCancellationPolicies(jsIdWhr, idWhr, idPo, keyHrm)
{
  var idCancellationPoliciesContainer = jsIdWhr + '_cancellationPoliciesContainer';
  var spinnerContainerObj = $(idCancellationPoliciesContainer).firstDescendant();
  if( spinnerContainerObj!=null && $(spinnerContainerObj).hasClassName('spinnerContainer') )
  {
    var options = {
      //onComplete:getCurrentImageGallery,
      asynchronous:true,
      parameters: PARAM['KEYHRM'] + '=' + keyHrm + '&' + PARAM['ID_WHR'] + '=' + idWhr + '&' + PARAM['ID_PO'] + '=' + idPo
    };
    new Ajax.Updater( idCancellationPoliciesContainer, URL_HOTEL_POLICIES, options);  
  }
}

function changeCreditCardLogo(logo, basePath)
{
  $('idImgCreditCardLogo').src = basePath + 'icon_credit_card_' + ( logo != '' ? logo.toLowerCase() : 'nothing') + '.gif';
}

function bookingWorkFlow(execSubmit, idBtn) 
{
  Modalbox.show(MESSAGES['message.waiting_for_operation'], {
    title: MESSAGES['message.waiting'],
    width: 600,
    height: 100,
    overlayOpacity: .25
  });
  Modalbox.deactivate(); 
  setTimeout(function(){
    if(execSubmit)
      $(idBtn).submit();
    else
      eval('$(\'' + idBtn + '\').href');
  }, 1500); 
  return false;  
}

function manageBookingHotelDetails(idTitleToBold, idBodyToShow, idTitleToNormal, idBodyToHide)
{
  if( $(idBodyToShow).style.display == 'none' )
  {
    Effect.BlindUp(idBodyToHide, { 
      duration: 0.5
    });
    Effect.BlindDown(idBodyToShow, {
      duration: 0.5,
      queue: 'end'
    });
    $(idTitleToBold).toggleClassName("active");
    $(idTitleToNormal).toggleClassName("active");
  }
}

function showGalleryFromThumbnail(resulJsId, resultId, keyHrm) 
{
  toggleTab(resulJsId, 'Main',1,2, false);
  toggleTab(resulJsId, 'Sub',3,5, false);
//showHotelImageGallery(resulJsId, resultId, keyHrm);
}


function addToFavourite(idWhr,keyHrm) {
  
  var options = {
    
    asynchronous:true,
    parameters: PARAM['KEYHRM'] + '=' + keyHrm + '&' + PARAM['ID_WHR'] + '=' + idWhr
  };
  var updater = new Ajax.Request( URL_ADD_FAVOURITE, options);

}


function removeToFavourite(idWhr,keyHrm) {

  var options = {
    
    asynchronous:true,
    parameters:  PARAM['KEYHRM'] + '=' + keyHrm + '&' +PARAM['ID_WHR'] + '=' + idWhr
  };
  var updater = new Ajax.Request( URL_REMOVE_FAVOURITE, options);
}
