	String.prototype.ivtrim = function () {
	    return this.replace(/^\s*/, "").replace(/\s*$/, "");
	}
	ivIsShowing = false;	
	searchPanel  =null;
	function updateSearchPanel(){
		searchPanel = document.getElementById("searchWindow");	
		var searchInput = document.getElementById("searchBox");
		var pos = AJS.absolutePosition(searchInput)
/*		searchPanel.style.top = (pos.y  + 5 + searchInput.offsetHeight) +"px";
		searchPanel.style.left = pos.x + "px";// + searchInput.offsetWidth;*/
		searchPanel.style.top = (pos.y  + 5 + searchInput.offsetHeight) +"px";
		var w = searchPanel.offsetWidth;
		if(0 == w){
			w = 300;
		}
//		window.console.log("searchPanel width: "+w);
		searchPanel.style.left = (pos.x + searchInput.offsetWidth - w) + "px";// + searchInput.offsetWidth;
//		searchPanel.style.top = "400px";//  - searchInput.offsetHeight -5;
//		searchPanel.style.left = "400px";// + searchInput.offsetWidth;
		//window.console.log(pos.x + ", "+pos.y);
	}	
	function doSearch(source){
		var search = source.value;
		if(search.length < 3){
			hideSearch();
			return;
		}else{
			showSearch(search);			
		}		
		updateSearch(search);
	}
	var updateCounter = 0;
	function updateView(counter, o){
		if(updateCounter != counter){
			// trying to update previous fetch
			//window.console.log("not updating fetch" + counter + " vs. "+updateCounter);
			return;
		}else{
			//window.console.log("updating " +counter);
		}
		var content = "<ul class=\"search\">" ;
		var response = o.response.docs;
		for(var i in response){
			var doc = response[i];
			var title = doc.title[0];
			if("" == title){
				title = doc.url.match(/.*\/(.*?)$/)[1];
			}			
			title = title.ivtrim();
			content += "<li class=\"search\">";
			content += "<a href=\""+doc.url+"\">"+title+"</a>";
			if(o.highlighting[doc.url]){
				content += "<br /> ... "+ o.highlighting[doc.url].content[0] + "...";
			}
			content += "</li>\n";
		} 
		content += "</ul>";
		var n = o.response.numFound;
		if(n== 0){
			content += "No matches."
		}else if (n == 1){
			content += "1 match."
		}else{
			content += n+" matches.";
		}
		content += "<a href=\"javascript:hideSearch()\"> Close search.</a>";
		searchPanel.innerHTML = content;
	}
	var lastSearch = "";

	function updateSearch(text){
		if(lastSearch == text){
			return;
		}
		lastSearch = text;
		var url = "/search.php?query=" + urlencode(text);
	    var d = AJS.loadJSON(url);
		var counterForUpdate = ++updateCounter;
//		d.addCallback(updateView);
		d.addCallback(function(o) { updateView(counterForUpdate,o)});
		d.addErrback(function(err) { 
			//window.console.log(counterForUpdate + " - "+err); 
			});
		d.sendReq();
	}
	function hideSearch(){
		if(! searchPanel){
			updateSearchPanel();
		}
		searchPanel.style.display = "none";
		ivIsShowing = false;
	}
	function showSearch(){
		if(ivIsShowing) return;
		if(! searchPanel){
			updateSearchPanel();
		}
		searchPanel.style.display = "block";
		ivIsShowing = true;
	}
	function fillResult(results){
		if(ivIsShowing){
			// tehdään <li> -elementtejä hakutuloksista
			// <a href="[URL]">[TITLE]</a><br /><p>description/snippet</p>
		}
	}
	function updatePlaceHolder(sender, inEvent){
		if(sender.value == 'search...' && inEvent == 'focus'){
			sender.value = '';
			sender.style.color = "#000000";
		}else 
		if(sender.value == '' && inEvent == 'blur'){
			sender.value = 'search...';
			sender.style.color = "#888888";
		}
		
	}

