
////////////////////////////// Utilities ///////////////////////////



// Simon Wilson's addLoadEvent - http://simon.incutio.com/archive/2004/05/26/addLoadEvent 
// Attach more onload functions without worrying about erasing previously applied onload function. 
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
/* To use:
addLoadEvent(nameOfFunctionToRunOnPageLoad);
addLoadEvent(function() {
  // more code to run on page load 
  thisFunctionHasArguements(foo,bar,suckit);
});
*/



// ProtoType's DollarSign($) getElementByID Function 
// This is a shorthand way to call document.getElementById("foo")  (http://prototype.conio.net) 
function $() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    if (arguments.length == 1)
      return element;
    elements.push(element);
  }
  return elements;
}
/* To use:
	var foo = $("foo"); (finds the element with ID of foo)
*/




// Get Elements By Class Function
//http://www.dustindiaz.com/getelementsbyclass/
function getElementsByClass(SEARCHCLASS,NODE,TAG) {
	var classElements = new Array();
	if ( NODE == null )
		NODE = document;
	if ( TAG == null )
		TAG = '*';
	var els = NODE.getElementsByTagName(TAG);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+SEARCHCLASS+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
/* To Use:
var foo = getElementsByClass("special") // gets all tags w/ class of special.
var foo = getElementsByClass("special",varblock,"input") 
// gets all inputs w/ class "special" that are children of collection varblock .
*/




// GP's Modify Class Function
// Specify a target, a class, and whether you want to add that class or remove it.
// Prevents attatching multiple instances of a class to an element.
function changeClass(prmTarget,prmClass,prmAddRemove) {
	if (prmAddRemove==null) prmAddRemove = "add";
	var oldclass = prmTarget.className;
	if (prmAddRemove=="add") {
		if (oldclass=="")
			prmTarget.className = prmClass;
		else if (oldclass.indexOf(prmClass)==-1)
			prmTarget.className += " " + prmClass;
	} 
	else if (prmAddRemove == "remove" && oldclass.indexOf(prmClass)!=-1)
		prmTarget.className = oldclass.replace(prmClass,"");
}









// Add Alternating Class Function
// This function will apply a custom class to every other instances of a tag inside an ID'd parent element.
function stripeAnything(CONTAINER,ITEM,ALTCLASS) {
	var stripeInThis = $(CONTAINER);
	var items = gtags(ITEM,stripeInThis);
	var counter = 0;
	for (var i=0; i<items.length; i++) {
		counter = counter + 1;
		if (counter % 2 == 0) { 
			appendClass(items[i],ALTCLASS);
		}	
	}
}
// To use, see sample below which applies a class of 'altItem' to alternating P tags inside div#stripeThese.
//   addLoadEvent( function() {
//  	  stripeAnything("stripeThese","p","alt2");
//   });





// Insert Element After function - via Jeremy Keith
function insertAfter(NEWELEMENT,TARGETELEMENT) {
	var parent = TARGETELEMENT.parentNode;
	if (parent.lastChild == TARGETELEMENT) {
		parent.appendChild(NEWELEMENT);
	} else {
		parent.insertBefore(NEWELEMENT,TARGETELEMENT.nextSibling);
	}
}






// 3 Cookie function from Dustin Diaz that are very useful
// http://www.dustindiaz.com/top-ten-javascript

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}



function rollovers() {
	var img = getElementsByClass("nav")[0].getElementsByTagName("img");
	for (var i=0; i<img.length; i++) {
		img[i].onmouseover = function() {
			this.src = this.src.replace("-off","-on");	
		}
		img[i].onmouseout = function() {
			this.src = this.src.replace("-on","-off");	
		}		
	}
}
addLoadEvent(rollovers);



