/* 
globals.js
==========
contents:
init onload - calls functions AFTER content has loaded
getElementsByClassName - returns array of elements where classname matches var
zebratables - alternating table row style
fixIEquotes - sorts quotes in <q> tags for ie
*/


/*  ---------------- init onload ------------------------
	Dean Edwards/Matthias Miller/John Resig
*/

function init() {
	// quit if this function has already been called
	if (arguments.callee.done) return;

	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;

	// kill the timer
	if (_timer) clearInterval(_timer);
		//make call to functions
		releaseTheZebras();
		/* for Internet Explorer */
		/*@cc_on @*/
		/*@if (@_win32)
		fixIEQuotes();
		fixIEabbr();
		/*@end @*/
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
			init(); // call the onload handler
		}
	};
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			init(); // call the onload handler
		}
	}, 10);
}

/* for other browsers */
window.onload = init;

/////////////////////////// end /////////////////////////////

/*
	---------------- getElementsByClassName ------------------------
	Written by Jonathan Snook, http://www.snook.ca/jonathan
	Add-ons by Robert Nyman, http://www.robertnyman.com
	
	usage: 	getElementsByClassName("classname", "h3", document); 
*/
function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\s)" + className + "(\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}
/////////////////////////// end /////////////////////////////


/*
	---------------- zebratables ------------------------
	Heavily based on http://www.alistapart.com/articles/zebratables
	usage: 	call the function to stripe all tables with a classname of striped 
*/
function releaseTheZebras(){
	//get ALL elements with a class of 'striped'
	var zebras = getElementsByClassName("striped");
	//alert(zebras.length); //to test
	for (var z = 0; z < zebras.length; z++) {
		//the striping script requires an id
		//so ensure each element has one
		if (!zebras[z].id){zebras[z].id="appliedid"+z;}
		//set the stripes for each match
		stripe(zebras[z].id, 'even', 'odd');
	}
}

  // this function is need to work around 
  // a bug in IE related to element attributes
  function hasClass(obj) {
     var result = false;
     if (obj.getAttributeNode("class") != null) {
         result = obj.getAttributeNode("class").value;
     }
     return result;
  }   

 function stripe(id) {
    // the flag we'll use to keep track of 
    // whether the current row is odd or even
    var even = false;
  
    // if arguments are provided to specify the colours
    // of the even & odd rows, then use the them;
    // otherwise use the following defaults:
    var evenColor = arguments[1] ? arguments[1] : "even";
    var oddColor = arguments[2] ? arguments[2] : "odd";
  
    // obtain a reference to the desired table
    // if no such table exists, abort
    var table = document.getElementById(id);
    if (! table) { return; }
    // by definition, tables can have more than one tbody
    // element, so we'll have to get the list of child
    // &lt;tbody&gt;s 
    var tbodies = table.getElementsByTagName("tbody");

    // and iterate through them...
    for (var h = 0; h < tbodies.length; h++) {
    
     // find all the &lt;tr&gt; elements... 
      var trs = tbodies[h].getElementsByTagName("tr");
      
      // ... and iterate through them
      for (var i = 0; i < trs.length; i++) {
	    // avoid rows that have a class attribute
        // or backgroundColor style
	    if (!hasClass(trs[i])) {
         
            var mytr = trs[i];
       
			  mytr.className = even ? evenColor : oddColor;

        }
        // flip from odd to even, or vice-versa
        even =  ! even;
      }
    }
  }
/////////////////////////// end ///////////////////////////// 
  
  
/*  
	---------------- fixIEQuotes ------------------------
*/
  function fixIEQuotes() {
  var objQuotes = document.getElementsByTagName('q');
  var strOpen, strClose;

  for (var i=0; i<objQuotes.length; i++)
  {
    if (isNested(objQuotes[i]))
    {
      // Double-quotes
      strOpen = document.createTextNode('\u2018');
      strClose = document.createTextNode('\u2019');
    }
    else
    {
      // Single-quotes
      strOpen = document.createTextNode('\u201c');
      strClose = document.createTextNode('\u201d');
    }
    // Insert quotation marks around quote
    objQuotes[i].parentNode.insertBefore(strOpen, objQuotes[i]);
    objQuotes[i].appendChild(strClose);
  }

  objQuotes = null;
}

function isNested(objElement)
{
  var objParent = objElement;
  do // Check if nested quote
  {
    objParent = objParent.parentNode;
    if (objParent.tagName && objParent.tagName.toLowerCase() == 'q')
      return true;
  } while (objParent.parentNode);

  return false;
}
/////////////////////////// end ///////////////////////////// 
  
  
/*  
	---------------- fixIEabbr ------------------------
*/
function fixIEabbr(){
    // dont bother, this breaks page formatting so screws anything with white-space pre
	//oldBodyText = document.body.innerHTML;
    //reg = /<ABBR([^>]*)>([^<]*)<\/ABBR>/g;
    //newBodyText = oldBodyText.replace(reg, '<ABBR $1><SPAN class=\"abbr\" $1>$2</SPAN></ABBR>');
    //document.body.innerHTML = newBodyText;
}