function updateResult(el) {
  var url = prepareLoadResult(el,el.href);
  $('result').load(url);
  return true;
}

if($('searchmask')) {
  if ($('location_button') && ((typeof (google)!="undefined" && typeof (google.gears)!="undefined") || typeof (navigator.geolocation)!="undefined")) {
    function updatePosition(position) {
      var s = $('searchmask_location');
      if (typeof (position.gearsAddress)!="undefined" && position.gearsAddress) {
        adr = position.gearsAddress;
        s.value = '';
        if(typeof adr.street       != 'undefined') s.value = adr.street;
        if(typeof adr.streetNumber != 'undefined') s.value = s.value+' '+adr.streetNumber;
        if(s.value != '') s.value = s.value + ', ';
        if(typeof adr.postalCode   != 'undefined') s.value = s.value+adr.postalCode;
        if(typeof adr.city         != 'undefined') s.value = s.value+' '+adr.city;
      }
      else if (typeof (position.latitude)!="undefined" && position.latitude && position.longitude) {
        s.value = position.latitude +','+ position.longitude+' (Coordinates)';
      }
      else if (position.coords.latitude && position.coords.longitude) {
        s.value = position.coords.latitude +','+ position.coords.longitude+' (Coordinates)';
      }
      checkLocation();
      highlight('searchmask_location');
    }
    
    function noUpdatePosition() {
      $('searchmask_location').value = 'Could not find your position... sorry';
      highlight('searchmask_location');
    }
    
    function locateMe(ev) {
      highlight('searchmask_location');
      $('searchmask_location').value = '...';
      if (typeof (geo)!="undefined" && geo.getPermission()) {
        geo.getCurrentPosition(updatePosition,noUpdatePosition,{
          enableHighAccuracy: true,
          gearsRequestAddress: true,
          gearsLocationProviderUrls: ['http://www.google.com/loc/json']
        });
      }
      else if (typeof (navigator.geolocation)!="undefined") {
        navigator.geolocation.getCurrentPosition(updatePosition,noUpdatePosition);
      }
      Event.stop(ev);
    }
    
    if (typeof (google)!="undefined" && typeof (google.gears)!="undefined") var geo = google.gears.factory.create('beta.geolocation');
    $('location_button').addEvent('click', locateMe).style.display = 'block';
  }
  
  function setLocation(ev) {
    var el = this;
    if (!el.title) el = el.parentNode;
    $('searchmask_location').value = el.title;
    highlight('searchmask_location');
    checkLocation();
    Event.stop(ev);
  }
  if($('map')) {
    $('map').addEvent('click', setLocation).style.cursor = 'pointer';
    $$('#map li').each(function(s){
      s.addEvent('click', setLocation).style.cursor = 'pointer';
    });
  }
  
  // AJAX for searchmask removed because of back-button
  /*
  function submitSearchmask(ev){
    var el = this;
    var url = prepareLoadResult($('result'),el.action);
    var data = Form.serialize($('searchmask'));
    new Ajax.Updater('result', url,  { method: 'post', parameters:data });
    Event.stop(ev);
  }
  if ($('result')) $('searchmask').addEvent('submit', submitSearchmask);
  */

  function checkLocation(ev){
    if ($('searchmask_order').value != 'distance') {
      $('searchmask_order').value = 'distance';
      highlight('searchmask_order');
    }
  }
  if($('searchmask_location')) $('searchmask_location').addEvent('keyup', checkLocation);
  if($('searchmask_perimeter')) $('searchmask_perimeter').addEvent('change', checkLocation);
  
  function checkMinCategory(ev){
    if ($('searchmask_order').value != 'category DESC') {
      $('searchmask_order').value = 'category DESC';
      highlight('searchmask_order');
    }
  }
  if($('searchmask_minCategory')) $('searchmask_minCategory').addEvent('change', checkMinCategory);
  
  function checkMaxPrice(ev){
    if ($('searchmask_order').value != 'price') {
      $('searchmask_order').value = 'price';
      highlight('searchmask_order');
    }
  }
  if($('searchmask_maxPrice')) $('searchmask_maxPrice').addEvent('keyup', checkMaxPrice);
  
  function checkAdultCount(ev) {
    var el = this;
    if (el.value == 1 &  $('searchmask_roomType').value != 'single') {
      $('searchmask_roomType').value = 'single';
      highlight('searchmask_roomType');
    }
  }

  if ($('searchmask_adultCount')) $('searchmask_adultCount').addEvent('change', checkAdultCount);
  
  //var calDate = (location.href.match(/english/)) ? 'Y-m-d' : 'd.m.Y';
  //var datePicker = new Calendar({ searchmask_from: calDate,  searchmask_to: calDate });
  function addCalendarFrom(ev){
    var el = this;
    var cal = new calendar(this);
    cal.init(el.value, el, $('searchmask_to'));
  }
  if($('searchmask_from')) $('searchmask_from').addEvent('click', addCalendarFrom);

  function addCalendarTo(ev){
    var el = this;
    var cal = new calendar(this);
    cal.init(el.value, el, $('searchmask_from'));
  }
  if($('searchmask_to')) $('searchmask_to').addEvent('click', addCalendarTo);
}

if($('add_search') && typeof window.external == "object" && typeof window.external.AddSearchProvider != "undefined") {
  $('add_search').addEvent('click', addSearch).parentNode.style.display = 'block';
}
function addSearch(ev){
  var el = this;
  window.external.AddSearchProvider(el.href);
  Event.stop(ev);
}


function prepareLoadResult(el,url) {
  if (!cp) {
    var cp = $('cp');
  }
  if (cp) {
    var img = cp.getElementsByTagName('img');
    var date = new Date();
    var rnd  = (Math.random()*100000);
    for (var i=0; i<img.length; i++) {
      if (img[i].src.match(/ts=\d+&/)) {
        img[i].src = img[i].src.replace(/ts=\d+&/,'ts='+Math.round(date.getTime()/1000)+'&');
      }
      else if (img[i].src.match(/d=[0-9\.]+$/)) {
        img[i].src = img[i].src.replace(/d=[0-9\.]+$/,'d='+rnd);
      }
    }
  }
  if (el) {
    el.style.position = 'relative';
    if (!ajax_load) {
      var ajax_load = new Element('div', { 'class': 'message', 'html' : 'Loading...' });
    }
    ajax_load.inject($(el),'top');
  }
  return (url.match(/index.xhtml/)) ? url : url.replace(/\?/, 'index.php/index.xhtml?');
}

function highlight(id) {
  $(id).highlight('#ffff88');  
}
