﻿var subWin;
var cnt=0;
function thisMovie(movieName) {
    if(navigator.appName.indexOf("Microsoft") != -1){
        return window[movieName];
    }else{
        return document[movieName];
    }
}

function openSubwindow(url,option){
	openWindow(url,option);
}

function closeSubwindow(){
	closeFrame();
}

//
// Prototype Window Class
//
function openWindow(p_url,p_option) {
	//"width="+scoWidth+","+"height="+scoHeight;
	var width_height = p_option.split(",");
	if (width_height.length>=3){
		var tmpW = width_height[0].split("=");
		var tmpH = width_height[1].split("=");
		var tmpCloseBtn = width_height[2].split("=");
	}else{
		var tmpW = width_height[0].split("=");
		var tmpH = width_height[1].split("=");
		var tmpCloseBtn = ["closeBtn","on"];
	}
	var optionW = parseInt(tmpW[1]);
	var optionH = parseInt(tmpH[1]);
	var optionCloseBtn = String(tmpCloseBtn[1]);

	openFrame(p_url,optionW,optionH,optionCloseBtn);
}
//
// SCORM 1.2 API
//
function SCORMapi1_2() {
	//
    // Datamodel inizialization
    //
    var cmi = new Object();
        cmi.core = new Object();
        cmi.core.score = new Object();
        cmi.student_data = new Object();

    //
    // API Methods definition
    //
    var Initialized = false;

    /**************************************************
	/
	/***************************************************/
    function LMSInitialize(param) {
        errorCode = "0";
        if (param == "") {
            if (!Initialized) {
				thisMovie(swfNm).SCOInitialize("Initialized SCORM 1.2");
				Initialized = true;
                errorCode = "0";
                return "true";
            } else {
                errorCode = "101";
            }
        } else {
            errorCode = "201";
        }
        return "false";
    }

	function LMSFinish(param) {
		//alert("LMSFinish:"+param);
		//---
		window.focus();
		//---
        errorCode = "0";
        if (param == "") {
            if (Initialized) {
				thisMovie(swfNm).SCOFinish("Finished SCORM 1.2");
                Initialized = false;
                //alert("4:");
               	return "true";
            } else {
                errorCode = "301";
            }
        } else {
            errorCode = "201";
        }
        return "false";
    }

	function LMSGetValue(element) {
        errorCode = "0";
		var rStr;
        if(Initialized) {
			rStr = thisMovie(swfNm).SCOGetValue(element);
			if(rStr.result == true) {
				return rStr.data;
			} else {
				errorCode = rStr.data;
			}
		} else {
			errorCode = "301";
		}
        return "";
    }
	
    /**************************************************
	/
	/***************************************************/
    function LMSSetValue(element,value) {
    	
        errorCode = "0";
		var rTF;
		if (Initialized) {
			rTF = thisMovie(swfNm).SCOSetValue(element,value);
			if (rTF.result == true) {
				return "true";			
			} else {
				errorCode = rTF.data;
			}
        } else {
            errorCode = "301";
        }
        return "false";
    }
    
     /**************************************************
	/
	/***************************************************/
    function LMSCustomFunc(param) {
		thisMovie(swfNm).SCOCustomFunc(param);
    }
    
    /**************************************************
	/
	/***************************************************/
    function LMSCommit(param) {
    	
        errorCode = "0";
        if (param == "") {
            if (Initialized) {
				thisMovie(swfNm).SCOCommit("");
                return "true";
            } else {
                errorCode = "301";
            }
        } else {
            errorCode = "201";
        }
        return "false";
    }
    
    /**************************************************
	/
	/***************************************************/
    function LMSGetLastError() {
        return errorCode;
    }
    
    /**************************************************
	/
	/***************************************************/
    function LMSGetErrorString(param) {
        if (param != "") {
            var errorString = new Array();
            errorString["0"] = "No error";
            errorString["101"] = "General exception";
            errorString["201"] = "Invalid argument error";
            errorString["202"] = "Element cannot have children";
            errorString["203"] = "Element not an array - cannot have count";
            errorString["301"] = "Not initialized";
            errorString["401"] = "Not implemented error";
            errorString["402"] = "Invalid set value, element is a keyword";
            errorString["403"] = "Element is read only";
            errorString["404"] = "Element is write only";
            errorString["405"] = "Incorrect data type";
            return errorString[param];
        } else {
           return "";
        }
    }
    
    function LMSGetDiagnostic(param) {
        if (param == "") {
            param = errorCode;
        }
        return param;
    }

	this.LMSInitialize = LMSInitialize;
    this.LMSFinish = LMSFinish;
    this.LMSGetValue = LMSGetValue;
    this.LMSSetValue = LMSSetValue;
    this.LMSCommit = LMSCommit;
    this.LMSGetLastError = LMSGetLastError;
    this.LMSGetErrorString = LMSGetErrorString;
    this.LMSGetDiagnostic = LMSGetDiagnostic;
    
    /***************************************************/
    this.LMSCustomFunc = LMSCustomFunc;
    /***************************************************/
}
var API = new SCORMapi1_2();