/* $Id: assessmentItemCommonFlexapp.js 66664 2011-09-06 07:45:02Z jordi $ */

/* Should only reside in head of a document which may contain a flash/flex based question (and only once)
* PGS-522
* */
var arr_commonFlexappStatus = ["uninitialized", "initialized", "moved"];
var str_commonFlexappStatus = arr_commonFlexappStatus[0];

var flashRev = "?rev=9088";


/**
 * Requires swobject 2.2
 */
activateFlashObject = function(displayInElementId){
		writeToCallLog("activateFlashObject(" + displayInElementId + ") - if in assessment by moving");	
    var theSWF = document.getElementById('QTI_FLASH_PLAYER_MODULE');
//    alert("2activateFlashObject "+theSWF);
    var targetDiv = document.getElementById(displayInElementId);
    var displayInElement = targetDiv;
    if((theSWF == null || !theSWF) && !$('QTI_FLASH_PLAYER_MODULE_ASSESSMENT')){
        displayInElement.innerHTML = '<div id="QTI_FLASH_PLAYER_MODULE"/>';
        activateFlexappCommon();
    }else {
        try{
            //alert('assessmentItemCommonFlexapp.js / activateFlashObject / calling moveCommonFlash(true)');
            moveCommonFlash(true);
        }catch(err){
					writeToCallLog("UNCATCHED EXCEPTION " + err + " in activateFlashObject");				
        }

    }
}

onModuleLoaded = function(a_event){
    alert("onModuleLoaded");
    alert("onModuleLoaded "+logObject_str(a_event));
}


activateFlexappCommon2= function(){
		writeToCallLog("activateFlexappCommon2()");	
    var _QTI_FLASH_PLAYER_MODULE = $('QTI_FLASH_PLAYER_MODULE');
    if(typeof _QTI_FLASH_PLAYER_MODULE != 'undefined' && typeof swfobject != 'undefined'){
        // alert("activateFlexappCommon");
        var flashvarsObj = {bridgeName:"responseSwfCommon", MODULE_PATH:commonFolder+"gfx/assessment/", LOCALE:pageInfoLocale};
		// Placing stuff on top of a flash content, one should use wmode=opaque, because the performance is much better than transparent.
		// Transparent is if you need to place something behind flashcontent, or need it to be otherwise transparent.
        var parObj = {quality :"high", bgcolor:"#869cFF", allowScriptAccess:"always", allowNetworking:"all", wmode:"opaque"/*, wmode:"transparent"*/};
        var attObj = {play:"true"};
        //var swfWrapper = commonFolder+"scripts/test.swf";
        //var swfWrapper = commonFolder+"gfx/assessment/customInteraction_jsplugin_debug.swf"+flashRev;
        var swfWrapper = commonFolder+"gfx/assessment/customInteraction_jsplugin.swf"+flashRev;
        var expressInstallSWL = commonFolder+"scripts/expressInstall.swf";
		
		if ((!navigator.userAgent.match(/iPad/i)) && (!navigator.userAgent.match(/iPhone/i)) && (!navigator.userAgent.match(/iPod/i))){ 
		
			var onFlashLoaded = function(result){
				writeToCallLog("onFlashLoaded() - successful: " + result.success);	
				var failText="Det har vært et problem med å laste oppgaven. Vi anbefaler at du lukker nettleseren, åpner denne på nytt og logger på igjen. Dersom problemet vedvarer, forsøk igjen om noen minutter.";
				if(!result.success){
					alert(failText);
				}
			}
			swfobject.switchOffAutoHideShow();//Hide/Visible will be controlled by our styling
			swfobject.embedSWF(swfWrapper, "QTI_FLASH_PLAYER_MODULE", "700", "385",
					"9.0.0", swfWrapper, flashvarsObj, parObj, attObj, onFlashLoaded );

			initBridgeCommon();
		}
		
    }else{
        activateFlexappCommon();
        return;
    }
}

/**
 * Delay calling (only for non-assessments), swfobject.addLoadEvent is wrong in ff2 PGS-660
 *
 */
activateFlexappCommon = function(isAssessment){
		writeToCallLog("activateFlexappCommon(" + isAssessment + ")");	
    if (isAssessment != 'undefined' && isAssessment != null && isAssessment) {
        activateFlexappCommon2();
    } else {
        doSetTimeout("activateFlexappCommon2();", 1000);
    }
}

initBridgeCommon = function(){
		writeToCallLog("initBridgeCommon()");	
    if(typeof FABridge == 'undefined' || typeof FABridge.responseSwfCommon == 'undefined'){
        //doSetTimeout("initBridgeCommon();", 200);
        writeToCallLog("initBridgeCommon() skipping wait loop with setTimeout, to see if callback is enough!");	
        return;
    }
    var b_isfirst = (typeof flexAppCommon == 'undefined');
    if(b_isfirst){
    	flexAppCommon = FABridge.responseSwfCommon.root(); // ie6 PGSCF-262
    }
    str_commonFlexappStatus = arr_commonFlexappStatus[1];
}

initBridgeCommonCallBack = function(){
	writeToCallLog("initBridgeCommonCallBack()");	
	initBridgeCommon();
}

var b_initCommonFlex = false;
initCommonFlex = function(isAssessment){
		writeToCallLog("initCommonFlex(" + isAssessment + ")");	
    if (b_initCommonFlex){
        alert('initCommonFlex called');
        return;
    }
//    alert('initCommonFlex');
    try{
        if(typeof FABridge !== 'undefined'){
        		writeToCallLog("adding initBridgeCommon (as callback with marker version) from FA bridge");	
            FABridge.addInitializationCallback("responseSwfCommon", initBridgeCommonCallBack);
            activateFlexappCommon(isAssessment);
        }else{
            doSetTimeout('initCommonFlex('+isAssessment+');', 200);
            return;
        }
    }catch(err){
        alert('init common swf failed '+err);
    }
    b_initCommonFlex = true;
}

b_commonFlexappInitializer = false;

commonFlexappInitializer = function(isAssessment){
		writeToCallLog("commonFlexappInitializer(" + isAssessment + ")");	
    if(b_commonFlexappInitializer) return;
    initCommonFlex(isAssessment);
//    swfobject.addLoadEvent(activateFlexappCommon);
//    swfobject.addLoadEvent(initCommonFlex);


    b_commonFlexappInitializer = true;
}




