var lightbox = new Object();

lightbox.getAbsoluteOffset = function(obj){
	var offsetX = 0;
	var offsetY = 0;
	var o = obj;
	while(o != document.body.offsetParent){
		offsetX += o.offsetLeft;
		offsetY += o.offsetTop;
		o = o.offsetParent;
	}
	return [offsetX, offsetY];
};


lightbox.show = function (trigger){
	
	var lbcontainer = document.createElement("div");
	el = $(document.body || window);
	var availHeight = el.clientHeight;
	var availWidth = el.clientWidth;
	
	var shield = document.createElement("div");
	shield.className = "js-lightbox-shield";
	shield.style.background = "#000000";
	shield.style.position = "absolute";
	shield.style.zIndex = "999999";
	shield.style.top = "0px";
	shield.style.left = "0px";
	shield.style.width = availWidth + "px";
	shield.style.height = availHeight + "px";

	if (typeof(shield.style.opacity) == "undefined"){
		shield.style.filter = "Alpha(opacity=19)"
	} else {
		shield.style.opacity = ".19";
	}
	shield.style.visibility = "hidden";
	lbcontainer.appendChild(shield);
	lightbox.lbcontainer = lbcontainer;
	lbcontainer.close = function(){
		if (lightbox.lbcontainer){
			lightbox.lbcontainer.lightboxTable.parentNode.removeChild(lightbox.lbcontainer.lightboxTable);
			lightbox.lbcontainer.parentNode.removeChild(lightbox.lbcontainer);
			delete lightbox.lbcontainer;
		}
	};
	if (!trigger.lightboxTable){

		var lightboxTable = document.createElement("table");
		trigger.lightboxTable = lightboxTable;
		lightboxTable.className = "tpl-lightbox";
		var tbody = document.createElement("tbody");
		
			var tr = document.createElement("tr");
				var td = document.createElement("th"); td.className = "tpl-lightbox-nw"; tr.appendChild(td);
				var td = document.createElement("th"); td.className = "tpl-lightbox-n"; tr.appendChild(td);
				var td = document.createElement("th"); td.className = "tpl-lightbox-ne"; tr.appendChild(td);
			tbody.appendChild(tr);
			var tr = document.createElement("tr");
				var td = document.createElement("th"); td.className = "tpl-lightbox-w"; tr.appendChild(td);
				var td = document.createElement("td"); td.className = "tpl-lightbox-content"; tr.appendChild(td); var lightboxContent = td;
				var td = document.createElement("th"); td.className = "tpl-lightbox-e"; tr.appendChild(td);
			tbody.appendChild(tr);
			var tr = document.createElement("tr");
				var td = document.createElement("th"); td.className = "tpl-lightbox-sw"; tr.appendChild(td);
				var td = document.createElement("th"); td.className = "tpl-lightbox-s"; tr.appendChild(td);
				var td = document.createElement("th"); td.className = "tpl-lightbox-se"; tr.appendChild(td);
			tbody.appendChild(tr);
			

		var lightboxTitle = document.createElement("a");
		lightboxTitle.href = "#";
		lightboxTitle.innerHTML = trigger.innerHTML;
		lightboxTitle.className = "tpl-lightbox-title";
		// $(lightboxTitle).click(lightbox.lbcontainer.close)
		lightboxContent.appendChild(lightboxTitle);
		
		var classRegex = /(^|\s+)js-([^\s$]+)-trigger($|\s+)/.exec(trigger.className);
		if (classRegex){
			var cRegex = new RegExp("(^|\\s+)js-" + classRegex[2] + "-content($|\\s+)");
			var allEls = document.getElementsByTagName("*");
			for (var i in allEls){
				if (typeof(allEls[i]) != "object" || !(cRegex.test(allEls[i].className))) continue;
				var chunk = allEls[i];
				chunk.parentNode.removeChild(chunk);
				chunk.style.display = "block";
				lightboxContent.appendChild(chunk);
			}
		}
		
		lightboxTable.appendChild(tbody);
	} else {
		lightboxTable = trigger.lightboxTable;
	}
	lightboxTable.style.zIndex = "999999";
	
	lightboxTable.style.visibility = "hidden";
	lightboxTable.style.top = "0px";
	lightboxTable.style.left = "0px";

	lbcontainer.lightboxTable = lightboxTable;
	lbcontainer.appendChild(lightboxTable);
	
	document.body.appendChild(lbcontainer);
	
	var offsets = lightbox.getAbsoluteOffset(trigger);
	var lightboxTop = offsets[1] - 7;
	var lightboxLeft = offsets[0] - 20;

	lightboxTop = Math.min(lightboxTop, availHeight - 10 - lightboxTable.clientHeight);
	
	lightboxTable.style.top = lightboxTop + "px";
	lightboxTable.style.left = lightboxLeft + "px";

	
	
	shield.style.visibility = "visible";
	lightboxTable.style.visibility = "visible";
}

lightbox.toggleAcc = function (trigger){
	
	jTrigger = $(trigger);
	jGroup = jTrigger.parents(".tpl-personal-group");
	jData = $("dd", jGroup);
	data = jData.get(0);
	
	
	if (jGroup.hasClass("tpl-personal-group-active")){
		jGroup.removeClass("tpl-personal-group-active");
		$("dd", jGroup).slideUp(300);
		return true;
	};
	
	
	$(".tpl-personal-group").each(function(i){
		dynData = $("dd", this).get(0);
		if (!dynData) return true;
		
		if (dynData == data){
			$(this).addClass("tpl-personal-group-active");
			$(dynData).slideDown(300);
		} else {
			$(this).removeClass("tpl-personal-group-active");
			$(dynData).slideUp(300);
		};
	})
	
}


lightbox.dispatcher = new Object();
lightbox.dispatcher.click = function(e){
	var trigger = (e.srcElement||e.target);
	if (/(^|\s+)js-[^\s$]+-trigger($|\s+)/.test(trigger.className)){
		lightbox.show(trigger);
		if (e.stopPropagation) e.stopPropagation();
		if (e.preventDefault) e.preventDefault();
		return false;
	};
	if (/(^|\s+)(js-lightbox-shield|tpl-lightbox-title)($|\s+)/.test(trigger.className) && lightbox.lbcontainer){
		lightbox.lbcontainer.close();
		if (e.stopPropagation) e.stopPropagation();
		if (e.preventDefault) e.preventDefault();
		return false;
	}
};

for (var i in lightbox.dispatcher){
	if (typeof(lightbox.dispatcher[i]) != "function") continue;
	if (document.addEventListener){
		document.addEventListener(i, lightbox.dispatcher[i], false);
	} else {
		document.attachEvent('on' + i, lightbox.dispatcher[i]);
	};
};