/**
 * AS4PortalPanel 
 * Abstract model object represents a single panel within the portal page
 */
 
var AS4PortalPanel = Class.create({
	
	/**
	 * @type {String} Unique identifier for this Javascript object
	 */
	id: null,
	
	/**
	 * @type {Element} The element to which this model is attached
	 */
	element: null,

	/**
	 * @type {String} The panel's title
	 */
	title: '{New Panel}',
	
	/**
	 * @type {Object} Bespoke configuration options for this panel
	 */
	options: { },
	
	/**
	 * @type {string} The type of panel represented
	 */
	type: null,
	
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	
	/**
	 * @constructor 
	 * @param {String} title The displayed title of the panel
	 * @param {Hash} options Option parameters for the panel
	 * @param {String} type The type of panel represented
	 */
	initialize: function(title, options, type) {
		this.title = title;
		this.options = options || { }
		this.type = type;
	},

	/**
	 * Returns a Hash representation of this panel
	 */	
	toHash: function()
	{
		return {
			id: this.id,
			options: this.options
		};
	},
	
	/**
	 * 
	 * 
	 */
	reload: function()
	{
		this.contentElement().update('');
		this.contentElement().addClassName('ajax_panel_loading');
	},
	
	/**
	 * Return the panel's content element
	 * @return {Element}
	 */
	contentElement: function()
	{
		return this.element.down('.content');
	},
	
	/**
	 * Return the panel's title element
	 * @return {Element}
	 */
	titleElement: function()
	{
		return this.element.down('.title h4');
	},
	
	// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
	// Interface methods
	
	/**
	 * @param {Event} event
	 */
	toggle: function(event)
	{
		Element.toggle(this.contentElement());

		if(this.contentElement().style.display == 'none')
			$(this.element.id + '_collapse').addClassName('closed');
		else
			$(this.element.id + '_collapse').removeClassName('closed');
	},
	
	/**
	 * Close this portal panel and remove it from the DOM
	 * @return {void}
	 */
	close: function()
	{
		this.element.remove();
		this.onClose();
	},
	
	// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
	// Virtual accessor methods
	
	/**
	 * Optional hook method to return any custom HTML links that will be appended to the panel's control strip
	 *
	 * @param {String} uuid The UUID of the panel being generated, passed in by the controller class
	 * @return {String}
	 */
	getControlStripHTML: function(uuid)
	{
		return '<a href="javascript:$(\'' + uuid + '\').controller.reload();" id="' + uuid + '_reload" title="Refresh this panel\'s content" class="portal_reload"><span /></a> ' +
			   '<a href="javascript:$(\'' + uuid + '\').controller.toggle();" id="' + uuid + '_collapse" title="Toggle this panel closed" class="portal_toggle"><span /></a> ' +
			   '<a href="javascript:$(\'' + uuid + '\').controller.close();" id="' + uuid + '_close" title="Remove this panel from your homepage" class="portal_close"><span /></a>';
	},
	
	/**
	 * Set the title of the panel
	 * @param {String} title The new title
	 */
	setTitle: function(title)
	{
		this.titleElement().update(title);
	},
	
	// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
	// Callback methods
	
	/**
	 * Generic callback occurs when a panel has finished loading
	 */
	onReload: function(transport, target)
	{
		this.contentElement().removeClassName('ajax_panel_loading');
	},
	
	onClose: function() {},

	// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
	// Private methods
	
	/**
	 * Generates a unique identifier for this panel object (note this does not maintain reference to any element)
	 * @return {String}
	 */
	generateUniqueId: function()
	{
		var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		return chars.substr(Math.random() * (chars.length - 1), 1 ) + [4, 2, 2, 2, 6].map(function(length)
		{
			return $R(0, length, true).map(function()
			{
				return (Math.ceil(Math.random() * 256)).toString(16);
			}).join('');
		}).join('');
	}
});

// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
// Static methods/properties

AS4PortalPanel.STATIC_PANEL		= 0;
AS4PortalPanel.FEED_PANEL		= 1;
AS4PortalPanel.CONTROL_PANEL	= 2;
AS4PortalPanel.NAVIGATION_PANEL	= 3;
AS4PortalPanel.ULEARN_PANEL		= 4;
