﻿Type.registerNamespace("Explorica");

Explorica.Popup = function() {
	Explorica.Popup.initializeBase(this);

	this._p = null;
	this._c = null;
	this._w = 400;
	this._h = 200;
	this._offX = 0;
	this._bf = { w: 0, h: 0 };
}

Explorica.Popup.prototype = {
	_rzh: function() { if (this.get_isInitialized()) { for (var i = 3; i < 6; i++) this._p.childNodes[i].style.height = this._h + "px"; } },
	_rzw: function() { if (this.get_isInitialized()) { for (var i = 1; i < 8; i = i + 3) this._p.childNodes[i].style.width = this._w + "px"; } },
	initialize: function() {
		Explorica.Popup.callBaseMethod(this, 'initialize');
		var id = this.get_id();
		if ($get(id) == null) {
			var a, b, c, d, x = new Array(9), p = document.createElement("div");
			p.className = "popup";
			p.id = id;
			p.innerHTML = String.format("<div class=\"topLeft\"></div><div class=\"topMid\" style=\"width:{0}px;\"></div><div class=\"topRight\"></div><div class=\"midLeft\" style=\"height:{1}px;\"></div>" +
				"<div class=\"midMid\" style=\"width:{0}px;height:{1}px;\"><div class=\"xOut\"><img src=\"images/popup/x.gif\" style=\"cursor:pointer;\" /></div><div id=\"{2}_c\" class=\"content\"></div></div>" +
				"<div class=\"midRight\" style=\"height:{1}px;\"></div><div class=\"botLeft\"></div><div class=\"botMid\" style=\"width:{0}px;\"><div id=\"{2}_a\" class=\"arrow\"></div></div><div class=\"botRight\"></div>",
				this._w, this._h, id);
			this._p = p;
			p.getElementsByTagName("img")[0].onclick = Type.createDelegate(this, this.close);
			document.body.appendChild(p);
			for (var i = 0; i < x.length; i++) x[i] = Sys.UI.DomElement.getBounds(p.childNodes[i]);
			p.style.width = (this._w + x[0].width + x[2].width) + "px";
			this._bf = { w: x[3].width + x[5].width, h: x[1].height + x[7].height };
			this._h -= this._bf.h;
			this._w -= this._bf.w;
			this._rzh();
			this._rzw();
			this._c = $get(id + "_c");
			a = $get(id + "_a");
			b = Sys.UI.DomElement.getBounds(a);
			c = Sys.UI.DomElement.getBounds(p);
			this._offX = ((c.width / 5) + (b.width / 2));
			a.style.marginLeft = ((c.width / 5) - (b.width / 2)) + "px";
			p.style.display = "none";
		} else throw "Element with ID \"" + id + "\" already defined.";
	},
	close: function() { this._p.style.display = "none"; },
	show: function(el) {
		this._p.style.display = "";
		if (el) {
			var b = Sys.UI.DomElement.getBounds(this._p), x = Sys.UI.DomElement.getBounds(el);
			this._p.style.top = (x.y - this.get_height()) + "px";
			this._p.style.left = (x.x - this._offX + Math.ceil(x.width / 2)) + "px";
		}
	},
	setContent: function(el) { this._c.innerHTML = ""; this._c.appendChild(el); },
	setContentHTML: function(html) { this._c.innerHTML = html; },
	get_height: function() { return this._h + this._bf.h; },
	set_height: function(h) { if (h !== this._h) { this._h = (h - this._bf.h); this._rzh(); this.raisePropertyChanged('height'); } },
	get_width: function() { return this._w + this._bf.w; },
	set_width: function(w) { if (w !== this._w) { this._w = (w - this._bf.w); this._rzw(); this.raisePropertyChanged('width'); } },
	get_contentHeight: function() { return this._h; },
	get_contentWidth: function() { return this._w; },
	add_close: function(handler) { this.get_events().addHandler("close", handler); },
	remove_close: function(handler) { this.get_events().removeHandler("close", handler); }
}

Explorica.Popup.registerClass("Explorica.Popup", Sys.Component);

if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();