// AVOID ENTER
function keypress(e) {
  var keycode = e ? e.which : window.event.keyCode;
  return keycode != 13;
}
document.onkeypress = keypress;
if (document.layers) document.captureEvents(Event.KEYPRESS);



Event.observe(window, 'load', async_search, false);

// Init asynchron search when load
function async_search(){
  Event.observe('livesearch', 'keyup', async_search_key_press, false);
  Event.observe('livesearch', 'focus', async_search_focus, false);
  Event.observe('livesearch', 'blur', async_search_hide_delayed, false);
  document.getElementById('livesearch').setAttribute("autocomplete","off");
}


// Async Search - Hide Delay
function async_search_hide_delayed() {
  window.setTimeout("async_search_hide()", 300);
}

// Async Search - Hide
function async_search_hide() {
  document.getElementById("LSResult").style.display = "none";
  var highlight = document.getElementById("LSHighlight");
  if (highlight) {
    highlight.removeAttribute("id");
  }
}


// Async Search - Key pressed
function async_search_key_press(event) {

  // KEY UP
  if (event.keyCode == 40) {
    highlight = document.getElementById("LSHighlight");
    if (!highlight) {
      highlight = document.getElementById("LSRes").firstChild;
    } else {
      highlight.removeAttribute("id");
      highlight = highlight.nextSibling;
    }
    if (highlight) {
      highlight.setAttribute("id","LSHighlight");
    } 
  }
  // KEY DOWN
  else if (event.keyCode == 38) {
    highlight = document.getElementById("LSHighlight");
    if (!highlight) {
      highlight = document.getElementById("LSRes").lastChild;
    } else {
      highlight.removeAttribute("id");
      highlight = highlight.previousSibling;
    }
    if (highlight) {
      highlight.setAttribute("id","LSHighlight");
    }
  }
  // ENTER
  else if (event.keyCode == 13) {
	  highlight = document.getElementById("LSHighlight");
	  document.forms.searchform.city.value = highlight.firstChild.innerHTML;
	  async_search_hide();
    ajax_destination();
    ajax_featured_hotels();
  }
  // AJAX REQUEST
  else {
    ajax_destination();
    ajax_featured_hotels();
    document.getElementById("LSResult").style.display = "block";
  }
}


// Async Search - Clicked
function async_search_clicked(el) {
  highlight = document.getElementById("LSHighlight");
  if (highlight) {
    highlight.removeAttribute("id");
  }
  el.setAttribute("id", "LSHighlight");
  document.forms.searchform.city.value = el.firstChild.innerHTML;
  ajax_destination();
  ajax_featured_hotels();
}


// Async Search - Hover
function async_search_hover(el) {
  highlight = document.getElementById("LSHighlight");
  if (highlight) {
    highlight.removeAttribute("id");
  }
  el.setAttribute("id", "LSHighlight");
}


// Async Search - Focus
function async_search_focus(event) {
  ajax_destination();
  document.getElementById("LSResult").style.display = "block";
}


var current_search;

// Ajax - Destinations
function ajax_destination(){
     var url = ROOT_HTTP + 'ajax_destination.php';
     var pars = 'destination='+escape($F('livesearch'));
     current_search = $F('livesearch');
     var myAjax = new Ajax.Request(url, {method: 'get', parameters: pars, onComplete: ajax_destination_complete});
}


// Ajax - Destinations Complete
function ajax_destination_complete(transport) {
  search = transport.request.parameters.destination;
  if (search == current_search) {
    var res = $('LSRes');
	  res.update(transport.responseText);
	  if (res && res.childNodes.length == 0) {
	    $('LSResult').hide();
	  } else {
	    $('LSResult').show();
	  }
	}
}

// Ajax - Featured Hotels
function ajax_featured_hotels(){
  var url = ROOT_HTTP + 'ajax_featured_hotels.php';
  var pars = 'destination='+escape($F('livesearch'));
  var myAjax = new Ajax.Request(url, {method: 'get', parameters: pars, onComplete: ajax_featured_hotels_complete});
}


// Ajax - Featured Hotels Complete
function ajax_featured_hotels_complete(transport) {

  search = transport.request.parameters.destination;
  if (search == current_search) {
	  $('featured-hotels').update(transport.responseText);
	  if ($('featured-hotels').childElements().length != 0) {
	    $('top-destination').hide();
	    $('top-feature_title').update("<div class='process-title-active'>Featured Hotels</div><div class='process-title-arrow'></div><div class='process-title-active'>" + $F('livesearch')) + "</div>";
	  } else {
	    $('top-destination').show();
	    $('top-feature_title').update("Top Destinations");
	  }
	}
}