//BTL related functions
var m_debugAspire = false;

//var sURL = unescape(window.location.pathname);
var sURL = unescape((window.location.toString().toLowerCase().indexOf("singleviewmode=1") < 0) ? window.location.pathname : window.location);

var m_timer;
var finishUrl = "";
var exitUrl = "";
var exitInvalidTokenUrl = "";
var clientModeValidator = "";
var m_aspireIntegration;

document.onload = onDocumentLoad();

function ShowStyleProfileReport(params)
{
	//console.log(params); don't uncomment - conflict in IE
	// parsexml params
	var xml = jQuery(jQuery.parseXML(params));
	// get the node with the url
	var url = xml.find('param[name="url"]');
	if (url)
	{
		// navigate to url
		//console.log(url.text());don't uncomment - conflict in IE
		window.location = url.text();
	}
}

function onDocumentLoad(){

	var m_application = "";
	setTimeout(function(){
		var o = document.getElementById("Itembank_sqa_solar");
		if(!o)
		{
			m_application = "AssessmentCentral";
			o = document.getElementById(m_application);
		}
		if(!o)
		{
			m_application = "Itembank";
			o = document.getElementById(m_application);
		}
		if(o)
		{
			o.tabIndex = 0
			o.focus();
			m_aspireIntegration = (typeof(AspireIntegration) == "function") ? new AspireIntegration(m_application) : null;
		}
	}, 500);
	
}

function quit() 
{
	setTimeout(quitAfterDelay, 10);
}
function quitAfterDelay()
{
	if (exitUrl.length > 0)
	{
		document.location = exitUrl;
	}
	else
	{
		window.top.close();
	}
}

function finish()
{
	setTimeout(finishAfterDelay, 10);
}

function finishAfterDelay()
{
	if(finishUrl && finishUrl.length > 0)
	{
		document.location = finishUrl;
	}
	else if(exitUrl && exitUrl.length > 0)
	{
		document.location = exitUrl;
	}
	else
	{
		window.top.close();
	}
}

function quitInvalidToken()
{
	setTimeout(quitInvalidTokenAfterDelay, 10);
}

function quitInvalidTokenAfterDelay()
{
	if(exitInvalidTokenUrl.length > 0)
	{
		document.location = exitInvalidTokenUrl;
	}
	else
	{
		quitAfterDelay();
	}
}

function setFocusToFlash()
{
	setTimeout(setFocusToFlashAfterDelay, 10);
	return "done";
}

function downloadExams(aList)
{
	if (downloadUrl.length > 0)
	{
		document.location = downloadUrl + aList;
	}
	else
	{
		document.location = aList;
	}
	//alert("downloadExams:" + aList);
}

function setClientModeValidatorValue (aValue)
{
	clientModeValidator = aValue;
}
function getClientModeValidatorValue()
{
	return clientModeValidator;
}

function setFocusToFlashAfterDelay()
{
	window.document.AssessmentDelivery.focus();
}

function applicationRefresh()
{
	if (exitUrl.length > 0)
	{
		quit();
	}
	else
	{
		window.location.replace( sURL );
	}
}

function applicationRefreshInvalidToken()
{
	quitInvalidToken();
}

function opennav() 
{
	var aPage = "./SecureAssessDelivery.html";
	var popUpWindow = window.open(aPage,"Assessment_Engine","fullscreen=yes, scrollbars=no,resizable=no,menubar=no,status=yes,statusbar=yes,toolbar=no,width=1024,height=768");
	if (!popUpWindow)
	{
		popupAlert();
	}
}

function applicationReady()
{
	if(m_aspireIntegration) m_aspireIntegration.notifyReady();	
	if(m_debugAspire)
	{
		//alert("ApplicationReady: call the aspireintegration.js notifyReady() method when the ui is ready to accept calls");
		if(parent)
		{
			parent.notifyReady();
		}
	}
			
}
			
function getFlashMovie(movieName) 
{
  	var isIE = navigator.appName.indexOf("Microsoft") != -1;
  	return (isIE) ? window[movieName] : document[movieName];
}

function getPermissionForToken(movie, token, username, permission)//, lang) 
{
	var myMovie = getFlashMovie(movie);
 	myMovie.viewPermissionForToken(token, username, permission)//, lang);
}

function onCompleteGetPermissionForToken()
{
	try
	{
		if(m_aspireIntegration) m_aspireIntegration.notifyAuthorizationComplete();
	}
	catch(err)
	{
	}
	if(m_debugAspire)
	{
		//alert("onCompleteGetPermissionForToken: call the aspireintegration.js notifyAuthorizationComplete() method when the ui is ready to accept calls");
		if(parent)
		{
			parent.notifyAuthorizationComplete();
		}
	}
}

function testGetPermissionForToken(movie)
{
	//alert("Movie: " + movie + ", token.text: " + document.getElementById("tokenTxt").value + ", username.text: " + document.getElementById("usernameTxt").value + ", permissionID.text: " + document.getElementById("permissionIDTxt").value);
	var myTokenID = document.getElementById("tokenTxt").value;
	var myUsername = document.getElementById("usernameTxt").value;
	var myPermissionID = document.getElementById("permissionIDTxt").value
	var myLanguageCode = document.getElementById("languageCodeTxt").value
	getPermissionForToken(movie, myTokenID, myUsername, myPermissionID)//, myLanguageCode)
}

function setDebugAspireMode(debug)
{
	m_debugAspire = debug;
}

//________________________________________ PDF related

var pdfWidthPadding = 0;
var pdfHeightPadding = 40;

var m_documentUrl = "";

function lauchPdfDocumentInWindow()
{
	var aPage = m_documentUrl;
	var popUpWindow = window.open(aPage,"Report","fullscreen=no, scrollbars=yes,resizable=yes,menubar=no,status=yes,statusbar=yes,toolbar=no,width=1024,height=768");
	if (!popUpWindow)
	{
		popupAlert();
	}
}
function launchPdfDocument(documentUrl)
{
	m_documentUrl = documentUrl;
	resizePdfArea();
	
	$('#pdfHeader').show();
	
		var myPDF = new PDFObject({ 
		
			url: documentUrl,
			pdfOpenParams: {
				navpanes: 0,
				toolbar: 1,
				statusbar: 0,
				view: "FitV",
				pagemode: "thumbs"
			}
		
		}).embed("pdfContent");
	  
	  $('#pdfBackground').fadeIn('fast');
	  $("#pdfArea").fadeIn("slow");
	
}
var pdfHeaderSize = 25;//$("#pdfHeader").height()
function resizePdfArea()
{
    var win = $(window);
    var doc = $(document);
    
    if (win.width() != doc.width() || win.height() != doc.height())
    {
        onResizeEventWait();
    }
    
	$("#pdfBackground").css("width", win.width());
	$("#pdfBackground").css("height", win.height());
	$("#pdfBackground").css("top", 0);
	$("#pdfBackground").css("left", 0);
	$("#pdfBackground").css("position", "absolute");
	$("#pdfBackground").css("z-index", 105);
	$("#pdfBackground").css("background-color", "#000000");
	$("#pdfBackground").css("opacity", 0.8);

	var pdfAreaWidth = win.width() - 2 * pdfWidthPadding;
	var pdfAreaHeight = win.height() - pdfHeaderSize;//2 * pdfHeightPadding;
	//alert("pdfback = " + pdfAreaWidth + ", " + pdfAreaHeight);
	$("#pdfArea").css("width", pdfAreaWidth);
	$("#pdfArea").css("height", pdfAreaHeight);
	$("#pdfArea").css("top", 0);
	$("#pdfArea").css("left", 0);
	$("#pdfArea").css("z-index", 106);
	$("#pdfArea").css("position", "absolute");
	
	$("#pdfContent").css("width", pdfAreaWidth);
	$("#pdfContent").css("height", pdfAreaHeight - pdfHeaderSize);
	
	$("#pdfHeader").css("width", pdfAreaWidth);
	$("#pdfHeader").css("height", pdfHeaderSize);
	//alert("pdfHeader:" + $("#pdfHeader").css("left") + ", " + $("#pdfHeader").css("top") + ", " + $("#pdfHeader").css("width") + ", " + $("#pdfHeader").css("height"));
}

function closePdf()
{
	m_documentUrl = "";
	if ($('#pdfContent').html().toLowerCase().indexOf("object") > 0)
	{
		$('#pdfContent').empty();
	}
	$("#pdfBackground").fadeOut("fast");
	$("#pdfArea").fadeOut("fast");

}
function onResizeEvent()
{
	if (m_documentUrl.length > 0)
	{
		launchPdfDocument(m_documentUrl);
	}
}

function setNewToken(movie, token)//, lang) 
{
	var myMovie = getFlashMovie(movie);
 	myMovie.setNewToken(token);
}

function tokenExpired()
{	
	if(m_aspireIntegration) m_aspireIntegration.notifySessionExpired();
	if(m_debugAspire)
	{
		alert("Token timed out. Please set a new token.");
	}
}

var resizeInterval;
function onResizeEventWait()
{
	clearTimeout(resizeInterval);
	resizeInterval = setTimeout(onResizeEvent, 200);  
}

$(window).resize(onResizeEventWait);

