var throphost = window.location.host;
var hotelmatch = /hotels/;
var restaurantmatch = /restaurants/;
var bSafari = navigator.userAgent.indexOf("AppleWebKit/") != -1 && navigator.userAgent.indexOf("Safari/") != -1;
var propType;

if(hotelmatch.test(window.location))
    propType = "Hotel";

if(restaurantmatch.test(window.location))
    propType = "Restaurant";

if(typeof bFixedForSafari == "undefined")
    bFixedForSafari = false;
    
if (bSafari && !bFixedForSafari)
{
    // for safari browser use prototype's ajax updater if Scriptaculous loaded older than v1.8.1
    $('locale').onkeyup = searchUpdater;
} 
else 
{
    // for all other browsers use scriptaculous ajax autocompleter
    searchAutoCompleter();
}

function searchUpdater()
{
    $('locale_auto_complete').setStyle({ display: 'block', position: 'absolute', zIndex: 10, width: '200px' });

    new Ajax.Updater('locale_auto_complete', 'http://' + throphost + '/throp/livesearch.cfm?propertytype='+propType, {
        method: "post",
        parameters: 'bCloseIcon=true&locale='+ $('locale').value,
        onFailure: function(){
            //alert("Technical problems please try reloading page");
        }
    }); 
}

function searchAutoCompleter()
{
    $('locale_auto_complete').setStyle({ display: 'block', position: 'absolute', zIndex: 10, width: '200px' });
    new Ajax.Autocompleter('locale',
                       'locale_auto_complete', 
                       'http://' + throphost + '/throp/livesearch.cfm?propertytype=' + propType, 
                       {paramName: "locale", updateElement: thropDropDownSearch});
}

function thropDropDownSearch()
{
    list = document.getElementById('locale_auto_complete');
    listelement = list.getElementsByTagName('li');
        
    for (i=0; i<listelement.length; i++){
        if(listelement[i].className == "selected") {
            propLink = listelement[i].getElementsByTagName('a');
            window.location = propLink[0]; 
        }
    }
}

function closePopup()
{
  $('locale_auto_complete').setStyle({ display: 'none' });
}