// Globally used constants for sizing the browser window and frames for the player.
// Note that the "LernBar Player" comprises both the content frame and navigator.
//
var PLAYER_WIDTH     = 1012; // width (in pixels) required by the LernBar player
var PLAYER_HEIGHT    =  713; // height (in pixels) required by the LernBar player
var NAVIGATOR_HEIGHT =  237; // height (in pixels) required by the LernBar navigator


function closeWindow() {
	test = window.open('','_self','');

	test.close();
}

function getBrowser() {
	var userAgent = navigator.userAgent.toLowerCase();
	
	if(userAgent.search(/safari.+/) != -1) { return 'sa'; }
	if(userAgent.search(/firefox.+/) != -1) { return 'ff'; }
	if(userAgent.search(/msie 7.+/) != -1) { return 'ie7'; }
	if(userAgent.search(/msie 6.+/) != -1) { return 'ie6'; }
	
	return 'na';
}

//
// ansicht ändern ???
function switchSonstiges( show, showValue, hideID ) {
	if (document.getElementById( show ).value == showValue) {
		document.getElementById( hideID ).style.display="block";
		document.getElementById( hideID ).style.display="table-row";
	} else {
		document.getElementById( hideID ).style.display="none";
	}
}

// 
// activate course
//
// +/- bei zusätzlichen topics
function addNewTopic( source, target) {
	if (source.value!="") {
		target.options[target.length] =new Option(source.value, source.value, false, true);
		source.value = "";
	}
}

// general
//
// standard popup funktion
function openWindow(theURL, winName, features) {
	window.open(theURL,winName,features).moveTo(125,125);
}

// wird vom studio aufgerufen um sich automatisch anzumelden
//
function autoLogin() {
    document.forms['login'].elements['submit_h'].value = true;
    document.forms['login'].submit();
}

function download(file)
{
	var l = (screen.availWidth - 300) / 2;
    var t = (screen.availHeight - 200) / 2;

	options="toolbar=0,menubar=0,status=0,scrollbars=0,resizable=0,location=0,width=300,height=200,left=" + l +",top=" + t + "";

	download = window.open(file, '_blank', options);
	//download.setTimeout('this.close()', 500);
}

//////////////////////////////////////////////////////////////////////////////////
// _lernBar_openCourse()
// =============================================================================
// Opens a particular course, and navigates to a specific page, if requested.
// The course itself will determine whether it will be allowed to navigate to
// the requested page, based on answered/unanswered questions, as well as
// possible page blocks.
//
// NOTE: From outside of a course, "path" will be interpreted as is. Both
//       relative and absolute paths are allowed.
//
//       Based on the fact that every course has its own Player (residing inside
//       the lernbar folder directly in the course's own directory),
//       interpretation of "path" from inside a course is as follows:
//
//         - an absolute path will be intrepreted and used as is
//         - relative paths will be interpreted as relative to the course's
//           directory itself; this means that "../course_xy" will open the
//           course_xy directory assumed to be on the same level as the
//           current course's own directory. This follows the notion of the
//           course being represented by its "root" (course) directory. This
//           also means that a relative path without the prefix ".." will begin
//           inside the course directory itself.
//
//       The path must not include the "lernbar/index.htm" part, and must not
//       have a trailing slash. Example:
//
//         field_abc/__project_def/__topic_1/__topic_2/__course_xy
// -----------------------------------------------------------------------------
// PARAMETERS:
//  path (String)        - path to the course to be opened (no trailing slash)
//
//  lessonIndex (Number) - 1-based index of the lesson to be opened
//  pageType (String)    - "m" (main), "a" (abstract), or "e" (extension)
//  pageindex1 (Number)  - 1-based index of the main or abstract page
//  pageIndex2 (Number)  - 1-based index of the extension page (pageType == "e")
//
//  userId (String)      - the user's ID, as used for LernBar Portal login (or
//                         null, or empty for public courses)
//  lang (String)		 - langugage of the course
//
// RETURNS:
// 	(none)
//
function _lernBar_openCourse(path, layout, language, id)
{
	if (top && (top.lernBar_navigatorFrame != null))
	{
		if (path.toLowerCase().indexOf("http://") == 0)
		{
			path += "/lernbar/index.html";
		}
		else
		{
			var playerPath = top.location.pathname;

			var lastIndexOfSlash = playerPath.lastIndexOf("/");
			if (lastIndexOfSlash == -1)
			{
				lastIndexOfSlash = 0;
			}
			
			if (navigator.appName.indexOf("Microsoft") != -1)
			{
				playerPath = playerPath.substring(1, lastIndexOfSlash);
			}
			else
			{
				playerPath = playerPath.substring(0, lastIndexOfSlash);
			}
			
			path = playerPath + "/../" + path + "/lernbar/index.html";
		}
	}
	else
	{
		path += "/lernbar/index.html";
	}

	// layout and language
	//
	path += (layout ? "?layout=" + layout : "?layout=infobox");
	path += (language ? "&lang=" + language : "&lang=de");

	// Optionally, the player can receive information about a specific page to be opened:
	// lesson#, pageType (m-main, a-abstract, e-extension), page# (main/abstract), page# (extension)
	//
	path += (id ? "&id=" + id : "&id=51c519e0284964e5831b267976b3338bc746a8");
	
	// Here is a good location to notify the current Navigator of the newly opened player, if
	// necessary.

	PLAYER = window.open(path,
	            "LernBarPlayer" + _lernBar_createUID(),  
      		    "left=" + (screen.width - PLAYER_WIDTH) / 2 + ",top=" + (screen.height - PLAYER_HEIGHT) / 4
	            + ",width=" + PLAYER_WIDTH + ",height=" + (screen.height <= 768 ? 673 : PLAYER_HEIGHT)
	            + ",status=no,toolbar=no,scrollbars=no,resizable=no,menubar=no,hotkeys=no,dependent=no"
	);
	
	if(PLAYER) { PLAYER.focus(); }

}


function _lernBar_openCourseOldReleases(path)
{
	if (top && (top.lernBar_navigatorFrame != null))
	{
		if (path.toLowerCase().indexOf("http://") != 0)
		{
			var playerPath = top.location.pathname;

			var lastIndexOfSlash = playerPath.lastIndexOf("/");
			if (lastIndexOfSlash == -1)
			{
				lastIndexOfSlash = 0;
			}
			
			if (navigator.appName.indexOf("Microsoft") != -1)
			{
				playerPath = playerPath.substring(1, lastIndexOfSlash);
			}
			else
			{
				playerPath = playerPath.substring(0, lastIndexOfSlash);
			}
			
			path = playerPath + "/../" + path;
		}
	}

	
	// Here is a good location to notify the current Navigator of the newly opened player, if
	// necessary.

	PLAYER = window.open(path,
	            "LernBarPlayer" + _lernBar_createUID(),  
      		    "left=" + (screen.width - PLAYER_WIDTH) / 2 + ",top=" + (screen.height - PLAYER_HEIGHT) / 4
	            + ",width=" + PLAYER_WIDTH + ",height=" + (screen.height <= 768 ? 673 : PLAYER_HEIGHT)
	            + ",status=no,toolbar=no,scrollbars=no,resizable=no,menubar=no,hotkeys=no,dependent=no"
	);
	
	if(PLAYER) { PLAYER.focus(); }

}

//////////////////////////////////////////////////////////////////////////////////
// _lernBar_createUID()
// =============================================================================
// Creates a unique, eight-character string, which can be used to open
// several different JavaScript popup windows (LernBar players).
// -----------------------------------------------------------------------------
// PARAMETERS:
//	(none)
//
// RETURNS:
// 	(String): Random, eight-character UID string.
//
function _lernBar_createUID()
{
	var uid = "";
	for (var i = 1; i <= 8; i++)
	{
		uid += Math.floor(Math.random() * 16.0).toString(16);
	}
	
	return uid;
}
