
function Marker()
{	

this.arrWords = Array();
this.arrTextNode = Array();	
this.firstHighlightedWordOffsetTop = Number.MAX_VALUE;
this.arr_bgcolor = Array( "#B90275", "#4C1F66", "#0378BA", "#2559A3", "#D74C23");   

function CollectSearchWords()
{
   var str_referrer = '';	
   if(document)
   {	
      if(document.referrer)
      {	
         var str_referrer = document.referrer;
      }	
   }	
	
//   str_referrer = 'http://www.google.de/search?sourceid=navclient&hl=de&ie=UTF-8&rls=GGLD,GGLD:2005-03,GGLD:de&q=Sound+cd+soundkarte+1998 - Server+Software++dVD+++monopol+++BiOs++++site%3Anickles%2Ede+++++h';
	
   if( ( str_referrer != '') && ( str_referrer.indexOf("www.google.") >= 0))
   {
      var arr_questionmark = str_referrer.split('?');

      if( arr_questionmark.length != 2)
         return '';

      var arr_ampersand = arr_questionmark[1].split('&');
   
      for( var i = 0; i < arr_ampersand.length; i++)
      {
         var arr_equalsign = arr_ampersand[i].split('=');
	
         if( ( arr_equalsign[0] == 'q') || ( arr_equalsign[0] == 'as_q') || 
	     ( arr_equalsign[0] == 'as_epq') || ( arr_equalsign[0] == 'as_oq'))
         {
	    while( arr_equalsign[1].indexOf('++') >= 0)
	       arr_equalsign[1] = arr_equalsign[1].replace(/\+\+/g, '+');

	    var len = arr_equalsign[1].length;
	    if( arr_equalsign[1].lastIndexOf('+') == ( len - 1))
	       arr_equalsign[1] = arr_equalsign[1].substr( 0, len - 1);

	    var arr_tmp =  arr_equalsign[1].split('+');
	
	    for( var u = 0; u < arr_tmp.length; u++)
	    {	
	       arr_tmp[u] =  decodeURI( arr_tmp[u]);

	       if( arr_tmp[u] != '' && arr_tmp[u].length >= 2 && ( arr_tmp[u].indexOf('site%3A') < 0))
	       {
	          this.arrWords[this.arrWords.length] = arr_tmp[u].toLowerCase();
	       }
	    }
         }
      }
   }
}

function HighLightPage()
{
   try
   {  
      this.CollectSearchWords();
      this.arrWords.sort( StrLengthSort);
      
      this.CollectTextNodes( document.body);
      
      for( var g = 0; g < this.arrWords.length; g++)
      {		
         for( var n = 0; n < this.arrTextNode.length; n++)
	 {
	    this.HighLightText( g, n);
	 }
	
         if( g == 3)
	    break;	
      }	
      
      this.ScrollToFirstWord();
   }
   catch(e)
   {
      // do nothing
   }
}

function StrLengthSort( a, b)
{
   return b.length - a.length;
}
   
   
function GetFontObject(i)
{
   var oFont = document.createElement("font");
   oFont.style.background = this.arr_bgcolor[(i % this.arr_bgcolor.length)];
   oFont.color = '#FFFFFF';
   return oFont;
}

function CollectTextNodes( oNode)
{  
   // tree geht noch weiter
   if( oNode.hasChildNodes())
   {
      for( var e = 0; e < oNode.childNodes.length; e++)
      {	
	this.CollectTextNodes( oNode.childNodes[e]);
      }	
   }
   // if textNode
   else if( oNode.nodeType == 3)
   {
      this.arrTextNode[ this.arrTextNode.length] = Array( oNode, oNode.data.toLowerCase());	 
   }
}

	
function HighLightText( iSearchWord, nTextNode)
{  
   var oTextNode = null;
   var word = this.arrWords[iSearchWord];
   var oNode = this.arrTextNode[nTextNode][0];

   // this.arrTextNode[nTextNode][1] == data of textnode to lowercase
   var word_start = this.arrTextNode[nTextNode][1].indexOf( word);
   
   if( word_start == -1) return;
      
   if( word_start == 0)
   {
      /* 123456789 */
      oTextNode = oNode;
	     
      /* 123-456789 */
      if( word.length < oTextNode.data.length)
      {
         var rest = oTextNode.splitText(word.length);
         this.arrTextNode[ this.arrTextNode.length] = Array( rest, rest.data.toLowerCase());
      }	

      this.arrTextNode[nTextNode][1] = '';
      this.arrTextNode[nTextNode][0] = null;
      
      var oFont = this.GetFontObject(iSearchWord);
      oEle = oNode.parentNode.insertBefore( oFont, oTextNode);
      oFont.appendChild( oTextNode);
	
      fontOffsetTop = parseInt( oFont.offsetTop, 10);
   
      if( fontOffsetTop > 100)
      {	
	 this.firstHighlightedWordOffsetTop = Math.min( this.firstHighlightedWordOffsetTop, fontOffsetTop);
      }	
   }
   /* 123456-789 || 123-456-789 */
   else
   {
      var rest = oNode.splitText(word_start);
      this.arrTextNode[ this.arrTextNode.length] = Array( rest, rest.data.toLowerCase());
      
      this.arrTextNode[nTextNode][1] = oNode.data.toLowerCase();
      this.arrTextNode[nTextNode][0] = oNode;
   }
}
   
function ScrollToFirstWord()
{
   if(  this.firstHighlightedWordOffsetTop > 0)
   {
     window.scrollTo( 0, this.firstHighlightedWordOffsetTop);
   }
}

function getSearchWords()
{
   try
   {  
      this.CollectSearchWords();
      
      var str = '';
      for( var g = 0; g < this.arrWords.length; g++)
      {		
	 str += this.arrWords[g] + '+';
      }	
      
      return str;
   }
   catch(e)
   {
      // do nothing
   }
}   
   
this.CollectTextNodes = CollectTextNodes;   
this.GetFontObject = GetFontObject;	
this.HighLightText = HighLightText;	
this.HighLightPage = HighLightPage;
this.CollectSearchWords = CollectSearchWords;
this.ScrollToFirstWord = ScrollToFirstWord;	
this.getSearchWords = getSearchWords;
}
