/**
 * @file JavaScript library for MyGames SDK
 * @version $Revision: 1.0 $
 * @created April 19, 2007
 * @author bbasile
 *
 * This SDK supports IE6+ and Firefox 1.5+
 */
 
/**
* Global Constants
*/
var APP_STATE_OBJECT	= "RAComponents.RAAppState";
var RESOURCE_MGR_OBJECT	= "RAComponents.RAResourceMgr";
var DATASTORE_OBJECT	= "RADataStore.RADataSourceMgr";
var APP_STATE_CORE	= "RAComponents.RAAppCore";
var MY_GAMES		= "myGames";

/**
* Game List sort parameters
*/
var SORT_BY_GAMENAME	= "gamename";
var SORT_BY_GENRE	= "genre";
var SORT_BY_DEVELOPER	= "developer";
var SORT_BY_TIMEREM	= "MinutesPlayed";
var SORT_BY_LASTPLAY	= "LastPlayedDate";

/**
* This provides for cross-browser ActiveXObject instantiation
*/
var ActiveXObject = ActiveXObject;
if( navigator.userAgent.toLowerCase().indexOf( "firefox" ) != -1 ) {
	ActiveXObject = GeckoActiveXObject;
}

/**
* appCore_startUp MUST be called in window.onload
*/
function appCore_startUp() {
	try	{
		oAppCore = new ActiveXObject( APP_STATE_CORE );
		oAppCore.startUp();
		initializeApplication();
		return true;
	}
	catch(e) {
		return false;
	}
}

/**
* appCore_shutDown MUST be called in window.onunload
*/
function appCore_shutDown() {
	if( window.oAppCore ) {
		try {
			oAppCore.shutDown();
		}
		catch(e) {
			//alert( e );
		}
	}
}

/**
* initializeApplication is called in appCore_startUp
* This function loads all apis necessary to support
* game and game list objects
*/
function initializeApplication() {
	try {
		oAppState = new ActiveXObject( APP_STATE_OBJECT );
	}
	catch(e) {
		//alert( e );
	}

	try {
		oResourceManager = new ActiveXObject( RESOURCE_MGR_OBJECT );
	}
	catch(e) {
		//alert( e );
	}

	try {
		oDataSourceMgr = new ActiveXObject( DATASTORE_OBJECT );
		oDataSource = oDataSourceMgr.defaultDataSource;	
	}
	catch(e) {
		//alert( e );
	}
}

/**
* getGameFromGameList iterates through a game list array
* and returns the game object for the specified gameId, if
* it is installed on the user's machine
*/
function getGameFromGameList( gameList, gameId ) {
    try {
	    for ( i=0; i<gameList.count; i++ ){
		    var myGame = gameList.item(i);
		    //alert("Game: " + myGame.gameName);
		    if( myGame.gameid == gameId ){
			    return myGame;
		    }
	    }
	    return null;
	}
	catch(e) {	
		return null;
	}
}

/**
* sortGameList sorts a game list according to a specified
* parameter.  Supports sorting ascending or descending.
*/
function sortGameList( gameList, sortBy, sortAscending ) {
	try {
		gameList.sortOrderAscending = sortAscending;
		gameList.sort( sortBy );
	}
	catch(e) {
		return false;
	}
}

/**
* Constructor for the game list factory
*/
function myGamesListFactory() {}

/**
* Game list factory method - returns the My Games list
*/
myGamesListFactory.createMyGamesList = function () { 
	try {
		return oDataSource.loadGameList( MY_GAMES ); 
	}
	catch(e) {
		return null;
	}
}


/**
* Constructor for the game object factory
*/
function gameObjectFactory() {}

/**
* Game object factory method - returns game object for specified gameId
*/
gameObjectFactory.createGameObject = function ( gameId ) { 
	try {
		return getGameFromGameList( myGamesListFactory.createMyGamesList(), gameId );
	}
	catch(e) {
		return null;
	}
}
