// Some Flash-movies do not work with relative argument paths.
// cpath is a variable that is defined in each HTML-file that calls this JavaScript-file
var r = document.location.pathname

var coursepath = "";
if (typeof(cpath) == "undefined") {
	alert( "This course page does not have a required variable definition.\nPlease, contact course administration to correct the situation." );
}
else
{
	coursepath = r.substring(0,r.lastIndexOf("/")+1) + cpath;
}

// Cookie will be valid for this many days
var expire_days = 30;

// forms the cookie date format correctly
// argument: offset to future in milliseconds
function cookieDate(timeoffset) {
	var today = new Date();
	var newdate = new Date();
	newdate.setTime(today.getTime() + timeoffset);
	
	var wdy = newdate.toGMTString().substr(0,3);
	var dd = newdate.getDate();
	if (dd < 10)
		dd = '0' + dd.toString();
	var mm = newdate.toGMTString().substr(8,3);
	var yy = newdate.getFullYear().toString();
	var cookiedate = wdy + ', '+dd+'-'+mm+'-'+yy+' 00:00:00 GMT';
	return cookiedate;
}

// The learning path Flash calls this when user clicks an item.
// This updates the cookie and changes to the given location.
// arguments: idx - position of the clicked link
//            url - the URL to go to
function openUrl(idx, url) {
	var aVisited=used_paths.split("|");
	aVisited[idx] = 1;
	used_paths=aVisited.join('|');
	
	document.cookie = 'used_paths=' + used_paths + ';EXPIRES=' + cookieDate(expire_days*24*60*60*1000) + ';PATH=/'; 
	
	// change location
	document.location=url;
}

// When the user creates/updates the learning path, that Flash calls this function.
// This forms the cookies and resets the "visited" indicator
function buildCookie(paths) {

	var expire = cookieDate(expire_days*24*60*60*1000);

	document.cookie = 'learning_paths=' + paths + '; expires=' + expire + '; path=/';
	// reset visited links parameter
	document.cookie = 'used_paths=0|0|0|0|0|0|0|0|0|0; expires=' + expire + '; path=/';

	// for some unknown reason we cannot use history to go back where we were... 
	// so go to opening page
	document.location="introduction.html";
}

// read the cookie and parse it for Flash-parameters
var tmp=document.cookie;

// get path and used -parameters from cookie
var re_learn = /learning_paths=([^\;]*)/g; //regular expression
var learn = tmp.match(re_learn);

var re_used = /used_paths=([^\;]*)/g; //regular expression
var used = tmp.match(re_used);

var path_exists = false;

var learn_paths="";
if (learn != null)
{
	path_exists = true;
	learn_paths = learn[0].substr(15,learn[0].length-1); // extract the data
}
var used_paths="";
if (used != null)
	used_paths = used[0].substr(11,used[0].length-1); // extract the data

// This functions writes the Flash-object code and inserts required parameters
// argument: rootpath - the path to "course root"

function writeLearnFlash() {
	if (path_exists == true) 
	{
		// Here we calculate the y-size for the LP-flash
		var base_y = 200; // LP's solid parts
		var elem_y = 10; // one link element requires this much y

		var cy = learn_paths.split("|").length * elem_y; // n:o of elements * space for one
		var y = base_y + cy; // total height
		
		// tämänhetkinen kiinteä y:n arvo, poista tämä rivi kun dynaaminen otetaan käyttöön
		y = 270; 
		document.write('<a href="javascript:setUrl();"></a><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"  WIDTH="165" HEIGHT="'+y+'" id="learPath_show" align=""> <param name=movie value="'+coursepath+'flash/learPath_show.swf?rootpath='+coursepath+'&path='+coursepath+'flash/&document=path_list&linkpaths='+learn_paths+'&usedpaths='+used_paths+'&alterurl='+coursepath+'my_tutorial.html&size='+y+'"> <param name=quality value=high> <param name=bgcolor value=#FFFFFF> <embed src="'+coursepath+'flash/learPath_show.swf?rootpath='+coursepath+'&path='+coursepath+'flash/&document=path_list&linkpaths='+learn_paths+'&usedpaths='+used_paths+'&alterurl='+coursepath+'my_tutorial.html&size='+y+'" quality=high bgcolor=#FFFFFF   WIDTH="165" HEIGHT="'+y+'" name="learPath_show" align="" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>');
	}
	else
	{
		document.write('<br><a href="'+coursepath+'my_tutorial.html"><img src="'+coursepath+'images/plp_teaser.gif" width="165" height="146" border="0" alt="Create your own Tutorial!"></a>');
	}
}

// This functions writes the Flash-object code and inserts required parameters
// arguments: flashfile   - the name of the Flash-template
//            xmldocument - the name of the XML-document to be given as argument
//            w           - width
//            h           - height
function writeTemplateFlash(flashfile, xmldocument, w, h) {
	document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+w+'" height="'+h+'" id="" align=""><param name=movie value="'+coursepath+'flash/'+flashfile+'?path='+coursepath+'flash/&document='+xmldocument+'"><param name=quality value=high><param name=bgcolor value=#FFFFFF><embed src="'+coursepath+'flash/'+flashfile+'?path='+coursepath+'flash/&document='+xmldocument+'" quality=high bgcolor=#FFFFFF  width="'+w+'" height="'+h+'" align="" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>');
}

