function addEvent(elm, evType, fn, useCapture) {
  // cross-browser event handling for IE5+, NS6 and Mozilla 
  // By Scott Andrew 
  if (elm.addEventListener) { 
    elm.addEventListener(evType, fn, useCapture); 
    return true; 
  } else if (elm.attachEvent) { 
    var r = elm.attachEvent('on' + evType, fn); 
    return r; 
  } else {
    elm['on' + evType] = fn;
  }
}

var DEF_VAL   = "3615 JeCherche"; // Default Value
var isSafari  = ((parseInt(navigator.productSub)>=20020000)&&     // detecting WebCore
               (navigator.vendor.indexOf("Apple Computer")!=-1));

function showResult() {
	searchResult = document.getElementById('searchResult');
	searchField = document.getElementById('searchField');
	if(searchField.value != "") {
		imgSpin = document.getElementById('SearchSpin');
		imgSpin.style.display = 'block';
		var url = '/search.swe';
		var pars = 'search='+searchField.value;
		var target = 'searchResult';
		var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars, onComplete: ScrollDown});
		searchResult.style.display = 'none';
	} else {
		new Effect.BlindUp(searchResult);
	}
}

function ScrollDown() {
	imgSpin = document.getElementById('SearchSpin');
	new Effect.BlindDown(document.getElementById('searchResult'));
	imgSpin.style.display = 'none';
}

function replaceSearchField() {
  // Replaces normal input text field with safari's neat search field
  if (!document.getElementById)
    return;
  var searchField = document.getElementById('searchField');
  if (isSafari) {
    // changing type to "search"
    searchField.setAttribute('type', 'search');
    searchField.setAttribute('autosave', 'ch.devrandom.search');
    searchField.setAttribute('results', '5');
    searchField.setAttribute('placeholder', DEF_VAL);
    addEvent(searchField, 'search', showResult, false);
  } else {
    // doing the "Search this Site..."-Displaying- & -Hiding-Stuff
    searchField.setAttribute('type', 'text');
    addEvent(searchField, 'focus', focusSearch, false);
    addEvent(searchField, 'blur',  blurSearch, false);
    addEvent(searchField, 'keypress', showResult, false);
    if (searchField.value=='') searchField.value = DEF_VAL;
  }
  
}

function focusSearch() {
  // removes the initial value from the search field
  if (this.value==DEF_VAL) {
    this.value = '';
    this.setAttribute('class', 'focus');
  }
}

function blurSearch() {
  // assigns default value, if no value has been entered
  if (this.value=='') {
    this.value = DEF_VAL;
    this.removeAttribute('class', 'focus');
  }
}

addEvent(window, 'load', replaceSearchField, false);

