//TODO: change all the varuseThisURL = useURL; to something like var useThisOnClickValue = useOnlickValue;
//Borrowed from http://alistapart.com/articles/behavioralseparation
//  which linked to http://simonwillison.net/2004/May/26/addLoadEvent
function addLoadEvent(func) {
	var oldOnLoad = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function () {
			var my1, my2;
			if (oldOnLoad) {
				my1 = oldOnLoad();
			}
			my2 = func();
			if (my1 == undefined) { return my2; }
			else if (my2 == undefined) { return my1; }
			else if (my1 === false || my2 === false) { return false; }
		}
	}
}
//Same as addLoadEvent above, only for adding onclick handlers
function addOnClickEvent(ele, func) {
	var oldOnClick = ele.onclick;
	var useThisURL = useURL;
	if (typeof ele.onclick != 'function') {
		ele.onclick = func;
		//alert('set first onclick to '+func);
	} else {
		ele.onclick = function () {
			var my1, my2;
			if (oldOnClick) {
				my1 = oldOnClick();
			}
			my2 = func();
			if (my1 == undefined) { return my2; }
			else if (my2 == undefined) { return my1; }
			else if (my1 === false || my2 === false) { return false; }
		}
		//alert('set subsequent onclick to '+func);
	}
}
//Same as addLoadEvent above, only for adding onfocus handlers
function addOnFocusEvent(ele, func) {
	var oldOnFocus = ele.onfocus;
	var useThisURL = useURL;
	if (typeof ele.onfocus != 'function') {
		ele.onfocus = func;
	} else {
		ele.onfocus = function () {
			var toReturn = true;
			if (oldOnFocus) {
				my1 = oldOnFocus();
			}
			my2 = func();
			if (my1 == undefined) { return my2; }
			else if (my2 == undefined) { return my1; }
			else if (my1 === false || my2 === false) { return false; }
		}
	}
}
//Same as addLoadEvent above, only for adding onblur handlers
function addOnBlurEvent(ele, func) {
	var oldOnBlur = ele.onblur;
	var useThisURL = useURL;
	if (typeof ele.onblur != 'function') {
		ele.onblur = func;
	} else {
		ele.onblur = function () {
			var toReturn = true;
			if (oldOnBlur) {
				my1 = oldOnBlur();
			}
			my2 = func();
			if (my1 == undefined) { return my2; }
			else if (my2 == undefined) { return my1; }
			else if (my1 === false || my2 === false) { return false; }
		}
	}
}
//Same as addLoadEvent above, only for adding onmouseover handlers
function addOnMouseOverEvent(ele, func) {
	var oldOnMouseOver = ele.onmouseover;
	var useThisURL = useURL;
	if (typeof ele.onmouseover != 'function') {
		ele.onmouseover = func;
	} else {
		ele.onmouseover = function () {
			var toReturn = true;
			if (oldOnMouseOver) {
				my1 = oldOnMouseOver();
			}
			my2 = func();
			if (my1 == undefined) { return my2; }
			else if (my2 == undefined) { return my1; }
			else if (my1 === false || my2 === false) { return false; }
		}
	}
}
//Same as addLoadEvent above, only for adding onmouseout handlers
function addOnMouseOutEvent(ele, func) {
	var oldOnMouseOut = ele.onmouseout;
	var useThisURL = useURL;
	if (typeof ele.onmouseout != 'function') {
		ele.onmouseout = func;
	} else {
		ele.onmouseout = function () {
			var toReturn = true;
			if (oldOnMouseOut) {
				my1 = oldOnMouseOut();
			}
			my2 = func();
			if (my1 == undefined) { return my2; }
			else if (my2 == undefined) { return my1; }
			else if (my1 === false || my2 === false) { return false; }
		}
	}
}
//safe function to find an element with a specified id
//  We may not be supporting NN4.x or IE4.x in the very near future
function getEleById (id) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		return (document.getElementById(id));
	}
	else {
		if (document.layers) { // Netscape 4
			return (document.id);
		}
		else { // IE 4
			return (document.all.id);
		}
	}
}

// trackLinksWithGA() dynamically adds urchinTracker handle calls to all non-document.domain links
//   for the time being, I am seeing if we care about tracking all project links separate from myresource views from all other links.
//    I think this is a good idea for now since we are still learning about tracking.
function trackLinksWithGA() {
	if (document.getElementById && document.getElementsByTagName && window.RegExp) { //is there any way to make this NN4 or IE4 compatible?
		var aTags = document.getElementsByTagName('a');
		var regex = new RegExp('^https?:\/\/(?!'+document.domain.replace(/([.\/])/g, '\\$1')+')(.*)', 'i');
		var regex2 = new RegExp('^https?:\/\/'+document.domain.replace(/([.\/])/g, '\\$1')+'.*trackme\.php[?](.*)', 'i');
		var regex3 = new RegExp('^https?:\/\/'+document.domain.replace(/([.\/])/g, '\\$1')+'.*\/(.*\\.php.*)$', 'i');
		var regex4 = new RegExp('^https?:\/\/'+document.domain.replace(/([.\/])/g, '\\$1')+'.*viewproject\.php[?](.*)', 'i');
		var uidPrefix = 'u/';
		if (uid != -1) uidPrefix = 'u'+uid+'/';
		var gidPrefix = 'g/';
		if (gid != -1) gidPrefix = 'g'+gid+'/';

		for (var i=0, imax=aTags.length; i < imax; i++) { //for each a tag, if not IA link, attach GA handler
			var captured = regex.exec(aTags[i].href);
			var captured2 = regex2.exec(aTags[i].href);
			var captured3 = regex3.exec(aTags[i].href);
			var captured4 = regex4.exec(aTags[i].href);
			var showalert = false;

				//track links off site in /LINK/ and full URL
			var myURL = aTags[i].href;
			var trackPrefix = '/LINK/';
			if (aTags[i].href == '' || (aTags[i].href.match("^\#") != null)) {
				continue;
			} else if (document.location.href.match('authorproject2|myresources') && captured2 != null) {
				//track links off site in /RES/ and query string
				trackPrefix = '/MY_RES/';
				myURL = captured2[1];
			} else if (document.location.href.match('searchnsdl') && captured != null) {
				//track links off site in /NSDL/ and query string
				trackPrefix = '/NSDL/';
				myURL = captured[1];
			} else if (document.location.href.match('searchnsdl') && captured2 != null) {
				//track links off site in /NSDL/ and query string
				trackPrefix = '/NSDL/';
				myURL = captured2[1];
			} else if (document.location.href.match('viewproject|authorproject3') && captured2 != null) {
				//track links off site in /P_RES/ and query string
				trackPrefix = '/P_RES/';
				myURL = captured2[1];
			} else if (document.location.href.match('publicprojects|projectshowcase') && captured4 != null) {
				//track links off site in /P_RES/ and query string
				trackPrefix = '/P_PROJ/';
				myURL = captured4[1];
			} else if (document.location.href.match('teacherprojects') && captured4 != null) {
				//track links off site in /P_RES/ and query string
				trackPrefix = '/T_PROJ/';
				myURL = captured4[1];
			} else if (captured == null && captured3 != null) {
				//track onsite links with /IALINK/ and page name
				trackPrefix = '/IALINK/';
				myURL = captured3[1];
			} else if (captured == null) {
				//track onsite links with /IALINK/ and page name
				trackPrefix = '/IALINK/';
				myURL = aTags[i].href;
			}
			useURL = trackPrefix+uidPrefix+gidPrefix+myURL;
			addOnClickEvent(aTags[i], createGATrackHandle(useURL));
			if (showalert) {
				alert('checking:'+i+' of '+aTags.length+' -> '+aTags[i].href
						+"\nregex:"+regex
						+"\ncaptured:|||"
						+captured+"|||"
						+"\nand\nregex2:"
						+regex2+"\ncaptured2:"
						+captured2
						+"\nand\nregex3:"
						+regex3+"\ncaptured3:"
						+captured3
						+"\nand\nregex4:"
						+regex4+"\ncaptured4:"
						+captured4
						+"\n\nand in the end, send this:\n"+useURL);
				if (i > 20) break;
			}
		}
	}
}
function createGATrackHandle(useThisURL) {
	return function () {
//		alert('useThisURL:'+useThisURL);
		return urchinTracker(useThisURL);
	}
}
addLoadEvent(trackLinksWithGA);
