var cnn_omnitureData = new Array();
cnn_omnitureData = {
	pageName: null,
	section: null,
	path: null, // set automatically
	pageType: null,
	pageTopics: null,
	referrer: null,
	server: null // set automatically
}
cnn_omnitureData['path'] = cnn_omnitureGetPath();
cnn_omnitureData['pageName'] = cnn_omnitureGetPageName();
cnn_omnitureData['section'] = cnn_omnitureGetSection();
cnn_omnitureData['pageType'] = cnn_omnitureData['section'] + ' - ' + cnn_omnitureGetPageType();
cnn_omnitureData['referrer'] = cnn_omnitureGetReferrer();
cnn_omnitureData['server'] = document.domain;
//cnn_omnitureData['path'] = "worldcup.si.com/2010/06/18/united-states-2-slovenia-2/";
//cnn_omnitureData['pageName'] =  "SI Blogs - World Cup - Home";
//cnn_omnitureData['section'] = "SI Blogs"; 
//cnn_omnitureData['pageType'] = "SI Blogs - World Cup";
//cnn_omnitureData['referrer'] = "xid/eref things"
//cnn_omnitureData['server'] = "worldcup.si.com";
//alert(cnn_omnitureData['path']);
//alert(cnn_omnitureData['pageName']);
//alert(cnn_omnitureData['section']);
//alert(cnn_omnitureData['pageType']);
//alert(cnn_omnitureData['referrer']);
//alert(cnn_omnitureData['server']);



function cnn_omnitureGetPath() {
	//finds url
	retValue = document.location.hostname;
	//i.e. worldcup.si.com
	retValue += document.location.pathname;
	//gives /2010/06/18/united-states-2-slovenia-2/
	retValue += document.location.search;
	//will give any xid's, erefs, whatever.
	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
		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(){
	var retValue = 'SI Blogs';
	return retValue;
}

function cnn_omnitureGetPageType() {
	var host = location.hostname;
	//parse the subdomain
	var retDomain = host.split('.');
	var subDomain = retDomain[0];
	switch( subDomain ){
		//this is where you add new sites's name
		case 'worldcup': retValue = "Worldcup"; break;
		case 'joeposnanski': retValue = "Joe Posnanski"; break;
		case 'lebron-james-watch': retValue = "SI Lebron James Watch";break;
		case 'wp': retValue = "Testing";break;
		default: break;
	}
	return retValue;

}

function cnn_omnitureGetPageName() {
	var retValue = '';
	var tempValue = document.location.pathname;
	if( ( tempValue == '/' ) || ( tempValue.charAt( tempValue.length ) == "/" ) ) { tempValue += "index.htm"; }// make sure there's always a filename
	if(  tempValue == '/index.htm' ) { 
		retValue = 'SI Blogs - ' + cnn_omnitureGetPageType() + ' - Home'; 
	}
	else{
		var path_array = ( tempValue.substr( 1 ).split( "/" ));
		if(path_array[0].match( /^\d{4}/ ) ) {
			if(path_array[1] == '' || path_array[1] == 'undefined'){
				retValue = path_array[0] + ' - Archive';
			}
			else if(path_array[2] == '' || path_array[2] == 'undefined'){
				retValue = path_array[0] + '/' + path_array[1]  + ' - Archive';
			}
			else if(path_array[3] == '' || path_array[3] == 'undefined'){
				retValue = cnn_omnitureGetDate() + ' - Archive';
			}
			else{
				retValue = path_array[3];
			}
		}
		else if( (path_array[0] == "tag") || (path_array[0] == "category") ){
			switch(path_array[0])
			{
				case "tag": retValue = 'Tag - ' + path_array[1]; break;
				case "category": retValue =  'Category - ' + path_array[1]; break;
				default: retValue =  'Undefined Plugin Section'; break;
			}
		}
		else if( path_array[0] != '' || path_array[0] != 'undefined') {
			switch(path_array[0])
			{
				case "joes-words": retValue = 'Joes Words'; break;
				case "halls-of-fame": retValue = 'Halls of Fame'; break;
				case "glossary": retValue = 'Glossary'; break;
				case "contact": retValue = 'Contact'; break;
				case "books": retValue = 'Books'; break;
				case "joe": retValue = 'About Joe'; break;
				case "archive": retValue = 'Archive'; break;
				default: retValue = "Unknown Page"; break;
			}
		}
		else{
		retValue = "Unknown Page"
		}
	retValue ='SI Blogs - ' + cnn_omnitureGetPageType() + ' - ' + retValue;
	}
	return retValue;
	
}

function cnn_omnitureGetDate(){
	var retValue = '';
	var tempValue = document.location.pathname;
	var path_array = ( tempValue.substr( 1 ) ).split( "/" );
	retValue = path_array[0] + '/' + path_array[1] +  '/' + path_array[2];
	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( '<a name="cnnOmniture"></a>' );
	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_omnitureGetColumns();
	cnn_omnitureData['pageName'] = cnn_omnitureGetPageName();
	if( WM_readCookie( 'cnnad_omniture' ) == 'set' ) {
		writeData();
	}
}

function $c (className,tagName,node) {
	var classElements = new Array();
	if ( node == null ) node = document;
	if ( tagName == null ) tagName = '*';
	if (!node.getElementsByTagName) return false;
	var els = node.getElementsByTagName(tagName);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+className+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) { classElements[j] = els[i]; j++; }
	}
	return classElements;
}

function cnn_OmnitureGetHeadline() {
	var retValue = '';
	if( $c( 'siv_pageTitle', 'div' ) ) {
		var matchTag = /<(?:.|\s)*?>/gi;
		var counter = 0;
		var temp = '';
		for( classname in  $c('siv_pageTitle') ){
			if( counter == 0 ) {
				temp = $c('siv_pageTitle')[classname].innerHTML;
			}
			counter++;
		}
		retValue += ( ( temp.replace( /<h1>/i, "" ) ).replace( /<\/h1>/i, "" ) ).replace( matchTag, " " );
	}
	return retValue;
}
	
function cnnJS(src){
	siLog.debug('cnnJS: '+src);
	document.write('<scr'+'ipt src="'+src+'" type="text/javascript"></scr'+'ipt>'+"\n");
}

function cnnJSrun(js){
	document.write('<scr'+'ipt type="text/javascript">'+js+'</scr'+'ipt>'+"\n");
}



