/*
 
This javascript file is responsible for making analytics do its job. The main functionality is collecting all portlet ID's
and stuffing them inside a cookie. This cookie is overwritten upon each page load.

*/

function _supplyWebAnalytics(){
	 _trackAnchorClicks();
	 //_placePortletIDsInCookie();
}

//
function _trackAnchorClicks() {
    // Remove any existing handlers...this prevents multiple calls
    jQuery("a[onclick]").unbind("click");
	jQuery("a[onclick]").click(function(el) {
		  //alert("Trying to reach URL: " + this.onclick);
		  _makeAnalyticsAjaxCall(this.onclick);
		  return true;
		 });
}


function _makeAnalyticsAjaxCall(payload)
{
    var theUrl = window.location.protocol + "//" + window.location.host + "/webanalytics/webAnalytics.html";
	if(payload != "")
	{
	 theUrl+= "?externalCall=" + payload;
	}
    //alert("Placing ajax call to: " + theUrl);
 
	// Make the ajax call
	jQuery.ajax({
	   type: "GET",
	   cache: false,
	   url: theUrl,
	   dataType: "html",
	   data: "",
	   async: false,
	   success: function(data, textStatus){
	   _handleAnalyticsSuccessResponse(data);
	   },
	   error: function(XMLHttpRequest, textStatus, errorThrown){
		 _handleAnalyticsErrorResponse(textStatus, errorThrown);
	   }
	});
}

//Handle the ajax success response
function _handleAnalyticsSuccessResponse(response) {
//alert("Success");
}

//Handle the ajax success response
function _handleAnalyticsErrorResponse(response, errorThrown) {
//alert("Error - reason: " + response + " " + errorThrown);
}

