var API = parent.API;
var DHTML = (document.getElementById || document.all || document.layers);
var detailMessage = "";
var closeConsoleOnOpen = PREF_CLOSECONSOLEONOPEN;
var closeConsoleOnToolOpen = PREF_CLOSECONSOLEONTOOLOPEN;

var tw_none=0;
var tw_help=1;
var tw_comments=2;

var cERROR = "error";

// Get DHTML Object
function getObj(doc,name)
{
	if (document.getElementById)
	{ this.obj = doc.getElementById(name); }
	else if (document.all)
	{ this.obj = doc.all[name]; }
	else if (document.layers)
	{ this.obj = doc.layers[name]; }
}

// get locale id from params object
function getLocaleID(params)
{
	lang = params["lang"]; 
	if (lang.length==2) // two char langcode
	{
		if (lang=='en') 
			{ lang='en_US' }
		else
			{ lang = lang + '_' + lang.toUpperCase(); }
	}
	return lang;
}

// get start http parameters
function getGETParameters(locationObject)
{
	locationlist = locationObject.href.split("?");
	if (locationlist.length>1)
	{
		paramlist = locationlist[1].split("&");
		var tmppair = new Array();
		var params = new Object();
		for (var i=0; i<paramlist.length; i++)
		{
			tmppair=paramlist[i].split("=");
			params[unescape(tmppair[0])] = unescape(tmppair[1]);
		}
		return params;
	}
	return null;
}

// Open window with the specified parameters
function openWindow(link,width,height,id,retWinObj)
{
 	theFeats = "left="+(screen.width-width)/2+",top="+(screen.height-height)/2+",height="+height+",width=" + width 		+",location=no,menubar=no,resizable=no,scrollbars=1,status=no,toolbar=no";
	winObj = window.open(link, id, theFeats); 
	if (retWinObj) return winObj; 
}

// Open Content in Content Frame
function openContent(href)
{
	var contentFrame = parent.frames.contentFrame;
	if (contentFrame)
	{
		downloading(true);
		contentFrame.location.replace(href);
	}
}

// opens error.htm page
function showErrorPage()
{
	var contentFrame = parent.frames.contentFrame;
	if (contentFrame)
	{
		OpenConsole(false);
		replaceFrame(contentFrame, "error.htm");
	}
}

// Header text write
function setHeaderText(text)
{
	var headerFrame = parent.frames.headerFrame;
	if (headerFrame && text)
	{ 
		var textDiv = new getObj(headerFrame.document,'header_info').obj;
		textDiv.innerHTML = text; 
	}
}
// Footer text write
function setFooterText(text)
{
	var footerFrame = parent.frames.footerFrame;
	if (footerFrame)
	{
		var textDiv = new getObj(footerFrame.document,'footer_info').obj;
		textDiv.innerHTML = text;
	}
}
// Close Tool Window
function closeToolWindow()
{
	if (parent.toolActive)
	{
		consoleW = parent.consoleOpened ? parent.old_consoleW : null;
		resizeFrames(consoleW,0,'*');
		parent.frames.toolFrame.location.replace('empty.htm');
		parent.toolActive = false;
		parent.activeWindow = tw_none;
	} 
}

// Open Comments window
function openComments()
{
	if (parent.activeWindow == tw_comments) closeToolWindow();
	else {
		parent.toolTitle = API.getLangString('if.labels.btn_comments')
		parent.frames.toolFrame.location.replace('commentsviewer.htm');
		openToolWindow(tw_comments);
	}
}

// Write Tool Title to tool document
function writeToolTitle()
{
	ensureLangStrings();
	var ws = '<table width="100%" height="15" cellpadding="0" cellspacing="0" border="0" class="table_toolTitle">';
	ws += '<tr><td class="td_title" width="100%">'+parent.toolTitle+'</td>';
	ws += '<td nowrap><a class="CDMbutton" href="javascript:closeToolWindow();">'+parent.toolClose+'</a></td>';
	ws += '</tr></table>';
	document.write(ws);
}

// open a tool window
function openToolWindow(winToOpen)
{
		parent.activeWindow = winToOpen;
		parent.toolActive = true;
		consoleW = (closeConsoleOnToolOpen && parent.consoleOpened) ? 0 : null;
		resizeFrames(consoleW,'*',0);
}

// Returns last error message from applet
function getLastAppletError()
{
	if (API!=null)
		return API.getLastAppletError();
	return "";
}

// Returns key information string
function getKeyInformation()
{
	if (API!=null)
		return API.getKeyInformation();
	return "";
}

// Returns version information string
function getVersion()
{
	if (API!=null)
		return API.getVersion();
	return "";
}

// load js langstrings
function ensureLangStrings()
{
	if (!parent.toolClose) parent.toolClose = API.getLangString('if.labels.btn_tool_close');
}

// Open Metadata viewer window
function openMetadataViewer(type)
{
	if (API!=null)
	{
		parent.toolTitle = type == 1 ? API.getLangString('if.labels.lb_coursemd') : API.getLangString('if.labels.lb_scomd')
		parent.frames.toolFrame.location.replace('metadataviewer.htm?type='+type);
		openToolWindow();
	}
}

// Open Help window
function openHelp()
{
	if (parent.activeWindow == tw_help) closeToolWindow();
	else {
		parent.toolTitle = API.getLangString('if.labels.btn_help');
		parent.frames.toolFrame.location.replace(parent.lang+'_help.htm');
		openToolWindow(tw_help);
	}
}

// Console interface
function c_changeLessonStatusIcon(itemID, lesson_status)
{
	consoleFrame = parent.frames.consoleFrame;
	if (consoleFrame) consoleFrame.changeLessonStatusIcon(itemID, lesson_status);
}

function c_selectMenuItem(itemID)
{
	top.TestHarvestFrame.testStarted(itemID);
	consoleFrame = parent.frames.consoleFrame;
	if (consoleFrame) consoleFrame.selectMenuItem(itemID);
}


// Show an error or warning message
function showMessage(message,additional,type)
{
	detailMessage = additional;
	// show icon
	var footerFrame = parent.frames.footerFrame;
	var grPath = 'interface/buttons_graphics/';
	if (footerFrame) {
		var footer_infobox = new getObj(footerFrame.document,'footer_infobox').obj;
		footer_infobox.style.display = message =='' ? 'none' : '';
		var detailDiv = new getObj(footerFrame.document,'we_detail').obj;
		if (detailDiv)
			detailDiv.style.display = additional=='' ? 'none' : '';
		var iconImg = new getObj(footerFrame.document,'we_icon').obj;
		if (type==1) { iconImg.src = grPath + 'warninga_small.gif'; } else
		if (type==2) { iconImg.src = grPath + 'errora_small.gif'; } else
		if (type==0) { iconImg.src = grPath + 'empty.gif'; }
	setFooterText(message);
	}
}
// Open a window and write additional message
function showMessageWindow(message)
{
	msgWnd = openWindow('',450,400,'CDMmsgWnd',true);
	var docText = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
	docText+='<link rel="stylesheet" href="interface/styles/engine.css" type="text/css"><title>Info</title></head>';
	docText+='<body bgcolor="#44445d" text="#000000"><table class="table_border" width="100%" height="100%" cellpadding="5">';
	docText+='<tr><td valign="top" align="left"><div class="msg_text">';
	docText+=message;
	docText+='</div></td></tr></table></body></html>';
	msgWnd.document.write(docText);
}

// Downloading Flash control
// stops download animation after downloadTimeout milliseconds
var downloadTimeout = 1000;
var downloadStopInterval = null;
function clearDownloadTimeout() {
	if (downloadStopInterval != null) {
		clearTimeout(downloadStopInterval);
		downloadStopInterval = null;
	}
}

// main download movie controller
function downloading(istrue){
	var footerFrame = parent.frames.footerFrame;
	if (footerFrame) {
		var dl_flash = new getObj(footerFrame.document,'dl_flash').obj;
		if (dl_flash) {
			if (istrue==true) {
				dl_flash.play();
				clearDownloadTimeout();
				downloadStopInterval = setTimeout("downloading(false)", downloadTimeout);
			} else {
				clearDownloadTimeout();
				dl_flash.stop();
			}
		}
	}
}

// Open an SCO
function openSCO(scoID,fromConsole)
{
	var canStartTest = true;
	
	if (typeof(top.window.testRunning) != "undefined") {
		canStartTest = !top.window.testRunning;
		
		if (!canStartTest) {
			alert("Amíg a kitöltés alatt álló tesztet nem fejezi be, nem tud újat megnyitni!");
		}
		
		if (top.window.testResultHarvesting) {
			// do not allow test change if we are currently harvesting test results
			canStartTest = false;
		}
	}

	if (canStartTest) {
		// If the external interface is available, use it
		if (typeof(top.window.external.CanStartTest) != "undefined") {
			
			canStartTest = top.window.external.CanStartTest(scoID);
		}
		
		if (canStartTest) {
			if (fromConsole && closeConsoleOnOpen) OpenCloseConsole();
			closeToolWindow();
			if (API.openSCO(scoID,fromConsole) == cERROR)
				showErrorPage();
		}
	}
}

// Open next SCO
function openNextSCO()
{
	if (parent.btnNextEnabled)
	{
		closeToolWindow();
		if (API.openNextSCO() == cERROR)
			showErrorPage();
	}
}

// Open previous SCO
function openPrevSCO()
{
	if (parent.btnPrevEnabled)
	{
		closeToolWindow();
		if (API.openPrevSCO() == cERROR)
			showErrorPage();
	}
}

// Resize Engine Frames
function resizeFrames(consoleW,toolH,contentH)
{
	if (consoleW!=null)
	{
		var frameset = parent.frames.console_content_fs;
		frameset.cols = consoleW+',*';
	}
	if (toolH!=null && contentH!=null)
	{
		var frameset = parent.frames.tool_content_fs;
		frameset.rows = contentH+','+toolH;
	}
}

// Open or Close console window
function OpenCloseConsole()
{
	if (parent.btnConsoleEnabled)
	{
		OpenConsole(!parent.consoleOpened);
	}
}

// If bOpen true, open a console window, else close it
function OpenConsole(bOpen)
{
	var frameset = parent.frames.console_content_fs;
	if (frameset)
	{
		consoleW = null;
		if (parent.toolActive) closeToolWindow();		
		if (bOpen)
		// open console
		{ 
			parent.consoleOpened = true;
			consoleW = parent.old_consoleW;
		}
		else
		// close console
		{
			parent.consoleOpened = false;
			consoleW = 0;
		}
		
		resizeFrames(consoleW,null,null);
	}
}

// Replace Frame Location
function replaceFrame(frame,href)
{
	if (!(frame))
		// FIX: Invalid parameter passing
		{ timerID = setTimeout('replaceFrame('+arguments.toString()+')', 20); }
	else
		{ frame.location.replace(href); }
}

// Set the exit request
function setExitRequest()
{
	if (API!=null) 
	{
		API.setExitRequest();
		parent.contentFrame.location.replace('closecourse.htm');
	}
}

// Write image tag with specified parameters and get language string from API
function writeImage(name,src,langKey,width,height)
{
	if (API!=null) document.write('<img name="'+name+'" border="0" src="'+src+
			'" width="'+width+'" height="'+height+'" alt="'+API.getLangString(langKey)+'">');
}

// Write language to document
function writeLangString(langKey)
{
	if (API!=null) document.write(API.getLangString(langKey));
}
// Rollover functions
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

