/* 
|	 
|	ra.appCore.js
|
*/

/* 
|	 
|	embed the codewalla plugin, where supported
|
*/
if( navigator.plugins && navigator.mimeTypes ){
	try {
		var plugin = navigator.mimeTypes["application/realarcade-npapi-scriptable-plugin"];
	    if(plugin){
	    	document.write('<embed id="RANPAPIPlugin" type="application/realarcade-npapi-scriptable-plugin" pluginspage="http://games-dl2.real.com/gameconsole/v3/npapiplugin/RAPluginInstaller.exe" width="0" height="0"></embed>');
	    }
	}
	catch(e) {
		//alert(e);
	}
}

var ActiveXObject = ActiveXObject;
if( (navigator.userAgent.toLowerCase().indexOf( "chrome" ) != -1) || (navigator.userAgent.toLowerCase().indexOf( "firefox" ) != -1) ) {
	ActiveXObject = NPPluginObject;
}

function NPPluginObject( className ) {
	try {
		var raNPAPIPlugin = document.getElementById( 'RANPAPIPlugin' );
		
		raNPAPIPlugin.onGamePlayFinished = "onGamePlayFinish";
		raNPAPIPlugin.onGameInstalled = "onGameInstalled";
		raNPAPIPlugin.onGameUninstalled = "onGameUninstalled";
		raNPAPIPlugin.onGamePurchased = "onGamePurchased";
		raNPAPIPlugin.onGameReturned = "onGameReturned";	
		
		return raNPAPIPlugin.getRANPObject( className );
	}
	catch(e) {
		return false;
	}
}

dojo.addOnLoad(appCore_startUp);
dojo.addOnUnload(appCore_shutDown);

function appCore_startUp() {
	if( "fullclient" == realArcade.ractype && realArcade.racInstalled ) {
		try	{
			oAppCore = new ActiveXObject( APP_STATE_CORE );
			oAppCore.startUp();
			initializeApplication();
		}
		catch(e) {
			//alert( createErrorMessage( ERROR_CODE_APP_STATE_CORE, ERROR_MSG_CREATE_ACTIVEX ) + "\n" + e );
			window.onerror = function() { return true; };
		}
	}
}

function appCore_shutDown() {
	if( detectV2Rac( createArcadeV2VersionControl() ) && window.oAppCore ) {
		try {
			oAppCore.shutDown();
		}
		catch(e) {
			//alert( createErrorMessage( ERROR_CODE_APP_STATE_CORE, ERROR_MSG_CLOSING_APP ) + "\n" + e );
		}
	}
}

function initializeApplication() {
	try {
		oAppState = new ActiveXObject( APP_STATE_OBJECT );
	}
	catch(e) {
		//alert( createErrorMessage( ERROR_CODE_APP_STATE, ERROR_MSG_CREATE_ACTIVEX ) );
	}

	try {
		oResourceManager = new ActiveXObject( RESOURCE_MGR_OBJECT );
	}
	catch(e) {
		//alert( createErrorMessage( ERROR_CODE_RESOURCE_MGR, ERROR_MSG_CREATE_ACTIVEX) );
	}

	try {
		var oLocalizedStrings = oResourceManager.loadLocalizedStrings();
		oLocText = oLocalizedStrings.getScopedValues( LOC_STRING_CONTEXTID );
	}
	catch(e) {
		//alert( createErrorMessage( ERROR_CODE_LOC_STRINGS, ERROR_MSG_CRITICAL_FAIL ) );
	}

	try {
		oDataSourceMgr = new ActiveXObject( DATASTORE_OBJECT );
		oDataSource = oDataSourceMgr.defaultDataSource;	
	}
	catch(e) {
		//alert( createErrorMessage( ERROR_CODE_DATA_SOURCE_MGR, ERROR_MSG_CREATE_ACTIVEX ) );
	}

	try {
		configureMgr = new ActiveXObject( CONFIG_MGR_OBJECT );
	}
	catch(e) {
		//alert( createErrorMessage( ERROR_CODE_CONFIGURE_MGR, ERROR_MSG_CREATE_ACTIVEX ) );
	}

	try {
		raUserObject = new ActiveXObject( RA_USER_OBJECT );
	}
	catch(e) {
		//alert( createErrorMessage( ERROR_CODE_USER_OBJ, ERROR_MSG_CREATE_ACTIVEX ) );
	}
}

/* 
|	 
|	Plugin event handlers for Firefox & Chrome
|
*/
function onGamePlayFinish( productId, exitCode )
{
	try {
		dojo.event.topic.publish( "topic-onGamePlayFinished", productId, exitCode );
	}
	catch(e) {
		//alert( "topic-onGamePlayFinished publish failed" );
	}		
	//alert( "onGamePlayFinish : " + productID + "  : " + exitCode );
	return 0;
}

function onGameInstalled( productId, exitCode )
{
	try {
		dojo.event.topic.publish( "topic-onGameInstalled", productId, exitCode );
	}
	catch(e) {
		//alert( "topic-onGameInstalled publish failed" );
	}	
	//alert( "onGameInstalled : " + productID + "  : " + exitCode );
	return 0;
}

function onGameUninstalled( productId, exitCode )
{
	try {
		dojo.event.topic.publish( "topic-onGameUninstalled", productId, exitCode );
	}
	catch(e) {
		//alert( "topic-onGameUninstalled publish failed" );
	}	
	//alert( "onGameUninstalled : " + productID + "  : " + exitCode );
	return 0;
}

function onGamePurchased( productId, exitCode )
{
	try {
		dojo.event.topic.publish( "topic-onGamePurchased", productId, exitCode );
	}
	catch(e) {
		//alert( "topic-onGamePurchased publish failed" );
	}
	//alert( "onGamePurchased : " + productID + "  : " + exitCode );
	return 0;
}

function onGameReturned( productId, exitCode )
{
	try {
		dojo.event.topic.publish( "topic-onGameReturned", productId, exitCode );
	}
	catch(e) {
		//alert( "topic-onGameReturned publish failed" );
	}	
	//alert( "onGameReturned : " + productID + "  : " + exitCode );
	return 0;
}
