
function NavEdit() {
	this.ICON_SIZE = 14;
	this.PREVENT_HIDE = 0;
	this.TIMEOUT = null;
	
	// prefix = 'NavExit_';
	
	this.elements = new Array(
		'div_controls',     // NavEdit_div_controls

		'NavEdit_MoveItemUp',     // NavEdit_link_up
		'NavEdit_MoveItemDown',   // NavEdit_link_down
		'NavEdit_InsertItemAbove',   // NavEdit_link_new_above
		'NavEdit_InsertItemBelow',   // NavEdit_link_new_below

		'div_NavEditDialog',// NavEdit_div_properties
		// SPOT 3
		'NavEdit_DeleteItem',       // NavEdit_NavEdit_DeleteItem

		// OUTLINE 3
		'NavEdit_div_outline',
		'NavEdit_HideItem',
		'NavEdit_UnhideItem',
		'NavEdit_EditItem',
		'NavEdit_CutItem',
//		'NavEdit_CopyItem',
		'NavEdit_PasteAbove',
		'NavEdit_PasteBelow',
		'NavEdit_PasteChild',
		'NavEdit_spanDisplayId'

	);
	
}

/////////////////////////////////////////
// global oNavEdit instance
/////////////////////////////////////////


var oNavEdit;

NavEdit.init = function(id) {
	oNavEdit = new NavEdit();
	oNavEdit.init(id);
}

NavEdit.prototype.init = function(id) {

	this.id = id;
	
	for(var i=0; i<this.elements.length; i++) {
		if (document.getElementById(this.elements[i]))
			this[this.elements[i]] = document.getElementById(this.elements[i]);
		else {
			if (this.elements[i] != 'NavEdit_PasteChild'
				 && this.elements[i] != 'NavEdit_PasteAbove'
				 && this.elements[i] != 'NavEdit_PasteBelow'
				 && this.elements[i] != 'NavEdit_InsertItemAbove'
				 && this.elements[i] != 'NavEdit_InsertItemBelow'
				) {
				alert('oNavEdit: Element "'+this.elements[i]+'" missing in document!');
			}
		}
	}
/*
	var icon = document.getElementById('icon');
	var NavEdit_MoveItemUp = document.getElementById('NavEdit_MoveItemUp');
	var NavEdit_MoveItemDown = document.getElementById('NavEdit_MoveItemDown');

	var link_new_above = document.getElementById('link_new_above');
	var link_new_below = document.getElementById('link_new_below');
*/
}

NavEdit.prototype.getpos = function(object) {
	var obj = object;
	var pos = new Object();
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;

	pos.x = curleft;

	obj = object;

	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;

	pos.y = curtop;
	
	return pos;
}



NavEdit.prototype._link_get_var = function(link, name) {
	re = /(?:\?|\&)id=(\d+)/;
	m = re.exec(link.href);
	if (m)
		return m[1]
	else
		return false;
}

NavEdit.prototype._link_set_var = function(link, name, value) {
	re = new RegExp("(\\?|\\&)"+name+"=[^\\&]*");

	if (re.test(link.href))
		// replace existing value
		link.href = link.href.replace(re, "$1"+name+"="+value);
	else {
		var q = link.href.lastIndexOf("?");

		if (q > -1) {
			// there is a '?'
			if (q < link.href.length-1)
				// append '&' if '?' is not the last char
				link.href += "&";
		} else {
			// there is no '?'
			link.href += "?";
		}
		link.href += name+"="+value;
	}
}

// mouse over link

NavEdit.prototype.l_over = function(link) {

	var node_id = this._link_get_var(link, 'id');
	
	if (!node_id) return;
	
	this._link_set_var(this.NavEdit_MoveItemUp, 'node_id', node_id);
	this._link_set_var(this.NavEdit_MoveItemDown, 'node_id', node_id);
	this._link_set_var(this.NavEdit_HideItem, 'node_id', node_id);
	this._link_set_var(this.NavEdit_UnhideItem, 'node_id', node_id);
	this._link_set_var(this.NavEdit_DeleteItem, 'node_id', node_id);
	this._link_set_var(this.NavEdit_CutItem, 'node_id', node_id);

//	this._link_set_var(this.NavEdit_CopyItem, 'node_id', node_id);

	if (this.NavEdit_PasteAbove)
		this._link_set_var(this.NavEdit_PasteAbove, 'node_id', node_id);
	if (this.NavEdit_PasteBelow)
		this._link_set_var(this.NavEdit_PasteBelow, 'node_id', node_id);
	if (this.NavEdit_PasteChild)
		this._link_set_var(this.NavEdit_PasteChild, 'node_id', node_id);

/*	
	var cgi_node_id = '&node_id='+id;

	this.NavEdit_MoveItemUp.href += cgi_node_id;
	this.NavEdit_MoveItemDown.href += cgi_node_id;
	// SPOT 1
	this.NavEdit_DeleteItem.href += cgi_node_id;
	// TODO: only handle necessary
	this.NavEdit_HideItem.href += cgi_node_id;
	this.NavEdit_UnhideItem.href += cgi_node_id;
*/

/*
	// replace 'id' with 'node_id'
	var href = link.href.replace(/(\?|\&)id=/, '$1node_id=');

	this.NavEdit_MoveItemUp.href = href + '&NavEdit_cmd=MoveUpItem&id=' + this.id;
	this.NavEdit_MoveItemDown.href = href + '&NavEdit_cmd=MoveDownItem&id=' + this.id;
	// SPOT 1
	this.NavEdit_DeleteItem.href = href + '&NavEdit_cmd=DeleteItem&id=' + this.id;
	// TODO: only handle necessary
	this.NavEdit_HideItem.href = href + '&NavEdit_cmd=HideItem&id=' + this.id;
	this.NavEdit_UnhideItem.href = href + '&NavEdit_cmd=UnhideItem&id=' + this.id;

	this.link_new_above.href = 'javascript:void(0)';
	this.link_new_below.href = 'javascript:void(0)';
*/	

	// get NavEdit link attributes
	
	var attributes = link.getAttribute('NavEdit').split(",");

	// attributes: first, last, hide, pos
	this.current = new Object();
	for(var i=0; i<attributes.length; i++) {
		var keyval = attributes[i].split("=");
		if (keyval[0] == 'pos') val = parseInt(keyval[1]);
		else val = keyval[1];
		this.current[keyval[0]] = val;
	}

	// for + above/below links
	//this.current_position = parseInt(link.getAttribute('NavEditPos'));

	//this.current_link_id = parseInt(link.getAttribute('NavEditLinkId'));

	this.current_link = link;
	this.current_value = link.getAttribute('NavEditValue');
	
	//var status = link.getAttribute('NavEditStatus');

	if (this.current.root)
		var display = 'none';
	else
		var display = 'inline';

	// SPOT
	// hide unhide
	this.NavEdit_HideItem.style.display = this.current.hide ? 'none' : display;
	this.NavEdit_UnhideItem.style.display = this.current.hide ? display : 'none';

	// up down
	this.NavEdit_MoveItemUp.style.visibility = this.current.first ? 'hidden' : 'visible';
	this.NavEdit_MoveItemDown.style.visibility = this.current.last ? 'hidden' : 'visible';

	// show id
	if (this.NavEdit_spanDisplayId)
		this.NavEdit_spanDisplayId.innerHTML = "&nbsp;ID: <b>"+node_id+"</b>&nbsp;";

	// position of the link
	var pos = this.getpos(link);
	//alert(icon.style.top);
	
	pos.width = link.offsetWidth;
	pos.height = link.offsetHeight;
	
	this.current_link_pos = pos;

	var vcenter = parseInt(pos.y + pos.height / 2 - this.ICON_SIZE / 2);

	var vtop, vbottom;
	if (pos.height > this.ICON_SIZE) {
		vtop = parseInt(pos.y - this.ICON_SIZE / 2);
		vbottom = parseInt((pos.y + pos.height) - this.ICON_SIZE / 2);
	} else {
		vtop = parseInt(vcenter - this.ICON_SIZE / 2);
		vbottom = parseInt(vcenter + this.ICON_SIZE / 2);
	}

//	vtop = parseInt(pos.y + pos.height / 2 - this.ICON_SIZE / 2);
	//alert(vcenter);
	
	var div_height = pos.height + 2 *  this.ICON_SIZE;
	
	// TODO: optimize (vcenter)
	var div_vpos = parseInt(pos.y + pos.height / 2 - div_height / 2);;
	var but_delta;
	
	if (div_height < 2 * this.ICON_SIZE) {
		div_height = 2 * this.ICON_SIZE;
		but_delta = 0;
	} else {
		but_delta = pos.height; // - this.ICON_SIZE;
	}
	

	this.showElement(this.NavEdit_CutItem, display, 0, 0);

	this.showElement(this.NavEdit_EditItem, display, 0, 0);

	this.showElement(this.NavEdit_MoveItemUp, display, 0, 0);

	// SPOT 2
	this.showElement(this.NavEdit_DeleteItem, display, 0, 0);
	
	if (this.NavEdit_InsertItemAbove)
		this.showElement(this.NavEdit_InsertItemAbove, display, 0, 0);

	if (this.NavEdit_InsertItemBelow)
		this.showElement(this.NavEdit_InsertItemBelow, display, 0, but_delta);
		
	if (this.NavEdit_PasteBelow)
		this.showElement(this.NavEdit_PasteBelow, display, 0, but_delta);
	
	this.showElement(this.NavEdit_MoveItemDown, display, 0, but_delta);

	this.showElement(this.NavEdit_spanDisplayId, display, 0, but_delta);

	this.showElement(this.div_controls, 'block', pos.x, div_vpos, 0, div_height);


	// OUTLINE 4
	this.showElement(this.NavEdit_div_outline, 'block', 0, this.ICON_SIZE-1, pos.width, pos.height);
	

	this.PREVENT_HIDE++;
}

NavEdit.prototype.click_current_link = function(link) {
	window.location.href = this.current_link.href;
}

NavEdit.prototype.click_edit  = function(link) {

	var form = document.getElementById('form_NavEditItem');
	
	form['mode'].value = 'edit';
	form['position'].value = '0';
	form['node_id'].value = this.current.id;
	form['title'].value = this.current_value;
	
	pos = this.current_link_pos;
	
	this.showElement(this.div_NavEditDialog, 'block', pos.x+pos.width, pos.y);

	form['title'].focus();

}

NavEdit.prototype.click_new_child  = function(link) {

	var form = document.getElementById('form_NavEditItem');
	
	form['mode'].value = 'add_child';
	form['position'].value = 1;
	form['node_id'].value = this.current.id;
	
	pos = this.current_link_pos;
	
	this.showElement(this.div_NavEditDialog, 'block', pos.x+pos.width, pos.y);

	form['title'].focus();

}

NavEdit.prototype.click_new_above  = function(link) {

	var form = document.getElementById('form_NavEditItem');
	
	form['mode'].value = 'add_sibling';
	form['position'].value = this.current.pos - 1;
	form['node_id'].value = this.current.id;
	form['title'].value = '';
	
	pos = this.current_link_pos;
	
	this.showElement(this.div_NavEditDialog, 'block', pos.x+pos.width, pos.y);

	form['title'].focus();

}

NavEdit.prototype.click_new_below  = function(link) {

	var form = document.getElementById('form_NavEditItem');

	form['mode'].value = 'add_sibling';
	form['position'].value = this.current.pos + 1;
	form['node_id'].value = this.current.id;
	form['title'].value = '';

	pos = this.current_link_pos;
	
	this.showElement(this.div_NavEditDialog, 'block', pos.x+pos.width, pos.y+pos.height);

	form['title'].focus();
	
}

NavEdit.prototype.cancel_edit_item_dialog  = function(link) {

	this.hideElement(this.div_NavEditDialog);
	
}

// TODO: check element type and choose display
NavEdit.prototype.showElement = function(element, display, x, y, w, h) {
	// TODO: allow percentage %
	if (w) element.style.width = w+'px';
	if (h) element.style.height = h+'px';

	element.style.left = x+'px';
	element.style.top = y+'px';
	element.style.display = display;
}

NavEdit.prototype.hideElement = function(element) {
	element.style.left = '0px';
	element.style.top = '0px';
	element.style.display = 'none';
}


NavEdit.prototype.l_out = function(link) {
	this.PREVENT_HIDE--;
//	hideIcons();
}

/*
NavEdit.prototype.hideIcons() {
	if (this.TIMEOUT) clearTimeout(this.TIMEOUT);
	this.TIMEOUT = setTimeout(hideIconsTimeout, 20);
}
*/

NavEdit.prototype.hideIcons = function() {
	if (this.PREVENT_HIDE == 0) {
		this.div_controls.style.display = 'none';
		if (this.NavEdit_InsertItemAbove)
			this.NavEdit_InsertItemAbove.style.display = 'none';
		if (this.NavEdit_InsertItemBelow)
			this.NavEdit_InsertItemBelow.style.display = 'none';
	}
	this.TIMEOUT = null;
}

NavEdit.prototype.d_over = function(link) {
	this.PREVENT_HIDE++;
}

NavEdit.prototype.d_out = function(link) {
	this.PREVENT_HIDE--;
//	hideIcons();
}

NavEdit.prototype.b_over = function() {
	this.hideIcons();
}

NavEdit.prototype.debug = function(text) {
	//var div = document.getElementById('debug');
	//div.innerHTML += text+"<br>\n";
}

