﻿
//#> POP-UPS
var popUp = function (instanceID) {
	this.instanceID = instanceID;

	this.elements = [];
	this.elements.opener = null;
	this.elements.popDark = null;
	this.elements.popOuter = null;
	this.elements.popInner = null;
	this.elements.windowIcons = [];
	this.elements.windowIcons.max = null;
	this.elements.windowIcons.close = null;

	this.popOpen = false;
	this.timeoutID = 0;
	this.closeDelay = 250;
	this.style = 0;
	this.width = -1;

	this.normalWidth = -1;
	this.normalHeight = -1;

	this.init = function () {
		if (!this.elements.popOuter) {
			var dark = document.createElement("DIV");
			var outer = document.createElement("DIV");
			var inner = document.createElement("DIV");
			var tools = document.createElement("DIV");
			var iconClose = document.createElement("A");
			dark.id = "div_DarkScreen";
			dark.className = "div_DarkScreen";
			dark.style.display = "none";
			eval("dark.onclick = function () { " + this.instanceID + ".closePopUp();return false; }");
			outer.id = "div_PopUp_" + instanceID;
			outer.className = "div_PopUpBox";
			outer.setAttribute("style", "display:none;");
			inner.id = "div_PopUp_" + instanceID + "_Inner";
			tools.className = "div_PopUpTools";
			eval("iconClose.onclick = function () { " + this.instanceID + ".closePopUp();return false; }");
			iconClose.className = "a_Icon a_Close";
			iconClose.innerHTML = "close";
			iconClose.href = "#";
			//iconClose.onmouseover = function () { hb.openBubble(this, "Close"); };
			outer.appendChild(inner);
			tools.appendChild(iconClose);
			outer.appendChild(tools);
			document.body.appendChild(dark);
			document.body.appendChild(outer);

			this.elements.popDark = dark;
			this.elements.popOuter = outer;
			this.elements.popInner = inner;
			this.elements.windowIcons.close = iconClose;
		}
	}
	this.openPopUp = function (e, content, args) {
		window.clearTimeout(this.timeoutID);
		this.elements.opener = e;
		//this.changeStyle(args ? args["style"] : null);
		this.width = args && args["width"] ? args["width"] : -1;
		this.elements.popOuter.style.width = this.width == -1 || this.width == "fullscreen" || this.width == "auto" ? "" : this.width + "px";
		var onComplete = args ? args["onComplete"] || null : null;
		onComplete += ";" + this.instanceID + ".checkPosition();";
		this.elements.popInner.innerHTML = "";
		this.elements.popInner.style.height = "";
		if (content) {
			if (content.substring(0, 5) == "load:") {
				var contentFile = siteRootURL + "assets/content/popups/" + content.replace("load:", "");
				var ajaxLoader = ajax.showAjaxLoader(null, false, 1, "html");
				this.changeContent(ajaxLoader);
				ajax.getRemoteHTML(contentFile, "Sorry, we could not load this content to display.", this.instanceID + ".changeContent('[RESPONSE]')");
			}
			else {
				this.elements.popInner.innerHTML = content;
			}
		}
		var s = this.instanceID + ".keyHandle";
		eval("document.onkeypress = function (event) {" + this.instanceID + ".keyHandle(event); }");
		this.elements.popDark.style.display = "block";
		this.elements.popOuter.style.display = "block";
	}
	this.keyHandle = function (event) {
		event = event || window.event;
		if (event.keyCode == 27 || event.which == 27) this.closePopUp();
		return false;
	}
	this.closePopUp = function (type) {
		document.onkeypress = null;
		this.popOpen = false;
		//if (isIE()) { this.elements.popOuter.style.visibility = "hidden"; return; }
		this.elements.popDark.style.display = "none"
		this.elements.popOuter.style.display = "none"
	}
	this.resizePopUp = function (e, w, h) {
		if (!w && !h) {
			var fullScreen = this.elements.popOuter.getAttribute("fullscreen") != "true";
			if (fullScreen) {
				var newWidth = Math.round(viewportSize()[0] * 0.96);
				var newPosY = Math.round(viewportSize()[1] * 0.02);
				var anim = new YAHOO.util.Anim(this.elements.popOuter.id, {
					width: { to: newWidth },
					marginLeft: { to: (newWidth / 2) * -1 },
					top: { to: newPosY }
				}, 0.2);
				anim.animate();
				this.elements.popOuter.setAttribute("fullscreen", "true");
				if (e) e.className = e.className.replace("a_Maximise", "a_Minimise");
			}
			else {
				var newWidth = this.normalWidth;
				var newPosY = Math.round(viewportSize()[1] * 0.1);
				var anim = new YAHOO.util.Anim(this.elements.popOuter.id, {
					width: { to: newWidth },
					marginLeft: { to: (newWidth / 2) * -1 },
					top: { to: newPosY }
				}, 0.2);
				anim.animate();
				this.elements.popOuter.setAttribute("fullscreen", "false");
				if (e) e.className = e.className.replace("a_Minimise", "a_Maximise");
			}
		}
		else {
			var anim;
			if (w && h) {
				anim = new YAHOO.util.Anim(this.elements.popOuter.id, {
					width: { to: w },
					height: { to: h },
					marginLeft: { to: (w / 2) * -1 }
				}, 0.2);
			}
			else if (w) {
				anim = new YAHOO.util.Anim(this.elements.popOuter.id, {
					width: { to: w },
					marginLeft: { to: (w / 2) * -1 }
				}, 0.2);
			}
			else if (h) {
				anim = new YAHOO.util.Anim(this.elements.popOuter.id, {
					height: { to: h }
				}, 0.2);
			}
			if (anim) anim.animate();
		}
	}
	this.checkPosition = function () {
		var viewport = viewportSize();
		var popOffsetLeft = getDistanceFrom(this.elements.popOuter, "l");
		var popOffsetTop = getDistanceFrom(this.elements.popOuter, "t");
		var popMaxWidth = viewport[0] - (popOffsetLeft * 2);
		var popMaxHeight = viewport[1] - (popOffsetTop * 2);
		var popInnerOffsetVert = this.elements.popOuter.offsetHeight - this.elements.popInner.offsetHeight;
		//alert(this.elements.popInner.firstChild.offsetHeight);
		if (this.elements.popInner.firstChild.offsetHeight > popMaxHeight) {
			this.elements.popInner.style.height = (popMaxHeight - popInnerOffsetVert) + "px";
			this.elements.popInner.style.overflow = "auto";
		}
		else {
			this.elements.popInner.style.height = "";
			this.elements.popInner.style.overflow = "";
		}
	}
	this.init();
}
//#<

