/**
 * ByngUI.DOM Handler
 * 
 * Provides methods to read responses from the framework and use
 * standard UI widgets in the basecode
 * 
 * @copyright Byng Systems LLP
 * 
 * @author Ollie Maitland
 */	
var ByngUIDom = new Class(
{	
	/**
	 * Holds the popup pages within a single popup
	 * 
	 * @param Array
	 */
	popupPage : [],
	
	/**
	 * Holds the stack of popups
	 * 
	 * @param Array
	 */
	popupStack : [],
	
	/**
	 * Holds the difference in z-index between layers
	 * 
	 * @para Int
	 */
	zIndexMargin : 10,
	
	/**
	 * Register an element as the popup container
	 * 
	 * @param DomElement el
	 */
	registerPopup : function (el)
	{
		// set the popup element
		this.popup = el;
		
		// check for response handler
		if (isObject(Byng.ui.response)) {
			// set this for the response handler too
			Byng.ui.response.setPromptContainer (el, this.showPopup);
			Byng.ui.response.setPromptCancel ( this.stepBack );
		}
		return el;
	},
	
	/**
	 * Return the input pop-up element
	 * 
	 * @param DomElement refElement
	 * @return DomElement
	 */
	getInputPopup : function ( refElement )
	{
		var popup = ge('input-popup');
		if ( refElement && refElement.getTop ) {
			popup.setStyle('margin-top' , refElement.getTop() + 'px');
		}
		return popup;
	},
	
	/**
	 * Return the prompt pop-up element
	 * 
	 * @return DomElement
	 */
	getPromptPopup : function ()
	{
		return ge('prompt-popup');
	},
	
	/**
	 * Return the popup object
	 * 
	 * @return DomElement
	 */
	getPopup : function ()
	{
		return Byng.ui.dom.popup;
	},
	
	/**
	 * Show the popup to the world
	 * 
	 * @param String html Push string to another page
	 * @return DomElement
	 */
	showPopup : function (html)
	{
		if (!isUndefined(html) && html != "") {
			// push onto the page stack
			Byng.ui.dom.popupPage.push(Byng.app.getPopup().innerHTML);
			Byng.ui.dom.popup.innerHTML = html;
			
			if (Byng.ui.dom.popupStack.length < 1) {
				// if nothing in the stack then add it
				Byng.ui.dom.popupStack.push(Byng.ui.dom.popup);
			}
			
		} else {
			
			// new popup window
			Byng.ui.dom.popupStack.push(Byng.ui.dom.popup);
			
		}
		
		Byng.ui.dom.doPopupWrap (true);
		Byng.ui.dom.popup.style.display = "block";
		
		if (Byng.ui.dom.loader) {
			Byng.ui.dom.hideLoader();
		}
		
		// fire the onPopup event
		Byng.app.fireEvent('popup', Byng.ui.dom.popup);
				
		return Byng.ui.dom.popup;
	},
	
	/**
	 * Cancel an active popup
	 * 
	 */
	cancelPopup : function () 
	{
		// condition: only one popup in stack
		if (Byng.ui.dom.popupStack.length < 2) {
			Byng.ui.dom.popup.style.display = "none";
			Byng.ui.dom.doPopupWrap (false);
			Byng.ui.dom.popupStack = new Array();
			current = Byng.ui.dom.popup;
		} else {
			current = Byng.ui.dom.popupStack.pop();
			current.style.display = "none";		
		}
	
		// condition: if there is an onCancel method then run it
		if (isObject(current) && current.onCancel) {
			current.onCancel();
		}
					
		
		// Truncate popup pages
		Byng.ui.dom.popupPage = new Array ();

		// condition: more than one pop-up active
		if (Byng.ui.dom.popupStack.length < 1) {
			Byng.ui.dom.doPopupWrap (false);
			Byng.ui.dom.popupStack = new Array();
		} else {

			// otherwise just remove the uppermost				
			Byng.ui.dom.popup = Byng.ui.dom.popupStack.pop();
			
			Byng.ui.dom.popupStack.push (Byng.ui.dom.popup);
		}
	},
	
	/**
	 * Step back in the list of tabs
	 * 
	 */
	stepBack : function () 
	{
		// if there are 1 or less pages then cancel the dialog
		if (Byng.ui.dom.popupPage.length < 2) {
			return Byng.ui.dom.cancelPopup();
		}
		
		// retreive last page of popup
		var html = Byng.ui.dom.popupPage.pop();
		if (html == ""){
			 return Byng.ui.dom.cancelPopup();
		}
		
		Byng.ui.dom.popup.innerHTML = html;
	},

	/**
	 * Show and hide the pop-up background
	 * 
	 * @param Boolean bool
	 */
	doPopupWrap : function (bool) 
	{		
		if (bool == true) {
			display = "block";
		} else {
			display = "none";
		}
		
		var fade = true;
		if (getBrowser() == "MSIE") {
			if (getBrowserVersion() < 7) {
				fade = false;
			}
		}
		
		if (fade == true) {
			$(ELEM_POPUP_WRAP).setStyle('display', display);
		}
	},
	
	/**
	 * Add a new loading image icon
	 * 
	 * @param HTMLElement el Parent element
	 * @param String src Image source
	 */
	showLoader : function (el, src)
	{
		var img = new Element('img', {'src' : src, 'alt' : 'loading...'});
			img.addClass('loader').setStyle('position','absolute');
		el.appendChild (img);
		this.loaderParent = el;
		this.loader = img;
		return img;
	},
	
	/**
	 * Hide the loader
	 * 
	 */
	hideLoader : function ()
	{
		// remove if exists
		if (this.loader) this.loaderParent.removeChild ( this.loader );
		// clear the references to the DOM elements
		this.loader = null;
		this.loaderParent = null;			
	},
	
	/**
	 * Write the status to the windows
	 * 
	 * @param String s
	 */
	writeStatus : function (s) 
	{
		window.status = s;		
	}
		
});