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

// path - add 'index.html' if the path ends with a slash and doesn't have a filename.  also set up an array representation of the path
cnn_omnitureData['path'] = document.location.pathname;
if( cnn_omnitureData['path'].charAt( cnn_omnitureData['path'].length - 1 ) == "/" ) cnn_omnitureData['path'] += "index.html";
var path_array = ( cnn_omnitureData['path'].substr( 1 ) ).split( "/" );
cnn_omnitureData['path'] += document.location.search;

// server
cnn_omnitureData['server'] = document.location.host;

// section
cnn_omnitureData['section'] = 'DP';
if( path_array[1] == 'index.html' ) { // if the path is the index, the page is the front
	cnn_omnitureData['section'] += ' Site Front';
} else if( path_array[1].indexOf( 'listenlive.player.html' ) == 0 ) { // if the path is the index, the page is the front
	cnn_omnitureData['section'] += ' Listen Live';
} else if( path_array.length == 3 ) { // else if there is a subfolder, the subfolder represents the section
	switch( path_array[1] ) {
		case 'aboutthemusic':	cnn_omnitureData['section'] += ' About the Music'; break;
		case 'affiliateinfo':	cnn_omnitureData['section'] += ' Affiliate Info'; break;
		case 'contactdan':	cnn_omnitureData['section'] += ' Contact Dan'; break;
		case 'dpdictionary':	cnn_omnitureData['section'] += ' Dictionary'; break;
		case 'fancomments':	cnn_omnitureData['section'] += ' Fan Comments'; break;
		case 'interviews':	cnn_omnitureData['section'] += ' Interview'; break;
		case 'podcasts':	cnn_omnitureData['section'] += ' Podcasts'; break;
		case 'showarchive':	cnn_omnitureData['section'] += ' Show Archive'; break;
		case 'showblog':	cnn_omnitureData['section'] += ' Show Blog'; break;
		case 'storyideas':	cnn_omnitureData['section'] += ' Story Ideas'; break;
		case 'streamingaudio':	cnn_omnitureData['section'] += ' Streaming Audio'; break;
		case 'tunein':		cnn_omnitureData['section'] += ' Tune In'; break;
		case 'upcomingshows':	cnn_omnitureData['section'] += ' Upcoming Shows'; break;
		case 'videos':		cnn_omnitureData['section'] += ' Videos'; break;
		default:		cnn_omnitureData['section'] += ' Unknown Section'; break;
	}
} else {
	cnn_omnitureData['section'] += ' Unknown Page';
}

// pageType - if the path is the index of the section, the page is the front of the section, otherwise it's a page of the section
cnn_omnitureData['pageType'] = cnn_omnitureData['section'];
if( path_array[1] == 'index.html' ) {
} else if( path_array.length == 3 ) {
	if( path_array[2] == 'index.html' ) {
		cnn_omnitureData['pageType'] += ' Front';
	} else {
		cnn_omnitureData['pageType'] += ' Page';
	}
}

// eref/xid - if eref exists, fill in referrer
if( queryString( 'eref' ) != "false" ) {
	cnn_omnitureData['referrer'] = "from " + queryString( 'eref' );
	if( queryString( 'xid' ) != "false" ) {
		cnn_omnitureData['referrer'] += ", xid " + queryString( 'xid' );
	}
	cnn_omnitureData['referrer'] += " to " + cnn_omnitureData['path'];
} else if( queryString( 'xid' ) != "false" ) {
	cnn_omnitureData['referrer'] = "from xid " + queryString( 'xid' ) + " to " + cnn_omnitureData['path'];
}

// pageName - section front plus the filename
cnn_omnitureData['pageName'] = cnn_omnitureData['section'];
if( path_array[1] == 'index.html' ) {
} else if( path_array.length == 3 ) {
	if( path_array[2] == 'index.html' ) {
		cnn_omnitureData['pageName'] += ' Front';
	} else {
		cnn_omnitureData['pageName'] += ' - ' + path_array[2];
	}
} else {
		cnn_omnitureData['pageName'] += ' - ' + ( path_array.slice( 1, path_array.length ) ).join( '/' );
}

/******************************
 HELPER 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 writeData() {
	document.write( '<table border="0" cellpadding="0" cellspacing="0" style="border:1px solid #f00;font-size:10px;margin:auto;">' );
	document.write( '<tr><td colspan="2" style="background-color:#f00;color:#fff;text-align:center;">testing omniture, will not show up to user once I place the cookie work</td></tr>' );
	for( var x in cnn_omnitureData ) {
		document.write( '<tr><td>' + x + ':&#160;</td><td>' + cnn_omnitureData[x] + '</td></tr>' );
	}
	document.write( '</table>' );
}
