/* -------------------------------------------------------------------------- */
// JavaScript Document
/* -------------------------------------------------------------------------- */

color_opened = "#e9e9e9";
color_closed = "#f0f0f0";

xopened = new Image();
xclosed = new Image();
xpage   = new Image();

xopened.src = "../../javascript/xplorer/xopened.gif";
xclosed.src = "../../javascript/xplorer/xclosed.gif";
xpage.src   = "../../javascript/xplorer/xpage.gif";

function init_nav() {
	for (i = 0; i < document.getElementsByTagName('ul').length; i++) {
		if (document.getElementsByTagName('ul')[i].className == "opened") {
			id = document.getElementsByTagName('ul')[i].parentNode.id;
			toggle_menu(id, false);
		}
	}
	if (window.name.length > 0) load_nav();
}

function toggle_menu(id, save) {
	ul  = "ul_"  + id;
	li  = "li_"  + id;
	img = "img_" + id;
	ul_element  = document.getElementById(ul);
	li_element  = document.getElementById(li);
	img_element = document.getElementById(img);
	if (ul_element) {
		if (ul_element.className == 'closed') {
			ul_element.className = "opened";
			if (li_element) li_element.style.backgroundColor = color_opened;
			img_element.src = xopened.src;
			idnum = eval(id.replace(/menu-id/g, ""));
			if ( typeof(structure_data[idnum]) == "object" ) structure_stat = structure_stat.substr(0,structure_data[idnum][3]) + "1" + structure_stat.substr(structure_data[idnum][3]+1);
		} else {
			ul_element.className = "closed";
			if (li_element) li_element.style.backgroundColor = color_closed;
			img_element.src = xclosed.src;
			idnum = eval(id.replace(/menu-id/g, ""));
			if ( typeof(structure_data[idnum]) == "object" ) structure_stat = structure_stat.substr(0,structure_data[idnum][3]) + "0" + structure_stat.substr(structure_data[idnum][3]+1);
		}
	}
	if (save == true) save_nav();
}

function toggle_open(id, save) {
	var ok = false;
	ul  = "ul_"  + id;
	li  = "li_"  + id;
	img = "img_" + id;
	ul_element  = document.getElementById(ul);
	li_element  = document.getElementById(li);
	img_element = document.getElementById(img);
	if (ul_element) {
		if (ul_element.className == 'closed') {
			ul_element.className = "opened";
			if (li_element) li_element.style.backgroundColor = color_opened;
			img_element.src = xopened.src;
			ok = true;
			idnum = eval(id.replace(/menu-id/g, ""));
			if ( typeof(structure_data[idnum]) == "object" ) structure_stat = structure_stat.substr(0,structure_data[idnum][3]) + "1" + structure_stat.substr(structure_data[idnum][3]+1);
		}
	}
	if (save == true) save_nav();
	return ok;
}

function toggle_close(id, save) {
	var ok = false;
	ul  = "ul_"  + id;
	li  = "li_"  + id;
	img = "img_" + id;
	ul_element  = document.getElementById(ul);
	li_element  = document.getElementById(li);
	img_element = document.getElementById(img);
	if (ul_element) {
		if (ul_element.className == 'opened') {
			ul_element.className = "closed";
			if (li_element) li_element.style.backgroundColor = color_closed;
			img_element.src = xclosed.src;
			ok = true;
			idnum = eval(id.replace(/menu-id/g, ""));
			structure_stat = structure_stat.substr(0,structure_data[idnum][3]) + "0" + structure_stat.substr(structure_data[idnum][3]+1);
		}
	}
	if (save == true) save_nav();
	return ok;
}

function save_nav() {
	var save = "";
	for (var i = 0; i < document.getElementsByTagName('ul').length; i++) {
		if ((document.getElementsByTagName('ul')[i].className == "opened" || document.getElementsByTagName('ul')[i].className == "closed") && document.getElementsByTagName('ul')[i].id != 'root')
			save = save + document.getElementsByTagName('ul')[i].id + "=" + document.getElementsByTagName('ul')[i].className + ",";
	}
	if (save.lastIndexOf(",") > 0) save = save.substring(0, save.lastIndexOf(","));
// this must have been something from the original xplorer code -
// disabled at this point, since frame properties are destroyed ...
//	window.name = save;
}

function load_nav() {
	var items = window.name.split(",");
	if (items.length > 0) {
		for (var i = 0; i < items.length; i++) {
			id_value = items[i].split("=");
			if (id_value.length == 2) {
				id = id_value[0];
				value = id_value[1];
				document.getElementById(id).className = value;
				img = "img_" + id.substring(3, id.length);
				img_element = document.getElementById(img);
				if (value == "closed") {
					img_element.src = xclosed.src;
				} else {
					img_element.src = xopened.src;
				}
			}
		}
	}
}

/* -------------------------------------------------------------------------- */
/* Special functions by Dr. Stefan Immel                       Copyright 2007 */
/* -------------------------------------------------------------------------- */

function folder_status(id) {
	var status = "undefined";
	ul  = "ul_"  + id;
	img = "img_" + id;
	ul_element  = document.getElementById(ul);
	img_element = document.getElementById(img);
	if (ul_element) {
		if        (ul_element.className == 'closed') {
			status = "closed";
		} else if (ul_element.className == 'opened') {
			status = "opened";
		} else {
			status = "unknown";
		}
	}
	return status;
}

function open_all() {
	for (var i = 0; i < structure_data.length; i++) {
		if (structure_data[i][0] == "newfolder") {
			void toggle_open('menu-id' + i, true);
		}
	}
}

function close_all() {
	for (var i = 0; i < structure_data.length; i++) {
		if (structure_data[i][0] == "newfolder") {
			void toggle_close('menu-id' + i, true);
		}
	}
}

function open_list(title) {
	for (var i = structure_data.length-1; i >= 0; i--) {
		if (structure_data[i][0] == "newfolder" && structure_data[i][1] == title) {
			toggle_open('menu-id' + i, true);
			l1 = 0;
			l2 = 0;
			for (var j = i; j > 0; j--) {
				if (structure_data[j][0] == "closefolder") l1--;
				if (structure_data[j][0] == "newfolder"  ) {
					l1++;
					if (l1 > l2) {
						l2 = l1;
						toggle_open('menu-id' + j, true);
					} else {
						toggle_close('menu-id' + j, true);
					}
				}
			}
			break;
		}
	}
}

var timer_id = null;
var timer_count = -1;

function slow_open_all(timer_delay) {
	if (timer_id != null) { window.clearInterval(timer_id); timer_id = null; }
	timer_count = -1;
	if (timer_id == null) timer_id = window.setInterval("slow_open_next()", timer_delay);
}
function slow_open_next() {
	timer_count++;
	if (timer_count < structure_data.length) {
		if (structure_data[timer_count][0]   == "newfolder") {
			if (structure_data[timer_count+1][0] != "closefolder") {
				if (! toggle_open('menu-id' + timer_count, true)) slow_open_next();
			} else {
				slow_open_next();
			}
		} else {
			slow_open_next();
		}
	} else {
		window.clearInterval(timer_id);
		timer_id = null;
		timer_count = -1;
	}
}

function slow_close_all(timer_delay) {
	if (timer_id != null) { window.clearInterval(timer_id); timer_id = null; }
	timer_count = structure_data.length;
	if (timer_id == null) timer_id = window.setInterval("slow_close_next()", timer_delay);
}
function slow_close_next() {
	timer_count--;
	if (timer_count >= 0) {
		if (structure_data[timer_count][0]   == "newfolder") {
			if (structure_data[timer_count+1][0] != "closefolder") {
				if (! toggle_close('menu-id' + timer_count, true)) slow_close_next();
			} else {
				slow_close_next();
			}
		} else {
			slow_close_next();
		}
	} else {
		window.clearInterval(timer_id);
		timer_id = null;
		timer_count = -1;
	}
}

/* -------------------------------------------------------------------------- */

function cookie_save () {
	if (structure_grps != 0) document.cookie = structure_grps + "|" + structure_stat;
}

function cookie_load () {
	if (document.cookie) {
		text = document.cookie;
		n = eval(text.replace(/\|.*$/, ''));
		text =   text.replace(/^.*\|/, '');
		if (n == structure_grps) {
			for (var i = 0; i < structure_grps; i++) {
				if (text.substr(i,1) != structure_stat.substr(i,1)) toggle_menu('menu-id' + structure_list[i], true);
			}
		}
	}
}

/* -------------------------------------------------------------------------- */
