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

/**
 * Show the slideshare content on a modalbox
 * 
 * @param embedcode the slideshare embed code
 * @param title the slideshare content title
 * 
 * @return
 */
function displaySlideshareBox(embedcode, title)
{
  var html = '<div style="width: 100%; margin: 0; padding: 0; text-align: center;">';
  html += embedcode;
  html += '</div>';
  
  Modalbox.show(html,
					  {
					    title : title,
					    width : 440,
					    height : 470
					  });
}

var periodicalSlideshareTimeSpentSender = false;
var frontTimerForSlideshareContent = 0;

/**
 * Show the slideshare content on a modalbox
 * for candidate (manage the time spent)
 * 
 * @param embedcode the slideshare embed code
 * @param title the slideshare content title
 * 
 * @return
 */
function displaySlideshareBoxFront(embedcode, title, reportId, closeText)
{
	// Create the periodical executer
	periodicalSlideshareTimeSpentSender = new PeriodicalExecuter(function() { updateCandidateSlideshareTimeSpent(reportId) }, 10);
	
	var html = '<div style="width: 100%; margin: 0; padding: 0; text-align: center;">';
	html += embedcode;
	html += '</div>';
	html += '<div style="text-align: center; margin-top: 5px;">';
	html += '<button type="button" onClick="javascript:Modalbox.hide();">';
	html += closeText;
	html += '</button>';
	html += '</div>';

	Modalbox.show(html,
						{
							title : title,
							width : 440,
							height : 500,
							overlayClose : false,
							afterLoad: startTimer,
							afterHide: function() { closeTimerAndAddTimeSpent(reportId) }
						});
}

function createSlidesharePlayer(playerParams)
{
  var atts =
  {
    id : "slideshareplayerfront_" + playerParams.reportId
  };

	playerParams.params.allowscriptaccess = 'always';
	
	swfobject.embedSWF(playerParams.url,
	                   'slideshareplayerdiv_' + playerParams.reportId,
	                   playerParams.width, playerParams.height,
	                   "8", null,
	                   null, playerParams.params, atts);

	// Create the periodical executer
	updateVideoTimeAndStateSpent(playerParams.reportId, 0, -1);
	periodicalSlideshareTimeSpentSender = new PeriodicalExecuter(function() { updateCandidateSlideshareTimeSpent(playerParams.reportId) }, 10);
}

/**
 * initialize the from/begin time
 */
function startTimer()
{
	frontTimerForSlideshareContent = new Date();
}

/**
 * Just reload the page
 */
function functionRefreshSlideshareFrontPage()
{
	window.location.reload();
}

/**
 * this function add 10 s to the time spent
 * for current candidate on the current
 * report (set whent lauching the content)
 * 
 * @return
 */
function updateCandidateSlideshareTimeSpent(reportId)
{
	// reset the timer
	startTimer();
	updateVideoTimeAndStateSpent(reportId, 10, -1);
}

/**
 * Retrieve diff between current date and
 * initialized time and post the result
 * 
 * @return
 */
function closeTimerAndAddTimeSpent(reportId)
{
	// Stop the periodical update if exsits
	if (periodicalSlideshareTimeSpentSender)
		periodicalSlideshareTimeSpentSender.stop();
		
	// Calculate the diff
	var currentDate = new Date();
	var utc1 = frontTimerForSlideshareContent.getTime();
	var utc2 = currentDate.getTime();
	
	if (utc2 > utc1)
	{
		// Calculate the diff
		var diff = ( utc2 - utc1 ) / 1000;		

		updateVideoTimeAndStateSpent(reportId, diff, -1, {onSuccess: functionRefreshSlideshareFrontPage});
	}
}
