
// Usage: DOMElement(Array, [String, Boolean, Boolean]);

function DivLayer(src, id, hCenter, vCenter) {
	
	this.src 			= src			|| []; // [Object(), Object(), etc...]
	this.id				= id 			|| "DivLayer";
	this.setIsIE();
	this.hCenter 		= hCenter;
	this.vCenter 		= vCenter;
	this.isOpen			= false;
		
	this.node = {};
	this.parent = document.getElementsByTagName("body")[0];
	this.children = [];	
}
DivLayer.prototype = {
	
	create: function() {
		
		// don't create this object if another with the same ID already exits
		if(document.getElementById(this.id)) return;
		
		this.node = document.createElement("div");
		this.node.setAttribute("id", this.id);
		this.node.style.visibility = "hidden";
		this.parent.appendChild(this.node);
		if(this.hCenter == true) this.centerHorizontally(this.leftPos);
		if(this.vCenter == true) this.centerVertically(this.topPos);
		
		this.createChildren();
	},
	
	createChildren: function() {
		
		if(this.src[0] == null) return;		
		
		// add Object nodes to this.children[]
		for(var i = 0; i < this.src.length; i++) {
			
			// parse out textNodes from the src array
			if(this.src[i].nodeType == 3) continue;
			
			// add this node to this.children[] and return index
			var n = this.children.push(document.createElement("div")) - 1;
			
			// set child attributes and append it to the DOM
			this.children[n].setAttribute("id", this.id + "_" + n);
			this.node.appendChild(this.children[n]);
			
			// copy the src html into the new node
			this.children[n].innerHTML = this.src[i].innerHTML;
			
			// remove any IDs from the new child nodes
			var innerNodes = this.children[n].getElementsByTagName("*");
			for(var s = 0; s < innerNodes.length; s++) {
				innerNodes[s].id = null;
			}
		}
	},
	
	remove: function() {
		this.parent.removeChild(this.node);
	},
	
	open: function() {
		if (this.isIE) {
			this.parent.setAttribute("scroll", "no");
		}
		this.node.style.visibility = "visible";
		this.isOpen = true;
	},
	
	close: function() {
		if (this.isIE) {
			this.parent.setAttribute("scroll", "yes");
		}
		this.node.style.visibility = "hidden";
		this.isOpen = false;
	},
	
	centerHorizontally: function() {
		leftPos = (this.parent.clientWidth/2) - (this.node.offsetWidth/2);
		this.node.style.left = (leftPos > 0) ? leftPos + "px" : 0 + "px";
	},
	
	centerVertically: function() {
		h = (typeof window.innerHeight != 'undefined') ? window.innerHeight : document.documentElement.clientHeight;
		topPos = (h/2) - (this.node.offsetHeight/2);
		this.node.style.top = (topPos > 0) ? topPos + "px" : 0 + "px";
	},
	
	setIsIE: function() {
		try { window.attachEvent(); this.isIE = true; }
		catch(e) { this.isIE = false; }
	}
};
