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['pageName'] = cnn_omnitureGetPageName();
cnn_omnitureData['section'] = cnn_omnitureGetSection();
cnn_omnitureData['path'] = cnn_omnitureGetPath();
cnn_omnitureData['pageType'] = cnn_omnitureGetPageType();
cnn_omnitureData['pageTopics'] = cnn_omnitureGetColumns(); // michael won wants to change this s.prop completely
cnn_omnitureData['referrer'] = cnn_omnitureGetReferrer();
cnn_omnitureData['server'] = document.domain;

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

function cnn_omnitureGetPath() {
	// return the path of the story complete with parameters but without the starting /vault/
	var retValue = document.location.pathname;
	retValue = retValue.replace( /\/\/+/, "/" ); // remove multiple slashes
	if( ( retValue == '' ) || ( retValue.charAt( retValue.length - 1 ) == "/" ) ) retValue += "index.htm"; // make sure there's always a filename
	retValue = retValue.replace( /^\/vault\//, "" ); // remove beginning /vault/
	retValue = retValue.replace( /^\//, "" ); // remove beginning /
	retValue += document.location.search;
	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() {
	// return the section name based on the path
	var retValue = '';
	var tempValue = document.location.pathname;
	if( ( tempValue == '' ) || ( tempValue.charAt( tempValue.length - 1 ) == "/" ) )  tempValue += "index.htm"; // make sure there's always a filename
	tempValue = tempValue.replace( /^\/vault\//, "" ); // remove beginning /vault/
	tempValue = tempValue.replace( /^\//, "" ); // remove beginning /
	if( ( tempValue == 'index.htm' ) || ( tempValue == 'home/index.htm' ) ) { retValue = 'home'; }
	else if( ( document.cnnAdPathArray[1] ).indexOf( 'edb' ) == 0 ) { retValue = 'reader'; }
	else if( ( document.cnnAdPathArray[1] ).indexOf( 'special_features' ) == 0 ) { retValue = 'specials'; } // not in production
	else { retValue = document.cnnAdPathArray[1]; }
	return retValue;
}

function cnn_omnitureGetPageType() {
	// return the page type
	var retValue = cnn_omnitureGetSection();
	switch( retValue ) {
		case 'article':		if( document.cnnAdPathArray[2] != 'home' ) { // this is an index
								retValue += "|" + document.cnnAdPathArray[2];
							}
							break; // web|magazine
		case 'swimsuit':
							switch( document.cnnAdPathArray[2] ) {
								case 'model':			retValue += "|" + document.cnnAdPathArray[3];
														break; // model id
								case 'modelfeatured':	retValue += "|" + document.cnnAdPathArray[3];
														break; // model id
								case 'specialfeatured':	retValue += "|" + "|specials";
														break; // specials
								case 'video':			retValue += "|" + document.cnnAdPathArray[5];
														break; // model id

								default:				break;
							}
							break;
		default:			break;
	}
	if( ( retValue == cnn_omnitureGetSection() ) && ( retValue != "specials" ) ) retValue = null;
	else retValue = 'siv|' + retValue;
	return retValue;
}

function cnn_omnitureGetColumns() {
	// return the column
	var retValue = cnn_omnitureGetSection();
	switch( retValue ) {
		case 'article':		if( document.cnnAdPathArray[2] != 'home' ) { // ignore indices
								var tempTitle = ( document.title ).split( " - " ); // date is after the title, so do a switch
								retValue += "|" + document.cnnAdPathArray[3] + "|" + tempTitle[ tempTitle.length - 2 ] + "|" + ( tempTitle.slice( 0, tempTitle.length - 2 ) ).join( " - " );
							}
							break; // magazine id
		case 'gallery':		if( document.cnnAdPathArray[2] != 'home' ) { // ignore indices
								retValue += "|" + document.cnnAdPathArray[3] + "|" + ( document.title ).replace( / - SI Vault$/, "" ); // remove ending " - SI Vault"
							}
							break; // gallery number
		case 'topic':		retValue += "|" + document.cnnAdPathArray[3];
							break; // topic id
/* result is getting tracked in another prop
		case 'result':		retValue += "[" + queryString( 'query' ) + "]" + "[" + queryString( 'exactClause' ) + "]" + "[" + queryString( 'notClause' ) + "]" + "[" + queryString( 'orClause' ) + "]" + "[" + queryString( 'startDt' ) + "]" + "[" + queryString( 'endDt' ) + "]";
							break; // topic id
*/
		case 'specials':	retValue += "|" + document.cnnAdPathArray[2];
							break; // specials id
		case 'swimsuit':
							switch( document.cnnAdPathArray[2] ) {
								case 'modelfeatured':	retValue += "|" + document.cnnAdPathArray[3] + "|" + document.cnnAdPathArray[4] + "|" + document.cnnAdPathArray[5];
														break; // model + year + gallery id
								case 'specialfeatured':	retValue += "|" + document.cnnAdPathArray[3] + "|" + document.cnnAdPathArray[4];
														break; // year + gallery id
								case 'video':			retValue += "|" + document.cnnAdPathArray[3];
														break; // video
								default:				break;
							}
							break;
		default:			break;
	}
	if( retValue == cnn_omnitureGetSection() ) retValue = null;
	else retValue = 'siv|' + retValue;
	return retValue;
}

function cnn_omnitureGetPageName() {
	// return the page name
	var retValue = cnn_omnitureGetSection();
	switch( retValue ) {
		case 'home':		break; // specials id
		case 'article':		if( document.cnnAdPathArray[2] == 'home' ) { // this is an index
								retValue += "|" + document.cnnAdPathArray[2] + "|" + document.cnnAdPathArray[3] + document.cnnAdPathArray[4];
							} else { // is an actual article
								var tempTitle = ( document.title ).split( " - " ); // date is after the title, so do a switch
								retValue += "|" + document.cnnAdPathArray[3] + "|" + tempTitle[ tempTitle.length - 2 ] + "|";
								if( document.cnnAdPathArray[4] == "index.htm" ) {
									retValue += "1";
								} else {
									retValue += document.cnnAdPathArray[4];
								}
								retValue += "|" + ( tempTitle.slice( 0, tempTitle.length - 2 ) ).join( " - " );
							}
							break; // magazine number + date + title
		case 'cover':		if( document.cnnAdPathArray[2] == 'home' ) { // this is an index
								retValue += "|" + document.cnnAdPathArray[2];
							} else if( document.cnnAdPathArray[2] == 'select' ) { // this is a search
								retValue += "|" + document.cnnAdPathArray[2] + "|" + document.cnnAdPathArray[3] + "-" + document.cnnAdPathArray[4] + "|" + document.cnnAdPathArray[5] + document.cnnAdPathArray[6];
							} else { // is an actual cover
								var tempTitle = ( document.title ).split( " - " ); // date is after the title, so do a switch
								retValue += "|" + document.cnnAdPathArray[3] + "|" + tempTitle[ tempTitle.length - 2 ] + "|" + ( tempTitle.slice( 0, tempTitle.length - 2 ) ).join( " - " );
							}
							break; // cover number + date + title
		case 'reader':		var tempTitle = queryString( 'issueDate' ); if( tempTitle == "false" ) tempTitle = "unknown issue";
							retValue += "|" + tempTitle;
							break; // issue number
		case 'gallery':		if( document.cnnAdPathArray[2] == 'home' ) { // this is an index
								retValue += "|" + document.cnnAdPathArray[2] + "|" + document.cnnAdPathArray[3] + document.cnnAdPathArray[4];
							} else { // is an actual gallery
								retValue += "|" + document.cnnAdPathArray[3] + "|" + document.cnnAdPathArray[4] + "|" + ( document.title ).replace( / - SI Vault$/, "" ); // remove ending " - SI Vault"
							}
							break; // gallery number + gallery name
		case 'video':		if( document.cnnAdPathArray[2] == 'home' ) { // this is an index
								retValue += "|" + document.cnnAdPathArray[2];
							}
							break; // cover number + date + title
		case 'topic':		retValue += "|" + document.cnnAdPathArray[3] + "|" + document.cnnAdPathArray[2] + "|";
							if( document.cnnAdPathArray[6] == "index.htm" ) {
								retValue += "1";
							} else {
								retValue += document.cnnAdPathArray[6];
							}
							if( document.cnnAdPathArray[2] != 'video' ) {
								if( document.cnnAdPathArray[7] == "index.htm" ) {
									retValue += "|1";
								} else {
									retValue += "|" + document.cnnAdPathArray[7];
								}
							} // video doesn't have a subset
							break; // topic id
		case 'result':		var tempValue = "result";
							if( ( queryString( 'exactClause' ) != "" ) || ( queryString( 'notClause' ) != "" ) || ( queryString( 'orClause' ) != "" ) || ( queryString( 'startDt' ) != "" ) || ( queryString( 'endDt' ) != "" ) ) {
								tempValue = "refine";
							}
							retValue += "|" + tempValue;
/*
							retValue += "|" + "[" + queryString( 'query' ) + "]" + "[" + queryString( 'exactClause' ) + "]" + "[" + queryString( 'notClause' ) + "]" + "[" + queryString( 'orClause' ) + "]" + "[" + queryString( 'startDt' ) + "]" + "[" + queryString( 'endDt' ) + "]" + " " + document.cnnAdPathArray[2] + " ";
							if( ( document.cnnAdPathArray[2] == 'gallery' ) || ( document.cnnAdPathArray[2] == 'cover' ) ) { // gallery and cover don't have a subset
								retValue += queryString( 'sortBy' ) + " " + queryString( 'currentPage' );
							} else if( document.cnnAdPathArray[2] == 'video' ) { // video doesn't have a sort by or subset
								retValue += queryString( 'currentPage' );
							} else {
								retValue += queryString( 'sortBy' ) + " " + queryString( 'searchType' ) + " " + queryString( 'currentPage' );
							}
*/
							break; // result id
		case 'specials':	retValue += "|" + document.cnnAdPathArray[2] + "|" + cnn_omnitureGetPath();
							break; // specials id
		case 'swimsuit':	switch( document.cnnAdPathArray[2] ) {
								case 'allmodel':		retValue += "|" + document.cnnAdPathArray[2] + "|";
														if( document.cnnAdPathArray[3] == "index.htm" ) {
															retValue += "1";
														} else {
															retValue += document.cnnAdPathArray[3];
														}
														break; // pagination
								case 'model':			retValue += "|" + document.cnnAdPathArray[3] + "|" + document.cnnAdPathArray[4];
														break; // model id
								case 'year':			retValue += "|" + document.cnnAdPathArray[3] + "|" + document.cnnAdPathArray[4];
														break; // year
								case 'modelfeatured':	var tempValue = document.cnnAdPathArray[6];
														if( tempValue == "index.htm" ) tempValue = 1;
														retValue += "|gallery|" + document.cnnAdPathArray[3] + "|" + document.cnnAdPathArray[4] + "|" + document.cnnAdPathArray[5] + "|" + tempValue;
														break; // model + year + gallery id + pagination
								case 'video':			retValue += "|" + document.cnnAdPathArray[2] + "|" + document.cnnAdPathArray[3];
														break; // video id
								case 'specialfeatured':	var tempValue = document.cnnAdPathArray[5];
														if( tempValue == "index.htm" ) tempValue = 1;
														retValue += "|gallery|" + document.cnnAdPathArray[3] + "|" + document.cnnAdPathArray[4] + "|" + tempValue;
														break; // year + gallery id + pagination
								case 'allvideo':		retValue += "|" + document.cnnAdPathArray[2] + "|";
														if( document.cnnAdPathArray[3] == "index.htm" ) {
															retValue += "1";
														} else {
															retValue += document.cnnAdPathArray[3];
														}
														break; // pagination
								case 'videoyear':		retValue += "|" + document.cnnAdPathArray[2] + "|" + document.cnnAdPathArray[3];
														break; // pagination
								case 'cover':			if( document.cnnAdPathArray[3] == 'coverfeatured' ) { // is an actual cover
															var tempTitle = ( document.title ).split( " - " ); // date is after the title, so do a switch
															retValue += "|" + document.cnnAdPathArray[4] + "|" + tempTitle[ tempTitle.length - 2 ] + "|" + ( tempTitle.slice( 0, tempTitle.length - 2 ) ).join( " - " );
														} else { // this is a search
															retValue += "|cover|select|" + document.cnnAdPathArray[3] + "-" + document.cnnAdPathArray[4] + "|" + document.cnnAdPathArray[5] + "|";
															if( document.cnnAdPathArray[6] == "index.htm" ) {
																retValue += "1";
															} else {
																retValue += document.cnnAdPathArray[6];
															}
														}
														break; // cover number + date + title
								case 'coverfeatured':	var tempTitle = ( document.title ).split( " - " ); // date is after the title, so do a switch
														retValue += "|cover|" + document.cnnAdPathArray[3] + "|" + tempTitle[ tempTitle.length - 1 ] + "|" + ( tempTitle.slice( 1, tempTitle.length - 1 ) ).join( " - " );;
														break; // year + gallery id + pagination
								default:				retValue += "|" + document.cnnAdPathArray[2];
														break;
							}
							break;
		default:			retValue = "unaccounted|" + cnn_omnitureGetPath();
							break;
	}
	retValue = 'siv|' + retValue;
	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( '<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();
	if( WM_readCookie( 'cnnad_omniture' ) == 'set' ) {
		writeData();
	}
}

