

//http://www.ilfilosofo.com/blog/2008/04/14/addevent-preserving-this/
//cross browser addEventListener
function addEvent( obj, type, fn ) 
{
	if (obj.addEventListener)
	{
		obj.addEventListener(type, fn, false);
	}
	else if (obj.attachEvent) 
	{
		obj.attachEvent('on' + type, function() { return fn.apply(obj, new Array(window.event));});
	}
}


//http://www.javascriptkit.com/jsref/event.shtml
//cross browser preventDefault
function stopDefault(e)
{
	//alert('default action has been cancelled');
	var evt = window.event || e;
 	if (evt.preventDefault)  //W3C
	{
  		evt.preventDefault();
	}	
 	else //IE browser
	{ 
  		return false;
	}
}


//Peter Paul Koch - www.quirksmode.org
//dynamically getting style of element without need for inline style.
function getStyle(el,styleProp)
{
	var x = document.getElementById(el);
	if (x.currentStyle)
	{
		var y = x.currentStyle[styleProp];
	}
	else if (window.getComputedStyle)
	{
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	}
	
	return y;
}


function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}


/**
* This is used to change the className property of a given element.
* http://onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html
* NOTE: function name changed to be more descriptive.
* @param a:String
* defines the action you want the function to perform. See below &
* @param el:element reference
* the element in question.
* @param c1:String
* the name of the first class
* @param c2:String
* the name of the second class
* 
* & Possible actions are:
* 
* swap
* replaces class c1 with class c2 in element el. c2 is optional if not swapping. 
(i.e. changeClassName('add',tohide,'hidden'))
* add
* adds class c1 to the element el.
* remove
* removes class c1 from the element el.
* check
* test if class c1 is already applied to element el and returns true or false.
*/
function changeClassName(a,el,c1,c2)
{
	switch (a)
	{
		case 'swap':
			el.className=!changeClassName('check',el,c1)?el.className.replace(c2,c1):el.className.replace(c1,c2);
		break;
		case 'add':
			if(!changeClassName('check',el,c1)){el.className+=el.className?' '+c1:c1;}
		break;
		case 'remove':
			var rep=el.className.match(' '+c1)?' '+c1:c1;
			el.className=el.className.replace(rep,'');
		break;
		case 'check':
			return new RegExp('\\b'+c1+'\\b').test(el.className)
		break;
	}
}


//shortcut wrapper function for getElementById
function $(id)
{
	return document.getElementById(id);
}


//shortcut wrapper function for getElementsByTagName
function $$(parent,tag)
{
	p = $(parent) || document;
	return p.getElementsByTagName(tag);
}



/**
* @fileURL - String - url to source of content.
* @placeholder - String - element to add content to.
* @replaceContent - Boolean - replace placeholder's content OR append to it?
*/
function getExternalContent(fileURL,placeholder,replaceContent)
{
	var xmlHttp;
  	try
	{
		// Web Standards
		xmlHttp = new XMLHttpRequest();
	}
	catch (error)
	{
		// Internet Explorer (newer)
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (error)
		{
			// Internet Explorer (older)
			try
			{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (error)
			{
				// browser not supporting Ajax
				alert("Your browser does not support Ajax!");
				return false;
			}
		}
	}

	//wait for Data to complete loading
	xmlHttp.onreadystatechange = function(){updatePage(xmlHttp,fileURL, placeholder, replaceContent)};

	xmlHttp.open("GET",fileURL,true);
	xmlHttp.send(null);

}

//for use by getExternalContent function above.
function updatePage(x, file, loc, replace)
{
	if(x.readyState == 4)
	{
		var externalData = x.responseText;
		var holder = document.createElement('div');
		var content = document.createTextNode(externalData);
		holder.appendChild(content);
		
		replace ? document.getElementById(loc).innerHTML = holder.innerHTML : document.getElementById(loc).appendChild(holder);
	}
}
