/**
 * PopupBox constructor
 * @constructor
 * @param {String} title The title of the poupbox
 * @param {String} content The content of the poupbox
 * @param {String} id The dom identifier to use in the popupboxes
 * @param {Boolean} flashContent Set to true or false
 */
function PopupBox(title, content, id)
{
	if (typeof id != "string") 
	{
		id = null;
	}

	// Private
	var dom				= new DOM(),
		active			= false,
		attachedTo		= null;

	// Public
	this.setTitle		= setTitle;
	this.setDescription = setDescription;
	this.setContent		= setContent;
	this.show			= show;
	this.close			= close;
	this.setFixedY 		= setFixedStartHeight;

	if (PopupBox.instance != undefined)
	{
		PopupBox.instance.close();

		delete PopupBox.instance;
	}

	var descriptionElement = dom.create(
		"div",
		{id : "popup_description"},
		[""]
	);

	PopupBox.instance = this;

	/**
	 * Reference to a created popupbox
	 * @field
	 */
	var popupBox = dom.create(
		"div",
		{
			className	: "popupbox",
			id			: id
		},
		[
			dom.create(
				"div",
				{
					className : "popupbox_title"
				},
				[
					dom.create(
						"h2",
						null,
						[title]
					),
					dom.create(
						"a",
						{
							href	: "#",
							onclick	: close
						},
						["Sluiten"]
					)
				]
			),
			dom.create(
				"div",
				{
					className : "popupbox_content"
				},
				[
					dom.create(
						"div",
						{
							className : "popupbox_content_wrapper"
						},
						content
					)
				]
			),
			dom.create(
				"div",
				{
					className : "popupbox_footer"
				},
				[
					descriptionElement,
					dom.create(
						"a",
						{
							href	: "#",
							onclick	: close
						},
						["Sluiten"]
					)
				]
			)
		]
	);

	/**
	 * Set the title of the PopupBox
	 * @param {String} title The title of the PopupBox as a string.
	 */
	function setTitle(title)
	{
		if (typeof title != "string") 
		{
			throw new Error("Invalid argument: title is missing or not a string");
		}

		popupBox.getFirstByTagName("h2").firstChild.nodeValue = title;
	}

	/**
	 * Set the description of the PopupBox
	 * @param {String} title The description of the PopupBox as a string.
	 */
	function setDescription(description)
	{
		if (typeof description != "string") 
		{
			throw new Error("Invalid argument: title is missing or not a string");
		}

		descriptionElement.firstChild.nodeValue = description;
	}

	/**
	 * Set the content of the PopupBox
	 * @param {String} content The content of the PopupBox as a string.
	 * @param {Boolean} useDom By default, the content is supplied as a string. Set this parameter to true for use of DOM objects (as an array)
	 */
	function setContent(content, useDom)
	{
		if (typeof useDom != "boolean") 
		{
			useDom = false;
		}

		if (typeof content != "string" && useDom == false) 
		{
			throw new Error("Invalid argument: content is missing or not a string");
		}

		var contentWrapper = popupBox.getByClassName("popupbox_content_wrapper", "div")[0];
		
		if (useDom == true)
		{
			var contentWrapperReplacement = dom.create(
				"div",
				{
					className : "popupbox_content_wrapper"
				},
				content
			);
		}
		else
		{
			var contentWrapperReplacement = dom.create(
				"div",
				{
					className : "popupbox_content_wrapper"
				}
			);

			contentWrapperReplacement.innerHTML = content;
		}

		contentWrapper.parentNode.replaceChild(contentWrapperReplacement, contentWrapper);
	}



	/**
	 * Set a fixed position of the PopupBox form the top of the viewscreen used in navigon flash popup
	 * @param {Number} fromTop The position from the top of the viewscreen.
	 */
	function setFixedStartHeight(fromTop)
	{
		// Set position
		popupBox.style.top	= document.documentElement.scrollTop+document.body.scrollTop + fromTop + "px";
	}


	/**
	 * Show the PopupBox
	 * @param {Object} attachTo The object the popupbox is attached to
	 * @return {Boolean} false
	 */
	function show(attachTo)
	{
		if (active == false)
		{
			active = true;

			fade();

			dom.getFirstByTagName("body").appendChild(popupBox);
		}

		return false;
	}

	/**
	 * Close the PopupBox
	 * @return {Boolean} false
	 */
	function close()
	{
		if (active == true)
		{
			active = false;

			popupBox.remove();

			fade();
		}

		return false;
	}
}
