/*****************************************************************************\
* Miscellaneous common functions                                              *
* Copyright (c) 2007, 2009 Joerg Tremmel                                      *
 ******************************************************************************
* 09.01.2007  J.Tremmel  initial
* 17.07.2009  J.Tremmel  added tweakMenuSearchField
\*****************************************************************************/

/**
* Safely adds a load event to the window.onload handler
* See: http://simon.incutio.com/archive/2004/05/26/addLoadEvent
*/
function addLoadEvent( func ) { //{{{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() { oldonload(); func(); }
  }
  //}}}
}

addLoadEvent(tweakMenuSearchField);
//addLoadEvent(retargetExternalLinks);

/** Trims leading/trailing whitespace from a string **/
function trim(text) {
  return text.replace(/^\s+/, '').replace(/\s+$/, '');
}

/** Shows a default text in the non-focused, empty search field */
function tweakMenuSearchField() { //{{{
  var inputField = document.getElementById('menuSearchField');
  if (!inputField) { return; }
  if (!trim(inputField.value)) { inputField.value = 'Suche...'; }
  inputField.onfocus = function() {
    if (trim(this.value) == 'Suche...') { this.value = ''; }
  }
  inputField.onblur = function() {
    if (!trim(this.value)) { this.value = 'Suche...'; }
  }
  //}}}
}

/** Retargets "external" links to open in a new window/tab */
function retargetExternalLinks() { //{{{
  links = document.links;
  host  = location.hostname.toLowerCase();
  host.split('.').slice(-2).join('.'); // use only server + single tld
  for (var i = 0; i < links.length; i++) {
    if (links[i].hostname) {
      if (links[i].hostname.substr(-host.length).toLowerCase() != host) {
        links[i].target = "_blank";
      }
    }
  }
  //}}}
}



