
/* IANR Publication Tracking with Google Analytics Event Tracking and User-Defined Variables */

// Categories

ipubTrack.cat_pub_prefix = "Publication Downloads by Publication";
ipubTrack.cat_dept_prefix = "Publication Downloads by Department";
ipubTrack.cat_auth_prefix = "Publication Downloads by Author";
ipubTrack.cat_detail_prefix = "Publication Downloads by Publication with Details";

ipubTrack.type_html = "html";
ipubTrack.type_pdf = "pdf";
ipubTrack.type_order = "order";
ipubTrack.type_total = "total";

ipubTrack.type_suff = new Object();

ipubTrack.type_suff[ipubTrack.type_html] = " - HTML";
ipubTrack.type_suff[ipubTrack.type_pdf] = " - PDF";
ipubTrack.type_suff[ipubTrack.type_order] = " - Order";
ipubTrack.type_suff[ipubTrack.type_total] = " - Total";

ipubTrack.cat_pub = new Object();
ipubTrack.cat_dept = new Object();
ipubTrack.cat_auth = new Object();
ipubTrack.cat_detail = new Object();

ipubTrack.cat_pub[ipubTrack.type_html] =  ipubTrack.cat_pub_prefix + ipubTrack.type_suff[ipubTrack.type_html];
ipubTrack.cat_pub[ipubTrack.type_pdf] =   ipubTrack.cat_pub_prefix + ipubTrack.type_suff[ipubTrack.type_pdf];
ipubTrack.cat_pub[ipubTrack.type_order] = ipubTrack.cat_pub_prefix + ipubTrack.type_suff[ipubTrack.type_order];
ipubTrack.cat_pub[ipubTrack.type_total] = ipubTrack.cat_pub_prefix + ipubTrack.type_suff[ipubTrack.type_total];

ipubTrack.cat_dept[ipubTrack.type_html] =  ipubTrack.cat_dept_prefix + ipubTrack.type_suff[ipubTrack.type_html];
ipubTrack.cat_dept[ipubTrack.type_pdf] =   ipubTrack.cat_dept_prefix + ipubTrack.type_suff[ipubTrack.type_pdf];
ipubTrack.cat_dept[ipubTrack.type_order] = ipubTrack.cat_dept_prefix + ipubTrack.type_suff[ipubTrack.type_order];
ipubTrack.cat_dept[ipubTrack.type_total] = ipubTrack.cat_dept_prefix + ipubTrack.type_suff[ipubTrack.type_total];

ipubTrack.cat_auth[ipubTrack.type_html] =  ipubTrack.cat_auth_prefix + ipubTrack.type_suff[ipubTrack.type_html];
ipubTrack.cat_auth[ipubTrack.type_pdf] =   ipubTrack.cat_auth_prefix + ipubTrack.type_suff[ipubTrack.type_pdf];
ipubTrack.cat_auth[ipubTrack.type_order] = ipubTrack.cat_auth_prefix + ipubTrack.type_suff[ipubTrack.type_order];
ipubTrack.cat_auth[ipubTrack.type_total] = ipubTrack.cat_auth_prefix + ipubTrack.type_suff[ipubTrack.type_total];

ipubTrack.cat_detail[ipubTrack.type_html] =  ipubTrack.cat_detail_prefix + ipubTrack.type_suff[ipubTrack.type_html];
ipubTrack.cat_detail[ipubTrack.type_pdf] =   ipubTrack.cat_detail_prefix + ipubTrack.type_suff[ipubTrack.type_pdf];
ipubTrack.cat_detail[ipubTrack.type_order] = ipubTrack.cat_detail_prefix + ipubTrack.type_suff[ipubTrack.type_order];
ipubTrack.cat_detail[ipubTrack.type_total] = ipubTrack.cat_detail_prefix + ipubTrack.type_suff[ipubTrack.type_total];


// Actions

ipubTrack.act_pub = "All Publications";
//department and author actions are the department and author names respectively
ipubTrack.act_detail = "All Publications with Details";

// Labels

// By Publication: 
// pub_title (pub_identifier) (pub_id cat_letter) by pub_first_author - pub_type
// Ex: Weight Management for Healthy Adults (NF32) (1172 p) by Sharon S. Skipton - PDF

// By Department: 
// pub_title (pub_identifier) (pub_id cat_letter) by pub_first_author - pub_type
// Ex: Weight Management for Healthy Adults (NF32) (1172 d) by Sharon S. Skipton - HTML

// By Author: 
// pub_title (pub_identifier) (pub_id cat_letter_with_index) - pub_type
// Ex: Weight Management for Healthy Adults (NF32) (1172 a0) - Order

// By Publication with Details: 
// pub_title (pub_identifier) (pub_id cat_letter) by pub_authors for pub_department - pub_type
// Ex: Weight Management for Healthy Adults (NF32) (1172 s) by Sharon S. Skipton and Randy Meyers for Nutrition and Health Sciences - Total

// pub_id = Database primary id
// cat_letter = 'p' for By Publication, 'd' for By Department, 
//				'aX' for By Author where X is 0, 1, 2... for the ith author, 
// 				and 's' for By Publication with Details


// User-Defined Variables

// This is used to allow custom reporting
// pub_title (pub_identifier) (pub_id cat_letter) by pub_authors for pub_department - Total
// Ex: Weight Management for Healthy Adults (NF32) (1172 u) by Sharon S. Skipton and Randy Meyers for Nutrition and Health Sciences - Total

// cat_letter = 'u' for User-Defined Variable
// Track by Total count only since can only associate user visit with one variable
// The integer value of _trackEvent() is not used


// Featured Pubs

ipubTrack.featuredPubs = new Object();

ipubTrack.featuredPubs["1161"] = "1161";
ipubTrack.featuredPubs["1189"] = "1189";
ipubTrack.featuredPubs["1026"] = "1026";

ipubTrack.download_featured = "/download/featured";

function ipubTrack(title, identifier, id, dept, authors, type)
{
	try
	{
		if (type==ipubTrack.type_html || type==ipubTrack.type_pdf || type==ipubTrack.type_order)
		{
			title = ipubClean(ipubCheckEmptyStr(title, "N/A"));
			identifier = ipubClean(!ipubIsEmptyStr(identifier) ? identifier.toUpperCase() : "N/A");
			id = ipubClean(ipubCheckEmptyStr(id, "N/A"));
			dept = ipubClean(ipubCheckEmptyStr(dept, "Uncategorized"));
			if (authors!=null)
				for (i=0; i<authors.length; i++)
					authors[i]=ipubClean(ipubCheckEmptyStr(authors[i], "N/A"));
			
			ipubTrackPub(title, identifier, id, dept, authors, type);
			ipubTrackDept(title, identifier, id, dept, authors, type);
			ipubTrackAuthor(title, identifier, id, dept, authors, type);
			ipubTrackDetails(title, identifier, id, dept, authors, type);
		}
	}
	catch (e)
	{

	}
}

function ipubTrackPub(title, identifier, id, dept, authors, type)
{	
	pageTracker._trackEvent(ipubTrack.cat_pub[type], ipubTrack.act_pub, ipubMakeTitleFirstAuthor(title, identifier, id, "p", authors, type));
	pageTracker._trackEvent(ipubTrack.cat_pub[ipubTrack.type_total], ipubTrack.act_pub, ipubMakeTitleFirstAuthor(title, identifier, id, "p", authors, ipubTrack.type_total));
	
	pageTracker._setVar(ipubMakeTitleFull(title, identifier, id, "u", dept, authors, ipubTrack.type_total));
	
	if (ipubTrack.featuredPubs[id])
	{
		pageTracker._trackPageview(ipubTrack.download_featured+"/"+type+"/"+identifier);
		pageTracker._trackPageview(ipubTrack.download_featured+"/"+ipubTrack.type_total+"/"+identifier);
	}
}

function ipubTrackDept(title, identifier, id, dept, authors, type)
{
	pageTracker._trackEvent(ipubTrack.cat_dept[type], dept, ipubMakeTitleFirstAuthor(title, identifier, id, "d", authors, type));
	pageTracker._trackEvent(ipubTrack.cat_dept[ipubTrack.type_total], dept, ipubMakeTitleFirstAuthor(title, identifier, id, "d", authors, ipubTrack.type_total));
}

function ipubTrackAuthor(title, identifier, id, dept, authors, type)
{
	if (authors!=null)
	{
		for (i=0; i<authors.length; i++)
		{
			pageTracker._trackEvent(ipubTrack.cat_auth[type], authors[i], ipubMakeTitleNoAuthors(title, identifier, id, "a" + i, type));
			pageTracker._trackEvent(ipubTrack.cat_auth[ipubTrack.type_total], authors[i], ipubMakeTitleNoAuthors(title, identifier, id, "a" + i, ipubTrack.type_total));
		}
	}
}

function ipubTrackDetails(title, identifier, id, dept, authors, type)
{	
	pageTracker._trackEvent(ipubTrack.cat_detail[type], ipubTrack.act_detail, ipubMakeTitleFull(title, identifier, id, "s", dept, authors, type));
	pageTracker._trackEvent(ipubTrack.cat_detail[ipubTrack.type_total], ipubTrack.act_detail, ipubMakeTitleFull(title, identifier, id, "s", dept, authors, ipubTrack.type_total));
}

function ipubMakeTitleFull(title, identifier, id, catletter, dept, authors, type)
{
	var titleStr = ipubMakeTitle(title, identifier, id, catletter);
	
	var authorsStr = " by ";
	
	if (authors!=null && authors.length>0)
	{
		if (authors.length>2)
		{
			var firstAuthors = "";
			
			for (i=0; i<authors.length-2; i++)
				firstAuthors += authors[i] + ", ";
			
			authorsStr += firstAuthors;
		}
		
		if (authors.length>1)
			authorsStr += authors[authors.length-2] + " and ";
		
		authorsStr += authors[authors.length-1];
	}
	else
	{
		authorsStr += "N/A";
	}
	
	var deptStr = " for " + dept;
	
	return titleStr + authorsStr + deptStr + ipubTrack.type_suff[type];
}

function ipubMakeTitleFirstAuthor(title, identifier, id, catletter, authors, type)
{
	var titleStr = ipubMakeTitle(title, identifier, id, catletter);
	
	var authorStr = " by " + (authors!=null && authors.length>0 ? authors[0] : "N/A");
	
	return titleStr + authorStr + ipubTrack.type_suff[type];
}

function ipubMakeTitleNoAuthors(title, identifier, id, catletter, type)
{
	var titleStr = ipubMakeTitle(title, identifier, id, catletter);
	
	return titleStr + ipubTrack.type_suff[type];
}

function ipubMakeTitle(title, identifier, id, catletter)
{
	return title + " (" + identifier + ") (" + id + " " + catletter + ")";
}

function ipubCheckEmptyStr(s, def)
{
	return ipubIsEmptyStr(s) ? def : s;
}

function ipubIsEmptyStr(s)
{
	return s==null || s=="null" || ipubTrim(s).length==0;
}

function ipubTrim(str)
{
	return str.replace(/^\s+|\s+$/g, "");
}

// TODO: Currently the server cleans the string - Add code here to client-side clean it.
function ipubClean(str)
{
	if (str!=null)
	{
		str = ipubTrim(str);
	}
	
	return str;
}


/* IANR Publications Mouse-Over Event Tracking */

// Tracks first event per label per page only
// Only tracking on home page - /epublic/pages/index.jsp

// Categories

useTrackMouseOver.cat = "Usability";


// Actions

useTrackMouseOver.act = "Mouseover";


// Labels

// Context-dependent


// Keep track of what's been tracked
useTrackMouseOver.tracked = new Object();
useTrackMouseOver.numTracked = 0;


// whatParam is for the home page check (request.getParameter("what") is empty for home page)
function useTrackMouseOver(label, whatParam)
{
	if (!ipubIsEmptyStr(label))
	{
		if (!useTrackMouseOver.tracked[label])
		{
			if (location.pathname && !location.search && ipubIsEmptyStr(whatParam) && (location.pathname.match(/epublic\/pages\/index.jsp/i) || location.pathname.match(/epublic\/pages/i)))
			{
				useTrackMouseOver.tracked[label] = true;
				useTrackMouseOver.numTracked++;
				
				pageTracker._trackEvent(useTrackMouseOver.cat, useTrackMouseOver.act, label, useTrackMouseOver.numTracked);
			}
		}
	}
}