/**
 * @author Ed Slocombe	
 */

var PopUpBox = {
	
	LOADING_TEXT : "loading...",
	CACHE : true,
	MAX_HEIGHT : 190,
	WIDTH : 500,
	HEIGHT : 250,
	
	width: this.WIDTH,
	height : this.HEIGHT,
	contentIsElement : false,
	maxHeightEnabled : false,
	
	box : null,
	iFrame : null,
	title : null,
	content : null,
	prevUrl : null, 
	scrollEvent : false,
	closeCallback : null,
	onOpenCallback : null,

	open :function (title, content, useiFrame, onCloseCallback, onOpenCallback, width, height, maxHeightEnabled) {
		if (!this.box) {
			if (document.getElementById("popUpBox")) this.box = document.getElementById("popUpBox");
			else {
				this.width = (width)? width : this.WIDTH;
				this.height = (height)? height : this.HEIGHT;
				this.box = document.createElement("div");
				this.box.setAttribute("id", "popUpBox");
				document.getElementsByTagName("body")[0].appendChild(this.box);
			}
			
			this.box.style.width = this.width+"px";
			if (height) this.box.style.height = this.height+"px";
				
			if (typeof content == "string") this.content = this.LOADING_TEXT;
		}
		
		this.closeCallback = (onCloseCallback)? onCloseCallback : null;
		this.onOpenCallback = (onOpenCallback)? onOpenCallback : null;
		this.maxHeightEnabled = (maxHeightEnabled != undefined)? maxHeightEnabled : true;
		
		this.title = title;
		this.updateBox();
		this.box.style.display = "inline";
		this.centre();
		
		if (useiFrame == true) {
			// content is url
			this.iFrame = content;
			this.updateBox();
		} else {
			if (typeof content == "string") {
				var url = content;
				if (this.prevUrl != url || !this.CACHE) AJAX.call(url, PopUpBox.setContent, false); 
				this.prevUrl = url;
			} else {
				this.contentIsElement = true;
				// Remove anything in box
				for (var elem in this.box)  this.box.removeChild(elem);
				// Content is element, add element to box
				this.box.appendChild(content);
			}
		}
		
		return false;
	},
	
	close : function() {
		if (PopUpBox.closeCallback) PopUpBox.closeCallback();
		PopUpBox.box.style.display = "none";
	},
	
	centre : function() {
		
		var scrollX = 0, scrollY = 0;
		var windowW = 0, windowH = 0;
		
		if (document.body && typeof document.body.scrollTop != "undefined") {
			scrollX += document.body.scrollLeft;
			scrollY += document.body.scrollTop;
			if (document.body.parentNode && typeof document.body.parentNode.scrollTop != "undefined" && document.body.scrollTop != document.body.parentNode.scrollTop) {
				scrollX += document.body.parentNode.scrollLeft;
				scrollY += document.body.parentNode.scrollTop;
			}
		} else if (typeof window.pageXOffset != "undefined") {
			scrollX += window.pageXOffset;
			scrollY += window.pageYOffset;
		}
		
		if (window.innerHeight) {
			windowH = window.innerHeight;
			windowW = window.innerWidth;
		} else {
			windowH = document.body.clientHeight;
			windowW = document.body.clientWidth;
		}

		if (!this.box) this.box = document.getElementById("popUpBox");
		
		// IE throws an error here but still seems to work fine, so...
		try {
			var offsetHeight = (this.scrollEvent === false)? this.height : this.box.offsetHeight;			
			var x = Math.round(windowW/2 - this.box.offsetWidth/2) + scrollX;
			var y = Math.round(windowH/2 - offsetHeight/2) + scrollY;
			this.box.style.left = x+"px";
			this.box.style.top = y+"px";
		
			if (!this.scrollEvent) {
				this.scrollEvent = true;
				this.addBoxListener(window, PopUpBox.centre, "scroll");
			}			
		} catch (e) { }
	},
	
	setTitle : function(title, updateBox) {
		this.title = title;
		if (updateBox !== false) this.updateBox();
	},
	
	setContent : function(content) {
		if (content == AJAX.ERROR_PAGE_NOT_FOUND) {
			alert("POP-UP: Page not found");
			PopUpBox.close();
			return;
		}
		PopUpBox.content = content;
		PopUpBox.updateBox();
		if (PopUpBox.onOpenCallback) PopUpBox.onOpenCallback();
	},
	
	updateBox : function() {
		
		if (!this.contentIsElement) {
			// Remove everything
			this.box.innerHTML = "";
		
			// Add stuff

			var closeBtn = document.createElement("div");
			closeBtn.id = "popUpBoxClose";
			closeBtn.style.marginLeft = (this.width - 20) + "px";
			this.addBoxListener(closeBtn, PopUpBox.close, "click");
			this.box.appendChild(closeBtn);
			
			var title = document.createElement("div");
			title.className = "sectionHeader";
			title.innerHTML = this.title;
			this.box.appendChild(title);
			
			var content = document.createElement("div");
			content.className = "sectionContent";
			if (this.maxHeightEnabled) content.style.maxHeight = this.MAX_HEIGHT+"px";
			if (!this.iFrame) content.innerHTML = this.content;
			else content.innerHTML = "<iFrame src=\"" + this.iFrame + "\" style=\"border:none\" />";
			this.box.appendChild(content);
		}
		
	},
	
	reOpen : function() {
		this.box.style.display = "inline";
		this.centre();
	},
	
	addBoxListener : function (obj, callback, listener) {
		if (obj.addEventListener) obj.addEventListener(listener, callback, false);
		else obj.attachEvent("on"+listener, callback, false);
	}
	
}
