/**
 * Inline popup window object.
 * 
 * Usage:
 * var popup = new Popup('awesomePopup'); // Optional id for popup
 * var el = document.createElement('p');
 * el.innerHTML = 'Hello there!';
 * popup.render(el);
 */
function Popup(containerId) {
	if (containerId)
		this.popWindow = HTMLDOMUtil.createElement('div', 'popup', containerId);
	else
		this.popWindow = HTMLDOMUtil.createElement('div', 'popup');
}
	// Properties
	Popup.prototype.overlay = true;
	Popup.prototype.overlayClass = null;
	Popup.prototype.addCloseButton = true;
	Popup.prototype.xPos = null;
	Popup.prototype.yPos = null;
	Popup.prototype.closeButtonLabel = 'Lukk vinduet';
	Popup.prototype.closeButtonClass = 'close_button';
	Popup.prototype.renderOutOfBounds = false;
	// Methods
	Popup.prototype.render = popupRender;
	Popup.prototype.setOverlay = popupSetOverlay;
	Popup.prototype.createOverlay = popupCreateOverlay;
	Popup.prototype.close = popupClose;
	Popup.prototype.display = popupDisplay;
	/**
	 * Render a window with the given content
	 *
	 * @param content A DOM element
	 */
	function popupRender(content) {
		this.close();
		if (this.addCloseButton) {
			var closeButton = HTMLDOMUtil.createElement('a', null, null, this.closeButtonLabel);
			closeButton.href = '';
			closeButton.frame = this;
			closeButton.onclick = function() { return this.frame.close(); };
			this.popWindow.appendChild(HTMLDOMUtil.createElement('p', null, this.closeButtonClass, closeButton));
		}
   		this.popWindow.appendChild(content);
		if (this.overlay && !this.renderOutOfBounds)
			this.createOverlay();
		if (this.renderOutOfBounds)
			HTMLDOMUtil.addClass(this.popWindow, 'out_of_bounds');
		// Append window to body element and absolute position it
		document.getElementsByTagName('body')[0].appendChild(this.popWindow);
	}
	/**
	 * Set whether or not the box should add an overlay
 	* to the background while active
	 *
 	* @param value - true/false
	 * @param class - Optional class when value is true
 	*/
	function popupSetOverlay(value, className) {
		this.overlay = value;
		this.overlayClass = className;
	}
	/**
	 * If popup was rendered out of bounds, display it on screen
	 */
	function popupDisplay() {
		HTMLDOMUtil.removeClass(this.popWindow, 'out_of_bounds');
		this.createOverlay();
	}
	/**
	 * Create overlay
	 */
	function popupCreateOverlay() {
		var overlay = HTMLDOMUtil.createElement('div', this.overlayClass, 'overlay');
		var pageSize = HTMLUtil.getPageSize();
		overlay.style.height = pageSize[1] + 'px';
		overlay.popup = this;
		overlay.onclick = function() { return this.popup.close(); };
		hideSelects(true);
		document.getElementsByTagName('body')[0].appendChild(overlay);
	}
	/**
	* Close the popup
	 */
	function popupClose() {
        HTMLDOMUtil.removeIfExists('overlay');
        if (this.popWindow && this.popWindow.parentNode)
            this.popWindow.parentNode.removeChild(this.popWindow);
		hideSelects(false);
		return false;
	}
	/**
	 * IE displays select boxes on top of everything - so they must be hidden
	 * when we pop up info.
	 */
	function hideSelects(hide) {
		var elements = document.getElementsByTagName('select');
		for (var i = 0; i < elements.length; i++) {
			if (hide) {
				HTMLDOMUtil.addClass(elements[i], 'hidden_select');
			} else {
				HTMLDOMUtil.removeClass(elements[i], 'hidden_select');
			}
		}
	}
/**
 * A popup offering the functionality of a Javascript confirm()
 * only customizable through CSS.
 *
 * Usage:
 * var popup = new ConfirmPopup('someId'); // Optional id for outer div
 * popup.render('Do you want to close me?', 'Yes', 'No', func);
 *
 * function func(value) { alert('Your choice: ' + value); }
 */
function ConfirmPopup(containerId) {
	if (containerId)
		this.popWindow = HTMLDOMUtil.createElement('div', 'popup', containerId);
	else
		this.popWindow = HTMLDOMUtil.createElement('div', 'popup');
}
	extend(ConfirmPopup, Popup);
	ConfirmPopup.prototype.renderWindow = popupRender;
	ConfirmPopup.prototype.render = confirmPopupRender;
	ConfirmPopup.prototype.INFO = 'info';
	ConfirmPopup.prototype.WARN = 'warning';
	ConfirmPopup.prototype.ERROR = 'error';
	/**
	 * Render a popup asking the user to confirm or cancel
	 *
	 * @param text     The text asking the question. May be an array where
	 *				   text[0] - header, text[1] - intro text and the rest are assumed p's
	 * @param confirm  The confirm button text, default 'Ja'
	 * @param cancel   The cancel button text, default 'Nei'
	 * @param callback The listener that will be notified when the user makes his choice.
	 *				   Should take a boolean parameter
	 * @param class    The type of of box
	 */
	function confirmPopupRender(text, confirmText, cancelText, callback, confirmClass) {
		if (confirmClass == null)
			confirmClass = this.INFO;
		HTMLDOMUtil.addClass(this.popWindow, confirmClass);
		this.addCloseButton = false;
		if (!(text instanceof Array))
			text = new Array(text);
		var container = HTMLDOMUtil.createElement('div', 'content clear');
		container.appendChild(HTMLDOMUtil.createElement('h2', null, null, text[0]));
		if (text.length > 1)
			container.appendChild(HTMLDOMUtil.createElement('p', 'intro', null, text[1]));
		if (text.length > 2)
			for (var i = 2; i < text.length; i++)
				container.appendChild(HTMLDOMUtil.createElement('p', null, null, text[i]));
		var buttons = HTMLDOMUtil.createElement('div', 'clear', 'confirm_buttons');
		// Confirm button
		if (confirmText) {
			var button = HTMLDOMUtil.createElement('a', null, null, confirmText);
			button.onclick = function(e) { callback(true); popupClose(); return false; };
			button.href = '';
			buttons.appendChild(HTMLDOMUtil.createElement('div', 'button', 'confirm_button', button));
		}
		// Cancel button
		if (cancelText) {
			button = HTMLDOMUtil.createElement('a', null, null, cancelText);
			button.onclick = function(e) { callback(false); popupClose(); return false; };
			button.href = '';
			buttons.appendChild(HTMLDOMUtil.createElement('div', 'button', 'cancel_button', button));
		}
		container.appendChild(buttons);
		this.renderWindow(container);
	}
/**
 * Inline popup window object. Pops up an iframe
 * 
 * Usage:
 * var popup = new IframePopup(myUrl, 'someid'); // optional container id
 * popup.render();
 */
function IframePopup(url, containerId) {
	this.iframeUrl = url;
	if (containerId)
		this.popWindow = HTMLDOMUtil.createElement('div', 'popup', containerId);
	else
		this.popWindow = HTMLDOMUtil.createElement('div', 'popup');
}
	extend(IframePopup, Popup);
	IframePopup.prototype.renderWindow = popupRender;
	IframePopup.prototype.render = iframePopupRender;
	/**
	 * Render a layer with an iframe
	 */
	function iframePopupRender() {
		var iframe = HTMLDOMUtil.createElement('iframe', null, 'external_resource_window');
		iframe.src = this.iframeUrl;
		iframe.frameBorder = '0';
		iframe.scrolling = 'no';
		this.renderWindow(iframe);
   	}

