/* ************************************************** */
// toggles the display of inline comments
function togglecomments (postid) { 

   var whichpost = document.getElementById(postid); 
   
   if (whichpost.className=="commentshown") { 
      whichpost.className="commenthidden"; 
   } 
   else { 
      whichpost.className="commentshown"; 
   } 
} 

// set all elements with class classname to vis
function toggleClassVisibility(classname, vis){
   var inc=0;
   var alltags=document.all? document.all : document.getElementsByTagName("*");
   for (i=0; i<alltags.length; i++){
      if (alltags[i].className==classname) {
         alltags[i].style.display = vis
      }
   }
}

function toggleMoreLinks() {		
   var more_links = document.getElementById("more_links");
   if (more_links.childNodes[0].nodeValue == "more...") {
      toggleClassVisibility('vis_inFrequent','inline');
      more_links.childNodes[0].nodeValue = "less";
   } else {
      toggleClassVisibility('vis_inFrequent','none');
      more_links.childNodes[0].nodeValue = "more...";
   }
}

/* ************************************************** */
/*
* blockquotes.js
*
* Simon Willison, 20th December 2002
*
* Explanation: 
*   http://simon.incutio.com/archive/2002/12/20/#blockquoteCitations
* Inspired by Adrian Holovaty: 
*   http://www.holovaty.com/blog/archive/2002/12/20/0454
* Alternative implementation of the same idea by Paul Hammond: 
*   http://www.paranoidfish.org/boxes/2002/12/20/
*/

function extractBlockquoteCitations() {
  quotes = document.getElementsByTagName('blockquote');
  for (i = 0; i < quotes.length; i++) {
    cite = quotes[i].getAttribute('cite');
    if ( !(cite == null) && (cite != '') ) {
      newlink = document.createElement('a');
      newlink.setAttribute('href', cite);
      newlink.setAttribute('title', cite);
      newlink.appendChild(document.createTextNode('Source'));
      newdiv = document.createElement('div');
      newdiv.className = 'blockquotesource';
      newdiv.appendChild(newlink);
      quotes[i].appendChild(newdiv);
    }
  }
}

/* ************************************************** */
/* Scott Andrew */
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
   obj.addEventListener(evType, fn, true);
   return true;
 } else if (obj.attachEvent){
   var r = obj.attachEvent("on"+evType, fn);
   return r;
 } else {
   return false;
 }
}


/* ************************************************** */
/* 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] == 'p') { // q= for Google, p= for Yahoo
			words = unescape(qsip[1].replace(/\+/g,' ')).split(/\s+/);
			for (w=0;w<words.length;w++) {
				highlightWord(document.getElementsByTagName("body")[0],words[w]);
            }
	        }
	}
}


/* ************************************************** */
/* fix png24 alpha transparency ie rendering sleight.js
http://youngpup.net/2001/sleight
*/
var IEPlatform = (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent);
if  (IEPlatform) {
	document.writeln('<style type="text/css">img, input.image { visibility:hidden; } </style>');
}

function fnLoadPngs() {
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);

	for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
		if (itsAllGood && img.src.match(/\.png$/i) != null) {
			fnFixPng(img);
			img.attachEvent("onpropertychange", fnPropertyChanged);
		}
		img.style.visibility = "visible";
	}

	var nl = document.getElementsByTagName("INPUT");
	for (var i = nl.length - 1, e = null; (e = nl[i]); i--) {
		if (e.className && e.className.match(/\bimage\b/i) != null) {
			if (e.src.match(/\.png$/i) != null) {
				fnFixPng(e);
				e.attachEvent("onpropertychange", fnPropertyChanged);
			}
			e.style.visibility = "visible";
		}
	}
}

function fnPropertyChanged() {
	if (window.event.propertyName == "src") {
		var el = window.event.srcElement;
		if (!el.src.match(/x\.gif$/i)) {
			el.filters.item(0).src = el.src;
			el.src = "x.gif";
		}
	}
}

function dbg(o) {
	var s = "";
	var i = 0;
	for (var p in o) {
		s += p + ": " + o[p] + "\n";
		if (++i % 10 == 0) {
			alert(s);
			s = "";
		}
	}
	alert(s);
}

function fnFixPng(img) {
	var src = img.src;
	img.style.width = img.width + "px";
	img.style.height = img.height + "px";
	img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')"
	img.src = "x.gif";
}




/* ************************************************** */
/* Pendantism at it's finest
 * hack for blogger

 * Fixes the pluralization of "comment" and "comments"; if there is more 0 or more than one comment, leave the text default as "comments". Otherwise, replace the text with "comment"

find all the span tags with the class "comment_plural" and 
check the "count" attribute. if it's == "1" then change the text

*/
function fixComment() {
  var alltags = document.all ? document.all : document.getElementsByTagName("span");
  for (i = 0; i < alltags.length; i++) {
    if (alltags[i].className == "comment_plural") {
      var attr = alltags[i].getAttribute("count");
      if (attr != "1") {
        alltags[i].appendChild(document.createTextNode('s'));
      }
    }
  }
}

/* ************************************************** */
/* onLoad execute */
function start() {
  fixComment();
  extractBlockquoteCitations();
  googleSearchHighlight();
  if (IEPlatform){
	fnLoadPngs();
  }
  
}
window.onload = start;
