window.addEvent('domready', function() {
    var HashCookie = new Hash.Cookie('highlightHash', {duration: 0});
    /**
      * highlightText
      * Extends all Matches with a span with style markierung
      * @param object node Defines the Area(Div) to look
      * @param string te Searchstring
      * @return mixed
      */
    highlightText = function(node, te) {
        var status = statusHighlight();
        if (status == false) {
            if ($('searchHighlight')) $('searchHighlight').innerHTML = '<a href="javascript:switchHighlight();">Suchwort-Markierung <strong>anzeigen</strong></a>';
            return false;
        } else {
        	if (te.indexOf(' ') > -1) {
        		var teArr = te.split(' ');
        		for(i=0; i < teArr.length; i++) {
        			highlightText(node, teArr[i]);
        		}
        		return false;
        	}
            var pos, skip, spannode, middlebit, endbit, middleclone;
            skip = 0;
            if (node.nodeType == 3) {
                pos = node.data.toUpperCase().indexOf(te);
                if (pos >= 0) {
                    if (Browser.Engine.trident) {
                        if ( typeof(node.parentNode) == 'object' ) {
                            if (node.parentNode.className == 'inhaltIndex' || node.parentNode.className == 'title_detail') return false;
                            
                            if (node.parentNode.className == 'markierung-del') {
                                node.parentNode.removeClass('markierung-del');
                                node.parentNode.addClass('markierung');
                                return true;
                            }
                        }
                    } else {
                        if (node.parentNode.hasClass('inhaltIndex') || node.parentNode.hasClass('title_detail')) return false;
                        
                        if (node.parentNode.hasClass('markierung-del')) {
                          node.parentNode.removeClass('markierung-del');
                          node.parentNode.addClass('markierung');
                          return true;
                        }
                        
                    }
                    
                     spannode = document.createElement('span');
                     spannode.className = 'markierung';
                     middlebit = node.splitText(pos);
                     endbit = middlebit.splitText(te.length);
                     middleclone = middlebit.cloneNode(true);
                     spannode.appendChild(middleclone);
                     middlebit.parentNode.replaceChild(spannode, middlebit);
                     skip = 1;
                }
            } else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
                for (var i = 0; i < node.childNodes.length; ++i) {
                    i += highlightText(node.childNodes[i], te);
                }
            }
            return skip;
        }
    }
     
     /**
      * removeHighlight
      * Removes the highlighting immediately and save the setting into a cookie
      * @return bool
      */
     removeHighlight = function() {
        HashCookie.extend({'remove': 1});
        $$('.markierung').each(function(elem) { 
          elem.removeClass('markierung');
          elem.addClass('markierung-del');
        });
        return true;
     }
     
     /**
      * clearHighlight
      * Clears the Cookie-Setting of Highlighting
      * @return bool
      */
     addHighlight = function() {
        HashCookie.empty();
        
        if ($('squery')) {
		if ($('squery').value.length > 0) {
			if ($('suche_ergebnisse')) {
				highlightText($('suche_ergebnisse'), $('squery').value.toUpperCase());
			} else if($('suche_ergebnisse_detail')) {
				highlightText($('suche_ergebnisse_detail'), $('squery').value.toUpperCase());
			}
		}
	}
        
        return true;
     }
     
     switchHighlight = function() {
       var status = statusHighlight();
       if (status == false) {
         addHighlight();
         if ($('searchHighlight')) $('searchHighlight').innerHTML = '<a href="javascript:switchHighlight();">Suchwort-Markierung <strong>ausschalten</strong></a>';
       } else {
         removeHighlight();
         if ($('searchHighlight')) $('searchHighlight').innerHTML = '<a href="javascript:switchHighlight();">Suchwort-Markierung <strong>anzeigen</strong></a>';
       }
     }
     
     /**
      * statusHighlight
      * Returns weather the Texthighlighting is active or not
      * @return bool
      */
     statusHighlight = function() {
        if (HashCookie.get('remove') == 1) return false;
        else return true;
     }
     
});
 

