/**
 * ByngPanel engine class
 * 
 * Provides base methods for a front panel
 * 
 * @requires Mootools-1.2.1
 * 
 * @author Ollie Maitland
 * @copyright Byng Systems LLP
 */
Byng.register('byng.panel', 
{
	Implements : [ByngView],
	Context		: 'abstract',
	
	pods : null,
	
	/**
	 * Initialise the entity panel
	 * 
	 */
	initialize : function ()
	{
		switch (this.getProperty('view')) {
			case "attachments":
			
				break;
			case "panel":
				if (Byng.library.has('crm.comms.pod')) {
					this.comms = Byng.init('crm.comms.pod', this);
					this.comms.meta = this.meta;
				}
		}
		
		// condition: pods available
		if (Byng.pods) {
			this.pods = new Hash();
			var pod = null;
			Byng.pods.each(function(options, ns) {
				// instantiate a pod
				pod = Byng.init(ns, this, options);
				// check for the alias
				var alias = pod.getProperty('alias');			
				// put the pod into the hash map
				this.pods.set((alias ? alias : ns), pod);
			}.bind(this));
			delete Byng.pods;
		}
	},
	
	/**
	 * Popup
	 * 
	 * @param {String} screen
	 */
	popup : function (screen)
	{
		Byng.input.popup({'request' : this.getRequest(screen).addParam('contractorId', this.subject.identifiers.userId)});
	},
	
	/**
	 * Get a document submission instance
	 * 
	 * @param {Integer} docId
	 */
	getDocument : function ( docId )
	{
		var doc = new DocumentSubmission(docId);
 			doc.setUserId(this.subject.identifiers.userId);
 
 		return doc;
	},
	
	/**
	 * Set the subject meta data
	 * 
	 * @param {Object} subject
	 */
	setSubject : function (subject)
	{
		this.subject = subject;
	},
	
	/**
	 * Add a pod
	 * 
	 * @param {Object} handle
	 */
	pod : function (handle)
	{
		if (this.pods.has(handle)) {
			return this.pods.get(handle);
		} else {
			throw "Unable to find \""+handle+"\" pod";
		}
	}
	
});


Byng.register('byng.panel.search', 
{	
	Implements : ByngView,
	
	initialize : function ()
	{	
		var fayt = Byng.input.fayt($('query-'+this.meta.entity+'-input'), this.getRequest());
			fayt.setContainer('live-search-'+this.meta.entity);
			fayt.input.addEvent('blur', fayt.looseDrop.bind(fayt));
			fayt.hiddenInput = $('value_'+this.meta.entity+'_id');
 			fayt.hintText = $('hint_'+this.meta.entity+'_id');
 		
		Byng.ui.response.setListener('select_'+this.meta.entity, function(value,text) {
			
			fayt.hiddenInput.value = value;
			fayt.input.value = text; 		
			fayt.input.blur();
			fayt.fireEvent('select');

		}.bind(fayt));
	}
	
});


/**
 * Holds a stack of pods
 * 
 * Holds a stack of pods to be added to this panel
 * 
 * @type Array
 */
Byng.pods = new Hash();