
$(document).ready(function() {
	
	// Logging the initial view
	 var referrer = document.referrer; 
	var referrer_path = '';
 //	alert('referrer:' + referrer);
	 if( referrer && !referrer.match(/science-engineering-careers/) && !referrer.match(/science_careers.shtml/)) { 
	    // The referring page is not within the career area. Log this initial view, as well as thereferrer contains the string 'test' 
		// strip all but the path and file name from the referrer.
		referrer_path = jQuery.url.setUrl(referrer).attr("path");
		if(!referrer_path) {
			referrer_path = jQuery.url.attr("file");
		}

		referrer_path = referrer_path.replace('/science-fair-projects/','');
		view_log(document.location,'view',referrer_path);
	}
	

	$('a').click(function() {
		view_log(this.href,'view','');
		document.location = this.href;
		return false;
	});

	 $("input[name='B6']").click(function(){
		view_log(document.location,'print','');
	//	document.location = document.location + "?print=1";
		document.location = document.location + "?from=noMenuRequest";
		return false;
	});

	function view_log(link_url,view_type,referrer_path) {
		var myDate = new Date();
		var timestamp = myDate.getTime();
		var originating_url = null;
		var gotoURL = jQuery.url.setUrl(link_url).attr("file");
		if(!gotoURL) {
			gotoURL = jQuery.url.attr("path");
		}
		if(jQuery.url.attr("host") && jQuery.url.attr("host") != '72.3.234.53' && jQuery.url.attr("host") != '72.32.164.28' && jQuery.url.attr("host") != 'www.sciencebuddies.org' && jQuery.url.attr("host") != 'www.sciencebuddies.com') {
			// external link
			gotoURL = link_url;
			originating_url = location.pathname.substr(location.pathname.lastIndexOf("/")+1,location.pathname.length);
		}
		// Need to check for a trailng '#' with no anchor value. This would signify a return to the top of the page.
		// In that case, just bypass logging alltogether.
//		alert('link_url:' + link_url);
		var lastChar = String(link_url).charAt(link_url.length - 1);
		if(lastChar == '#') {
			return;
		}
		if(jQuery.url.attr("anchor")) {
			gotoURL = gotoURL + "#" + jQuery.url.attr("anchor");
		}
		if(jQuery.url.param("ia")) {
			gotoURL = gotoURL + "?" + "ia=" + jQuery.url.param("ia");
		}
		gotoURL = escape(gotoURL);		
		var ajax_parameters = "/science-fair-projects/career_log.php?GoTo=" + gotoURL + "&vt=" + view_type;
		if(originating_url) {
			ajax_parameters = ajax_parameters + "&o_url=" + originating_url;
		}
		if(referrer_path) {
			ajax_parameters = ajax_parameters + "&r_url=" + referrer_path;
		}
		ajax_parameters = ajax_parameters + "&t=" + timestamp;
		$.ajax({
			type: 	"GET",
			url:    ajax_parameters,
			async:   false
		});
	}
	
});