var defaultAccount=null;
function validateAccount(accountPassedFromFlash) {
	if (defaultAccount == null && cto != null) {
		defaultAccount=cto.account;
		omni_log("Using the default account from the page cto object="+defaultAccount);
	}
	// alot of content items initially have their account set as "disneycom" from the US content export
	// we should ignore it and use the one set in the page (which has similar logic programmed into the tea that renders it)
	if (accountPassedFromFlash == null || accountPassedFromFlash=="" || accountPassedFromFlash=="disneycom") {
		return defaultAccount;
	} else {
		return accountPassedFromFlash;
	}
}
// used by the flash to make sure the javascript is present and is the correct version
function flashCall_getVersion() {
	return "EMEA_0.1_AS3";
}
// this function called from the flash for each page view
function flashCall_pageView(pageName,siteSection,account,property,category) {
	//alert("flashCall_pageView " + pageName + ", " + siteSection + ", " + account + ", " + property);
	if (cto) {
		account = validateAccount(account);
		omni_log("pageView category="+category+" pageName="+pageName+" siteSection="+siteSection+" account="+account+" property="+property);
		cto.pageName= pageName;
		cto.channelName=pageName;
		cto.siteSection=siteSection;
		cto.account=account;
		cto.eventType="channelView";
		cto.property = property;
		cto.category = category;
		cto.ajaxSubmit();
	} else {
		omni_error("Error with analytics cto object is not defined on the page");
	}
	
}
// this function called from the flash for each link click
function flashCall_linkTrack(pageName,siteSection,linkId,linkPosition) {
	//alert("flashCall_linkTrack");
	if (cto) {
		omni_log("linkTrack pageName="+pageName+" linkId="+linkId+" linkPosition="+linkPosition);
		link =  {
					"href":linkId,
					"type":"",
					"name":linkPosition+linkId
		}
		cto.tracklink(link);
	} else {
		omni_error("Error with analytics cto object is not defined on the page");
	}
}
// this function called from the flash for "events" currently this is only game plays
function flashCall_eventTrack(eventType,pageName,assetId,property,reportingAssetName,account) {
	if (cto) {
		omni_log("eventTrack eventType="+eventType+" pageName="+pageName+" assetId="+assetId+" property="+property+" reportingAssetName="+reportingAssetName+" account="+account );
	
		account = validateAccount(account);
		cto.initFlashPV(); // Let the DIMG analytics layer know that we are going to send a flash PV
		
		var gameName= assetId+((reportingAssetName != null)?"_"+reportingAssetName:"");
		cto.pageName=  eventType+":"+gameName	;
		cto.contentType = eventType+"_content";
		cto.property = property;
		cto.trackFlashPV(); // Submit the PV data along with updated values for the properties.
		omni_log("eventTrack gameName="+gameName+" assetId="+assetId+" property="+property+" reportingAssetName="+reportingAssetName+" account="+account );
	} else {
		omni_error("Error with analytics cto object is not defined on the page");
	}
}


function getConsole(){
	//need to check for console this way, testing console==null throws a reference error if it's not defined - and that stops the script running
	if (window["console"] == null){
		window["console"] = {log:function(value){}, error:function(value){}};
	}
	return window["console"];
}

function omni_log(msg) {		
	getConsole().log("Omniture JS:"+msg);		
}
function omni_error(msg) {
	getConsole().error("Omniture JS:"+msg);
}

function flashCall_log(msg) {
	getConsole().log("Omniture SWF:"+msg);
}
function flashCall_error(msg) {
	getConsole().error("Omniture SWF:"+msg);
}

omni_log("EMEA Omniture code loaded ok javascript version = "+flashCall_getVersion());