/*---[ Details ]---------------------------------------
Javascipt Commom Functions Library
Author: Lee Powell
Date Created: 12.05.2007
Date Modified: 12.05.2007
-------------------------------------------------------*/

/*-----------------------------------------------------
Contents:
[01] addLoadEvent: Simplified onLoad Event Attachment
[02] addLoadEvent: Simplified Event Attachment
[03] getElementsByClass: Get All Elements By Class
[04] toggle: Toggle Elements Display
[05] insertAfter: Insert Node After Element
[06] getCookie: Gets Cookie
[07] setCookie: Sets Cookie
[08] deleteCookie: Deletes Cookie
[09] $: Quick GetElement Reference
[10] getElementsByAttribute: Get Elements With Attribute
[11] getElementsWithinId: Get Elements Within ID
[12] swapBackgroundImage: Swaps Background Image
[13] addDOMLoadEvent: Adds Events To Fire At DOM Load
-------------------------------------------------------*/

/*-----------------------------------------------------
Thanks to Dustin Diaz, for providing references to
some of these functions:
http://www.dustindiaz.com/top-ten-javascript/
-------------------------------------------------------*/


/* [01] addLoadEvent: Simplified onLoad Event Attachment
-------------------------------------------------------*/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/* [02] addLoadEvent: Simplified Event Attachment
-------------------------------------------------------*/
function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}

/* [03] getElementsByClass: Get All Elements By Class
-------------------------------------------------------*/
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;
}

/* [04] toggle: Toggle Elements Display
-------------------------------------------------------*/
function toggle(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}

/* [05] insertAfter: Insert Node After Element
-------------------------------------------------------*/
/* http://developer.mozilla.org/en/docs/DOM:element.insertBefore*/
function insertAfter(parent, node, referenceNode) {
	parent.insertBefore(node, referenceNode.nextSibling);
}

/* [06] getCookie: Gets Cookie
-------------------------------------------------------*/
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 ) );
}

/* [07] setCookie: Sets Cookie
-------------------------------------------------------*/
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" : "" );
}

/* [08] deleteCookie: Deletes Cookie
-------------------------------------------------------*/
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";
}

/* [09] $: Quick GetElement Reference
-------------------------------------------------------*/
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;
}

/* [10] getElementsByAttribute: Get Elements With Attribute
-------------------------------------------------------*/
function getElementsByAttribute(attrTarget, attrValue) {
   var container = container || document;
   var allTags = container.all || container.getElementsByTagName("*");
   var attrArray = new Array;
   for (var i=0; i < allTags.length; i++) {
	 if (allTags[i].getAttribute(attrTarget)) {
		 if (allTags[i].getAttribute(attrTarget).indexOf(attrValue) > -1) {
			 attrArray.push(allTags[i]);
		 }
	 }
   }
   return attrArray;
}

/* [11] getElementsWithinId: Get Elements Within ID
-------------------------------------------------------*/
function getElementsWithinId(idTarget, elTarget) {
	var container = container || document;
	var targetHolder = container.getElementById(idTarget);
	var targetElements = targetHolder.getElementsByTagName(elTarget);
	if(targetElements.length != 0) {
		return targetElements;
	} else {
		return false;
	}
}


/* [12] swapBackgroundImage: Swaps Background Image
-------------------------------------------------------*/
function swapBackgroundImage(src, target) {
   	var container = container || document;
	if (container.getElementById(target)) {
		container.getElementById(target).style.backgroundImage = "url("+src+")";
	}
}

/* [13] addDOMLoadEvent: Adds Events To Fire At DOM Load
-------------------------------------------------------*/
/* (c)2006 Dean Edwards/Matthias Miller/John Resig
Thrown together by Jesse Skinner (http://www.thefutureoftheweb.com/)*/
function addDOMLoadEvent(func) {
   if (!window.__load_events) {
      var init = function () {
          // 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 (window.__load_timer) {
              clearInterval(window.__load_timer);
              window.__load_timer = null;
          }
          
          // execute each function in the stack in the order they were added
          for (var i=0;i < window.__load_events.length;i++) {
              window.__load_events[i]();
          }
          window.__load_events = null;
      };
   
      // for Mozilla/Opera9
      if (document.addEventListener) {
          document.addEventListener("DOMContentLoaded", init, false);
      }
      
      // for Internet Explorer
      /*@cc_on @*/
      /*@if (@_win32)
          document.write("<scr"+"ipt id=__ie_onload defer src=//0><\/scr"+"ipt>");
          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
          window.__load_timer = setInterval(function() {
              if (/loaded|complete/.test(document.readyState)) {
                  init(); // call the onload handler
              }
          }, 10);
      }
      
      // for other browsers
      window.onload = init;
      
      // create event function stack
      window.__load_events = [];
   }
   
   // add function to event stack
   window.__load_events.push(func);
}