/*!
 * http://www.kryogenix.org/code/browser/searchhi/ 
 * Modified 20021006 to fix query string parsing and add case insensitivity 
 * Modified 20070316 to stop highlighting inside nosearchhi nodes 
 * Modified 20090104 by Bruce Lawson to use html5 mark element rather than span 
     http://www.whatwg.org/specs/web-apps/current-work/multipage/text-level-semantics.html#the-mark-element 
 * Modified 20090105 by Pete Boere to clean-up namespace, add hostname referrer check for unconventional 
     query parameters and change behaviour to highlight the full search phrase and whole word matches ( not word sub-strings )
 * This script is by Stuart Langridge licensed under MIT license http://www.kryogenix.org/code/browser/licence.html
*/

(function () {

// Shortcuts
var win = window,
	doc = document,
	loc = win.location,
	
	// Helper for iterating arrays, nodelists and objects
	each = function ( obj, callback ) {
		if ( obj+'' === '[object Object]' ) {
			for ( var key in obj ) { 
				callback.call( obj, key, obj[ key ] ); 
			}
		}
		else {
			for ( var i = 0; i < obj.length; i++ ) { 
				callback.call( obj, obj[ i ], i ); 
			}
		}
	},
	
	// Cache word match regexs for performance gain
	reCache = {},
	
	highlightWord = function ( node, word ) {
		// Iterate into this nodes childNodes
		if ( node.hasChildNodes ) {
			each( node.childNodes, function ( cn ) {
				highlightWord( cn, word );
			});
		}
		// And do this node itself if it's text and it hasn't already been highlighted
		if ( node.nodeType == 3 && node.parentNode.className != 'searchword' ) { 
			
			var tempNodeVal = node.nodeValue.toLowerCase(),
				tempWordVal = word.toLowerCase(),
				// Setup regex outline
				wordPatt = '\\b' + tempWordVal + '[A-Za-z1-9]*\\b',
				// Get regex from cache or create it ( and cache for re-use )
				wordPatt = reCache[ wordPatt ] = ( reCache[ wordPatt ] || new RegExp( wordPatt ) ),
				
				match = wordPatt.exec( tempNodeVal );
				
			if ( match ) {
	//			actualWord = '\\b' + tempWordVal + '\\b';
				var pn = node.parentNode;
				var nv = node.nodeValue,
					ni = match.index,
					// Create a load of replacement nodes
					before = doc.createTextNode( nv.substr( 0, ni ) ),
					docWordVal = nv.substr( ni, match[0].length ),
					after = doc.createTextNode( nv.substr( ni + match[0].length ) ),
					hiwordtext = doc.createTextNode( docWordVal ),
					hiword = doc.createElement( 'mark' );
				hiword.className = 'searchword';
				hiword.appendChild( hiwordtext );
				pn.insertBefore( before, node );
				pn.insertBefore( hiword, node );
				pn.insertBefore( after, node );
				pn.removeChild( node );
			}
		}
	},
	
	searchTermHighlight = function () {
		searchTerm = getParameter( "searchVal" );
	    if (searchTerm != "") {
			highlightArea = document.getElementById( "sponsoredLinksTable" );
			if( highlightArea ) {
			    var haelements = highlightArea.getElementsByTagName('table');
			    if (haelements) {
				    for (i=0;i<haelements.length;i++) {
						highlightWord( haelements[i], searchTerm );
					}
				}
			}
		}
	};

if ( 'jQuery' in win ) {
	jQuery( doc ).ready( searchTermHighlight ); 
}
else if ( 'addEventListener' in win ) {
	win.addEventListener( 'load', searchTermHighlight, false );
}
else if ( 'attachEvent' in win ) {
	win.attachEvent( 'onload', searchTermHighlight );
}
else {
alert( "search" );
	addLoadEvent(searchTermHighlight());
}

function getParameter( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( unescape(window.location.href ));

  if( results == null )
    return "";
  else
    return results[1];
}
})();
