/**
 * See doc for example usage
 * @version 20081120
 * @version 20081118
 * @version 20081104
 */

// For the Google engine
_uacct = "UA-1152868-2";


// For the Omniture engine (always include this file after including the omniture library)
com.mtvi.reporting.Account = {
	name:                    'vianeopetspetpetpark',
	dynamicAccountSelection: 'false',
	trackExternalLinks:      false,
	trackDownloadLinks:      false
};

/**
 * @var object A com.mtvi.reporting.Dispatcher object used for specialized AJAX requests. This must be global.
 */
var dispatcher = new com.mtvi.reporting.Dispatcher();
com.mtvi.config.qs=com.mtvi.util.queryStringToHash(window.location.search); 

//below are sample code to create, read, or delete cookies.

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


/**
 * Singleton object that provides a unified interface to multiple statistics engines
 */
PageStats = {
	/**
	 * @var object A collection of user information
	 */
	config: {
		area: "park", // ("mall" for ncmall pages)
		user: {
			age:    "unknown",
			gender: "unknown"
		}
	},


	/**
	 * Initialization goodness
	 */
	init: function() {
		dispatcher.setDefaultData();
		
		//This property (PageStats.config.user.justRegistered) does not currently exist.  
		//We need to set this variable when an user just completed registration, and 
		//carry it over to the next page for the regular page load reporting to pick it 
		//up and send it over to Omniture
		if(PageStats.config.user.justRegistered) events+=",event1";
		
		var xid = com.mtvi.config.qs.xid?com.mtvi.config.qs.xid:"";
		var events = "";
		if(xid.length>0){ 
			createCookie("xid",xid,90);
			dispatcher.setAttribute("eVar3", xid);
			events="event7";
		}
		
		xid=readCookie("xid");
		xid=(xid==null)?"":xid;
		if(xid.length>0) dispatcher.setAttribute("prop6",xid);
		dispatcher.setAttribute("events", events);
		
		// The dispatcher is already initialized globally
		var pagename = dispatcher.getAttribute("pageName");
		
		dispatcher.setAttribute("hier1", PageStats.config.area + "/" + pagename);
		dispatcher.setAttribute("channel", PageStats.config.area);
		
		dispatcher.setAttribute("pageName", "/" + PageStats.config.area + "/" + pagename);
		dispatcher.setAttribute("prop1", "");
		dispatcher.setAttribute("prop2", "");
		dispatcher.setAttribute("prop4", PageStats.config.user.age);
		dispatcher.setAttribute("prop5", PageStats.config.user.gender);
		dispatcher.setAttribute("trackInlineStats", true);
		dispatcher.send();
		
		PageStats.logPseudoPage("/" + PageStats.config.area + "/ " + pagename);
	},

	/**
	 * Updates user information
	 *
	 * @param object params An object with members `age` and `gender` ("male" or "female").<b>
	 */
	updateUser: function(params) {
		PageStats.config.user.age    = params.age    ? params.age    : "unknown";
		PageStats.config.user.gender = params.gender ? params.gender : "unknown";
	},

	
	/**
	 * Executes a log request with the specified mode.
	 *
	 * @param string event The event name to log
	 * @param object params Extra parameters (such as user params) expected to be in the same structure as PageStats.config
	 *
	 * @see PageStats.modes
	 */
	Event: function(event, params) {
		PageStats.logSiteEvent(event, params);
	},
	
	
	/**
	 * Logs a game play event (action).
	 *
	 * @param string id The game's unique ID
	 * @param string mode "multiplayer" or "singleplayer"
	 * @param object params Extra parameters (such as user params) expected to be in the same structure as PageStats.config
	 *
	 * @see PageStats.config
	 */
	GamePlay: function(id, mode, params) {
		// Only two valid modes
		if ( mode != "multiplayer" || mode != "singleplayer" ) {
			mode = "singleplayer";
		}

		// Set any params that are sent from the client
		if ( params ) {
			PageStats.updateUser(params);
		}

		dispatcher.setAttribute("prop1",    id);
		dispatcher.setAttribute("prop2",    mode);
		dispatcher.setAttribute("prop4",    PageStats.config.user.age);
		dispatcher.setAttribute("prop5",    PageStats.config.user.gender);
		dispatcher.setAttribute("pageName", "/gameplay-" + id + "-" + mode);
		dispatcher.setAttribute("hier1",    PageStats.config.area + "/gameplays/" + mode + "/" + id);
		dispatcher.send();
		
		PageStats.logPseudoPage("/gameplay/" + id + "/" + mode);
	},
	


	/**
	 * Executes a log request to the logPseudoPage stats mode. Currently just a wrapper.
	 *
	 * @param mixed event An object with member `page` or a string with the page to log.
	 */
	logPseudoPage: function(event) {
		urchinTracker(event);
	},


	/**
	 * Logs a site event (page).
	 *
	 * @param object event The event object with member `page.`
	 * @param object params Extra parameters (such as user params) expected to be in the same structure as PageStats.config
	 *
	 * @see PageStats.config
	 */
	logSiteEvent: function(event, params) {
		// Set any extra params that are sent from the client
		if ( params ) {
			PageStats.updateUser(params);
		}
		
		dispatcher.setAttribute("pageName", event);
		dispatcher.setAttribute("hier1",    PageStats.config.area + "/" + event.page);
		dispatcher.setAttribute("prop4",    PageStats.config.user.age);
		dispatcher.setAttribute("prop5",    PageStats.config.user.gender);
		dispatcher.send();
		
		PageStats.logPseudoPage(event);
	},


	/**
	 * Called on body.unload to signal the end of the user's serssion
	 */
	endSession: function() {
		var pagename = dispatcher.getAttribute("pageName");
		
		dispatcher.setAttribute("pageName", pagename + "-window_close");
		dispatcher.setAttribute("hier1",    PageStats.config.area + "/" + pagename + "-windowClose");
		//dispatcher.send();
		
		PageStats.logPseudoPage(pagename + "-window_close");
	}
};


// Add load/unload handler
dojo.addOnLoad( PageStats.init );
dojo.addOnUnload( PageStats.endSession );

