var cBadjieHotelsearch = {
	iLastKBHit:		999,
	sSearchstring:	'',
	init: function() {
		$(document).ready(function(){
			log($('#search-input'));
			$('#search-input').attr('autocomplete','off');
		});
		// set trigger to check every 100ms
		window.setInterval("cBadjieHotelsearch.triggerAction();", 100);
	},
	triggerAction: function(){
		// if last kbhit is more than 300ms ago AND the content has changed => request new searchresults
		if(this.iLastKBHit==400) {
			// fire ajax request to get data
			this.getBestResults();
		}
		this.iLastKBHit+=100;
	},
	
	onKeyDown: function() {
		// reset time since last keydown
		this.iLastKBHit=0;
	},
	
	onFocus: function() {
		// show searchresult div
		this.getBestResults();
	},
	onBluer: function() {
		// hide searchresult div
		window.setTimeout("_('searchresult').style.display='none'", 400);
	},
	getBestResults: function() {
		// request server via ajax
		this.sSearchstring = _('search-input').value;
		var oCallback = {
			success:	this.getBestResultsRecall,
			failure:	this.getBestResults,
			scope:		this,
			argument:		this.sSearchstring
		};
		yuiAjaxRequest("cBadjieHotelSearch", "ajaxGetBestHits", {sSearchstring: this.sSearchstring}, oCallback);
		_('searchresult').innerHTML = 'Lade Suchergebnis ...';
		_('searchresult').style.display='block';
	},
	getBestResultsRecall: function(o) {
		// set innerHTML of div id=searchresult
		if(this.sSearchstring != o.argument) return;
		var oData = parseAjaxResponse(o);
		var sHtml ="";
		if(oData.hotels.count>0) {
			sHtml += "<strong><a href='/german/suche.html?sSearchstring="+encodeURIComponent(this.sSearchstring)+"&sArea=hotels'>"+oData.hotels.count + " Hotels gefunden (anzeigen...)</a></strong>";
			for(i in oData.hotels.results) {
				sHtml += "&nbsp;&nbsp;<a href='"+oData.hotels.results[i].url+"'>"+oData.hotels.results[i].title+"</a><br />";
			}
		}
		if(oData.rundreisen.count>0) {
			sHtml += "<strong><a href='/german/suche.html?sSearchstring="+encodeURIComponent(this.sSearchstring)+"&sArea=rundreisen'>"+oData.rundreisen.count + " Rundreisen gefunden (anzeigen...)</a></strong>";
			for(i in oData.rundreisen.results) {
				sHtml += "&nbsp;&nbsp;<a href='"+oData.rundreisen.results[i].url+"'>"+oData.rundreisen.results[i].title+"</a><br />";
			}
		}
		if(oData.exkursionen.count>0) {
			sHtml += "<strong><a href='/german/suche.html?sSearchstring="+encodeURIComponent(this.sSearchstring)+"&sArea=exkursionen'>"+oData.exkursionen.count + " Exkursionen gefunden (anzeigen...)</a></strong>";
			for(i in oData.exkursionen.results) {
				sHtml += "&nbsp;&nbsp;<a href='"+oData.exkursionen.results[i].url+"'>"+oData.exkursionen.results[i].title+"</a><br />";
			}
		}
		if(oData.hotels.count>0) {
			sHtml += "<strong><a href='/german/suche.html?sSearchstring="+encodeURIComponent(this.sSearchstring)+"&sArea=landundleute'>"+oData.landundleute.count + " Land &amp; Leute-Seiten gefunden (anzeigen...)</a></strong>";
			for(i in oData.landundleute.results) {
				sHtml += "&nbsp;&nbsp;<a href='"+oData.landundleute.results[i].url+"'>"+oData.landundleute.results[i].title+"</a><br />";
			}
		}
		if(sHtml == '') {
			_('searchresult').style.display='none';
		}
		else {
			_('searchresult').style.display='block';
			sHtml += "<strong><a href='/german/suche.html?sSearchstring="+encodeURIComponent(this.sSearchstring)+"&sArea='>Alle Ergebnisse anzeigen...</a></strong>";
		}
		_('searchresult').innerHTML = sHtml;
	}
}

cBadjieHotelsearch.init();





