// JavaScript Document

function getObj(id) {
	return document.getElementById(id);
}

function getTags(tagname) {
	return document.getElementsByTagName(tagname);
}

function getParent(obj) {
  ntype = (arguments.length)? arguments[1] : '';
	if (ntype) {
		p = obj;
		while(p = p.parentNode) {
			if (p.nodeName == ntype) return p;
		}
		return null;
	}
	else return obj.parentNode;
}

function getChilds(obj) {
	goodChilds = Array();
  ntype = (arguments.length)? arguments[1] : '';
	//alert('Object '+obj.nodeName+' has '+obj.childNodes.length+' childs');
	if (obj.childNodes.length) for (i=0; i < obj.childNodes.length; i++) {
		if (ntype) { // accept tagname <ntype> only
			if (obj.childNodes[i].nodeName == ntype) goodChilds.push(obj.childNodes[i]);
		}
		else if (arguments[2] && arguments[2] == true) { // <ntype> is null, filter for non-#text if 2nd param is true
			if (obj.childNodes[i].nodeName != '#text') goodChilds.push(obj.childNodes[i]);
		}
		else goodChilds.push(obj.childNodes[i]);
	}
	return goodChilds;
}

function getChildsById(obj) {
	var id  = (arguments.length > 1)? arguments[1] : null;
	var classname = (arguments.length > 2)? arguments[2] : null;
	var result = Array();
	var cn;
	//if (obj) alert('finding child "'+id+'" in '+obj.nodeName+' id='+obj.id);
	//if (obj.hasChildNodes()) cn = obj.childNodes;
	cn = obj.childNodes;
	t = '';
	if (cn) for (i=0; i<cn.length; i++) {
		//t += cn[i].nodeName+' "'+cn[i].id+'"\n';
		if (classname) {
			if ((cn[i].id == id) && (cn[i].className == classname)) result.push(cn[i]);
		}
		else {
			if (cn[i].id == id) result.push(cn[i]);
		}
		//if (cn[i].hasChildNodes()) result[result.length] = getChildsById(cn[i],id);
	}
  //alert(t+result.length+' occurences found.');
	return (result.length)? result : null;
}

function getPreviousSibling(obj) {
  tagname = (arguments.length > 1)? arguments[1] : obj.nodeName;
	if (!obj.previousSibling) return null;
	for(i=1; i<=2; i++) {
		obj = obj.previousSibling;
		if (obj && obj.nodeName==tagname) break;
	}
	return obj;
}

function getNextSibling(obj) {
  tagname = (arguments.length > 1)? arguments[1] : obj.nodeName;
	if (!obj.nextSibling) return null;
	while(obj = obj.nextSibling) {
		if (obj.nodeName==tagname) break;
	}
	return obj;
}

function setContent() {
	if (arguments) {
    if ((typeof arguments[0])=='string') {
			obj     = document.getElementById(arguments[0]);
		}
		else {
			obj     = arguments[0];
		}
	}
  if (obj && (typeof arguments[1])=='string') {
		obj.innerHTML = arguments[1];
	}
	else if (obj) {
		obj.innerHTML = arguments[1].innerHTML;
	}
}

function branch(linkobj) {
  var lopn = linkobj.parentNode;
	var lons = linkobj.nextSibling;
	var status_open = 0;
	if (arguments && arguments.length>1) status_open = arguments[1];
	linkobj.onfocus = function() { with(this) { this.blur(); } };
	if (lons) {
		//alert('linkobject\'s nextSibling "'+lons.nodeName+'" with style.display='+lons.style.display+' found.');
		//lopn.removeChild(lons);
		lons.style.display = (lons.style.display=='none')? 'block' : 'none';
  	if (status_open) lons.style.display = 'block';
		//return lons;
	}
	else {
		//alert('parent '+lopn.nodeName+' has '+lopn.childNodes.length+' childnodes.')
		sub_dl = document.createElement("dl");
		sub_dl.setAttribute("class","subtree");
		sub_dl.setAttribute("style","position:relative;clear:both;display:block;left:12px");
		sub_dl.style.left="12px";

    newsub = lopn.appendChild(sub_dl);
		newsub.setAttribute("class","subtree");
		linkobj.onrightclick = function() { alert('This is a '+this.nodeName); }
		//alert('new Child "'+newsub.nodeName+'" with style.display='+newsub.style.display+' created.');
		//alert(expose(newsub.style),'; ');
		//alert(sub_dl.className+''+newsub.className);
		//alert('parent '+lopn.nodeName+' has '+lopn.childNodes.length+' childnodes now.')
		return newsub;
	}
	return null;
}

function get_editbox() {
	editbox = document.getElementById('editbox');
	if (editbox) return editbox;
}

function set_editbox(url) {
	editbox = get_editbox(); //document.getElementById('editbox');
	//alert('editbox.is_open = '+editbox.is_open);
	if (editbox && editbox.is_open) trigger('edit_box_body',url);
}

function set_boxTitle(text) {
	setContent('box_title',text);
}

function open_editbox() {
	if (!editbox) editbox = get_editbox(); //document.getElementById('editbox');
	if (arguments) url = arguments[0];
	if (editbox) {
		editbox.is_open = 1;
  	if (url) set_editbox(url);
		editbox.style.visibility='visible';
		inputs = editbox.getElementsByTagName('INPUT');
		if (inputs) for (i=0; i<inputs.length; i++) {
		  if (inputs[i].getAttribute("type").match(/text/i)) {
		    inputs[i].focus();
		    inputs[i].select();
		    break;
		  }
		}

	}
}

function close_editbox() {
	if (!editbox) editbox = get_editbox(); //document.getElementById('editbox');
	if (editbox) {
    editbox.is_open = 0;
		editbox.style.visibility='hidden';
	}
	if (Picked) Picked.focus();
}

function makeBox(box_url,box_name,box_left,box_top,box_width,box_height) {
	if (document.getElementById(box_name)) document.getElementById(box_name).closeBox();
  bx = new Box(box_name,box_left,box_top,box_width,box_height);
	if (bx) document.body.appendChild(bx);
	setContent(box_name,'<p>&nbsp;Loading...</p>');
	trigger(box_name,box_url);
	return bx;
}

function makeBoxURL(box_url,box_name,box_left,box_top,box_width,box_height) {
  if (!document.getElementById(box_name)) bx = new Box(box_name,box_left,box_top,box_width,box_height);
	if (bx) document.body.appendChild(bx);
	setContent(box_name,'<p>&nbsp;Loading...</p>');
	triggerURL(box_name,box_url);
	return bx;
}


function Box() {
  var name;
	var pos_x;
	var pos_y;
	var dim_w;
	var dim_h;
	var inline_style;
	box = document.createElement('div');
	if (arguments) {
	  this.name  = arguments[0];
	  this.pos_x = arguments[1];
	  this.pos_y = arguments[2];
	  this.dim_w = arguments[3];
	  this.dim_h = arguments[4];
	  this.inline_style = arguments[5];
	}
	this.inline_style = (this.inline_style)? this.inline_style : "position:absolute;left:-10px;top:-40px;width:10px;height:40px";
	if (this.name) box.setAttribute("id",this.name);
	box.setAttribute("style",this.inline_style);
	box.style.left   = this.pos_x+"px";
	box.style.top    = this.pos_y+"px";
	box.style.width  = this.dim_w;
	box.style.height = this.dim_h;
  box.closeBox   = function() {
		if (typeof returnObj!='undefined') returnObj.focus();
		return removeObj(this);
	}
	box.close = box.closeBox;
	//box.onkeydown  = (getKeyFuncs)? getKeyFuncs : {};
  /*box.ondblclick = function() {
																this.closeBox();
															}*/
  return box;
}

function removeObj(obj) { // remove any object or element
  if (obj) if (pn = obj.parentNode) {
	  pn.removeChild(obj);
  	obj = null;
	}
	return obj;
}
