var ADMINMODE = false;

//small script to find the position of something
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

//simple array debug function
function dbgArray(arr, level) {
	if (typeof level == "undefined") {
		level = 0;
	}
	for (ind in arr) {
		if (typeof arr[ind] == "object") {
			dbgArray(arr[ind], level + 1);
		}else{ 
			alert(level + ":    " + ind + ": " + arr[ind]);
		}
	}
}

//Handles opacity highlights... neat effect
var UTIL_HL = ' onmouseover="highlight(this)" onmouseout="dehighlight(this)" ';
function highlight(obj) {
	s = obj.style;
	
	s.opacity = 1;
	s.filter = "alpha(opacity=100)"
}

function dehighlight(obj) {
	s = obj.style;
	
	s.opacity = .5
	s.filter = "alpha(opacity=50)"
}

//Special ease of use html generators
function popup_input(myName, myType, myValue, addnl) {
	if (typeof addnl == "undefined") {
		addnl = "";
	}
	return "<input name=\""+myName+"\" value=\""+myValue+"\" type=\""+myType+"\" class=\"hover_form\" " + addnl + " />";
}


//popup box functions
var POPUPBOX_IDS = new Array();

function destroy_box(obj) {
	var opacity = parseFloat(obj.style.opacity);
	if (opacity == NaN) {
		opacity = .8;
	}
	
	if (opacity < .08) {
		try {
			document.body.removeChild(obj);
		} catch(e) {
		}
		//alert(obj.id);
	}else{
		opacity -= .14;
		obj.style.opacity = opacity;
		obj.style.filter = "alpha(opacity=" + (opacity * 100) + ")";
		
		window.setTimeout(function(){destroy_box(obj);}, 50);
	}
}

function popupFactory() {
	id = "F_" + Math.floor(Math.random() * 1000000);
	
	if (typeof POPUPBOX_IDS[id] == "undefined") {
		var tmp = new popupBox(id);
		return tmp;
	}else{
		return popupFactory();
		
	}
}

function popupBox(id, parent) {
	this.id = id;
	this.children = new Array();
	this.popbox = document.createElement("div");
	this.text = document.createElement("div");
	this.parent = parent;
	
	this.showBox = function(giver, width, offsetx, offsety) {
		if (typeof width == "undefined") {
			width = 250;
		}
		if (typeof offsetx == "undefined") {
			offsetx = 0;
			offsety = 0;
		}
		
		var pos = findPos(giver);
		
		this.popbox.id = this.id;
		this.popbox.className = "hover_box";
		this.popbox.style.left = (pos[0] + offsetx) + "px";
		this.popbox.style.top = (pos[1] + offsety) + "px";
		this.popbox.style.opacity = 1;
		this.popbox.style.width = width + "px";
		
		this.popbox.appendChild(this.text);
		this.popbox.innerHTML += '<div class="box_destroyer" style="opacity: .5; filter: alpha(opacity=50);" onmouseup="'+this.destroyString()+'" '+UTIL_HL+'>[close]</div>';
		
		document.body.appendChild(this.popbox);
	}
	
	this.destroy = function() {		
		//first kill all children )=
		for (var i = 0; i < this.children.length; i++) {
			this.children[i].destroy();
		}
		destroy_box(this.popbox);
		
		
		delete POPUPBOX_IDS[this.id];
		//delete this;
	}
	
	this.destroyString = function() {
		return "POPUPBOX_IDS['" + this.id + "'].destroy();";
	}
	
	this.instance = function() {
		return "POPUPBOX_IDS['" + this.id + "']";
	}
	
	this.generateChild = function() {
		var t = new popupBox(this.id + "_c" + this.children.length, this);
		this.children.push(t);
		
		return t;
	}
	
	this.setText = function(inner) {
		this.text.innerHTML = inner;
		if (typeof this.popbox.childNodes[0] != "undefined" && this.popbox.childNodes[0] != null) {
			this.popbox.replaceChild(this.text, this.popbox.childNodes[0]);
		}
	}
	
	this.getText = function() {
		return this.text.innerHTML;
	}
	
	this.findRoot = function() {
		if (typeof this.parent == "undefined") {
			return this;
		}else{
			return this.parent.findRoot();
		}
	}
	
	this.destroyTree = function() {
		//first find the root of the tree
		var root = this.findRoot();
		
		root.destroy();
	}
	
	//create a reference to this object so it can be referred to later.
	POPUPBOX_IDS[this.id] = this;
	//alert(this.id);
}





