﻿function TSNOmniture() {
	var self = this;
	this.pagename = '';
	this.title = '';
	this.sitepath = '';
	this.sitesection = '';
	this.searchstring = '';
	this.errors = [];
	this.pagenamesource = '';
	this.urlpathname = window.location.pathname;
	this.cpaptrackingvar = 'hbx_hc1';

	this.track = function() {
		self.initialize();
		if (window['Tracking']) {
			if (false && self.searchstring) {
				Tracking.TrackSearch(self.searchstring);
			}
			else {
				Tracking.SetPageName(tsno.pagename);
				Tracking.SetContentTitle(tsno.title);
				if (tsno.sitesection) Tracking.SetSiteSection(tsno.sitesection);
			}
			Tracking.Track();
		}
		else {
			self.errors.push("Tracking not defined");
		}
	}
	
	this.initialize = function() {
		self.detectSitePath();
		self.detectSiteSection();
		self.detectPageName();
		self.detectSearch();
	}
	
	this.detectPageName = function() {
		var strWindowTitle;
		var strH1NodeValue;
		var strAdName;
		var rxGalleryCheck = new RegExp('galleryid=([0-9]*)');
		var rxPodcastCheck = new RegExp('podcastcentre.*id\=([0-9]*)');
		var rxSpecialtySection = new RegExp("/(scores|schedule|standings|statistics|teams|players|injuries|transactions)/");
		var arrGalleryCheck;
		
		strWindowTitle = document.title;
		strH1NodeValue =
			document.getElementsByTagName('h1')
			&& document.getElementsByTagName('h1').length
			&& document.getElementsByTagName('h1')[0].firstChild ?
			document.getElementsByTagName('h1')[0].firstChild.nodeValue : '';
		strAdName = window['adpg'] ? window ['adpg'] : 'Unknown';

		arrGalleryCheck = self.urlpathname.match(rxGalleryCheck);
		arrPodcastCheck = self.urlpathname.match(rxPodcastCheck);
		arrSpecialtySection = self.urlpathname.match(rxSpecialtySection);
		
		if (strAdName == 'main' && self.urlpathname.length < 8) {
			self.title = 'homepage';
			self.pagenamesource = 'homepage';
		}
		else if (arrGalleryCheck && arrGalleryCheck.length) {
			self.title = 'gallery_' + arrGalleryCheck[1];
			self.pagenamesource = 'gallery';
		}
		else if (arrPodcastCheck && arrPodcastCheck.length) {
			self.title = 'podcast_' + arrPodcastCheck[1];
			self.pagenamesource = 'podcast';
		}
		else if (self.searchstring) {
			self.title = 'Search Results';
			self.pagenamesource = 'searchresults';
		}
		else if (arrSpecialtySection && arrSpecialtySection.length) {
			self.title = arrSpecialtySection[1] + ' ' + strWindowTitle;
			self.pagenamesource = 'specialtysection';
		}
		else if (strWindowTitle != strAdName) {
			self.title = strWindowTitle;
			self.pagenamesource = 'windowtitle';
		}
		else if (!self.title && strH1NodeValue) {
			self.title = strH1NodeValue
			self.pagenamesource = 'h1';
		}
		else if (strAdName == 'main' && self.sitepath == 'home') {
			self.title = 'homepage';
			self.pagenamesource = 'home2';
		}
		else if (strWindowTitle.length > 8) {
			self.title = strWindowTitle;
			self.pagenamesource = 'windowtitle2';
		}
		else if (strAdName) {
			self.title = strAdName;
			self.pagenamesource = 'adname';
		}
		else {
			self.title = strWindowTitle;
			self.pagenamesource = 'windowtitle3';
			self.errors.push('Title not read. Using Window Title by default.');
		}

		strCpAp = window[self.cpaptrackingvar] ? (' (' + window[self.cpaptrackingvar] + ')') : '';
		
		self.pagename = self.sitepath + ':' + self.title + strCpAp;
	}
	this.detectSearch = function() {
		var rxSearchCheck = new RegExp('/search/.*for=([^&]*)');
		arrSearch=window.location.href.match(rxSearchCheck);
		if (arrSearch) {
			self.searchstring = arrSearch[1];
		}
	}
	this.detectSitePath = function() {
		var rxPath = new RegExp('/(.*)/[^/]*');
		var arrPath = self.urlpathname.match(rxPath);
		strPath = self.urlpathname;
     	if (arrPath && arrPath.length) {
			strPath = arrPath[1];
		}
		else if(strPath == '/') {
			strPath = "home";
		}
		self.sitepath = 'sports:' + strPath.replace(new RegExp("\/","g"), ':');
	}
	this.detectSiteSection = function() {
		var rxSiteSection = new RegExp('^/([^/]+)/');
		var arrSiteSection = self.urlpathname.match(rxSiteSection);
		if (arrSiteSection && arrSiteSection.length) {
			self.sitesection = arrSiteSection[1];
		}
	}
	this.trim = function(s) {
		var rxLTrim = new Regexp("\s*(.+)","i");
		var rxRTrim = new Regexp("(.*\S)\s*","i");
		var arrLeftMatches = s.match(rxLTrim);
		if (arrLeftMatches.length) {
			s = arrLeftMatches[1];
		}
		var arrRightMatches = s.match(rxRTrim);
		if (arrRightMatches.length) {
			s = arrRightMatches[1];
		}
		return s;
	}
}

tsno = new TSNOmniture()
tsno.track();