google.load("swfobject", "2.1");

var ytPlayers    = {};
var ytPlayerData = {};

function onYoutubePlayerError(errorCode)
{
  //alert("An error occured: " + errorCode);
}

/**
 *
 * @param forceInitReport first time we launch the player, intialize the report
 */
function updateytplayerInfo(reportId, forceInitReport)
{
  var iPlayerStatePlaying = 1;
  var iPlayerStateEnded = 0;
  var doSave = function()
  {
    var iTime = ytPlayers[reportId].getCurrentTime();
    ytPlayerData[reportId].iPlayedTime = Math.round(iTime) - ytPlayerData[reportId].iRefTime;
    ytPlayerData[reportId].iRefTime = Math.round(iTime);
    if(iTime < 0)
        iTime = 0;

		if (ytPlayerData[reportId].completedSent)
			iTime = -1;

		updateVideoTimeAndStateSpent(reportId, ytPlayerData[reportId].iPlayedTime, iTime);
  }

  if (ytPlayers[reportId] && ytPlayers[reportId].getCurrentTime)
  {
    // if ended or time >= 90 % of duration
    var iPercent = parseInt(ytPlayers[reportId].getCurrentTime() / ytPlayers[reportId].getDuration() * 100);

    if ((iPercent >= aJsConfig.completedLimit || ytPlayers[reportId].getPlayerState() == iPlayerStateEnded) && !ytPlayerData[reportId].completedSent && ytPlayers[reportId].getCurrentTime() >= 0)
    {
      ytPlayerData[reportId].completedSent = true;
      doSave();
    }

    // if playing
    if (ytPlayers[reportId].getPlayerState() == iPlayerStatePlaying || forceInitReport)
    {
      doSave();
    }
  }
}

function onYouTubePlayerReady(reportId)
{
  ytPlayers[reportId] = document.getElementById("ytplayerfront_" + reportId);
  if (ytPlayers[reportId])
  {
    ytPlayers[reportId].addEventListener("onError", "onYoutubePlayerError");
    ytPlayers[reportId].pauseVideo();
		
    if (ytPlayerData[reportId].iSeekTo)
      ytPlayers[reportId].seekTo(parseInt(ytPlayerData[reportId].iSeekTo), true);

		updateytplayerInfo(reportId, true);

    setInterval('updateytplayerInfo(' + reportId + ')', 2500);
  }
}

function loadYoutubePlayerFront(sFlashId, sDivId, reportId, iSeekTo)
{
  ytPlayerData[reportId] = {
		reportId: reportId,
		iSeekTo: iSeekTo,
		iPlayedTime : 0,
		iRefTime : 0,
		reportId : 0,
		iSeekTo : 0,
		completedSent : false
	};

  var params =
  {
    allowScriptAccess : "always"
  };
  var atts =
  {
    id : "ytplayerfront_" + reportId
  };
  swfobject.embedSWF("http://www.youtube.com/v/" + sFlashId + "?enablejsapi=1&playerapiid=" + reportId + "&fs=1", sDivId, aJsConfig.maxWidth, aJsConfig.maxHeight, "8", null, null, params, atts);
}

function displayYoutubeBoxFront(sFlashId, sTitle, reportId, iSeekTo)
{
  ytPlayerData[reportId].reportId = reportId;
  ytPlayerData[reportId].iSeekTo = iSeekTo;
  displayYoutubeBox(sFlashId, sTitle, "ytplayerfront_" + reportId, reportId);
}

function displayYoutubeBox(sFlashId, sTitle, flashId, reportId)
{
  var iExtraHeight = 90;
  if(sTitle.length > 70)
     iExtraHeight = 120;
  if(!flashId)
    flashId = "ytplayer";
  
  var fAfterLoadCallback = function()
  {
    $('MB_content').style.height = aJsConfig.maxHeight + iExtraHeight;
    var params =
    {
      allowScriptAccess : "always"
    };
    var atts =
    {
      id : flashId
    };
    swfobject.embedSWF("http://www.youtube.com/v/" + sFlashId + "?enablejsapi=1&playerapiid=" + reportId + "&fs=1", "ytapiplayer", aJsConfig.maxWidth, aJsConfig.maxHeight, "8", null, null,
        params, atts);
  }
  Modalbox.show('<div style="font-size:14px; font-weight: normal; padding: 5px 5px 5px 5px;" align="center"><form onsubmit="Modalbox.hide();return false;">'
      + '<div id="ytapiplayer" align="center">' + '</div>' + '<input style="font-size: 14px; font-weight: bold; margin-top: 10px;" type="submit" value="'
      + aJsConfig.localisation.close + '" /></form></div>',
  {
    title : sTitle,
    width : aJsConfig.maxWidth + 50,
    height : aJsConfig.maxHeight + iExtraHeight,
    overlayClose : false,
    afterLoad : fAfterLoadCallback,
		afterHide: function() { setTimeout(function() { window.location.reload(); }, 500); }
  });

}
