var the_subject = the_subsubject = the_chapter = the_slide = the_call = the_additional = found_images_array = '';
var src_hierarchy = '';

if(location.search.substring(1).indexOf('%7C') != -1) search_items = location.search.substring(1).split('%7C');
else search_items = location.search.substring(1).split('|');
if(search_items.length>0){ 
	the_program = search_items[0];
}
if(search_items.length>1){ 
	the_subject = search_items[1];
	var subsubject_hierarchy =  the_program + '|' + the_subject;
}
if(search_items.length>2){ 
	the_subsubject = search_items[2];
	var chapter_hierarchy =  the_program + '|' + the_subject + '|' + the_subsubject;
}
if(search_items.length>3){ 
	the_chapter = search_items[3];
	var slide_hierarchy =  the_program + '|' + the_subject + '|' + the_subsubject + '|' + the_chapter;
}
if(search_items.length>4){ 
	the_slide = search_items[4];
	if(the_slide.indexOf('X') == -1) var src_hierarchy =  the_program + '|' + the_subject + '|' + the_subsubject + '|' + the_chapter + '|' + the_slide;
}
if(search_items.length>5){ 
	the_call = search_items[5];
}
if(search_items.length>6){ 
	the_additional = search_items[6];
}
if(search_items.length>7){ 
	found_images_array = search_items[7].split('??');
}

var folderDepth = 0;
if(the_subject != 0) folderDepth = 1;

function write_subjects(){
	var theText = '';
	for(i = 1;i <= subjects.length-1;i++){
		if(subjects[i].indexOf('*') != -1){ // a window exists
			var titlesArray = subjects[i].split('*');
			theText += '<a href="javascript:openWindow(\'' + titlesArray[1] + '\',' + folderDepth + ',\'600\',\'400\')" onmouseover="this.T_WIDTH=180; return escape(\'' + titlesArray[2] + '\')">' + titlesArray[0] + '</a> ';
		}		
		else if(subjects[i].indexOf('|') != -1){
			var titlesArray = subjects[i].split('|');
			if(the_subject == titlesArray[1]){ // the chosen chapter hilited in red
				theText += '<a href="' + titlesArray[1] + '.html?' + the_program + '|' + titlesArray[1] + '" id="current" onmouseover="this.T_WIDTH=180; return escape(\'' + titlesArray[2] + '\')">' + titlesArray[0] + '</a> ';
			}
			else if(the_subject != 0){ // another subject (the same folder)
				theText += '<a href="../' + titlesArray[1] + '/' + titlesArray[1] + '.html?' + the_program + '|' + titlesArray[1] + '" target="_top" onmouseover="this.T_WIDTH=180; return escape(\'' + titlesArray[2] + '\')">' + titlesArray[0] + '</a> ';
			}
			else{ // a chapter one step down
				theText += '<a href="' + titlesArray[1] + '/' + titlesArray[1] + '.html?' + the_program + '|' + titlesArray[1] + '" onmouseover="this.T_WIDTH=180; return escape(\'' + titlesArray[2] + '\')">' + titlesArray[0] + '</a> ';
			}
		}
		else{ // a chapter not implemented yet
			theText += subjects[i];
		}
	}
	return theText;
}

function write_subsubjects(){
	theText = '';
	for(i = 1;i <= subsubjects.length-1;i++){
		if(subsubjects[i].indexOf('*') != -1){ // a window exists
			var titlesArray = subsubjects[i].split('*');
			theText += '<a href="javascript:openWindow(\'' + titlesArray[1] + '\',' + folderDepth + ',\'' + titlesArray[2] + '\',\'' + titlesArray[3] + '\')" onmouseover="this.T_WIDTH=180; return escape(\'' + titlesArray[4] + '\')">' + titlesArray[0] + '</a> ';
		}		
		else if(subsubjects[i].indexOf('|') != -1){ // a subsubject exists
			var titlesArray = subsubjects[i].split('|');		
			if(the_subsubject == titlesArray[1]){ // the chosen chapter
				theText += '<a href="' + titlesArray[1] + '.html?' + the_program + '|' + the_subject + '|' + titlesArray[1] + '" id="current" onmouseover="this.T_WIDTH=180; return escape(\'' + titlesArray[2] + '\')">' + titlesArray[0] + '</a> ';
			}
			else{ // a chosable chapter
				theText += '<a href="' + titlesArray[1] + '.html?' + the_program + '|' + the_subject + '|' + titlesArray[1] + '" target="_top" onmouseover="this.T_WIDTH=180; return escape(\'' + titlesArray[2] + '\')">' + titlesArray[0] + '</a> ';
			}
		}
		else{ // a chapter not implemented yet
			theText += subsubjects[i];
		}
	}
	return theText;
}

function write_chapter_titles(){
	var theText = '<ol id="chapters-list">';
	for(i = 1;i <= course_titles.length-1;i++){
		var titlesArray = course_titles[i].split('|');
		if(the_chapter == titlesArray[1]){ // this chapter is chosen
		    if(the_slide != ''){
				theText += '<li><a href="' + titlesArray[1] + 1 + '.html?' + chapter_hierarchy + '|' + titlesArray[1] + '|1" id="current">' + titlesArray[0] + '</a></li>';
				theText += write_slideList(); // the slides of the chosen Praktikum
			}
			else{ // the chosen chapter
				theText += '<li><a href="' + titlesArray[1] + 1 + '.html?' + chapter_hierarchy + '|' + titlesArray[1] + '|1" id="current">' + titlesArray[0] + '</a></li>';
				theText += write_slideList(); // the slides of the chosen Praktikum
			}
		}
		else{
				theText += '<li><a href="' + titlesArray[1] + 1 + '.html?' + chapter_hierarchy + '|' + titlesArray[1] + '|1">' + titlesArray[0] + '</a></li>';
		}
	}
	theText += '</ol>';
	return theText ;
}

function write_slideList(){
	var theText = '<ol id="slides-list">';
	for(j = 1;j <= page_elements.length-1;j++){
		var thePageContentArray = page_elements[j].split('|');
		if(the_slide == j){
			theText += '<li><a href="#" id="current">' + thePageContentArray[0];
		}
		else{
			theText += '<li><a href="' + thePageContentArray[1] + '.html?' + slide_hierarchy +'|' + j + '">' + thePageContentArray[0];
		}
		theText += '</a></li>';
	}
	theText += '</ol>';
	return theText;
}

function get_slide_pos(the_slide){
	for(i=1;i<page_elements.length;i++){
		elements_array = page_elements[i].split('|');
		if(the_slide == elements_array[5]) the_slide = i;
	}
	return the_slide;
}

function openWindow(titlesArray1, folderDepth, the_width, the_height, add_src){
	var winURL = '';
	for(i=0;i<folderDepth;i++){
		winURL += '../'
	}
	winURL += 'wins/' + titlesArray1 + '.html';
	if(add_src == 1) winURL += '?' + src_hierarchy + '|' + imageSrc;
	if(add_src == 2) winURL += '?' + the_program + '|' + the_subject + '|' + the_subsubject + '|' + the_chapter + '|' + the_slide;
	var newWin = window.open(winURL, titlesArray1, 'scrollbars=yes,toolbar=no,menubar=no,resizable=yes,width=' + the_width + ',height=' + the_height);
	newWin.focus()
}

function open_compare_win(){
	var winURL = the_subject + '_compare.html?' + the_program + '|' + the_subject + '|' + the_subsubject + '|' + the_chapter + '|' + the_slide + '|chapter';
	var newWin = window.open(winURL,'compare','scrollbars=yes,toolbar=yes,menubar=yes,resizable=yes,width=640,height=650');
	newWin.focus()
}

function write_eval_titles(){
	var theText = '<ol id="chapters-list">';
	for(i = 1;i <= course_titles.length-1;i++){
		var titlesArray = course_titles[i].split('|');
		if(the_chapter == titlesArray[1]){ // this chapter is chosen
			the_chapter = titlesArray[0];
			theText += '<li><a href="' + the_subject + '_' + the_subsubject + '_' + titlesArray[1] + '.html?' + chapter_hierarchy + '|' + titlesArray[1] + '" id="current">' + titlesArray[0] + '</a></li>';
		}
		else{
			theText += '<li><a href="' + the_subject + '_' + the_subsubject + '_' + titlesArray[1] + '.html?' + chapter_hierarchy + '|' + titlesArray[1] + '">' + titlesArray[0] + '</a></li>';
		}
	}
	return theText ;
}


