//global parameters
var g_bIsIE = (-1 != navigator.userAgent.indexOf("MSIE")); // are we using FF or IE
var g_ExentCtl = null;

//return array that contains all ready to play games.
//every cell array contains Game object
function GetMyGames()
{
   var arrGames = new Array();
   var arrGameIds = GPlayerApi_GetGamesIdsList( 143, false, false, true, false, "None", true, false);
   
   //for each game id we need to create game object with its data
   for ( var i = 0; i < arrGameIds.length; i++ )
   {
      var game = new Game(arrGameIds[i]);
      game.LoadData();
      arrGames[i] = game;
   }
   return arrGames;
}

//Class game
function Game( gameId )
{
   this.id = gameId;
   this.name = "";
   this.gamePageUrl = "";
   
   var that = this; //private member.  We need it in order to be able access this from private functions (see http://www.crockford.com/javascript/private.html)
   
   Game.prototype.LoadData = LoadData;
   this.GetGamePageUrl = GetGamePageUrl;
   
   //load data for the game object
   function LoadData()
   {
      this.name = GetGameName( this.id );
      //that = this;
      this.gamePageUrl = GetGamePageUrl();
   }
   
   function GetGamePageUrl()
   {
      var strGamePageUrl = "http://www.freeridegames.com/games/game?partner=Default";
      strGamePageUrl += "&game=" + that.id;
      strGamePageUrl += "&gameName=" + that.name.replace(/ /g, "-");
      return strGamePageUrl;
   }
}


//Return game name using the client
//if any error occurs, return ""
function GetGameName(strGameId, strMDId, strDefault)
{
   var strGameName = "";
   var strRequest = "<GetGameMDRequest>" +
                  "<ContentDescriptor Id=\"" + strGameId + "\" ProviderId=\"143\" >" +
                     "<MD Id=\"ProviderName\" Default=\"NoName\"/>" +
                  "</ContentDescriptor>" +
               "</GetGameMDRequest>";

   // Dispatch request.
   try
   {
      var strXmlResponse = DispatchXmlRequest( strRequest );
      var xmlDoc = GetXMLDocument();
      xmlDoc.async = "false";
      xmlDoc.loadXML( strXmlResponse );  
      
      var strQuery = "ClientInformation/Information/GetGameMDResponse/ContentDescriptor//MD[@Id='ProviderName']";
      var Node = xmlDoc.selectSingleNode( strQuery );      
      strGameName = Node.getAttribute("Value");
   }
   catch(e){}
   
   return strGameName;
   
   //return DispatchRequestAndReturnPlayerXml( strRequest, "GetGameMDResponse" );
}

//return array will ids of all games on the user's computer that are ready to play.
//in case the user doesn't any games or client installed, return empty array.
function GPlayerApi_GetGamesIdsList( strProviderId,
                                     bActiveGamesOnly,
                                     bInDownloadListOnly,
                                     bReadyToUseOnly,
                                     bNotInDownloadList,
                                     strSortBy,
                                     bCurrentOSOnly,
                                     bCacheUpdating)
{
   var arrGameIds = new Array();
   
   var strActiveGamesOnly = "0";
   if( bActiveGamesOnly )
   {
      strActiveGamesOnly = "1";
   }

   var strInDownloadListOnly = "0";
   if( bInDownloadListOnly )
   {
      strInDownloadListOnly = "1";
   }

   var strReadyToUseOnly = "0";
   if( bReadyToUseOnly )
   {
      strReadyToUseOnly = "1";
   }

   var strNotInDownloadList = "0";
   if( bNotInDownloadList )
   {
      strNotInDownloadList = "1";
   }

   var strCurrentOSOnly = "0";
   if( bCurrentOSOnly )
   {
      strCurrentOSOnly = "1";
   }

   var strCacheUpdating = "0";
   if(bCacheUpdating)
   {
      strCacheUpdating = "1";
   }

   var strRequest = "<GetGamesIdsListRequest ProviderId=\"" + strProviderId +
                                         "\" ActiveGamesOnly=\"" + strActiveGamesOnly +
                                         "\" InDownloadListOnly=\"" + strInDownloadListOnly +
                                         "\" ReadyToUseOnly=\"" + strReadyToUseOnly +
                                         "\" NotInDownloadList=\"" + strNotInDownloadList +
                                         "\" CurrentOSOnly=\"" + strCurrentOSOnly +
                                         "\" CacheUpdating=\"" + strCacheUpdating +
                                         "\" SortBy=\"" + strSortBy + "\"/>";

  
   // Dispatch request.
   var strXmlResponse = DispatchXmlRequest( strRequest );

   var xmlDoc = GetXMLDocument();
   xmlDoc.async = false;
   xmlDoc.loadXML( strXmlResponse );
   var strQuery = "ClientInformation/Information/GetGamesIdsListResponse/ContentDescriptor";
   var nodeList = xmlDoc.selectNodes( strQuery );
   
   for( var i = 0; i < nodeList.length ; i++ )
   {
      var nodeContent = nodeList[i];
      if( nodeContent )
      {
         var strGameID = nodeContent.getAttribute( "Id" );
         if( "0" != strGameID )
         {
            arrGameIds[arrGameIds.length] = strGameID;
         }
      }
   }

   return arrGameIds;
}

//=================================================================
//Function name:  DispatchXmlRequest
//
//Description:    Function dispatchs xml request to Exent OCX.
//
//Parameters:     strRequest
//
//Result:         Response XML string.
//                In case of an error return empty string
//=================================================================
function DispatchXmlRequest( strRequest )
{
   try
   {
      var strResponse = GetExentCtlInstance().Invoke( strRequest );
   }
   catch( e )
   {
      strResponse = "";
   }
   
   return strResponse;
}

function GetExentCtlInstance( )
{
   // Get Exent ActiveX object instance.
   if( null != g_ExentCtl )
   {
      return g_ExentCtl;
   }

   var tempCtl = GetExentCtlInstanceInternal();
   g_ExentCtl = tempCtl;

   return g_ExentCtl;
};

function GetExentCtlInstanceInternal()
{
   // Get get Exent instance.
   try
   {
      if( null == g_ExentCtl )
      {
         g_ExentCtl = document.EXENTCTL;
         if( null == g_ExentCtl )
         {
            g_ExentCtl = parent.document.EXENTCTL;
         }
         if( null == g_ExentCtl )
         {
            g_ExentCtl = top.document.EXENTCTL;
         }
      }
   }
   catch( e )
   {
      return null;
   }
   return g_ExentCtl;
}


//return the right xml doc for accrding to browser.
function GetXMLDocument( )
{
  
   var xmlDoc;
   /*
   if (window.DOMParser)
   {
      parser=new DOMParser();
      xmlDoc=parser.parseFromString(strXml,"text/xml");
   }
   else // Internet Explorer
   {
      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async="false";
      xmlDoc.loadXML(strXml);
   }*/
   
   if( g_bIsIE ) // In case of IE
   {
      xmlDoc = new ActiveXObject( "Msxml2.DOMDocument" );
   }
   else // In case of FireFox
   {
      xmlDoc = zXmlDom.createDocument();
   }
   
   return xmlDoc;
}

function GPlayerApi_LoadActiveX()
{
   var str="";
   
   var strActiveXVersion = "7,1,0,0"; 
   strParamsForActiveX = "<param name='player_skin_id' value='000005'><param name='player_style_id' value='1'><param name='player_provider_id' value='143'>"
   
   // we assume that if we aren't running internet explorer, we should support gecko plugin
   if (g_bIsIE)
   {
      str = "<object codebase='' CLASSID='clsid:6A060448-60F9-11D5-A6CD-0002B31F7455' id='EXENTCTL' name='MyCtrl' style='position:absolute;bottom:0pxright:0px;' width='1'>";
      str = str + strParamsForActiveX;

      str = str + "</object>";
   }
   else
   {
      // test that Exent gecko plugin is registered on machine - only if it is, we will create
      // an OBJECT tag
      if (null != navigator.mimeTypes["application/x-exent-aod"])
      {
         str = "<object width='0px' height='0px' type='application/x-exent-aod' id='EXENTCTL' name='MyCtrl' hidden='true'>";
         str = str + strParamsForActiveX;
         str = str + "</object>";
      }
   }
   
   document.writeln( str );
}