
/**
 * Multi-browser support
 */
function dmenu() {

	this.UseDOM = (typeof(document.getElementById) != 'undefined');
	this.UseDocAll = (typeof(document.all) != 'undefined');
	this.UseLayers = (typeof(document.layers) != 'undefined');
	
	this.EscapeHtml = dmenu_escape_html;

	this.info = dmenu_ignore;
	this.debug = dmenu_ignore;
	this._DebugEnubled = (("" + window.location).indexOf("//pegaso/") >= 0);
	this._dt = "";
	this._dtc = 0;

	this._PreloadArray = new Array();
	this._PreloadArray.count = 0;
	this.imgs = {}

	this.PreloadImage = dmenu_preload_image;

	if (this.UseDOM) {
		this.SetVisible = dmenu_dom_set_visible;
		this.SetImage = dmenu_dom_set_image;
		this.SetCellBgImage = dmenu_dom_set_cell_bg;
		if (this._DebugEnubled) {
			this.info = dmenu_dom_info;
			this.debug = dmenu_dom_debug;
		}
	}
	else if (this.UseDocAll) {
		this.SetVisible = dmenu_nop;
		this.SetImage = dmenu_all_set_image;
		this.SetCellBgImage = dmenu_nop;
		if (this._DebugEnubled) {
			this.info = dmenu_all_info;
			this.debug = dmenu_all_debug;
		}
	}
	else if (this.UseLayers) {
		this.SetVisible = dmenu_lay_set_visible;
		this.SetImage = dmenu_all_set_image;
		this.SetCellBgImage = dmenu_nop;
	}
	else {
		this.SetVisible = dmenu_nop;
		this.SetImage = dmenu_nop;
		this.SetCellBgImage = dmenu_nop;
	}

	this.debug("DOM: " + this.UseDOM + " - DOC.ALL: " + this.UseDocAll + " - Layers: " + this.UseLayers);

}

function dmenu_preload_image(path, id) {
	if (path) {
		this.info("Preloading: " + path);
		var img = new Image();
		img.src = path;
		this._PreloadArray[this._PreloadArray.count++] = img;
		if (id) {
			this.imgs[id] = path;
		}
	}
}

function dmenu_ignore(mex) {

}

function dmenu_nop() {
	this.debug("NOP");
}

function dmenu_dom_set_visible(tagid, display) {
	var obj = document.getElementById(tagid);
	if (obj) {
		obj.style.display = display ? 'block' : 'none';
		obj.style.visibility = display ? 'visible' : 'hidden';
	}
	else {
		this.info(tagid + ' not defined');
	}
}

function dmenu_dom_set_image(tagid, path) {
	var obj = document.getElementById(tagid);
	if (obj) {
		obj.src = path;
	}
	else {
		this.info(tagid + ' not defined');
	}
}

function dmenu_dom_set_cell_bg(tagid, path) {
	var obj = document.getElementById(tagid);
	if (obj) {
		obj.background = path;
		obj.style.backgroundImage = "url(" + path + ")";
	}
	else {
		this.info(tagid + ' not defined');
	}
}
function dmenu_dom_info(mex) {
	this._dt = "<div class=\"info\">" + this.EscapeHtml(mex) + "</div>" + this._dt;
	var obj = document.getElementById("debug");
	if (obj) {
		obj.innerHTML = this._dt;
	}
}
function dmenu_dom_debug(mex) {
	this._dt = "<div class=\"debug\">" + this.EscapeHtml(mex) + "</div>" + this._dt;
	var obj = document.getElementById("debug");
	if (obj) {
		obj.innerHTML = this._dt;
	}
}

function dmenu_all_set_image(tagid, path) {
	var obj = document.images[tagid];
	if (obj) {
		obj.src = path;
	}
	else {
		this.info(tagid + ' not defined');
	}
}

function dmenu_all_info(mex) {
	this._dt = "<div class=\"info\">" + this.EscapeHtml(mex) + "</div>" + this._dt;
	var obj = document.all["debug"];
	if (obj) {
		obj.innerHTML = this._dt;
	}
}

function dmenu_all_debug(mex) {
	this._dt = "<div class=\"debug\">" + this.EscapeHtml(mex) + "</div>" + this._dt;
	var obj = document.all["debug"];
	if (obj) {
		obj.innerHTML = this._dt;
	}
}

function dmenu_lay_set_visible(tagid, display) {
	var obj = document.layers[tagid];
	if (obj) {
		this.info("dmenu_lay_set_visible: " + tagid + " : " + obj.visibility + " => " + display);
		obj.visibility = (display ? 'show' : 'hide');
	}
	else {
		this.info(tagid + ' not defined');
	}
}

function dmenu_escape_html(text) {
	var res = "";
	for (var i = 0; i < text.length; i++) {
		var ch = text.charAt(i);
		if (ch <= '~' && ch != '&' && ch != '<' && ch != '>' && ch != '\"' && ch != '\'') {
			res += ch;
		}
		else {
			res += "&#" + text.charCodeAt(i) + ";";
		}
	}
	return res;
}

/**
 * Menu structure
 */

function MenuItem(lev, mlev) {
	this.lev = lev;
	this.childs = new Array();
	this.count = 0;
	this.level = 0;
	this.parent = null;
	this.add = MenuItem_Add;
	this.mlev = mlev;
}

function MenuItem_Add(child) {
	var i = this.count;
	// bubbles up childrens
	while (i > 0) {
		if (this.childs[i - 1].lev <= child.lev) {
			break;
		}
		this.childs[i] = this.childs[i - 1]
		i--;
	}
	this.childs[i] = child;
	this.count++;
	child.level = this.level + 1;
	child.parent = this;
}

function GetMenuItem(lev) {

	// normalizza lev
	var mlev = lev;
	var idx = mlev.lastIndexOf('.');
	while (idx >= 0 && mlev.substring(idx + 1) == '0') {
		mlev = mlev.substring(0, idx);
		idx = mlev.lastIndexOf('.');
	}

	// l'item esiste gia?
	var flev = 'f' + mlev;
	if (MenuTree[flev]) {
		return MenuTree[flev];
	}

	// crea il nuovo item
	var item = new MenuItem(lev, mlev);
	var parent = MenuTree;
	var idx = item.mlev.lastIndexOf('.');
	if (idx >= 0) {
		parent = GetMenuItem(item.mlev.substring(0, idx));
	}
	parent.add(item)
	MenuTree['f' + item.mlev] = item;
	return item;

}

function AddMenuItem(title, alt_title, img_base, img_roll, img_sel, img_curr, img_home, img_hroll, url, newwin, lev) {
	var item = GetMenuItem(lev);
	item.title = title;
	item.alt_title = alt_title;
	item.img_base = img_base;
	item.img_roll = img_roll;
	item.img_sel = img_sel;
	item.img_curr = img_curr;
	item.img_home = img_home;
	item.img_hroll = img_hroll;
	item.url = url;
	item.newwin = newwin.toLowerCase() != 'false';
	DMenuSupport.debug("added: " + item.mlev + " - " + item.title);
}

var DMenuSupport;
var MenuTree;

function DMenuInit() {
	if (typeof(DMenuSupport) == 'undefined' || DMenuSupport == null) {
		DMenuSupport = new dmenu();
		MenuTree = new MenuItem('', '');
	}
}

DMenuInit();
