/****************************************************

	Majorsmicrosites.com Omniture Implementation (PAGE CATEGORIZATION)

	This file is first responsible for categorizing each page and assiging the appropriate
	pre-staging vars for Omniture.  Within this file also lies a function, populateMetricsData(),
	which appends the collected data from the pre-staging vars to the necessary Omniture variables.
	The code will live as a complement to the s_code.js provided by Omniture.
	
	Table of Contents
	
	1. Initialization
	Helper Functions
	Populate Omniture Variables
	Page Categorization
********************************************************/
	


/*******************************************************
*****			Initilization			 *******
*******************************************************/
	
//initializing path_array: 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('/');
if (path_array[0] == '') path_array[0] = 'index.html';
var pL = path_array.length;

//Initializing the metrics json: holds data to be sent to Omniture
var metrics = {
	server: 	window.location.host,
	channel:	null,
	section: 	null,
	subsection:	null,
	pageName:	null,
	pageType:	null,
	traffic:	{
					prop2:  null,
					prop3:  null,
					prop5:  null,
					prop7:  null,
					prop8:  null,
					prop9:  null,
					prop11: null,
					prop12: null,
					prop13: null,
					prop14: null,
					prop15: null,
					prop16: null,
					prop17: null,
					prop18: null,
					prop19: null,
					prop22: null,
					prop24: null,
					prop25: null,
					prop26: null,
					prop29: null,
					prop30: null,
					prop31: null,
					prop32: null,
					prop37: null,
					prop38: null,
					prop39: null,
					prop40: null,
					prop42: null,
					prop43: null,
					prop44: null,
					prop47: null
				},
	conversion:	{
					event1:  null,
					event2:  null,
					event3:  null,
					event5:  null,
					event6:  null,
					event9:  null,
					event10: null,
					event11: null,
					event12: null, 
					event13: null, 
					event14: null, 
					event15: null, 
					event16: null, 
					event17: null, 
					event18: null, 
					event19: null, 
					event20: null, 
					eVar20: null, 
					eVar37: null,
					eVar38: null,
					eVar39: null,
					eVar40: null,
					eVar42: null,
					eVar43: null,
					eVar44: null,
					eVar47: null
				},
	hierarchy:	{
					hier1: null,
					hier2: null,
					hier3: null
				}
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Building Server Lists for Microsites

var server = {
	rydercup:'',
	majors:{
		dev:'jcmsdev8.turner.com:99', 
		ref:'jcmsref1.turner.com:99',
		prev:'pgapreview.turner.com:99',
		prod:'www.pga.com'
	},
	openchampionship:'',
	usopen:'',
	pgachampionship:'',
	wgc:{ 
		dev:'jcmsdev8.turner.com:93', 
		ref:'jcmsref1.turner.com:93',
		prev:'pgatourpreview.turner.com:93',
 		prod:'www.worldgolfchampionships.com'
		}
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var tourneyId, tourID = null;
var tourName = {	r: 'PGA Tour',	s: 'Champions Tour',	h: 'Nationwide Tour',	none: 'No Tour'};
var pgaMajors  = ['pgachampionship', 'openchampionship'];
var tourMajors = ['masters', 'usopen'];

//Initializing useful regular expressions
var yearRegExp = /20\d{2}/; //regular expression to check for a year

/*******************************************************
*****			END: Initilization			 *******
*******************************************************/

/*******************************************************
*****		Populate Omniture Vars			 *******
*******************************************************/

//When this function is called, the page data that is stored is set to the Omniture variables.
function populateMetricsData(){
	s.server = metrics.server;
	s.pageType = metrics.pageType;

	s.pageName = (metrics.pageName !== null) ? metrics.pageName : s.getPageName();
	s.channel = metrics.channel;
	
	s.hier1 = metrics.hierarchy.hier1;
	s.hier2 = metrics.hierarchy.hier2;
	s.hier3 = metrics.hierarchy.hier3;
	
	s.prop1 = metrics.traffic.prop1;
	s.prop2 = metrics.traffic.prop2
	s.prop3 = metrics.traffic.prop3
	s.prop5 = metrics.traffic.prop5
	s.prop7 = metrics.traffic.prop7
	s.prop8 = metrics.traffic.prop8
	s.prop9 = metrics.traffic.prop9
	s.prop11 = metrics.traffic.prop11
	s.prop12 = metrics.traffic.prop12
	s.prop13 = metrics.traffic.prop13
	s.prop14 = metrics.traffic.prop14
	s.prop15 = metrics.traffic.prop15
	s.prop16 = metrics.traffic.prop16
	s.prop17 = metrics.traffic.prop17
	s.prop18 = metrics.traffic.prop18
	s.prop19 = metrics.traffic.prop19
	s.prop22 = metrics.traffic.prop22
	s.prop24 = metrics.traffic.prop24
	s.prop25 = metrics.traffic.prop25
	s.prop26 = metrics.traffic.prop26
	s.prop29 = metrics.traffic.prop29
	s.prop30 = metrics.traffic.prop30
	s.prop31 = metrics.traffic.prop31
	s.prop32 = metrics.traffic.prop32
	s.prop37 = metrics.traffic.prop37
	s.prop38 = metrics.traffic.prop38
	s.prop39 = metrics.traffic.prop39
	s.prop40 = metrics.traffic.prop40
	s.prop42 = metrics.traffic.prop42
	s.prop43 = metrics.traffic.prop43
	s.prop44 = metrics.traffic.prop44
	s.prop47 = metrics.traffic.prop47
}

/*******************************************************
*****		END: Populate Omniture Vars		 *******
*******************************************************/

/*******************************************************
*****		Helper Functions				 *******
*******************************************************/

function readCookiesInformation() {

	// regUserFlag and regUserId
	var authid = getCookieValue("authid");
	var regUserFlag = (authid != null) ? 'Registered' : 'Casual';
	var regUserId = (authid != null) ? authid : null;

	// check highlighted players
	var lb_saved_highlights = getCookieValue("lb_saved_highlights");
	var lb_highlights = getCookieValue("lb_highlights");
	var playersHighlighted = (lb_saved_highlights != null || lb_highlights != null) ? 'Has Highlighted Players' : 'No Highlighted Players';
	
	metrics.traffic.prop14 = regUserFlag;
	metrics.traffic.prop19 = regUserId;

}

function getCookieValue(searchName) {
	var cookies = document.cookie.split( ';' );
	var cookieHolder = '';
	var cookieName = '';
	var cookieValue = '';
	var cookieFound = false;
	for ( i = 0; i < cookies.length; i++ )
	{
		cookieHolder = cookies[i].split( '=' );
		cookieName = cookieHolder[0].replace(/^\s+|\s+$/g, '');	
		if ( cookieName == searchName )
		{
			cookieFound = true;
			cookieValue = unescape( cookieHolder[1].replace(/^\s+|\s+$/g, '') );
			return cookieValue;
			break;
		}
		cookieHolder = null;
		cookieName = '';
	}
	if ( !cookieFound )
	{
		return null;
	}
}
function strReplaceChar(oldChar, newChar, thisString)
{
	var newString = "";
	for (var n = 0; n < thisString.length; n++)
	{
		newString += (thisString.charAt(n) == oldChar) ? newChar : thisString.charAt(n);
	}
	return newString;
}

function capitalizeThis(str) {
	val = str;
	newVal = '';
	val = val.split(' ');
	for(var c=0; c < val.length; c++) {
		newVal += val[c].substring(0,1).toUpperCase() + val[c].substring(1,val[c].length) + ' ';
	}
	return newVal.substring(0, (newVal.length-1));
}

function getTour(tid){	return tourName[tid.substring(0,1)]; }

function toObj(a)
{
  var newObj = {};
  for(var i=0;i<a.length;i++)
  {
    newObj[a[i]]='';
  }
  return newObj;
}

function translatePage(page, section)
{
  var pageLength = page.length;
  var extLoc = pageLength - 5;

  if (section == 'courses'){
  	page = (page == 'index.html') ? 'Overview' : page;
  }
  
  else if (section == 'tournaments' && (page =='index.html' || page == 'pro-am.html' )){
  switch(page){
  	case 'index.html':
		page = 'Homepage';
		break;
	case 'pro-am.html':
		page = 'Pro-Am';
		break;
  }
  return page;
  }

	  page = ( page.indexOf('.html') > -1 ) ? page.substring(0,extLoc) : page;
	  page = ( page.indexOf('-') > -1) ? strReplaceChar('-', ' ', page) : page;
	  page = ( page.indexOf('_') > -1) ? strReplaceChar('_', ' ', page) : page;

  return capitalizeThis(page);
}

function getBlogAuthor(author){
	var blogger = new Array(
		'Tiger Woods', 'Charles Warren', 'David Toms', 'Heath Slocum', 'John Rollins', 
		'Brett Quigley', 'Davis Love III', 'Carl Pettersson', 'Davis Love III', 'Trevor Immelman',
		'Charles Howell III', 'Lucas Glover', 'Daniel Chopra', 'Chip Beck', 'Brandt Snedeker'
	);
	var blogAuthor;
	for(i=0; i<blogger.length; i++){
		if(author.indexOf(blogger[i]) > -1) {
			blogAuthor = blogger[i];
			break;
		}
		else{ blogAuthor='Player Undefined'};
	}
	return blogAuthor;
}
function setStatsMetricsVars(tour, year, id){
	tourID = tour;
	var currYear = new Date().getFullYear();
	var statYear = (year == 'current' ) ? currYear : year;
	var statID = id;	

	metrics.traffic.prop16 = tourID;
	metrics.traffic.prop26 = statID;
	metrics.hierarchy.hier1N = 'Competition' + '/' + tourName[tourID] + '/' + 'Stats' + '/' + statID + '/' + statYear;
	metrics.hierarchy.hier2N = 'Stats' + '/' + statID + '/' + tourName[tourID] + '/' + statYear;
	metrics.pageNameX = 'pgatour' + ':' + tourID + ':' + 'stats' + ':' + 'info' + ':' + statID + ':' + statYear;

}


function pageQuery( q ) {
 if( q.length > 1 ) this.q = q.substring( 1, q.length );
 else this.q = null;
 this.keyValuePairs = new Array();
 if( q ) {
  for( var i = 0; i < this.q.split( "&" ).length; i++ ) {
   this.keyValuePairs[i] = this.q.split( "&" )[i];
  }
 }
 this.getKeyValuePairs = function() { return this.keyValuePairs; }
 this.getValue = function( s ) {
  for( var j = 0; j < this.keyValuePairs.length; j++ ) {
   if( this.keyValuePairs[j].split( "=" )[0] == s )
   return this.keyValuePairs[j].split( "=" )[1];
  }
  return false;
 }
 this.getParameters = function() {
  var a = new Array( this.getLength() );
  for( var j = 0; j < this.keyValuePairs.length; j++ ) {
   a[j] = this.keyValuePairs[j].split( "=" )[0];
  }
  return a;
 }
 this.getLength = function() { return this.keyValuePairs.length; } 
}

function queryString( key ){
 var page = new pageQuery( window.location.search ); 
 return unescape( page.getValue( key ) ); 
}

function getPageType(){
	var page = (	$('headerFgB').innerHTML == 'Page Not Found') ? 'errorPage' : null;
	return page;
}
/*******************************************************
*****		END: Helper Functions				 *******
*******************************************************/



/*******************************************************
*****		Page Categorization			 *******
*******************************************************/

/**********		Standard pages 			********/
if (metrics.server == 'www.majorschampionships.com' || metrics.server == 'majorschampionships.com' || metrics.server == 'pgatour.com' || metrics.server == 'www.pgatour.com'){
	var i = 0;
	
	//base of the pageName
	metrics.pageName = "pgatour:majorsmicrosites";
	
	//set metrics.pageName
	if (path_array[0] != null || path_array[0] != ""){
		for (i=0; i < pL; ++i) {
			metrics.pageName += ':'+path_array[i];
		}
	}
}
/*******************************************************
*******		END: Page Categorization	        *********
*******************************************************/
