/**
 * Load Coremetrics Sources
 */
var cmJsHost = (("https:" == document.location.protocol) ? "https://" : "http://");
document.write(unescape("%3Cscript src='" + cmJsHost + "www.victoriassecret.com/coremetrics/v40/eluminate.js' type='text/javascript'%3E%3C/script%3E"));
document.write(unescape("%3Cscript src='" + cmJsHost + "www.victoriassecret.com/coremetrics/cmdatatagutils.js' type='text/javascript'%3E%3C/script%3E"));

/**
 * Load Google Analytics Sources
 */
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


var PNMember = false;
/**
 * startAnalytics
 * initialize all necessary analytics packages
 * (note: keep calling self until sources are ready)
 *
 * @param void
 * @return void
 */
var analyticsReady = false;
function startAnalytics(){
	if (((typeof cmCreatePageviewTag) == 'function') && ((typeof _gat) == 'object')){
		// prepare google analytics object
		var gaProfile;
		var name = "kiosk";
		var kioskMode = false;

		var ca = document.cookie.split(';');
		for (var i=0; i<ca.length; i++)
		{
			if (ca[i].indexOf("kiosk=1") != -1)
			{
				kioskMode = true;
				break;
			}
		}

		if (kioskMode == true)
		{
			gaProfile = ((document.location.host.match(/^local\./) || document.location.host.match(/^dev\./) || document.location.host.match(/^stage\./)) ? "UA-1458228-4" : "UA-1458228-6");
		}
		else
		{
			gaProfile = ((document.location.host.match(/^local\./) || document.location.host.match(/^dev\./) || document.location.host.match(/^stage\./)) ? "UA-1458228-4" : "UA-1458228-1");
		}

		pageTracker = _gat._getTracker(gaProfile);
		
		if (!document.location.host.match(/^local\./) && !document.location.host.match(/^dev\./) && !document.location.host.match(/^stage\./))
		{
			pageTracker._setDomainName("vspink.com");
		}
		if (PNMember)
		{
			pageTracker._setVar("PN_members");
		}
		pageTracker._initData();

		// mark as ready
		analyticsReady = true;
	}
	else setTimeout(startAnalytics, 250);
}

/**
 * recordAnalytics
 * record all necessary analytics for a state change
 * (note: keep calling self until sources are ready)
 *
 * example usage (use ga, cm or both):
 * 		recordAnalytics(
 *			{
 *				'ga': {
 *					'tag': 'whatever/you/need/to/track'		// required
 *				},
 *				'cm': {
 *					'pageid': 'your/tag/here',				// required
 *					'searchstring':	'your search string',	// optional
 *					'categoryid': 'your_category_id'		// optional
 *				},
 *				'url': 'http://some.place.to/go/after'		// optional
 *			}
 *		);
 *
 * @param object
 * @return boolean (true if no url)
 */
function recordAnalytics(obj){
	
	 if (analyticsReady){
		 if ((typeof obj.cm) != 'undefined'){
			((typeof obj.cm.searchstring) != 'undefined') ? obj.cm.searchstring : null
			cmCreatePageviewTag(
				obj.cm.pageid,
				((typeof obj.cm.searchstring) != 'undefined') ? obj.cm.searchstring : null,
				((typeof obj.cm.categoryid) != 'undefined') ? obj.cm.categoryid : null
			);
		}
		 
		if ((typeof obj.ga) != 'undefined')
		{	
			pageTracker._trackPageview(obj.ga.tag);
		}
				
		if ((typeof obj.url) != 'undefined'){
			location.href = obj.url;
			return(false);
		}
		else return(true);
	}
	else{
		setTimeout(function(){recordAnalytics(obj)}, 250);
		if ((typeof obj.url) != 'undefined') return(false);
		else return(true);
	}
}

/**
 * initialize analytics libraries/functions
 */
addEvent(window, 'load', startAnalytics);

