/**
 *  playnow.js
 *
 *  - open/close the game container
 *  - hide/show buttons to open/close container
 *
 */

var clientUrl = "";
setClientUrl = function(url)
{
  clientUrl = url;
}

var paymentOptions;
var paymentPopupWindow;
var paymentRefreshId;
setPaymentOptions = function(options)
{
  paymentOptions = options;
}

bindPaymentPopup = function()
{
  $('a.buy_credits').click(function(event)
  {    
    event.preventDefault();
    paymentPopup()
  });
}

paymentPopup = function()
{
  paymentPopupWindow = window.open(paymentOptions['href'].replace(/\&amp;/g,'&'), "Buy_Credits", "width=" + paymentOptions['width'] + ", height=" + paymentOptions['height']);
  paymentRefreshId = setInterval("paymentPopupClosed()", 1000);
}

paymentPopupClosed = function()
{
  if (paymentPopupWindow)
  {
    if (paymentPopupWindow.closed)
    {
      var unity = GetUnity();
      
      if (unity != null)
      {
        unity.SendMessage("Browser", "ClosePopup", "");
      }
      
      clearInterval(paymentRefreshId);
    }
  }
}


$.extend
({
  playNow: function()
  {
    var game_container = $('#game_container');
       
    $('#game_container_paused > .commercial a.start_game').unbind().click(function($event)
    {
      $event.preventDefault();
      $('#game_container_paused').css('visibility', 'hidden');
      $('#game_container_paused > .commercial').css('visibility', 'hidden');

      var hasUnity = DetectUnityWebPlayer();
      var brokenUnity = false;

      var unityContent = "";

      if (hasUnity)
      {

       unityContent = '<object id="UnityObject" classid="clsid:444785F1-DE89-4295-863A-D46C3A781394" width="720" height="560"> \n';
       unityContent += '<param name="backgroundcolor" value="000000" \/>';
       unityContent += '<param name="bordercolor" value="000000" \/>';
       unityContent += '<param name="wmode" value="transparent" \/>';
       unityContent += '<param name="logoimage" value="/images/webplayer_logo.png" \/>';
       unityContent += '<param name="src" value="' + clientUrl + '" /> \n';
       unityContent += '<embed id="UnityEmbed" src="' + clientUrl + '"  width="720" height="560" wmode="transparent" type="application/vnd.unity" logoimage="/images/webplayer_logo.png" backgroundcolor="000000" bordercolor="000000" pluginspage="http://www.unity3d.com/unity-web-player-2.x" /> \n';
       unityContent += '</object>';

       // set unity content
        $('#game_container').html(unityContent);

       // if Unity does not define to GetPluginVersion on Safari on 10.6, we presume the plugin
       // failed to load because it is not compatible with 64-bit Safari.
       if (navigator.appVersion.indexOf("Safari") != -1
        && navigator.appVersion.indexOf("Mac OS X 10_6") != -1
        && document.getElementById("UnityEmbed").GetPluginVersion == undefined)
        brokenUnity = true;

       // 2.5.0 cannot auto update on ppc. Treat as broken.
       else if (document.getElementById("UnityEmbed") != null && document.getElementById("UnityEmbed").GetPluginVersion() == "2.5.0f5"
        && navigator.platform == "MacPPC")
        brokenUnity = true;
      }

      if (!hasUnity || brokenUnity)
      {

       var installerPath = GetInstallerPath();
       if (installerPath != "")
       {
        // Place a link to the right installer depending on the platform we are on. The iframe is very important! Our goals are:
        // 1. Don't have to popup new page
        // 2. This page still remains active, so our automatic reload script will refresh the page when the plugin is installed
        unityContent = '<div align="center" id="UnityPrompt"> \n';
        if (brokenUnity)
         unityContent += '  <a href= ' + installerPath + '><img src="http://webplayer.unity3d.com/installation/getunityrestart.png" border="0"/></a> \n';
        else
         unityContent += '  <a href= ' + installerPath + '><img src="http://webplayer.unity3d.com/installation/getunityrestart.png" border="0"/></a> \n';
         unityContent += '</div> \n';

        // By default disable ActiveX cab installation, because we can't make a nice Install Now button
        //	if (navigator.appVersion.indexOf("MSIE") != -1 && navigator.appVersion.toLowerCase().indexOf("win") != -1)
        if (0)
        {
         unityContent += '<div id="InnerUnityPrompt"> <p>Title</p>';
         unityContent += '<p> Contents</p>';
         unityContent += "</div>";

         $('#game_container').html(unityContent);

         var innerUnityPrompt = document.getElementById("InnerUnityPrompt");

         var innerHtmlDoc =
          '<object id="UnityInstallerObject" classid="clsid:444785F1-DE89-4295-863A-D46C3A781394" width="320" height="50" codebase="http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayer.cab#version=2,0,0,0">\n' +
             '</object>';

         innerUnityPrompt.innerHTML = innerHtmlDoc;
        }

       unityContent += '<iframe name="InstallerFrame" height="0" width="0" frameborder="0"></iframe>\n';

       // set unity content
       $('#game_container').html(unityContent);

       }
       else
       {
        unityContent = '<div align="center" id="UnityPrompt"> \n';
        if (brokenUnity)
         unityContent += '  <a href="javascript: window.open("http://www.unity3d.com/unity-web-player-2.x"); "><img src="http://webplayer.unity3d.com/installation/getunityrestart.png" border="0"/></a> \n';
        else
         unityContent += '  <a href="javascript: window.open("http://www.unity3d.com/unity-web-player-2.x"); "><img src="http://webplayer.unity3d.com/installation/getunityrestart.png" border="0"/></a> \n';
         unityContent += '</div> \n';

        // set unity content
        $('#game_container').html(unityContent);
       }
      }
    });
  }
});

