/** This function populates "s" with data stored in the omniData array
 *  defined below.
 */
 
 //omniture.js 1.0
 
function populateOmnitureStructure() {
	s.server = omniData['server'];
	s.pageType = omniData['pageType'];
	// only used for 404 pages
	// override the pageName if set explicitely. Use the plugin function instead
	if (omniData['pageName'] != null) {
		s.pageName = omniData['pageName'];
	} else {
		s.pageName = s.getPageName();
	}

	// Channels
	s.channel = omniData['channel'];
	s.prop1 = omniData['prop1'];
	s.prop2 = omniData['prop2'];
	s.prop3 = omniData['prop3'];
	s.prop4 = omniData['prop4'];
	s.prop5 = omniData['prop5']; 

	// standalone properties
	s.prop11 = omniData['articleTitle'];
	s.prop12 = omniData['articleAuthor'];
	s.prop13 = omniData['articleDate'];
	s.prop14 = omniData['regUserFlag'];
	s.prop15 = omniData['year'];
	s.prop16 = omniData['tourCode'];
	s.prop17 = omniData['tournId'];
	s.prop18 = omniData['playerId'];
	s.prop19 = omniData['regUserId'];

	s.prop20 = omniData['majorPageType'];
	s.prop21 = omniData['specificPageType'];
	s.prop22 = omniData['playersHighlighted'];
	s.prop23 = s.getQueryParam('loc');
	// populate the link location property
	s.prop24 = omniData['tourCastUser'];

	s.prop25 = omniData['courseId'];
	s.prop26 = omniData['statId'];

	// populate the campain
	s.campaign = s.getQueryParam('pgat_cid');

	// search string if available
	s.eVar3 = s.getQueryParam('search_string');
	
}


var omniData = new Array();
omniData = {
	path: null, // set automatically but we aren't sending to omniture
	server: null, // set automatically
	pageType: null, // only for 404 page
	pageName: null, // will be set by s.getPageName, can be overridden

	channel: null,
	prop1: null,
	prop2: null,
	prop3: null,
	prop4: null,
	prop5: null,

	majorPageType: null,
	specificPageType: null,

	articleTitle: null, // prop11
	articleAuthor: null, // prop12
	articleDate: null, // prop13

	year: null,
	tourCode: null,
	tournId: null,
	courseId: null,
	playerId: null,
	primaryTour: null,
	statId: null,
	holeNo: null,

	regUserFlag: null, // prop14, either "Registered" or "Casual"
	regUserId: null,
	tourCastUser: null,

	playersHighlighted: null
};

// sets path variable
// we don't send this to omniture but we use it
omniData['path'] = ( document.URL ).replace(/https?:\/\/([^\/]+)/, "");
omniData['path'] = ( omniData['path'] ).replace(/\/\//, "/");
if (omniData['path'].charAt(omniData['path'].length - 1) == "/") omniData['path'] += "index.html";
// make sure there's always a filename
if (omniData['path'].charAt(omniData['path'].length - 1) == "#") {
	omniData['path'] = omniData['path'].substr(0, omniData['path'].length - 1);
}  // take off any # from a href="#" links
var path_array = ( omniData['path'].substr(1) ).split("/");
var pL = path_array.length;
var lastPathNode = path_array[pL-1];

//remove the query strin from the last node in the path_array
if (lastPathNode.indexOf(location.search) > -1){
	path_array[pL-1] = lastPathNode.substr(0, lastPathNode.indexOf(location.search) );
}

// START FILLING ARRAY WITH CONTENT

// sets server variable
omniData['server'] = window.location.host;


/******************************
 START CATEGORIZING
 ******************************/

// fantasy.pgatour.com
if ( omniData['server'] == "fantasy.pgatour.com" ) {
		
	omniData['channel'] = "FANTASY";
	omniData['prop1'] = path_array[0];
	omniData['prop2'] = path_array[1];
	omniData['prop3'] = path_array[2];
	omniData['pageName'] = 'pgatour' + ':' + 'fantasy';
	
	if ( path_array[0] != "" ){
		for (i=0; i < pL; ++i) {	omniData['pageName'] += ':'+path_array[i];	}
	}
}

// Cookie based information
readCookiesInformation();


/******************************
 SETTING FUNCTIONS
 ******************************/

/** Sets the tournament central variables year, tourCode, tournId and courseId.
 * Those variables are generated and inserted into the page header.
 * 
 * Note that this function has to be called at the end of the page, not from
 * htmlHead (as this is inserted before the generated javascript)    
 */

/** Reads all cookie related information into omniData. */
function readCookiesInformation() {

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

	// check highlighted players
	var lb_saved_highlights = getCookieValue("lb_saved_highlights");
	var lb_highlights = getCookieValue("lb_highlights");
	omniData['playersHighlighted'] = (lb_saved_highlights != null || lb_highlights != null) ?
									 'Has Highlighted Players' : 'No Highlighted Players';

	// check whether the user signed into tourcast
	var pid = getCookieValue("pid");
	omniData['tourCastUser'] = (pid != null) ? "TOURCast User" : "Non TOURCast User";
}

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;
	}
}


/******************************
 HELPER FUNCTIONS
 ******************************/

/** Send microdata information
*
*
*/
function omniTrackLink(linkName) {
	var link_s = s_gi(s_account);
	link_s.linkTrackVars="None";
	link_s.linkTrackEvents="None";
	link_s.tl(this,'o',linkName);
}

/************************************
 *******  REFERRER FUNCTIONS ********
 ************************************/

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 translateTourCode( tc ){
	if (tc == 'r') return "PGA TOUR"
	else if (tc == 's') return "Champions Tour"
	else if (tc == 'h') return "Nationwide Tour"
	else return tc;
	
}