var hotelXML;
var locationsXML;
var testXML;
var oldPattern = '';
var hotelCount;
var locationCount;
var offerHint = 'Enter Offer Code';
var arrivalHint = 'MM / DD / YYYY';
var searchHint = "Enter Hotel or Location";

function hotelNameSearch(searchText, restrictCountry, restrictState, restrictCity){
  $.ajax({
    type: "POST",
    data: {"name" : searchText, "restrictCountry" : restrictCountry, "restrictState" : restrictState, "restrictCity": restrictCity},
    url: "/core/search.asmx/HotelName",
    datatype: "xml",
    success: loadHotelNameDataXML
  });
}
function loadHotelNameDataXML(xml){
  hotelXML = xml;
  var currentSearchString = $('#search').val();
  $(hotelXML).find("hotels:containsi('" + currentSearchString + "')").each(function(){
    fillSelectHotel($(this).find('code').text(), $(this).find('hotelName').text());
  });
  handleStyles();
}

function fillSelectHotel(val, main){
  if (hotelCount < 5){
    var tempMain = new RegExp('(' + $('#search').val() + ')', 'gi');
    tempMain = main.replace(tempMain, '<span class="hl">$1</span>');
    $('#autoCompleteHotels').append('<dd><a href="#" onclick="return selectHotel(\'' + val + '\')">' + tempMain + '</a><dd>');
  }
  hotelCount ++;
}

function locationsSearch(searchText, restrictCountry, restrictState){
  $.ajax({
    type: "POST",
    data: {"name" : searchText, "restrictCountry" : restrictCountry, "restrictState" : restrictState},
    url: "/core/search.asmx/Locations",
    datatype: "xml",
    success: loadLocationsDataXML
  });
}

function loadLocationsDataXML(xml){
  locationsXML = xml;
  var currentSearchString = $('#search').val();
  $(locationsXML).find("locations:containsi('" + currentSearchString + "')").each(function(){
    fillSelectLocations($(this).find('type').text(), $(this).find('location').text());
  });
  handleStyles();
}

function fillSelectLocations(val, main){
  if (locationCount < 5){
    var tempMain = new RegExp('(' + $('#search').val() + ')', 'gi');
    tempMain = main.replace(tempMain, '<span class="hl">$1</span>');
    $('#autoCompleteLocations').append('<dd><a href="#" onclick="return selectLocation(\'' + val + '\',\'' + main + '\')">' + tempMain + '</a></dd>');
  }
  locationCount ++;
}

function doSearch(){
  $('#autoCompleteHotels').find('dd').remove();
  $('#autoCompleteLocations').find('dd').remove();
  var searchString = $(this).val();
  hotelCount = 0;
  locationCount = 0;
  if(searchString.length > 1){
    var newSearch = true;
    moreHotelsTrip = true;
    moreLocsTrip = true;
    if(oldPattern != ''){
      if(searchString.indexOf(oldPattern) > -1){
        $(hotelXML).find("hotelName:containsi('" + searchString + "')").parent().each(function(){
          fillSelectHotel($(this).find('code').text(), $(this).find('hotelName').text());
        });
          $(locationsXML).find("location:containsi('" + searchString + "')").parent().each(function(){
            fillSelectLocations($(this).find('type').text(), $(this).find('location').text());
          });
        newSearch = false;
      }
    }
    if(newSearch){
      hotelNameSearch(searchString, getSelectedCountry(), getSelectedState(), getSelectedCity());
        locationsSearch(searchString, getSelectedCountry(), getSelectedState());
      oldPattern = searchString;
    }
  }
  handleStyles();
}

function getSelectedCountry(){
  return ''
}

function getSelectedState(){
  return '';
}

function getSelectedCity(){
  return '';
}
var moreHotelsTrip = true;
var moreLocsTrip = true;
function handleStyles(){
  if(hotelCount > 0 || locationCount > 0){
    if(hotelCount > 0){
      if(hotelCount > 5 && moreHotelsTrip){
        $('#autoCompleteHotels').append('<dd><a href="/search.aspx?hotelname=' + $('#search').val() + '" class="looklink">View More...</a><dd>');
        moreHotelsTrip = false;
      }
      $('#autoCompleteHotels').css('display', 'block');
    }else{
      $('#autoCompleteHotels').css('display', 'none');
    }
    if(locationCount > 0){
      if(locationCount > 5 && moreLocsTrip){
        $('#autoCompleteLocations').append('<dd><a href="/search.aspx?locationname=' + $('#search').val() + '" class="looklink">View More...</a><dd>');
        moreLocsTrip = false;
      }
      $('#autoCompleteLocations').css('display', 'block');
    }else{
      $('autoCompleteLocations').css('display', 'none');
    }
    $('#searchFlyOut').css('display', 'block');
  }else{
    $('#searchFlyOut').css('display', 'none');
  }
}
function fillDrillDown(id, selectedName, code){
  $('#drilldown').html('<input type="hidden" id="'+ id + '" name="'+ id + '" value="' + code + '" />');
}

function selectHotel(code){
  var selectedName = $(hotelXML).find("code:contains('" + code + "')").parent().find('hotelName').first().text();
  fillDrillDown("selectedHotel", selectedName, code);
  $('#autoCompleteHotels').find('dd').remove();
  $('#autoCompleteLocations').find('dd').remove();
  $('#searchFlyOut').css('display', 'none');
  $('#search').val(selectedName);
  oldPattern = '';
  return false;
}

function selectLocation(code, name){
  fillDrillDown('selected' + code, name, name);
  $('#searchFlyOut').css('display', 'none');
  $('#search').val(name);
  oldPattern = '';
  return false;
}

$(document).ready(function(){

  $('#search')
    .keyup(doSearch)
    .focusin(function(){
      if($(this).val() == searchHint){
        $(this).val('');
      }else{
        this.select();
      }
    })
    .focusout(function(){
      if($(this).val() == ''){
        $(this).val(searchHint);
      }
    });
  $("#txbxOffer")
    .focusin(function(){
      if($(this).val() == offerHint){
        $(this).val('');
      }
    })
    .focusout(function(){
      if($(this).val() == ''){
        $(this).val(offerHint);
      }
    });
    $("#txbxArive").datepicker({
        buttonImageOnly: true,
        showButtonPanel: true,
        numberOfMonths: 2,
        onSelect : function(dateText, inst){
          if ($("#txbxDepart").val() == arrivalHint){
            $("#txbxDepart").datepicker('setDate', dateText);
            $("#txbxDepart").datepicker('setDate', 'c+1d');
          }
        }
    })
    .focusin(function(){
      if($(this).val() == arrivalHint){
        $(this).val('');
      }
    })
    .focusout(function(){
      if($(this).val() == ''){
        $(this).val(arrivalHint);
      }
    });
    $('#cal_for_txbxArive').click(function(){
      $("#txbxArive").focus();
    });
    $("#txbxDepart").datepicker({
        buttonImageOnly: true,
        showButtonPanel: true,
        numberOfMonths: 2
    })
    .focusin(function(){
      if($(this).val() == arrivalHint){
        $(this).val('');
      }
    })
    .focusout(function(){
      if($(this).val() == ''){
        $(this).val(arrivalHint);
      }
    });
    $('#cal_for_txbxDepart').click(function(){
      $("#txbxDepart").focus();
    });
    $('body').click(function(){
      $('#searchFlyOut').css('display', 'none');
    });
    $('#searchFlyOut').click(function(event){
      event.stopPropagation();
    });
});

// this is a modification to jQuery to make the contians() selector work regardless of case
$.extend($.expr[':'], {
  'containsi': function(elem, i, match, array)
  {
    return ($(elem).text().toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0) ||
    (scwtf($(elem).text()).toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0);
  }
});
function scwtf(source){
  return source.replace(/\xE0/g, 'a').replace(/\xC7/g, 'c').replace(/\xE7/g, 'c').replace(/\xE8/g, 'e').replace(/\xE9/g, 'e').replace(/\xEB/g, 'e').replace(/\xED/g, 'i').replace(/\xF3/g, 'o').replace(/\xF4/g, 'o').replace(/\xF6/g, 'o').replace(/\xFA/g, 'u').replace(/\xFC/g, 'u').replace(/\xAE/g, '').replace(/\x2122/g, '').replace(/\343/g,'a').replace(/\u0101/g, 'a').replace(/\361/g,'n').replace(/\316/g,'I');
}

