var Done_margin="46px 0px 0px 24px",menuMargin={left: "58px",top: "498px"};
var menuback_marginTop_when_Submit_disable="508px";
var scoreHeight_when_submit_disable="95px";
var isSubmitClick=false;
var isScormAvailable=false;
var isSubmitInOpenState=false;
function checkWithScorm(){
$('#menuback').css({
"position":"absolute",
"left":menuMargin.left,
"top":menuMargin.top,
"cursor":"pointer"
});

if(!isCheckScormAvailable())
{
disableSubmit();
}else{
$('#help_button').css({'margin':Done_margin});
}
};
function secretScormCheck(){
if(typeof getAPIHandle==='function')
{
var obj=getAPIHandle();
if(typeof obj ==='object')
isScormAvailable = true;
else
isScormAvailable = false;
}
}
function isCheckScormAvailable(){
	initializeCommunication();
	var mode = LMSGetValue("cmi.mode");
return isScormAvailable && (mode != "browse");
};
function disableSubmit(){
var $sub=document.getElementById('help_button');
if(typeof $sub==='object')
{
$sub.style.display='none';
$('#menuback').css({"top":menuback_marginTop_when_Submit_disable});
$('#score').css('height',scoreHeight_when_submit_disable);
}
};
$(function(){
secretScormCheck(); /*check for scorm*/
checkWithScorm();
$('#menuback').click(function(){
if(!isSubmitInOpenState){
window.location.reload(false);
}
});
});

function windowOpener(){
	if(!isSubmitClick && !isSubmitInOpenState){
	ig.system.stopRunLoop(); 
	$('#overlay').css('display','block');
	$('#submit_pop_container').css({'display':'block'});
	$('.data_in_submit_pop').html('¿Quieres enviar la actividad?').css({"top":"42px","left":"70px"});

		DeactivateCursor();
	}
	
}
 
 function ActivateCursor(){
 isSubmitInOpenState=false;
 $('#toggleGame').css('cursor','pointer');
  $('#audioControl').css('cursor','pointer');
   $('#menuback').css('cursor','pointer');
   $('#help_button').css('cursor','pointer');
 }
 function DeactivateCursor(){
 isSubmitInOpenState=true;
  $('#toggleGame').css('cursor','default');
  $('#audioControl').css('cursor','default');
   $('#menuback').css('cursor','default');

 $('#help_button').css('cursor','default');
 }
 
 
 /*initialise the submit data collections buttons*/
 $(function(){
 
 $('.submit_cancel').on('touchstart click',function(){
 	ig.system.startRunLoop();
	$('#overlay').css('display','none');
 $('#submit_pop_container').css({'display':'none'});
 
 $('.data_submit_popup_buttons').show();
 //isSubmitInOpenState=false;
 ActivateCursor();
 });
 
 
 /*binding events to imgage x*/
 $('#x_img').on('mouseover',function(e){
 this.src="media/submit_button/x_Over.png";
 })
 .on('mouseout',function(){
 this.src="media/submit_button/x_Default.png";
 })
 .on('click',function(){
 //isSubmitInOpenState=false;
  ActivateCursor();
 this.src="media/submit_button/x_Pressed.png";
  if(isSubmitClick)
  {
  	window.close();
  }
 });
 
 /*binding events to submit button*/
 $('#sub_button').on('mouseover',function(e){
 this.src="media/submit_button/submit_Over.png";
 })
 .on('mouseout',function(){
 this.src="media/submit_button/submit_Default.png";
 })
 .on('click',function(){
 this.src="media/submit_button/submit_Pressed.png";
 });
 
 /*binding events to cancel button*/
 $('#can_button').on('mouseover',function(e){
 this.src="media/submit_button/cancel_Over.png";
 })
 .on('mouseout',function(){
 this.src="media/submit_button/cancel_Default.png";
 })
 .on('click',function(){
 this.src="media/submit_button/cancel_Pressed.png";
 });
 
 $('#sub_button').on('click',function(){
 $('.data_in_submit_pop').html('Has enviado la actividad.').css({"left":"12px","top":"53px"});
 $('.data_submit_popup_buttons').hide();
	isSubmitClick=true;
	/*course complete status place here*/
	if(typeof setCourseCompleted=='function' && isCheckScormAvailable())
		setCourseCompleted();
	
 });

 if( typeof loadPage=='function' && isCheckScormAvailable())  /*initialisation of scorm*/
  	onLoad=loadPage();

	$('#help_button').bind('click touchstart',toggleStop);
 });
 
function toggleStop(){
windowOpener();
}


var exited = false;
 function exitSCO(){
    // do exit SCORM Calls
    if(!exited){
          if(typeof computeTime=='function' && isCheckScormAvailable())
					computeTime();
		  
		  if(typeof exitLesson=='function' && isCheckScormAvailable())
					exitLesson();
         
		exited = true;
	}
 }
$(window).bind("beforeunload",exitSCO);
$(window).bind("unload",exitSCO);
