﻿/**
 * Init variables
 *
 * _mode: 0-offline, 1-AICC, 2-SCORM, 3-LC
 * _sound: 1 - on, 0 - off
 * _animations: 1 - on, 0 - off
 * _lmsMyLearning: 1 = true, 0 - false
 *
 * Version 1.2
 */

var _mode = 0;
var _sound = 1;
var _autoresize = 1;
var _newWindow = 0;
var _showExitAlert = 1;
var _playerW = 800;
var _playerH = 600;
var _playerswf = 'loader.swf';
var _animations = 1;
var _lmsMyLearning = 1;

/**
 * AICC utilites
 */
function findParams(_win) {
  var result = getParams(_win.document.location);
  if (result == null && _win != window.top) {
    result = findParams(_win.parent);
  }
  return result;
}

function getParams(_url) {
  if (_url == null) {
    return null;
  }
  var url = new String(_url);
  if (url.length < 1) {
    return null;
  }

  var aiccUrl = parseParam(url, 'AICC_URL', null);
  var aiccSid = parseParam(url, 'AICC_SID', null);
  var auPassword = parseParam(url, 'AU_PASSWORD', '');

  if (aiccUrl != null && aiccSid != null) {
    var result = new Object();
	/*
    if (aiccUrl.toUpperCase().indexOf('HTTP://') < 0) {
      aiccUrl = 'http://' + aiccUrl;
    }
	*/
    result.aiccUrl = aiccUrl;
    result.aiccSid = aiccSid;
    result.auPassword = auPassword;
    return result;
  }

  return null;
}

function parseParam(_url, _param, _default) {
  var begin = _url.toUpperCase().indexOf(_param.toUpperCase());
  if (begin >= 0) {
    begin += _param.length + 1;
    var end = _url.indexOf('&', begin);
    return (end >= 0) ? unescape(_url.substring(begin, end)) : unescape(_url.substring(begin));
  }
  return _default;
}
										      
function openWindow(lesson){
	var params = findParams(window);
	if (params == null) {
			alert('Missing AICC PARAMS');
			return;
		}
	window.open("l"+lesson+".html?AICC_URL="+params.aiccUrl+"&AICC_SID="+params.aiccSid+"&AU_PASSWORD="+params.auPassword,"lesson"+lesson,"'width="+_playerW+", height="+_playerH+"'");
}

/* 
 * Utlities
 *
 */


/* 
 * Set lesson window internal size 
 */
function setSize(width,height){
  try {
    winW = document.body.clientWidth;
    winH = document.body.clientHeight;
    top.window.resizeBy(width-winW,height-winH);

    //resize again, fix for missing scrollbars
    winW = document.body.clientWidth;
    winH = document.body.clientHeight;
    
    var addh=0;
    if (navigator.appName.indexOf ("Microsoft") ==-1) {
        addh=10;
    }
    top.window.resizeBy(width-winW,addh+height-winH);
  } catch(e) { }
}

/* 
 * Embed course player
 */

function showContent(_crsId, _auId, _lesson) {
	if (_newWindow ==1 && window.name!="MNPOPUP"){
		var win = window.open(window.location,"MNPOPUP","width="+_playerW+",height="+_playerH);		
		if (!win) {
			alert("Okno szkolenia zostało zablokowane przez Twoją przeglądarkę. Wyłącz blokowanie wyskakujących okien dla szkolenia.");
		} else {
			win.focus();
		}
		return;
	}
	
	window.onunload = handleOnUnload;
	
	if (_mode == 1 ){
		var params = findParams(window);
		if (params == null) {
			alert('Missing AICC parameters.');
			return;
		}
		swf = _playerswf + '?AICC_CRS_ID=' + escape(_crsId) + '&AICC_AU_ID=' + escape(_auId) + '&AICC_SID=' + escape(params.aiccSid)
	            + '&AICC_URL=' + escape(params.aiccUrl) + '&AU_PASSWORD=' + escape(params.auPassword)
	            + '&_lesson=' + _lesson + '&_sound=' + _sound + '&_tracker=1' + '&_animations=' + _animations +'&_vid=' + gVersionId;
	} else if (_mode == 2 ){
		swf = _playerswf + '?_lesson=' + _lesson + '&_sound=' + _sound + '&_tracker=2' + '&_animations=' + _animations  + '&_vid=' + gVersionId;
	} else if (_mode == 0 ){
		swf = _playerswf + '?_lesson=' + _lesson + '&_sound=' + _sound + '&_animations=' + _animations  + '&_vid=' + gVersionId;
	}

  var content ='<table cellpadding=0 cellspacing=0 height="100%" width="100%"><tr><td align=center><OBJECT CLASSID="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" CODEBASE="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" WIDTH="'+_playerW+'" HEIGHT="'+_playerH+'" ID="swfplayer" ALIGN="middle">';
  content += '<PARAM NAME="allowScriptAccess" VALUE="sameDomain" />';
  content += '<PARAM NAME="movie" VALUE="' + swf + '" />';
  content += '<PARAM NAME="quality" VALUE="high" />';
  content += '<PARAM NAME="bgcolor" VALUE="#FFFFFF" />';
  content += '<EMBED SRC="' + swf + '" QUALITY="high" BGCOLOR="#FFFFFF" WIDTH="'+_playerW+'" HEIGHT="'+_playerH+'" NAME="swfplayer"  ALIGN="middle" ALLOWSCRIPTACCESS="sameDomain" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" swliveconnect="true" />';
  content += '</OBJECT></tr></td></table>';
  document.body.innerHTML = content;

  checkResolution();  
  if (_autoresize==1 ){
 	 setSize(_playerW,_playerH);
  }
}

/* 
 * Shows exit alert
 */
function showExitAlert(){
	alert('Błąd!. Aby poprawnie wyjść z lekcji skorzystaj następnym razem z przycisku "Wyjdź z lekcji".');
}

function handleOnUnload() {
	if (_mode==1 && _showExitAlert==1){
			showExitAlert();
	} else if (_mode==2) {
		doLMSFinish();
	}	
}

/* 
 * Close lesson window
 */
function closeLesson(){
	_showExitAlert = 0;
	if (_newWindow == 1 || _lmsMyLearning && window.top.opener) {
		window.top.close();
	} else {
		window.document.location = "about:blank";
	}
}

/* 
 * Check for screen resolution
 */
function checkResolution(){
	scrnW = screen.width;
	scrnH = screen.heigth;
	if (scrnW<=800 || scrnH<=600){
		alert("Twoja rozdzielczość ekranu jest zbyt mała. \nRozdzielczość ekranu powinna wynosić minimum 1024x768.");		
	}
}

