Suagencia.Classes.Legenda = Class.create( {
	initialize: function( vDOM , vOptions ) {
		vDOM = $(vDOM);
		this.ID = vDOM.identify();
		
		if( vOptions )
			{this.Options = Suagencia.Legendas.defaultOptions.merge( vOptions );}
		else
			{this.Options = Suagencia.Legendas.defaultOptions.clone( );}
		
		Suagencia.Legendas.items.set( this.ID , this );
		 var vID = ("legenda-for-"+this.ID);
		this.DivLegenda = new Element('div', { 'class': this.Options.get("className"), 'id' : vID });
		
		$$("body").entries()[0].insert( this.DivLegenda );
		 
		 this.Options.get("type") 
		if( this.Options.get("type")  == "text" ){
			this.DivLegenda.insert( this.Options.get("value") );
			this.loaded = true;
		}
		else {
			this.loaded = false;
		}
		
		this.positioned = false;
		this.DivLegenda.setStyle( {"visibility":"hidden","position":"absolute"} )
		
		vDOM.observe( "mouseover" , this._mouseOver.bindAsEventListener( this ) );
		vDOM.observe( "mouseout" , this._mouseOut.bind( this ) );
		this.DivLegenda.observe( "mouseover" , this._overLegenda.bind(this) );
		this.DivLegenda.observe( "mouseout" , this._mouseOut.bind(this) );
	},
	_mouseOver: function( ev ) {
			if( this.tryHide )
				{window.clearTimeout(this.tryHide);}

			var vLista = Suagencia.Legendas.items.values()
			for( i = 0 ; i < vLista.length ; i ++ )
			{
				if( this.ID != vLista[i].ID )
					{vLista[i].hideLegenda();}
			}
			this.showLegenda( ev );
	},
	_mouseOut: function( ev ) {
		this.tryHide = this.hideLegenda.bind( this ).delay( 1 )
	},
	_overLegenda: function (){
			if( this.tryHide )
				{window.clearTimeout(this.tryHide);}
	},
	hideLegenda: function() {
		this.sumir();
		//this.DivLegenda.setStyle( {"visibility":"hidden"} );
		
	},
	showLegenda: function( ev ) {

		var vPosition = this.Options.get("position").split(" ");
		if( vPosition[0] == "mouse" || !this.positioned )
			{ this.setPosition( ev , vPosition ); }
		if( !this.loaded )
			{this.loadContent()}
		//this.DivLegenda.setStyle({"visibility":"visible"});
		this.aparecer();
	},
	setPosition: function( ev , vPosition ) {
		
		var vDimensions = this.DivLegenda.getDimensions();
		var vX = 0;
		var vY = 0;
		var oX = 0;
		var oY = 0;
		
		if( vPosition[0] == "relative" || vPosition[0] == "mouse" )
		{
			if( vPosition[1] == "center" )
				{vX = -parseInt(vDimensions.width/2)}
			else if(vPosition[1] == "left" )
				{vX = -vDimensions.width}
			else if(vPosition[1] == "right" )
				{vX = 0}
				
			if( vPosition[2] == "center" )
				{vY = -parseInt(vDimensions.height/2)}
			else if(vPosition[2] == "top" )
				{vY = -vDimensions.height}
			else if(vPosition[2] == "bottom" )
				{vY = 0 }				
		}
		
		if( vPosition[0] == "relative" )
		{
			var pOrigem = $( this.ID ).cumulativeOffset();
			var pDimensions = $( this.ID ).getDimensions();

			if( vPosition[1] == "center" )
				{oX = pOrigem.left + parseInt(pDimensions.width/2)}
			else if(vPosition[1] == "left" )
				{oX = pOrigem.left}
			else if(vPosition[1] == "right" )
				{oX = pOrigem.left + pDimensions.width}
				
			if( vPosition[2] == "center" )
				{oY = pOrigem.top + parseInt(pDimensions.height/2)}
			else if(vPosition[2] == "top" )
				{oY = pOrigem.top}
			else if(vPosition[2] == "bottom" )
				{oY = pOrigem.top + pDimensions.height}		
			
			this.DivLegenda.setStyle({"visibility":"visible","display":"none"});
			
		}
	
		if( vPosition[0] == "mouse" )
		{
			oX = ev.pointerX();
			oY = ev.pointerY();
		}
		
		this.DivLegenda.setStyle({"left":(oX + vX) + "px" , "top":(oY + vY) + "px"} )
		this.positioned = true;
	},
	loadContent: function( ){
		if( this.Options.get("type")  == "text" ){
			this.DivLegenda.insert( this.Options.get("value") );
			this.loaded = true;
		} else if( this.Options.get("type")  == "img" ){
			this.DivLegenda.update( "<img src='" + this.Options.get("value") + "' />" );
			this.loaded = true;
		} else if( this.Options.get("type")  == "url" ){
			this.DivLegenda.addClassName("loading");
			new Ajax.Request(this.Options.get("value"), {method:'get',onComplete: this.receiveContent.bind( this )});
		}
	},
	receiveContent: function( vAjax ) {
		this.loaded = true;
		this.DivLegenda.removeClassName("loading");
		this.setContent( vAjax.responseText );		
	},
	setContent: function( vContent ) {
		this.DivLegenda.update( vContent );
	},
	sumir: function() {
		if( this.animacao )
			{this.animacao.cancel();}
		this.animacao = new Effect.Fade( this.DivLegenda , {durarion:0.1});
	},
	aparecer: function () {
		if( this.animacao )
			{this.animacao.cancel();}
		this.animacao = new Effect.Appear( this.DivLegenda , {durarion:0.1} );
	}
});

Suagencia.Legendas = {
	defaultOptions: $H( {"type":"text","position":"relative center bottom","className":"legenda", "value":"defaultValue"} ),
	changeDefaultOptions: SuagenciaFramework.changeDefaultOptions,
	items: new Hash(),
	config: function (event , vDOM) {
		if( vDOM )
			{vElements = $(vDOM).select("[suag:legenda]");}
		else
			{vElements = $$("[suag:legenda]");}		
		vElements.each( function( obj ) { 
			new Suagencia.Classes.Legenda(obj , obj.readAttribute("suag:legenda").evalJSON() );
		});
	}
}
