
var widgetContentCheckAjaxJsTimeout = null;
function widgetContentCheckAjaxJs(panelId) {
    clearTimeout(widgetContentCheckAjaxJsTimeout);
    widgetContentCheckAjaxJsTimeout = null;

    var oDiv = document.getElementById(panelId);
    if (oDiv.innerHTML != "") {
        var myScript = oDiv.innerHTML;
        oDiv.innerHTML = "";
        eval(myScript);
    }
    widgetContentCheckAjaxJsTimeout = setTimeout('widgetContentCheckAjaxJs("' + panelId + '");', 250);
}

var scormWindow = null;
var inLaunch = false;
function launchScormModule(sessionId, callbackUrl, launchUrl, title, w, h, fullScreen, previewParams, remoteParams) 
{
    var scLaunchPage = 'sc_launch.aspx';
    if (document.location.pathname.indexOf("/admin/") >= 0) scLaunchPage = "../" + scLaunchPage;
    
    var url = scLaunchPage + '?session_id=' + escape(sessionId) + '&callback_url=' + escape(callbackUrl) + '&launch_url=' + escape(launchUrl) + '&title=' + escape(title) + '&width=' + w + '&height=' + h;

    if (previewParams != "")
    {
        url = url + "&" + previewParams;
    }
    
	if ( remoteParams != "" )
	{
		document.location = url + "&" + remoteParams;
    }
    else
    {
		if (!inLaunch)
		{
			var lastLaunchWidth = w;
			var lastLaunchHeight = h;

			if (w >= screen.availWidth - 14 || h >= screen.height - 35)
			{
				scormWindow = window.open(url, 'scormWindow', 'fullscreen,menubar=no,location=no,status=no,resizable=yes,scrollbars=no');
			}
			else
			{
				l = (screen.width - w) / 2;
				t = (screen.height - h) / 2;


				if (fullScreen)
				{

					scormWindow = window.open(url, 'scormWindow', 'fullscreen,menubar=no,location=no,status=no,resizable=yes,scrollbars=no');
				}
				else
				{
					scormWindow = window.open(url, 'scormWindow', 'height=' + h + ',width=' + w + ',left=' + l + ',top=' + t + ',menubar=no,location=no,status=no,resizable=yes');
				}
			}

			//Check popup opened
			if (scormWindow == null)
			{
				document.getElementById('moduleContent').style.display = 'none';
				document.getElementById('popupAlert').style.display = 'block';
			}
			else
			{
				scormWindow.focus();
				inLaunch = true;
			}
		}
		else
		{
			if (scormWindow)
			{   
				var tryFocus = true;
	            
				try 
				{
					if (!scormWindow.focus()) tryFocus = false;
				} 
				catch (e) 
				{
					tryFocus = false;  
				}
	            
				if ( !tryFocus ) 
				{
					inLaunch = false; 
					launchScormModule(sessionId, callbackUrl, launchUrl, title, w, h, fullScreen, previewParams, remoteParams) 
				}
	            
	            
			}
			else
			{
				document.location = "../code/home.aspx";
			}

		}
    }
}



var certWindow = null;
function launchCertificate(assignmentId) {

    if (certWindow) {
        try {
            certWindow.close();
        }
        catch (e) { }
    }

    var url = "../code/cert_popup.aspx?aid=" + escape(assignmentId);
    var w = 800;
    var h = 600;
    certWindow = window.open(url, 'certWindow', 'height=' + h + ',width=' + w + ',menubar=no,location=no,status=no,resizable=no');
    //Check popup opened
    if (certWindow == null) {
        alert("The certificate popup failed to open.");        
    }
    else {
        certWindow.focus();
    }
}
