if(jQuery) {
	jQuery.noConflict();
}

jQuery(document).ready(function() {
	var pageTimeStart = (new Date()).getTime();
	
	/* Banner toggle menu */
	jQuery("#purpose-nav>ul>li").each(function (i) {
		var item = jQuery(this);
		if(i == 0) {
			item.addClass("over");
		}
		var label = item.children('a').text();
		item.hover(function () {
			if(!jQuery(this).hasClass("over")) {
				jQuery("#purpose-nav>ul>li").each(function (j) {
					if(j != i) {
						jQuery(this).removeClass("over");
					} else {
						jQuery(this).addClass("over");
					}
				});
				jQuery("#banner>div").each(function (j) {
					if(j != i) {
						jQuery(this).addClass("bannerOff");
						jQuery(this).removeClass("bannerOver");
					} else {
						jQuery(this).addClass("bannerOver");
						jQuery(this).removeClass("bannerOff");
					}
				});
			}
		}, 
		function () {
		}); 
	});
	
	/* Track banner clicks */
	jQuery("#banner>div>div>a").each(function (i) {
		var label = jQuery("#purpose-nav>ul>li>a").eq(i).text();
		jQuery(this).click(function() {
			widgetUsed("HomePageBanner-Clicked", "Banner " + (i+1) + ": " + label);
		});
	});
	
	
	/* Listen for a mouse button to be released on any link element or child of a link element (image link) */
	jQuery(document).mouseup(function(e) {
		var action = "";
		var target = (e.target.tagName == "A") ? e.target : e.target.parentNode;
		
		switch(e.which) {
			case 1:
				action = "left-click";
				break;
			case 2:
				action = "middle-click";
				break;
			case 3:
				action = "right-click";
				break;
		}
		if(target.tagName == "A") {
			linkClicked(jQuery(target), action);
		}
	});
	
	
	
	/* Save the link click data */
	function linkClicked(link, action) {
		var container = getLinkContainer(link);
		var clickedTime = (new Date()).getTime();
		var time = clickedTime - pageTimeStart;
		var timeOnPage = formatTime(time);
		var targ = link.attr('target');
		var href = link.attr('href');
		
		targ = (targ == undefined || targ == "") ? "_self" : targ;
		href = (href == undefined) ? "" : href;
		
		var data = action + " at " + timeOnPage + " to (" + href + ") with target: " + targ;
		
		try {
			//console.log("Link Clicked: " + container + " | " + data); 
			_gaq.push(['_trackEvent', 'homepage-click-data', container, data]);
			_gaq.push(['_trackEvent', 'homepage-click-button', container, action]);
			_gaq.push(['_trackEvent', 'homepage-click-target', container, targ]);
			_gaq.push(['_trackEvent', 'homepage-click-destination', container, href]);
			_gaq.push(['_trackEvent', 'homepage-click-time', container, timeOnPage]);
		} catch(err) {}
	}
	
	/* Returns the ID attribute of the given link or the first ancestor of the link that has an ID attribute. 
	If none is found, all the way up to the body element, then return the empty string.
	*/
	function getLinkContainer(link) {
		var found = false;
		var itm = link;
		var container = "";
		var count = 0;
		if(link.attr('href') != undefined) {
			while(!found && !(itm.get(0).tagName == "BODY")) {
				if(itm.attr('id') != "" && itm.attr('id') != undefined) {
					container = itm.attr('id');
					break;
				} else {
					itm = itm.parent();
				}
			}
		}
		return container;
	}
	
	/* converts a number milliseconds into a string with the format hh:mm:ss */
	function formatTime(time) {
		var s = Math.round(time/1000)
		var h = Math.floor(s / 3600);
		s -= h * 3600;
		var m = Math.floor(s / 60);
		s -= h * 60;
		
		var hours = ((h < 10) ? "0" : "") + h + ":";
		var minutes = ((m < 10) ? "0" : "") + m + ":";
		var seconds = ((s < 10) ? "0" : "") + s;
		
		return  hours + minutes + seconds;
	}

});
