// Add event handler to body when window loads
function addLoadEvent(func) {
	var oldonload = window.onload;
	
	if (typeof window.onload != "function") {
		window.onload = func;
	} else {
		window.onload = function () {
			oldonload();
			func();
		}
	}
}

addLoadEvent(function () {
	GoogleLinkTracker.init();
});

/*-----------------------------------------------------------------------------------------+
 | GoogleLinkTracker - Add click tracking for Google Analytics to files and outgoing links |
 +-----------------------------------------------------------------------------------------*/
var GoogleLinkTracker = {
	init : function() {
		var links = document.getElementsByTagName("a");
		
		for (var i = 0; i < links.length; i++) {
			var theLink = links[i];
			var theURL = theLink.href.toLowerCase();
			
			if (typeof(pageTracker) != "undefined")
			{
				if (/\.(bmp|doc|docx|gif|jpg|pdf|png|xls|xlsx|ppt|pptx|zip)/.test(theURL)) {
					var func = function () { if (pageTracker) pageTracker._trackPageview("/files/" + this.href); };
					
					if (typeof(jQuery) != "undefined")
						jQuery(theLink).click(func);
					else
						theLink.onclick = func;
				}
					
				if (theURL.indexOf("aiachicago.org") == -1) {
					var func = function () { if (pageTracker) pageTracker._trackPageview("/outbound/" + this.href); };
					
					if (typeof(jQuery) != "undefined")
						jQuery(theLink).click(func);
					else
						theLink.onlick = func;
				}
			}
		}
	}
};


/*--------------------------------------------------+
 | Tog - Toggle visibility of two opposing elements |
 +--------------------------------------------------*/
var Tog = {
	swap : function (a, b) {
		a = document.getElementById(a);
		b = document.getElementById(b);
		
		if (!a || !b) return false;
		
		if (a.className.indexOf("closed") != -1) {
			oldClass = a.className;
			newClass = oldClass.replace(/closed/g, "");
			a.className = newClass;
			
			b.className += " closed";
		} else {
			oldClass = b.className;
			newClass = oldClass.replace(/closed/g, "");
			b.className = newClass;
			
			a.className += " closed";
		}
	},
	toggle : function (a) {
		a = document.getElementById(a);
		
		if (!a) return false;
		
		if (a.className.indexOf("closed") != -1) {
			oldClass = a.className;
			newClass = oldClass.replace(/closed/g, "");
			a.className = newClass;
		} else {
			a.className += " closed";
		}
	},
	togglePropertyValue : function (a, prop, value1, value2) {
		a = document.getElementById(a);
		
		if (!a) return false;
		
		if (a[prop].indexOf(value1) != -1) {
			a[prop] = value2;
		} else {
			a[prop] = value1;
		}
	}
};



jQuery(document).ready(function(){
	// Add icons to links to files
	$("a[href$='.pdf']:not(a.imglink)").addClass("file file-pdf");
	$("a[href$='.pdf']").attr({title: "Adobe PDF - Opens in a new window"});
	
	$("a[href$='.doc']:not(a.imglink), a[href$='.docx']:not(a.imglink)").addClass("file file-doc");
	$("a[href$='.doc']").attr({title: "Word document - Opens in a new window"});
	
	$("a[href$='.xls']:not(a.imglink), a[href$='.xlsx']:not(a.imglink)").addClass("file file-xls");
	$("a[href$='.xls']").attr({title: "Excel spreadsheet - Opens in a new window"});
	
	$("a[href$='.zip']:not(a.imglink), a[href$='.rar']:not(a.imglink)").addClass("file file-zip"); 
	$("a[href$='.zip']").attr({title: "ZIP file - Opens in a new window"});
	$("a[href$='.rar']").attr({title: "RAR file - Opens in a new window"});
	
	$("a[href$='.ppt']:not(a.imglink)").addClass("file file-ppt");
	$("a[href$='.ppt']").attr({title: "PowerPoint presentation - Opens in a new window"});
	
	// Make links to files open in new windows
	$("a[class*='file']").attr({ target: "_blank"});
});