/****************************************************
		PGA.com Omniture Implementation


	This file is responsible for appending data about the page to the necessary Omniture variables.
	The code will live as an include inside of the s_code.js provided by Omniture.
	
********************************************************/
	
//prepares the URL to be parsed
var pathname = window.location.pathname.substring(1);
if (pathname.charAt(pathname.length - 1) == "/") pathname += "index.html";
var path_array = pathname.split('/');

//Initializing the JSON that holds the necessary data to be sent to Omniture
var metrics = {
	server: 	window.location.host,
	pageName:	null
}

var yearRegExp = /20\d{2}/g; //regular expression to check for a year
	
var pathname = window.location.pathname;
var hostName = window.location.hostname;

//When this function is called, the page data that is stored is set to the Omniture variables.
function populateMetricsData(){
	s.server = metrics.server;
	s.pageName = (metrics.pageName != null) ? metrics.pageName : s.getPageName();
}
	
var buzUnitCode = "pga";
var buc_p32 = buzUnitCode + ":";
//find third level domain
var allDomainLevels = hostName.split(".");
var currentDomainLevel = allDomainLevels.length - 3;
var thirdLevelDomain = (currentDomainLevel >= 0 ? allDomainLevels[currentDomainLevel] : "");
thirdLevelDomain = thirdLevelDomain.replace("www","");
//if s.pageName has more than 100 char, use only 5 first char of 3rd level domain
//if s.pageName still has more than 100 char, truncate from the beginning of pathname
if (thirdLevelDomain == "") {
	var pageNameLen = buc_p32.length + 2 + pathname.length;
	if (pageNameLen <= 100) {
		metrics.pageName = buc_p32 + ":" + pathname;
	} else {
		var r_len = pageNameLen - 100;
		metrics.pageName = buc_p32 + ":" + pathname.substring(r_len);
	}
} else {
	var pageNameLen = buc_p32.length + 1 + thirdLevelDomain.length + 2 + pathname.length;
	if (pageNameLen <= 100) {
		metrics.pageName = buc_p32 + ":" + thirdLevelDomain + ":" + pathname;
	} else {
		if (thirdLevelDomain.length <= 5) {
			var r_len = pageNameLen - 100;
			metrics.pageName = buc_p32 + ":" + thirdLevelDomain + ":" + pathname.substring(r_len);
		} else {
			thirdLevelDomain = thirdLevelDomain.substring(0,5);
			pageNameLen = buc_p32.length + 1 + thirdLevelDomain.length + 2 + pathname.length;
			if (pageNameLen <= 100) {
				metrics.pageName = buc_p32 + ":" + thirdLevelDomain + ":" + pathname;
			} else {
				var r_len = pageNameLen - 100;
				metrics.pageName = buc_p32 + ":" + thirdLevelDomain + ":" + pathname.substring(r_len);
			}
		}
	}
}
