
window.onload = postProcessPage;

function postProcessPage() {
  clearCurrentLink();
  googleSearchHighlight();
  if(document.getElementById) { TJKpop(); }
}

/*
CLCP v2.1 Clear Links to Current Page
Jonathan Snook
This code is offered unto the public domain
http://www.snook.ca/jonathan/
*/


function clearCurrentLink(){
  var a = document.getElementsByTagName("a");
  for(var i=0;i<a.length;i++) {
    if(a[i].href == window.location.href.split("#")[0]) {
      removeNode(a[i]);
    }
  }
}

function removeNode(n){
  if(n.hasChildNodes()) {
    for(var i=0;i<n.childNodes.length;i++) {
      n.parentNode.insertBefore(n.childNodes[i].cloneNode(true),n);
      n.parentNode.setAttribute('class', 'current_highlight');
    }
  }
  n.parentNode.removeChild(n);
}



/* http://www.kryogenix.org/code/browser/searchhi/ */
/* Modified 20021006 to fix query string parsing and add case insensitivity */
function highlightWord(node,word) {
	// Iterate into this nodes childNodes
	if (node.hasChildNodes) {
		var hi_cn;
		for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
			highlightWord(node.childNodes[hi_cn],word);
		}
	}
	
	// And do this node itself
	if (node.nodeType == 3) { // text node
		tempNodeVal = node.nodeValue.toLowerCase();
		tempWordVal = word.toLowerCase();
		if (tempNodeVal.indexOf(tempWordVal) != -1) {
			pn = node.parentNode;
			if (pn.className != "searchword") {
				// word has not already been highlighted!
				nv = node.nodeValue;
				ni = tempNodeVal.indexOf(tempWordVal);
				// Create a load of replacement nodes
				before = document.createTextNode(nv.substr(0,ni));
				docWordVal = nv.substr(ni,word.length);
				after = document.createTextNode(nv.substr(ni+word.length));
				hiwordtext = document.createTextNode(docWordVal);
				hiword = document.createElement("span");
				hiword.className = "searchword";
				hiword.appendChild(hiwordtext);
				pn.insertBefore(before,node);
				pn.insertBefore(hiword,node);
				pn.insertBefore(after,node);
				pn.removeChild(node);
			}
		}
	}
}

function googleSearchHighlight() {
	if (!document.createElement) return;
	ref = document.referrer;
	if (ref.indexOf('?') == -1) return;
	qs = ref.substr(ref.indexOf('?')+1);
	qsa = qs.split('&');
	for (i=0;i<qsa.length;i++) {
		qsip = qsa[i].split('=');
	        if (qsip.length == 1) continue;
        	if (qsip[0] == 'q' || qsip[0] == 's') { // q= for Google, p= for Yahoo, s= for wordpress
			words = unescape(qsip[1].replace(/\+/g,' ')).split(/\s+/);
	                for (w=0;w<words.length;w++) {
				highlightWord(document.getElementsByTagName("body")[0],words[w]);
                	}
	        }
	}
}


function TJKpop(){ 
  // v1.0 | www.TJKDesign.com
  // taken from http://www.tjkdesign.com/articles/popups.asp
  // added document.domain to automatically detect the current domain name
  // domain name check uses toLowerCase instead of toUpperCase
  // changed 'wrapper' to 'page' 
  // explicitly sets target to null
  var e = document.getElementById('page');
  if (e){
    var a=e.getElementsByTagName('a');
    for (var i=0;i<a.length;i++){

    // tinyMCE automatically puts target="_self" on every link by default, so these extra checks won't work
    // they probably never really added much anyway
    //if (a[i].getAttribute('href') != null && a[i].getAttribute('href').indexOf("://") >= 0 && a[i].getAttribute('href').toLowerCase().indexOf(document.domain.toLowerCase()) == -1 && a[i].getAttribute('target') == null && (a[i].getAttribute('onclick') == null || a[i].getAttribute('onclick').indexof('window.open') == -1)){
    
    if (a[i].getAttribute('href') != null && a[i].getAttribute('href').indexOf("://") >= 0 && a[i].getAttribute('href').toLowerCase().indexOf(document.domain.toLowerCase()) == -1) {
        a[i].className+=a[i].className?' outlink':'outlink';
        a[i].title+=' (opens in new window)';
        a[i].target = null;
        a[i].onclick=function(){newWin=window.open(this.href,'TJKWin');if(window.focus){newWin.focus()} return false;}
//      a[i].onkeypress=function(){newWin=window.open(this.href,'TJKWin');if(window.focus){newWin.focus()} return false;}
      }
    }
  }
}

// =====================================================
// SEARCH FORM
// =====================================================

function prepare_search_form(selector, default_string, search_term) {
  var default_term = 'search...'
  if (default_string && default_string != '') {
    default_term = default_string;
  }
  
  $(selector).unbind();
  if (search_term != '') {
    $(selector).val(search_term);
  } else {
    $(selector).val(default_term);
  }

  $(selector).focus(
    function() {
     if ($(selector).val() == default_term) {
       $(selector).val('');
     }
    });
  $(selector).blur(
    function() {
     if ($(selector).val() == '') {
       $(selector).val(default_term);
     }
    });
}
//window.onload = function(){if(document.getElementById) TJKpop();}
//window.onload = TJKpop();
