var cnn_omnitureData = new Array();
cnn_omnitureData = {
	server: null, // set automatically
	path: null, // set automatically
	referrer: null,
	section: null,
	pageType: null,
	pageName: null,
	pageTopics: null
}

cnn_omnitureData['server'] = document.domain;
cnn_omnitureData['path'] = cnn_omnitureGetPath();
cnn_omnitureData['referrer'] = cnn_omnitureGetReferrer();
cnn_omnitureData['section'] = cnn_omnitureGetSection();
cnn_omnitureData['pageType'] = cnn_omnitureGetPageType();
cnn_omnitureData['pageName'] = cnn_omnitureGetPageName();
// cnn_omnitureData['pageTopics'] = cnn_omnitureGetPageTopics();

/* ========================================================================== */

function cnn_omnitureGetPath() {
	// return the path of the story
	var retValue = ( document.URL ).replace( /http:\/\/([^\/]+)/, "" ); // remove the hostname
	// retValue = ( retValue ).replace( /\/\//, "/" ); // get rid of double slashes
	// if( retValue.charAt( retValue.length - 1 ) == "/" ) retValue += "index.htm"; // make sure there's always a filename
	return retValue;
}

function cnn_omnitureGetReferrer() {
	// return a proper string for referrer if one exists
	var retValue = '';
	var myQuery = queryString( 'eref' );
	var myQuery2 = queryString( 'xid' );
	if( myQuery != "false" ) {
		retValue += "from " + myQuery;
		if( myQuery2 != "false" ) { retValue += ", xid " + myQuery2; }
		retValue += " - " + cnn_omnitureGetPath();
		retValue = retValue.replace( 'eref=' + myQuery, "" ); // remove our eref
		retValue = retValue.replace( 'xid=' + myQuery2, "" ); // remove our xid
		retValue = retValue.replace( /\?&$/, "" ); // remove ending question mark cause there are no more parameters
	} else if( myQuery2 != "false" ) {
		retValue += "xid " + myQuery2 + " - " + cnn_omnitureGetPath();
		retValue = retValue.replace( 'xid=' + myQuery2, "" ); // remove our xid
		retValue = retValue.replace( /\?$/, "" ); // remove ending question mark cause there are no more parameters
	}
	if( retValue == '' ) retValue = null;
	return retValue;
}

function cnn_omnitureGetSection() {
	// return the section name based on the path
	var retValue = '';
	if( ( window.location.protocol + '//' + window.location.host + window.location.pathname ) == 'http://vault.sportsillustrated.cnn.com/' ) { retValue = 'home'; }
	else if( location.hostname == 'siwiki.sportsillustrated.cnn.com' ) { retValue = 'wiki'; }
	else if( ( document.cnnAdPathArray[1] ).indexOf( 'reader' ) == 0 ) { retValue = 'reader'; }
	else { retValue = document.cnnAdPathArray[1]; }
	return retValue;
}

function cnn_omnitureGetPageType() {
	// return the page type, which is the section name plus 'Home' if home exists in the path, or 'Story'
	var retValue = cnn_omnitureGetSection();
	if( retValue == 'home' ) { retValue = 'main ' + retValue; }
	else if( retValue == 'reader' ) {}
	else if( retValue == 'wiki' ) {}
	else { retValue += ' ' + document.cnnAdPathArray[2]; }
	return retValue;
}

function cnn_omnitureGetPageName() {
	// return the page name, which is the pageType.  if the pageType is a story, add the path of the story
	var retValue = cnn_omnitureGetPageType();
	if( retValue == 'reader' ) { retValue += ' - ' + queryString( 'issueDate' ); }
	else if( retValue == 'wiki' ) { retValue += ' - ' + cnn_omnitureData['path']; }
	else if( retValue == 'swimsuit modelfeatured' ) { retValue +=  ' - ' + document.cnnAdPathArray[3] + ' ' + document.cnnAdPathArray[4] + ' ' + document.cnnAdPathArray[5] +' - ' + document.cnnAdPathArray[6]; }
	else if( retValue == 'swimsuit specialfeatured' ) { retValue += ' - ' + document.cnnAdPathArray[3] + ' ' + document.cnnAdPathArray[4] +' - ' + document.cnnAdPathArray[5]; }
	else if( retValue.indexOf( 'home' ) < 0 ) { retValue += ' - ' + document.cnnAdPathArray[3]; }
	return retValue;
}

function cnn_omnitureGetPageTopics() {
	var retValue = cnn_omnitureGetPageType() + ' - ';
	if( cnn_omnitureGetPageType() == 'swimsuit modelfeatured' ) { retValue += document.cnnAdPathArray[3] + ' ' + document.cnnAdPathArray[4] + ' ' + document.cnnAdPathArray[5] + ' - '; }
	else if( cnn_omnitureGetPageType() == 'swimsuit specialfeatured' ) { retValue += document.cnnAdPathArray[3] + ' ' + document.cnnAdPathArray[4] + ' - '; }
	if( document.sivTopics ) {
		if( ( document.sivTopics ).length >= 0 ) {
			retValue += ( document.sivTopics ).join( '|' );
		} else {
			retValue += 'none';
		}
	}
	return retValue;
}

/* ========================================================================== */

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 writeData() {
	document.write( '<table border="0" cellpadding="0" cellspacing="0" style="font-size:10px;">' );
	document.write( '<tr><td colspan="2" style="background-color:#f00;color:#fff;text-align:center;">Omniture Values</td></tr>' );
	for( key in cnn_omnitureData ) {
		document.write( '<tr><td>' + key + '</td><td>' + cnn_omnitureData[key] + '</td></tr>' );
	}
	document.write( '</table>' );
}

function cnn_omnitureWriteData() {
	cnn_omnitureData['pageTopics'] = cnn_omnitureGetPageTopics();
	if( WM_readCookie( 'cnnad_omniture' ) == 'set' ) {
		writeData();
	}
}
