/**
 * The "dashboard" object is a single object used by CARGO Library.  It
 * contains utility function for setting up the widgets that are visualized
 * inside of dashboard panel.
 * @title  CARGO
 * @module dashboard
 * @author Angel Carro CNIO (acarro@cnio.es)
 * @author Jose Manuel Rodriguez CNIO-INB (jmrodriguez@cnio.es)
 * @version 2.0
 */

/**
 * dashboard is an abstract base class.
 * @constructor
 * @param {String} sContainerName "div" identifier of HTML Element that will be contain dashboard panel
 * @param {Integer} iMargin Initial width of dashboard
 * @param {String} sIconProject URL of icon file of current project
 */
function dashboard(sContainerName, iMargin, sIconProject)
{
	this.name="dashboard";
	this.widgets=new Array();
	this.totalWidgets=0;
	this.zIndex=new Array();
	this.ultimatezIndex=0;
	this.margen=iMargin;
	
	var p = document.createElement("div");
	p.setAttribute("id", this.name);
	p.setAttribute("class","dashboard-panel");
	p.style.background="url('"+sIconProject+"') no-repeat center";
	document.getElementById(sContainerName).appendChild(p);

	this.HTMLdashboard=document.getElementById(this.name);
};

dashboard.prototype = {

/* ATTRIBUTES */ // MODIFIED: add these attributes

	/**
	* The name of dashboard
	* @type String
	*/
	name: "dashboard",
	/**
	* List of widget windows that are located within dashboard
	* @type Array
	*/
	widgets: null,
	/**
	* Total of the widgets
	* @type Integer
	*/
	totalWidgets: 0,
	/**
	* List of integers corresponding on zIndex values for each widget window
	* @type Array
	*/
	zIndex: null,
	/**
	* The last zIndex value
	* @type Integer
	*/
	ultimatezIndex: 0,
	/**
	* Inside margin of the dashboard
	* @type Integer
	*/
	margen: 0,
	/**
	* HTML document referencing dashboard zone
	* @type String
	*/
	HTMLdashboard: null,

/* Public METHODS */

	/**
	* Add a widget (window) into dashboard.
	* @param {Object} widget Widget object
	*/
	addwidget: function (widget)
	{
		this.widgets[this.totalWidgets]=widget;
		this.zIndex[this.totalWidgets]=this.ultimatezIndex;
		this.ultimatezIndex++;
		this.totalWidgets++;
		widget.constrainTo(this);
	},
	/**
	* Delete given widget (window) from dashboard.
	* @param {Object} widgetname Name of widget we want to delete
	*/
	delwidget: function (widgetname)
	{
		for(var iWidget=0;iWidget<this.totalWidgets;iWidget++)
		{
			if(this.widgets[iWidget])
			{
				if(this.widgets[iWidget].name==widgetname)
				{
					this.widgets[iWidget]=null ;
				}
			}
		}
		var widget=document.getElementById(widgetname);
		var widgetdelete = document.getElementById(this.name).removeChild(widget);
	
	},
	/**
	* Set focus a selected widget, the rest of the widget (window) set unfocus.
	* @param {Object} widget Selected widget object
	*/
	setFocus: function (widget)
	{
		var iwidget;
		for(iwidget=0;iwidget<this.totalWidgets;iwidget++)
		{
			if(this.widgets[iwidget])
			{
				if(this.widgets[iwidget].name==widget.name)
				{
					widget.changeFocus(true);
				}
				else
				{
					this.widgets[iwidget].changeFocus(false);
				}
			}
		}
	},
	/**
	* Get value of width of dashboard margin.
	* @return {Integer} margen Margin of dashboard
	*/
	getMargen: function ()
	{
		return this.margen;
	},
	/**
	* Get the top margin of dashboard.
	* @return {Integer} getY Top margin of dashboard
	*/
	getTop: function ()
	{
		return YAHOO.util.Dom.getY(this.HTMLdashboard);
	},
	/**
	* Get the bottom margin of dashboard.
	* @return {Integer} getY Bottom margin of dashboard
	*/
	getBottom: function ()
	{
		return YAHOO.util.Dom.getY(this.HTMLdashboard)+this.HTMLdashboard.clientHeight;
	},
	/**
	* Get the left margin of dashboard.
	* @return {Integer} getX Left margin of dashboard
	*/
	getLeft: function ()
	{
		return YAHOO.util.Dom.getX(this.HTMLdashboard);
	},
	/**
	* Get the right margin of dashboard.
	* @return {Integer} getX Right margin of dashboard
	*/
	getRight: function ()
	{
		return YAHOO.util.Dom.getX(this.HTMLdashboard)+this.HTMLdashboard.clientWidth;
	},
	/**
	* Get the last zIndex value that has been used
	* by widget inside of dashboard.
	* @return {Integer} ultimatezIndex Last zIndex
	*/
	getZindex: function ()
	{
		var ret=this.ultimatezIndex;
		this.ultimatezIndex++;
		return ret;
	},
	/**
	* Add background image into dashboard.
	* @param {String} sURLImage URL of image of current project
	*/
	setIconBackground: function (sURLImage)
	{
		document.getElementById(this.name).style.background="url('"+sIconProject+"') no-repeat center";
	},
	/**
	* DEPRECATED
	* Reload all widgets that are contained in dashboard.
	* @param {String} ensemblId Ensembl identifier
	*/
	reloadDashboard: function (ensemblId)
	{
		for(iwidget=0;iwidget<this.totalWidgets;iwidget++)
		{
			if(this.widgets[iwidget])
			{
				if(!this.widgets[iwidget].isclose)
				{
					this.widgets[iwidget].reloadMainFunction(this.widgets[iwidget].initRef+"?ensemblId="+ensemblId);
				}
			}
		}
	}

};