/**
 * The "widget" object is a single object used by CARGO Library.
 * It contains utility function for setting up windows of cargo services.
 * @title  CARGO
 * @module widget
 * @author Angel Carro CNIO (acarro@cnio.es)
 * @author Jose Manuel Rodriguez CNIO-INB (jmrodriguez@cnio.es)
 * @version 2.0
 */

/*
   Function: widget

   Constructor of the Class

   Parameters:

      name - Name of the widget
      widgetWidth - The inicial Width of the widget.
      widgetHeight - The inicial Height of the widget.

   Returns:

      A object of the type widget.

   Herencia:

      YAHOO.util.DragDrop.

   variable: Atributes of the class widget

      ButtonUser - Array that container the buttons associatted to user functions.
      FunctionButtonUser - Array that container the user functions.
      totalButtonUser - Total of the user buttons.

      maximize - default value false, if the widget is maximized, the variable is true.
      minimized - default value false, if the widget is minimized, the variable is true.
      isclose - default value false, if the widget is closed, the variable is true.
      isconstrain - default value  false, if the widget is contraint, the variable is true.
      focus - default value true, if the widget have the focus, the variable is true.

      constrainleft - the left limit of the widget.
      constrainright - the right limit of the widget.
      constraintop - the top limit of the widget.
      constrainbotton - the bottom limit of the widget.
      margen - the magin inside of the dashboard.

      HTMLResize - Reference to zone that resized widget
      HTMLBody - Reference to zone that body widget
      HTMLIframe - Reference to zone that content the client webServer
      HTMLGhost - Reference to special zone 
      HTMLButtons - Reference to zone that content the standart buttons
      HTMLButtonsFooter - Reference to zone that content the user buttons
      HTMLHead - Reference to head zone
      HTMLCornerTopLeft - Reference to corner top left. 
      HTMLCornerTopRight - Reference to corner top right.
      HTMLCornerBottomLeft - Reference to corner bottom left.
      HTMLCornerBottomRight - Reference to corner bottom right.
      HTMLCornerSize - Size corner

      maxImgeButton - Path to image  when the status of the widget is not maximize
      maxImgeButton2 - Path to image when the status of the widget is maximize
      minImgeButton - Path to image  when the status of the widget is not minimized
      minImgeButton2 -  Path to image  when the status of the widget is minimized. Only if not constaint widget 
      closeImgeButton - Path to close image ;

      widgetWidth - Actual width;
      widgetHeight - Actual Height;
      oldwidth - Old width.
      oldheight - Old height.
      minResizeWidth - Is the minimun width size of the widget, default value 70;
      minResizeHeight -Is the minimun height size of the widget, default value 70;
      hande - is the actual zone selected 
 */
// MODIFIED: widget=function(name,widgetWidth,widgetHeight,taskbar,ref,defaultVisiblity,dashboard,minwidth,minheight)

/**
 * widget is an abstract base class. It is defined by means of
 * list of attributes needed for widget window.
 * @constructor
 * @param {Integer} iServId Identifier of widget (service)
 * @param {String} name Name of widget (widget window)
 * @param {String} type Name of widget type
 * @param {Integer} widgetWidth Initial width of the widget.
 * @param {Integer} widgetHeight Initial height of the widget
 * @param {String} ref URL of widget
 * @param {Boolean} defaultVisiblity Indicator that says if the window is visible or not
 * @param {Object} dashboard Interface where widget will locate
 */
function widget (iServId, name,type, widgetWidth,widgetHeight,ref,dashboard,minwidth,minheight)
{
	this.serviceId=iServId; // MODIFIED: new attribute

	this.name=name;
	this.type=type;

	if(dashboard)
	{
		var p = document.createElement("DIV");
		p.setAttribute("id", this.name);
		p.setAttribute("class","panel");
		document.getElementById(dashboard.name).appendChild(p);
		//document.body.appendChild(p);
	}


	this.init(this.name);

	this.dashboard=dashboard; //MODIFIED: add dashboard

	this.ButtonUser=new Array();
	this.FunctionButtonUser=new Array();
	this.totalButtonUser=0;

//Variables de estado
	this.maximize=false;
	this.minimized=false;
	this.isclose=false;
	this.isconstrain=false;
	this.focus=true;

// Variables que contolan las margenes
	this.constrainleft="";
	this.constrainright="";
	this.constraintop="";
	this.constrainbotton="";
	this.margen=0;

// Variables referentes a la ventana
 	this.panel=this.getEl();
	if(BrowserDetect.browser =='Explorer')
	{
		this.getPanelIE();
		this.HTMLCornerTopLeft=document.getElementById('head_corner_left'+this.name);
		this.HTMLCornerTopRight=document.getElementById('head_corner_right'+this.name);
		this.HTMLCornerBottomLeft=document.getElementById('foot_corner_left'+this.name);
		this.HTMLCornerBottomRight=document.getElementById('foot_corner_right'+this.name);
		this.HTMLCornerSize=document.getElementById('head_corner_left'+this.name).clientHeight;
	}
	else
	{
		this.getPanel();
		this.HTMLCornerTopLeft=document.getElementById('head_corner_left'+this.name);
		this.HTMLCornerTopRight=document.getElementById('head_corner_right'+this.name);
		this.HTMLCornerBottomLeft=document.getElementById('foot_corner_left'+this.name);
		this.HTMLCornerBottomRight=document.getElementById('foot_corner_right'+this.name);
		this.HTMLCornerSize=document.getElementById('head_corner_left'+this.name).clientHeight;
	}
	
	this.HTMLHead=document.getElementById('head'+this.name);
	
	this.HTMLFooter=document.getElementById('foot'+this.name);

	

	this.HTMLResize=document.getElementById('resize'+this.name);
	this.HTMLBody=document.getElementById('body'+this.name);
	this.HTMLIframe="";
	this.HTMLGhost=document.getElementById('ghost'+this.name);
	this.HTMLInfo=document.getElementById('info_body'+this.name);

	this.HTMLButtons=document.getElementById('buttons'+this.name);
	this.HTMLInfoButton=document.getElementById('info'+this.name);
	this.HTMLButtonsFooter=document.getElementById('buttonsfooter'+this.name);


	this.maxImgeButton="";
	this.maxImgeButton2="";
	this.minImgeButton="";
	this.minImgeButton2="";
	this.closeImgeButton="";
	this.widgetWidth=widgetWidth;
	this.widgetHeight=widgetHeight;
	this.oldwidth=this.widgetWidth;
	this.oldheight=this.widgetHeight;
	
	if(minwidth)
	{
		this.minResizeWidth=minwidth;
	}
	else
	{
		this.minResizeWidth=70;
	}
	if(minheight)
	{
		this.minResizeHeight=minheight;
	}
	else
	{
		this.minResizeHeight=70;
	}
	this.panel.style.width=this.widgetWidth;
	this.panel.style.height=this.widgetHeight;
	this.setTitle(name);
	this.handle="";
	
	this.addBtnMin("css/img/minBtn.png","css/img/minBtn.png");
	this.addBtnMax("css/img/maxBtn.png","css/img/maxBtn.png");
	this.addBtnClose("css/img/closeBtn.png","css/img/closeBtn.png");
	
	this.addIconInfo("css/img/helpBtn.png");
	this.addIconConnect("css/img/online.gif");

	this.initRef=ref;
	
	this.setMainFunctionRef(ref);
	
	if(dashboard)
	{
		dashboard.addwidget(this);
		
	}
	
	this.destroy = new YAHOO.util.CustomEvent("cargo.destroyWidget",this); // MODIFIED: this.destroy = new YAHOO.util.CustomEvent("destroy",this);
	this.minimize = new YAHOO.util.CustomEvent("cargo.minimizeWidget",this);

	this.panel.style.opacity=0;
	var attributes = { 
		opacity: { from: 0 , to: 1 }
	}; 
	var anim = new YAHOO.util.Anim(this.name, attributes, 1, YAHOO.util.Easing.easeOut);
	anim.animate();


	if (cargo.searchboard.expanded) {
		this.panel.style.left=230; // Appear jumping the searchboard
	} else {
		this.panel.style.left=40; // Appear jumping the searchboard
	}

	
	this.connected = true;
};


YAHOO.extend(widget, YAHOO.util.DragDrop);

/* Public METHODS */

	/**
	* Change to focus of the widget.
	* @param {Boolean} state if the widget have focus the value is TRUE, otherwise FALSE.
	*/
	widget.prototype.changeFocus=function (state)
	{
		if(state)
		{
			this.HTMLBody.className="selected";
			this.HTMLHead.style.backgroundPosition="0px 0px";
			this.HTMLCornerTopLeft.style.backgroundPosition="0px 0px";
			this.HTMLCornerTopRight.style.backgroundPosition="0px 0px";
	
			this.HTMLFooter.style.backgroundPosition="0px -"+this.HTMLCornerSize+"px";
			this.HTMLCornerBottomLeft.style.backgroundPosition="0px -"+this.HTMLCornerSize+"px";
			this.HTMLCornerBottomRight.style.backgroundPosition="0px -"+this.HTMLCornerSize+"px";
			this.panel.style.visibility="visible";
			if(this.HTMLIframe)
			{
				this.HTMLIframe.style.display="block";
				this.HTMLIframe.style.overflow="auto";
			}
			this.HTMLGhost.style.zIndex=-1;
			if(this.dashboard)
			{
				this.setzIndex(this.dashboard.getZindex());
			}
			this.minimized=false;
	
			this.HTMLButtons.style.opacity = 1;
			this.HTMLButtonsFooter.style.opacity = 1;
			this.HTMLInfoButton.style.opacity = 1;
	
			this.panel.style.opacity=1;
		}
		else
		{
			this.HTMLBody.className="unselected";
			this.HTMLHead.style.backgroundPosition="0px -"+this.HTMLCornerSize+"px";
			this.HTMLCornerTopLeft.style.backgroundPosition="0px -"+this.HTMLCornerSize+"px";
			this.HTMLCornerTopRight.style.backgroundPosition="0px -"+this.HTMLCornerSize+"px";
	
			this.HTMLFooter.style.backgroundPosition="0px 0px";
			this.HTMLCornerBottomLeft.style.backgroundPosition="0px 0px";
			this.HTMLCornerBottomRight.style.backgroundPosition="0px 0px";
	
			if(this.HTMLIframe)
			{
				this.HTMLIframe.style.overflow="hidden";
				
			}
			this.HTMLGhost.style.zIndex=this.panel.style.zIndex;
			this.HTMLButtons.style.opacity = 0.5;
			this.HTMLButtonsFooter.style.opacity = 0.5;
			this.HTMLInfoButton.style.opacity = 0.5;
	
		}
	}
	/**
	* Close the widget and change the status to close.
	*/
	widget.prototype.close=function ()
	{
		var anim = new YAHOO.util.Anim(this.name, { opacity: { to: 0 } }, 1, YAHOO.util.Easing.easeOut);
		anim.animate();
		anim.onComplete.subscribe(widget.prototype.free,this)
	// 	var panel = this.panel;
	// 	panel.style.visibility="hidden";
	// 	if(this.HTMLIframe)
	// 	{
	// 		this.HTMLIframe.style.display="none";
	// 	}
	// 	this.isclose=true;
	}
	widget.prototype.free=function(type, args, me)
	{
		if(me.dashboard)
		{
			me.dashboard.delwidget(me.name);
		}
		me.destroy.fire(me);
	}
	/**
	* Open the widget and change the status to open.
	*/
	widget.prototype.open=function ()
	{
		var panel = this.panel;
		panel.style.visibility="visible";
		this.isclose=false;
		if(this.HTMLIframe)
		{
			this.HTMLIframe.style.display="block";
		}
		this.changeFocus(true);
		if(this.dashboard)
		{
			this.dashboard.setFocus(this);
		}
	}
	/**
	* Set the property zindex.
	* @param {Integer} zindex z-Index value
	*/
widget.prototype.setzIndex=function (zindex)
{
	this.panel.style.zIndex=zindex;
}
/*
   Function: setTitle

   set the title to widget

   Parameters:

      title - string.
*/
widget.prototype.setTitle=function(title)
{
	document.getElementById('name'+this.name).innerHTML=title;
}
/*
   Function: constrainTo

   Set constraint to dashboard 

   Parameters:

      dashboard - Object dashboard.
*/
widget.prototype.constrainTo=function(dashboard)
{
	if(dashboard)
	{
		this.dashboard=dashboard;
		this.margen=this.dashboard.getMargen();
		var el = this.getDragEl();
		var CornerTopLeftDashboard=[this.dashboard.getLeft()+this.dashboard.getMargen(),this.dashboard.getTop()+this.dashboard.getMargen()];
		YAHOO.util.Dom.setXY(this.panel, CornerTopLeftDashboard);
	}

	this.constrainleft=this.dashboard.getLeft();
	this.constrainright=this.dashboard.getRight();
	this.constraintop=this.dashboard.getTop();
	this.constrainbotton=this.dashboard.getBottom();



	this.isconstrain=true;
}


/*
   Function: addBtnMax

   Add maximize buttom to widget 

   Parameters:

      pathtoimg - Path to image when the status of the widget is not maximize
      pathtoimg2 - Path to image when the status of the widget is maximize
*/
widget.prototype.addBtnMax=function (pathtoimg,pathtoimg2)
{
	if(this.HTMLButtons.innerHTML=='')
	{
		this.HTMLButtons.innerHTML="<img border=\"0\" style=\"padding-top:2px;padding-right:2px;\" id=\"maxBtn"+this.name+"\" src=\""+pathtoimg+"\">";
	}
	else
	{
		this.HTMLButtons.innerHTML=this.HTMLButtons.innerHTML+"<img border=\"0\" style=\"padding-top:2px;padding-right:1px;\" id=\"maxBtn"+this.name+"\" src=\""+pathtoimg+"\">";
	}
	this.maxImgeButton=pathtoimg;
	this.maxImgeButton2=pathtoimg2;
}
/*
   Function: addBtnClose

   Add close buttom to widget

   Parameters:

      pathtoimg - Path to close image.
*/
widget.prototype.addBtnClose=function (pathtoimg)
{
	if(this.HTMLButtons.innerHTML=='')
	{
		this.HTMLButtons.innerHTML="<img border=\"0\" style=\"padding-top:2px;padding-right:2px;\" id=\"closeBtn"+this.name+"\" src=\""+pathtoimg+"\">";
	}
	else
	{
		this.HTMLButtons.innerHTML=this.HTMLButtons.innerHTML+"<img border=\"0\" style=\"padding-top:2px;padding-right:1px;\" id=\"closeBtn"+this.name+"\" src=\""+pathtoimg+"\">";
	}
	this.closeImgeButton=pathtoimg;
}
/*
   Function: addBtnMin

   Add minimized buttom to widget 

   Parameters:

      pathtoimg - Path to image when the status of the widget is not minimized
      pathtoimg2 - Path to image when the status of the widget is minimized.  Only if not constaint widget
*/
widget.prototype.addBtnMin=function (pathtoimg,pathtoimg2)
{
	if(this.HTMLButtons.innerHTML=='')
	{
		this.HTMLButtons.innerHTML="<img border=\"0\" style=\"padding-top:2px;padding-right:2px;\" id=\"minBtn"+this.name+"\" src=\""+pathtoimg+"\">";
	}
	else
	{
		this.HTMLButtons.innerHTML=this.HTMLButtons.innerHTML+"<img border=\"0\" style=\"padding-top:2px;padding-right:1px;\" id=\"minBtn"+this.name+"\" src=\""+pathtoimg+"\">";
	}
	this.minImgeButton=pathtoimg;
	this.minImgeButton2=pathtoimg2;
}

widget.prototype.addIconInfo=function (pathtoimg)
{
	this.HTMLInfoButton.innerHTML="<img border=\"0\" style=\"padding-top:2px;\" id=\"helpBtn"+this.name+"\" src=\""+pathtoimg+"\">";
}

widget.prototype.addIconConnect=function (pathtoimg)
{
	var sTitle = '';
	if (this.connected) { sTitle = 'connected'; } else { sTitle = 'unconnected'; }
	this.HTMLButtonsFooter.innerHTML =	"<div class='connect' id='connect"+this.name+"' title='"+sTitle+"'>"+
						"<img border=\"0\" id=\"connect"+this.name+"\" src=\""+pathtoimg+"\">"+
					"</div>";
}


/*
   Function: addBtn

   Add user buttons in the HTMLButtonsFooter zone

   Parameters:

      name - the buttom name
      img - Path to buttom image.
*/
widget.prototype.addBBtn=function (name,img)
{
	if(this.HTMLButtonsFooter.innerHTML=='')
	{
		this.HTMLButtonsFooter="<img id=\""+name+this.name+"\" src=\""+img+"\">";
	}
	else
	{
		this.HTMLButtonsFooter.innerHTML=this.HTMLButtonsFooter.innerHTML+"<img id=\""+name+this.name+"\" src=\""+img+"\">";
	}
	this.ButtonUser[this.totalButtonUser]=name;
	this.totalButtonUser++;
}

/*
   Function: getPanel

   set the content panel

*/
widget.prototype.getPanel = function()
{
	var panel=	"<div class=\"head_corner_left\" id='head_corner_left"+this.name+"'>&nbsp;</div>\n"+
			"<div class='head' id='head"+this.name+"'>\n"+
				"<div class='buttons' id='buttons"+this.name+"'></div>\n"+
				"<div class='name' id='name"+this.name+"'></div>\n"+
				"<div class='info' id='info"+this.name+"' title='widget help'></div>\n"+
			"</div>\n"+
			"<div class=\"head_corner_right\" id='head_corner_right"+this.name+"'>&nbsp;</div>\n"+
			"<div class='info_body' id='info_body"+this.name+"'></div>\n"+
			"<div class='body' id='body"+this.name+"'></div>\n"+
			"<div class='ghost' id='ghost"+this.name+"'></div>\n"+
			"<div class=\"foot_corner_left\" id='foot_corner_left"+this.name+"'>&nbsp;</div>\n"+
			"<div class='foot' id='foot"+this.name+"'>\n"+
				"<div class='buttonsfooter' id='buttonsfooter"+this.name+"'></div>\n"+
				"<div class='resize' id='resize"+this.name+"'></div>\n"+
			"</div>\n"+
			"<div class=\"foot_corner_right\"id='foot_corner_right"+this.name+"'>&nbsp;</div>\n";
	this.panel.innerHTML=panel;
}

widget.prototype.getPanelIE = function()
{
	var panel=	"<div class=\"head\" id=\"head"+this.name+"\">\n"+
				"<div class=\"head_corner_left\" id=\"head_corner_left"+this.name+"\">&nbsp;</div>\n"+ 	
				"<div class=\"buttons\" id=\"buttons"+this.name+"\"></div>\n"+
				"<div class=\"name\" id=\"name"+this.name+"\"></div>\n"+
				"<div class=\"info\" id=\"info"+this.name+"\" title='widget help'></div>\n"+
				"<div class=\"head_corner_right\" id=\"head_corner_right"+this.name+"\">&nbsp;</div>\n"+ 	
			"</div>\n"+
			"<div class='info_body' id='info_body"+this.name+"'></div>\n"+
			"<div class=\"body\" id=\"body"+this.name+"\"></div>\n"+
			"<div class=\"ghost\" id=\"ghost"+this.name+"\"></div>\n"+
			"<div class=\"foot\" id=\"foot"+this.name+"\">\n"+
			"<div class=\"foot_corner_left\" id=\"foot_corner_left"+this.name+"\">&nbsp;</div>\n"+ 	
			"<div class=\"buttonsfooter\" id=\"buttonsfooter"+this.name+"\"></div>\n"+
			"<div class=\"resize\" id=\"resize"+this.name+"\"></div>\n"+
			"<div class=\"foot_corner_right\" id=\"foot_corner_right"+this.name+"\">&nbsp;</div>\n"+ 	
			"</div>\n"
			;
	this.panel.innerHTML=panel;
}
/*
   Function: setMainFunctionRef

   set main function in the widget

   Parameters:

      ref - URL.
*/
widget.prototype.setMainFunctionRef=function(ref)
{
	var iframe= "<iframe FRAMEBORDER=\"0\""+
			" class=\"iframe\""+
			" name=\"iframe"+this.name+"\""+
			" id=\"iframe"+this.name+"\""+
			" src=\"\">"+ 
			
			"</iframe>";
	this.HTMLBody.innerHTML=iframe;
	this.HTMLIframe=document.getElementById('iframe'+this.name);
	this.HTMLIframe.src=ref;
}
/*
   Function: reloadMainFunction

   reload main function in the widget

   Parameters:

      ref - URL.
*/
widget.prototype.reloadMainFunction=function(ref)
{
        if(document.getElementById('iframe'+this.name))
        {
                document.getElementById('iframe'+this.name).src=ref;
        }

}

widget.prototype.getRefMainfunction=function()
{
    if(document.getElementById('iframe'+this.name))
    {
           var ref=document.getElementById('iframe'+this.name).src.split('?');
    }
	return ref[0];
}

/*
   Event: onMouseUp

    Event handler that fires when a drag/drop obj gets a mouseup

   Parameters:

      e - <Event>  the mouseup event
*/
widget.prototype.onMouseUp = function(e)
{
	var handle=YAHOO.util.Event.getTarget( e , false ).id;
	var el = this.getDragEl();
	var panel = this.panel;

	var ibutton;
	for(ibutton=0;ibutton<this.totalButtonUser;ibutton++)
	{
		if(this.handle==this.ButtonUser[ibutton]+this.name)
		{
			this.FunctionButtonUser[ibutton]();
		}
	}	

	if(this.handle=='maxBtn'+this.name)
	{
		if(!this.maximize)
		{
			this.oldposX=YAHOO.util.Dom.getX(el);
			this.oldposY=YAHOO.util.Dom.getY(el);
			this.oldwidth=panel.style.width;
			this.oldheight=panel.style.height;
			if(this.isconstrain)
			{
  				YAHOO.util.Dom.setXY(el,[this.constrainleft+this.dashboard.getMargen(),this.constraintop+this.dashboard.getMargen()]);
				panel.style.width=this.constrainright-this.constrainleft-2*this.margen;
				panel.style.height=this.constrainbotton-this.constraintop-2*this.margen;
			}
			else
			{
				YAHOO.util.Dom.setXY(el,[this.oldposX,this.oldposY]);
				panel.style.width=window.innerWidth;
				panel.style.height=window.innerHeight;
			}
			this.widgetWidth=parseInt(panel.style.width);
			this.widgetHeight=parseInt(panel.style.height);
			document.getElementById('maxBtn'+this.name).src=this.maxImgeButton2;
			this.maximize=true;
		}
		else
		{
			YAHOO.util.Dom.setXY(el,[this.oldposX,this.oldposY]);
			panel.style.width=this.oldwidth;
			panel.style.height=this.oldheight;
			this.widgetWidth=parseInt(panel.style.width);
			this.widgetHeight=parseInt(panel.style.height);
			document.getElementById('maxBtn'+this.name).src=this.maxImgeButton;
			this.maximize=false;
		}
		
	}
	if(this.handle=='closeBtn'+this.name)
	{
		this.close();
	}

	if(this.handle=='minBtn'+this.name)
	{
		if(!this.minimized)
		{
			this.oldposX=YAHOO.util.Dom.getX(el);
			this.oldposY=YAHOO.util.Dom.getY(el);
			this.oldwidth=parseInt(panel.style.width);
			this.oldheight=parseInt(panel.style.height);

			this.connected = false;
			this.addIconConnect("css/img/offline.gif");

			this.minimized=true;

			//this.oldheight=parseInt(panel.style.height);
			//this.oldwidth=parseInt(panel.style.height);
			//panel.style.height=2*document.getElementById('head'+this.name).clientHeight +"px";
			//this.widgetHeight=2*document.getElementById('head'+this.name).clientHeight;
			//document.getElementById('head'+this.name).style.zIndex=999;
			//document.getElementById('minBtn'+this.name).src=this.minImgeButton2;

			var attributes = {
				points: { to: [30, 750] },
				height: { to: 0 },
				width: { to: 0 },
				opacity: { to: 0 }
			};
			var anim = new YAHOO.util.Motion(this.name, attributes, 1, YAHOO.util.Easing.easeOut);
			anim.animate();
			this.minimize.fire(this);

		}
		else
		{
			if(this.isconstrain)
			{
				if(this.startPos[1]+this.oldheight<this.constrainbotton)
				{
					panel.style.height=this.oldheight+"px";
					this.widgetHeight=this.oldheight;
				}
				else
				{
					YAHOO.util.Dom.setXY(el,[YAHOO.util.Dom.getX(el),this.constrainbotton-this.oldheight-dashboard.getMargen()]);
					panel.style.height=this.oldheight+"px";
					this.widgetHeight=this.oldheight;
				}
			}
			else
			{
				
				panel.style.height=this.oldheight+"px";
				this.widgetHeight=this.oldheight;
			}
			document.getElementById('minBtn'+this.name).src=this.minImgeButton;
			this.minimized=false;
		}
	}

	if(handle=='helpBtn'+this.name)
	{
		var sInfoContent = "<div class='info_content_icon' id='info_content_icon"+this.name+"'></div>"+
				"<div class='info_content_close' id='info_content_close"+this.name+"'>"+
					"<img id='info_content_close"+this.name+"' src='css/img/closeHelp.png'></img></div>"+
				"<div class='info_content' id='info_content"+this.name+"'>"+
					"<div class='info_content_desc'><span>Description:</span> "+cargo.services[this.serviceId].description+"</div>"+
					"<div class='info_content_doc' id='info_content_doc"+this.name+"'></div>"+
					"<div class='info_content_org'><span>Organization:</span> "+cargo.services[this.serviceId].providers[0].organization+"</div>"+
					"<div class='info_content_orglink'><span>Org. Link:</span> "+cargo.services[this.serviceId].providers[0].orglink+"</div>"+
					"<div class='info_content_author'><span>Author:</span> "+cargo.services[this.serviceId].providers[0].name+"</div>"+
					"<div class='info_content_email'><span>Email:</span> "+cargo.services[this.serviceId].providers[0].email+"</div>"+
				"</div>";
				// IMP: We print the first provider
		document.getElementById('info_body'+this.name).innerHTML = sInfoContent;
		if (cargo.services[this.serviceId].documentation && (cargo.services[this.serviceId].documentation != null) && (cargo.services[this.serviceId].documentation != 'NULL')) {
			document.getElementById('info_content_doc'+this.name).innerHTML = "<span>Online Documentation:</span> <a href='"+cargo.services[this.serviceId].documentation+"' target='_blank'>"+cargo.services[this.serviceId].documentation+"</a>";
		}

		if (cargo.services[this.serviceId].icon && (cargo.services[this.serviceId].icon != null) && (cargo.services[this.serviceId].icon != 'NULL')) {
			document.getElementById('info_content_icon'+this.name).innerHTML = "<img src='"+cargo.services[this.serviceId].icon+"'></img>";
		}

		this.HTMLBody.style.opacity = 0.2;
		document.getElementById('info_body'+this.name).style.zIndex= panel.style.zIndex + 1;
		document.getElementById('info_body'+this.name).style.display= 'block';
	}
	if (handle=='info_content_close'+this.name)
	{
		document.getElementById('info_body'+this.name).innerHTML = '';
		document.getElementById('info_body'+this.name).style.display= 'none';
		this.HTMLBody.style.opacity = 1;
	}

	if ( (handle=='connect'+this.name) && (this.connected))
	{
		this.connected = false;
		this.addIconConnect("css/img/offline.gif");
	} else {
		if ( (handle=='connect'+this.name) && !(this.connected))
		{
			this.connected = true;
			this.addIconConnect("css/img/online.gif");
		}
	}

	this.HTMLGhost.style.zIndex=-1;
	this.handle='';
}


widget.prototype.doMaximum = function()
{
	var el = this.getDragEl();
	var panel = this.panel;

	if(this.isconstrain)
	{
		//var iNewX = this.constrainleft+this.dashboard.getMargen();
		//var iNewY = this.constraintop+this.dashboard.getMargen();
 		YAHOO.util.Dom.setXY(el,[this.oldposX,this.oldposY]);
/*		panel.style.width=this.constrainright-this.constrainleft-2*this.margen;
		panel.style.height=this.constrainbotton-this.constraintop-2*this.margen;*/
		panel.style.width=this.oldwidth;
		panel.style.height=this.oldheight;

	}
	else
	{
		YAHOO.util.Dom.setXY(el,[this.oldposX,this.oldposY]);
		panel.style.width=window.innerWidth;
		panel.style.height=window.innerHeight;
	}
	this.widgetWidth=parseInt(panel.style.width);
	this.widgetHeight=parseInt(panel.style.height);
	document.getElementById('maxBtn'+this.name).src=this.maxImgeButton2;
	this.maximize=false;
	this.HTMLGhost.style.zIndex=-1;
	this.changeFocus(true);
}


/*
   Event: onMouseDown

    Event handler that fires when a drag/drop obj gets a mousedown

   Parameters:

      e - <Event>  the mousedown event
*/
widget.prototype.onMouseDown = function(e) {

 	var handle=YAHOO.util.Event.getTarget( e , true ).id;
	
	if(this.dashboard)
	{
		this.constrainTo();
		this.dashboard.setFocus(this);
	}

	var ibutton;
	for(ibutton=0;ibutton<this.totalButtonUser;ibutton++)
	{
		if(handle==this.ButtonUser[ibutton]+this.name)
		{
			this.handle=this.ButtonUser[ibutton]+this.name;
		}
	}

	if(handle=='head'+this.name)
	{
		this.startPos = [YAHOO.util.Event.getPageX(e),YAHOO.util.Event.getPageY(e)];
		this.handle='head'+this.name;
		var el = this.getDragEl();
		this.posIniX=YAHOO.util.Dom.getX(el);
		this.posIniY=YAHOO.util.Dom.getY(el);
		this.HTMLGhost.style.zIndex=this.panel.style.zIndex;
	}
	if(handle=='name'+this.name)
	{
		this.startPos = [YAHOO.util.Event.getPageX(e),YAHOO.util.Event.getPageY(e)];
		this.handle='name'+this.name;
		var el = this.getDragEl();
		this.posIniX=YAHOO.util.Dom.getX(el);
		this.posIniY=YAHOO.util.Dom.getY(el);
		this.HTMLGhost.style.zIndex=this.panel.style.zIndex;
	}
	if(handle=='info'+this.name)
	{
		this.startPos = [YAHOO.util.Event.getPageX(e),YAHOO.util.Event.getPageY(e)];
		this.handle='info'+this.name;
		var el = this.getDragEl();
		this.posIniX=YAHOO.util.Dom.getX(el);
		this.posIniY=YAHOO.util.Dom.getY(el);
		this.HTMLGhost.style.zIndex=this.panel.style.zIndex;
	}
	if(handle=='foot'+this.name)
	{
		this.startPos = [YAHOO.util.Event.getPageX(e),YAHOO.util.Event.getPageY(e)];
		this.handle='foot'+this.name;
		var el = this.getDragEl();
		this.posIniX=YAHOO.util.Dom.getX(el);
		this.posIniY=YAHOO.util.Dom.getY(el);
		this.HTMLGhost.style.zIndex=this.panel.style.zIndex;
	}
	if(handle=='buttonsfooter'+this.name)
	{
		this.startPos = [YAHOO.util.Event.getPageX(e),YAHOO.util.Event.getPageY(e)];
		this.handle='buttonsfooter'+this.name;
		var el = this.getDragEl();
		this.posIniX=YAHOO.util.Dom.getX(el);
		this.posIniY=YAHOO.util.Dom.getY(el);
		this.HTMLGhost.style.zIndex=this.panel.style.zIndex;
	}

	if(handle=='resize'+this.name)
	{
		this.startWidth = this.panel.offsetWidth;
		this.startHeight = this.panel.offsetHeight;
		this.posIniX=YAHOO.util.Dom.getX(this.panel);
		this.posIniY=YAHOO.util.Dom.getY(this.panel);
		this.startPos = [YAHOO.util.Event.getPageX(e),YAHOO.util.Event.getPageY(e)];
 		this.HTMLGhost.style.zIndex=this.panel.style.zIndex;
		this.handle='resize'+this.name;
		this.maximize=false;
	}
	if(handle=='maxBtn'+this.name)
	{
		this.handle='maxBtn'+this.name;
	}
	if(handle=='closeBtn'+this.name)
	{
		this.handle='closeBtn'+this.name;
	}
	if(handle=='minBtn'+this.name)
	{
		this.handle='minBtn'+this.name;
		this.startPos = [YAHOO.util.Event.getPageX(e),YAHOO.util.Event.getPageY(e)];
	}

};
/*
   Event: onDrag

    Abstract method called during the onMouseMove event while dragging an object.

   Parameters:

      e - <Event>  the mousemove event
*/
widget.prototype.onDrag = function(e)
{
	var panel= this.panel;
	var margen=this.margen;

	if(this.handle=='resize'+this.name)
	{
		var newPos = [YAHOO.util.Event.getPageX(e),YAHOO.util.Event.getPageY(e)];
		var offsetX = newPos[0] - this.startPos[0];
		var offsetY = newPos[1] - this.startPos[1];
		var newWidth = Math.max(this.startWidth + offsetX, 10);
		var newHeight = Math.max(this.startHeight + offsetY, 10);
		
		if(this.isconstrain)
		{
			if(this.posIniX+margen+newWidth<this.constrainright)
			{
				if(newWidth>this.minResizeWidth)
				{
					panel.style.width = newWidth + "px";
				}
			}
			else
			{
				panel.style.width=this.constrainright-this.posIniX-margen;
			}
			if(this.posIniY+newHeight+margen<this.constrainbotton)
			{
				if(newHeight>this.minResizeHeight)
				{
					if(BrowserDetect.browser=='Explorer')
					{
						panel.style.height = (newHeight-this.HTMLHead.clientHeight*2) + "px";
					}
					else
					{
						panel.style.height = newHeight + "px";
					}
				}
			}
			else
			{
				panel.style.height=this.constrainbotton-this.posIniY-margen;
			}
		}
		else
		{
			if(newWidth>this.minResizeWidth)
			{
				panel.style.width = newWidth + "px";
			}
			if(newHeight>this.minResizeHeight)
			{
				panel.style.height = newHeight + "px";
			}
		}
		this.widgetWidth=parseInt(panel.style.width);
		this.widgetHeight=parseInt(panel.style.height);
	}

	if(this.handle=='head'+this.name||this.handle=='name'+this.name||this.handle=='info'+this.name||this.handle=='foot'+this.name)
	{
 		var newPos = [YAHOO.util.Event.getPageX(e),YAHOO.util.Event.getPageY(e)];
		var el = this.getDragEl();
		var offsetX = newPos[0] - this.startPos[0];
		var offsetY = newPos[1] - this.startPos[1];

		if(this.isconstrain)
		{
			if((this.posIniX+offsetX-margen>this.constrainleft&&this.posIniX+offsetX+this.widgetWidth+margen<this.constrainright))
			{
				YAHOO.util.Dom.setX(el,this.posIniX+offsetX);
			}
			if((this.posIniY+offsetY-margen>this.constraintop&&this.posIniY+offsetY+this.widgetHeight+margen<this.constrainbotton))
			{
				YAHOO.util.Dom.setY(el,this.posIniY+offsetY);
			}
			
			//Si te mueves rapido fuera del dashboard que el widget se ajuste al final
			if(newPos[0]>this.constrainright)
			{
				YAHOO.util.Dom.setX(el,this.constrainright-this.widgetWidth-margen);
			}
			if(newPos[0]<this.constrainleft)
			{
				YAHOO.util.Dom.setX(el,this.constrainleft+margen);
			}
			if(newPos[1]<this.constraintop)
			{
				YAHOO.util.Dom.setY(el,this.constraintop+margen);
			}
			if(newPos[1]>this.constrainbotton)
			{
				YAHOO.util.Dom.setY(el,this.constrainbotton-this.widgetHeight-margen);
			}
			
		}
		else
		{
			YAHOO.util.Dom.setXY(el,[this.posIniX+offsetX,this.posIniY+offsetY]);
		}
	}
}

/*
   Function: addFunction

   Add user function to user buttoms

   Parameters:

      idButton - The name of the user buttoms.
      namefunction - Is the user function.

*/
// widget.prototype.addFunction=function(idButton,namefunction)
// {
// 	var ibutton;
// 	for(ibutton=0;ibutton<this.totalButtonUser;ibutton++)
// 	{
// 		if(idButton==this.ButtonUser[ibutton])
// 		{
// 			this.FunctionButtonUser[ibutton]=namefunction;
// 		}
// 	}		
// }

/*

widget.prototype.onDragDrop   = function(e,id)
{
	document.getElementById('alert').innerHTML="hola";
};*/

// widget.prototype.onInvalidDrop = function(e)
// {
// // 	alert(e);
// };
// widget.prototype.origZ = 0;



// widget.prototype.endDrag = function(e) 
// {
//     this.getEl().style.zIndex = this.origZ;
// };

