
// Global Variables
var gameID = "";
var gameName = "";
var gameTypeWeb = false;
var gameTypeDownload = false;
var gameImage = "";
var gameLoadTime = "";
var gameDescriptionLong = "";
var gameWebPlayURL = "";
var gameDownloadPlayURL = "";
var gameBuyURL = "";
var gameDownloadSize = "";
var gamePurchaseType = "";
var gamePrice = "";
var gameFeatures = "";
var gameSystemReqOs = "";
var gameSystemReqProcessor = "";
var gameSystemReqRam = "";
var gameSystemReqDiskSpace = "";
var gameSystemReqDirectX = "";
var gameSystemReq3DVideo = "false";


// This is the main function.  When called it loads all the data from the game XML file.
// It must be called before any of the get functions can be called.
// It gets the game id from the query string of the parent url, i.e. ?gameid=21blitz
// It returns a 1 if the game was loaded sucessfully and the browser supports ActiveX
// It returns a 2 if the browser does not support ActiveX
// It returns a 3 if the game id is invalid

function loadGameData()
{
  var priceID = "";
  gameID = gameid;
  var loadResult = false;
  var xmlPath = "/assets/games/onlinegames/games/" + gameID + "/EN/" + gameID + ".xml";
  var xmlDocument;

  // this function is in xmlLoader.js
  loadXMLDocument( "/assets/games/onlinegames/pagedata/EN/price.xml", holdData );         // this XML data will be used by _getPrice()
  loadXMLDocument( xmlPath, loadData );
}


function loadData( xmlDocument )
{
  if( xmlDocument != null )
  {

    // Game Types
    var element = getNodeValues( xmlDocument, 'gameTypeList/gameType' );   

    for( var i = 0; i < element.length; i++ )
    {
        if( element[ i ] == 'web' ) gameTypeWeb = true;
        else if( element[ i ]  == 'download' ) gameTypeDownload = true;
    }

    if( gameTypeWeb )
    {
        // Game Name
        element = getNodeValues( xmlDocument, 'webGameData/gameName' );
        gameName = element[ 0 ];

        // Image
        element = getNodeValues( xmlDocument, 'webGameData/images/gameImage' );
        if( element.length > 0 ) gameImage = '<img src=/assets/games/onlinegames/games/' + gameID + '/' + element[ 0 ] + '>'

        // Load Time
        element = getNodeValues( xmlDocument, 'webGameData/loadTimeFor56KModem', 'units' );
        if( element.length > 0 )
        {
            var loadTimeUnits = element[ 0 ][ 1 ];
            var loadTimeValue = element[ 0 ][ 0 ];
            gameLoadTime = loadTimeValue + " " + loadTimeUnits;            
        }

        // Description Long
        element = getNodeValues( xmlDocument, 'webGameData/description/long' );
        if( element.length > 0 ) gameDescriptionLong = element[ 0 ];

        // Play Button
        gameWebPlayURL = getGame( gameID , 'web' );
    }

    if( gameTypeDownload )
    {
        // Game Name
        element = getNodeValues( xmlDocument, 'downloadableGameData/gameName' );
        if( element.length > 0 ) gameName = element[ 0 ];

        // Image
        element = getNodeValues( xmlDocument, 'downloadableGameData/images/gameImage' );
        if( element.length > 0 ) gameImage = '<img src=/assets/games/onlinegames/games/' + gameID + '/' + element[ 0 ] + '>';

        // Description Long
        element = getNodeValues( xmlDocument, 'downloadableGameData/description/long' );
        if( element.length > 0 ) gameDescriptionLong = element[ 0 ];

        // Feature List
        element = getNodeValues( xmlDocument, 'downloadableGameData/featureList/feature' );
        if( element.length > 0 )
        {
            for ( var i = 0; i < element.length; i++)
            {
                gameFeatures = gameFeatures + "<LI>" + element[ i ] + "</LI>";
            }
        }

        // System Requirements - Operating System
        element = getNodeValues( xmlDocument, 'downloadableGameData/systemRequirements/operatingSystemList/operatingSystem', 'description' );
        if( element.length > 0 )
        {
            for ( var i = 0; i < element.length; i++)
            {
                if( i != 0 )
                {
                    gameSystemReqOs = gameSystemReqOs + ", ";
                }
                gameSystemReqOs = gameSystemReqOs + element[ i ][ 1 ];                
            }
        }

        // System Requirements - Processor
        element = getNodeValues( xmlDocument, 'downloadableGameData/systemRequirements/processor/description' );
        if( element.length > 0 )
        {
            gameSystemReqProcessor = element[ 0 ];

            element = getNodeValues( xmlDocument, 'downloadableGameData/systemRequirements/processor/speed', 'units' );
            if( element.length > 0 )
            {
                gameSystemReqProcessor = gameSystemReqProcessor + ", " + element[ 0 ][ 0 ];
                gameSystemReqProcessor = gameSystemReqProcessor + element[ 0 ][ 1 ];
            }
        }

        // System Requirements - Memory
        element = getNodeValues( xmlDocument, 'downloadableGameData/systemRequirements/ram', 'units' );
        if( element.length > 0 )
        {
            gameSystemReqRam = element[ 0 ][ 0 ];
            gameSystemReqRam = gameSystemReqRam + " " + element[ 0 ][ 1 ];
        }

        // System Requirements - Disk Space
        element = getNodeValues( xmlDocument, 'downloadableGameData/systemRequirements/diskSpaceRequired', 'units' );
        if( element.length > 0 )
        {
            gameSystemReqDiskSpace = element[ 0 ][ 0 ];
            gameSystemReqDiskSpace = gameSystemReqDiskSpace + " " + element[ 0 ][ 1 ];
        }

        // System Requirements - DirectX
        element = getNodeValues( xmlDocument, 'downloadableGameData/systemRequirements/graphics/minDirectXVersion' );
        if( element.length > 0 )
        {
            gameSystemReqDirectX = element[ 0 ];
        }

        // System Requirements - 3D Video Card Required
        element = getNodeValues( xmlDocument, 'downloadableGameData/systemRequirements/graphics/required3DVideoCard' );
        if( element.length > 0 )
        {
            gameSystemReq3DVideo = element[ 0 ];
        }

        // Purchase Type
        element = getNodeValues( xmlDocument, 'downloadableGameData/purchaseType' );
        if( element.length > 0 )
        {
            gamePurchaseType = element[ 0 ]
        }

        // Price
        element = getNodeValues( xmlDocument, 'downloadableGameData/priceID' );
        if( element.length > 0 )
        {
            priceID = element[ 0 ]
            gamePrice = _getPrice( priceID, "USD" );
        }

        // Download Size
        element = getNodeValues( xmlDocument, 'downloadableGameData/downloadSize', 'units' );
        if( element.length > 0 )
        {
            var downloadSizeUnits = element[ 0 ][ 1 ];
            element = getNodeValues( xmlDocument, 'downloadableGameData/downloadSize/freeSize' );
            var downloadSizeValue = element[ 0 ];
            gameDownloadSize = downloadSizeValue + " " + downloadSizeUnits;
        }

        // Download URL
        gameDownloadPlayURL = getGame(gameID, 'download' );

        // Buy URL
        if( gamePurchaseType == "tryAndBuy" || gamePurchaseType == "buyOnly" )
        {
            gameBuyURL = buyGame(gameID, priceID, 'download' );
        }
    }
    
    renderGameData();

  }
  else
  {
    document.write( '<br><h2>Game "' + getGameID() + '" Not Found.<br><br>Find arcade games <a href="/entertainment/ms-arcade-games/" target="_top">here</a>!</h2>' );
  }
}


var priceXML;

function holdData( xmlDocument ) { priceXML = xmlDocument; }      // hold this data until it's ready to use by _getPrice()

function _getPrice( priceID, currency )
{
  targetPriceID = priceID;
  targetCurrency = currency;

  var price = getNodeValues( priceXML, "priceDefinition/amount", null, "priceID/currency", priceID + "/" + currency );
  
  if( price.length > 0 && price[ 0 ] != null && price[ 0 ] != "" )
    return price[ 0 ];
  
  return "Unknown";
}


function getGame( gameID, gameType )
{
  var thirdParty = "disney_";                     // Set a variable to identify the partner
  initGameScript( thirdParty );                   // Initialize the base url string (no return)
  var PageUrl;                                    // This will hold the generated URL

  if( gameType == "web" ) PageUrl = getLaunchWebGameUrl( gameID, 1 );               // Get the full URL
  else if( gameType == "download" ) PageUrl = getDownloadUrl( gameID, gameType );
  
  return PageUrl;
}


function buyGame( gameID, priceID, gameType )
{
  var thirdParty = "disney_";                       // Set a variable to identify the partner
  initGameScript( thirdParty );                     // Initialize the base url string (no return)
  var PageUrl;                                      // This will hold the generated URL
  PageUrl = getBuyUrl( gameID, priceID, gameType ); // Get the full URL
  return PageUrl;
}


// Web and Download
function getGameID()                  { return gameID; }
function getGameName()                { return gameName; }
function getGameImage()               { return gameImage; }
function getGameLoadTime()            { return gameLoadTime; }
function getGameDescriptionLong()     { return gameDescriptionLong; }


// Web only
function isGameTypeWeb()              { return gameTypeWeb; }
function getGameWebPlayURL()          { return gameWebPlayURL; }


// Download only
function isGameTypeDownload()         { return gameTypeDownload; }
function getGameDownloadSize()        { return gameDownloadSize; }
function getGameDownloadPlayURL()     { return gameDownloadPlayURL; }
function getGameBuyURL()              { return gameBuyURL; }
function getGamePurchaseType()        { return gamePurchaseType; }
function getGamePrice()               { return gamePrice; }
function getGameFeatures()            { return gameFeatures; }

function getGameSystemReqOs()         { return gameSystemReqOs; }
function getGameSystemReqProcessor()  { return gameSystemReqProcessor; }
function getGameSystemReqRam()        { return gameSystemReqRam; }
function getGameSystemReqDiskSpace()  { return gameSystemReqDiskSpace; }
function getGameSystemReqDirectX()    { return gameSystemReqDirectX; }
function getGameSystemReq3DVideo()    { return gameSystemReq3DVideo; }


function deliverPopDisclaimer( mLink )        // global disclaimer function for ad served elements
{
  var popDisclaimer=window.open( "", "disclaimer", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=370,height=210,top=100,left=150,screenX=180,screenY=100" );
  popDisclaimer.document.write ( '<html><head><title>FamilyFun.com - Disclaimer</title>' );
  popDisclaimer.document.write ( '<SCR'+'IPT TYPE="text/javascript" LANGUAGE="JavaScript">' );
  popDisclaimer.document.write ( 'sendTimer = setTi'+'meout("sendToClientSide()",1500);closeTimer = setT'+'imeout("window.close()",15000);' );
  popDisclaimer.document.write ( 'func'+'tion sendToCl'+'ientSide(){window.op'+'en("'+mLink+'"); this.focus();}' );
  popDisclaimer.document.write ( '</scr'+'ipt>' );
  popDisclaimer.document.write ( '</head><body bgcolor="#FFFFFF" text="#0065CE" link="#FFFF80" vlink="#FFFF80" alink="#FFFF80" leftmargin=0 topmargin=0 marginwidth=0 marginheight=0>' );
  popDisclaimer.document.write ( '<table width="370" height="100%" border="0" cellspacing="0" cellpadding="0"><tr><td><img src="/Resources/global/ff_disclaimer_top.gif" width="370" height="27" alt="" border="0"></td></tr><tr><td><table width="330" border="0" cellspacing="0" cellpadding="0" align="center"><tr><td><br><FONT FACE="Verdana,sans-serif" SIZE="2" COLOR="#0065CE"><b>The web site you are about to link to is not controlled by FamilyFun.com and different terms of use and privacy policy will apply. By proceeding you agree and understand that FamilyFun.com is not responsible for the site you are about to access.</b></FONT><br><br></td></tr></table>' );
  popDisclaimer.document.write ( '</td></tr><tr bgcolor="#ffffff"><td align="center" height="20"><a href="javascript:window.close();"><FONT FACE="Verdana,sans-serif" SIZE="1" COLOR="#006699"><b>Close this window</b></font></a><BR></td></tr></table>' );
  popDisclaimer.document.write ( '</body></html>' );
  popDisclaimer.document.close();
}


      // this function will be called by gameData.js once the XML is loaded
      function renderGameData()
      {

          document.write( '<div id="gamedata">\n' );
          
          document.write( ' <div id="gamebox">\n' );
          document.write( '   <div id="gameimage">\n' );
          var image = getGameImage();
          document.write( image );
          document.write( '   </div>\n' );
          document.write( '   <div id="gamebuttons">\n' );
          if( isGameTypeWeb() )
          {
            // Play Button
            document.write( '<div id="playbutton"><a href="?URL=' + getGameWebPlayURL() + '" target="_top"><img src="/images/games/btn_playnow.gif" border="0" width="119" height="17" alt="Play the Game!"></a>' );

            // Load Time
            var loadTime = "Load Time: " + getGameLoadTime();
            document.write( '<span style="font-size: 7pt; color: #4c4c4c;">' );
            document.write( loadTime + '</span></div>' );
          }


          if( isGameTypeDownload() )
          {
            // Download Button
            if( getGamePurchaseType() == "tryAndBuy" || getGamePurchaseType() == "free" )
            {
              document.write( '<div id="downloadbutton"><a href="' + getGameDownloadPlayURL() + '"><img src="/images/games/btn_download.gif" border="0" width="119" height="17" alt="Download the Game!"></a>' );

              // Download Size
              var loadTime = "Download Size: " + getGameDownloadSize();
              document.write( '<span style="font-size: 7pt; color: #4c4c4c;">' );
              document.write( loadTime + '</span></div>' );
            }

            // Buy Button
            if( getGamePurchaseType() == "tryAndBuy" || getGamePurchaseType() == "buyOnly" )
            {
              document.write( '<div id="buybutton"><a href="' + getGameBuyURL() + '" target="_blank"><img src="/images/games/btn_buy.gif" border="0" width="119" height="17" alt="Buy the Game!"></a>' );
              document.write( '<span style="font-size: 7pt; color: #4c4c4c;">' );
              document.write( getGamePrice() + ' Regular Price</span></div>' );
            }
          }          
          document.write( '   </div>\n' );
          document.write( ' </div>\n' );
          


          document.write( '<table width="370" border="0" cellpadding="0" cellspacing="0">' );
          document.write( '<tr>' );
          document.write( '    <td width="370" valign="top" colspan="3">' );
          document.write( '        <span style="font-size: 8pt; color: #4c4c4c"><br>' );

          // Description Long
          var description = '<p>' + getGameDescriptionLong() + '<p>';
          document.write(description);
          document.write( '        </span>' );
          document.write( '</td>' );
          document.write( '</tr>' );
          document.write( '<tr>' );
          document.write( '<td width="370" align="left" valign="top">' );

          if ( isGameTypeDownload() )
          {
            document.write("<style>#fullVersionFeatures li { font-size: 12px; color: #666666; }</style>");
            document.write("<p id=\"fullVersionFeatures\" style=\"font-size: 12px; color: #666666;\"><B>Full Version Features</B><br>" + getGameFeatures() + "<p>");
          }

          document.write( '        <span style="font-size: 8pt; color: #4c4c4c">' );

          if ( isGameTypeDownload() )
          {
            document.write( '<p><b>System Requirements</b>' );
            document.write( '<br>OS: ' + getGameSystemReqOs() );
            document.write( '<br>Processor: ' + getGameSystemReqProcessor() );
            document.write( '<br>Memory: ' + getGameSystemReqRam() );
            document.write( '<br>Disk Space: ' + getGameSystemReqDiskSpace() );

            if( getGameSystemReqDirectX() != "" ) { document.write( '<br>DirectX: ' + getGameSystemReqDirectX() ); }
            if( getGameSystemReq3DVideo() == "true" ) { document.write( '<br>3D Video Card Required' ); }
          }

          document.write( '        </span>' );
          document.write( '    </td>' );
          document.write( '</tr>' );
          document.write( '</table>' );
          document.write( '</div>' );
        }