/*
-----------------------------------------------
 Sistema Integrado FUNIBER 
 Componente de Tabs
 @autor:   Rafael Nevarez
 @version:  2006
----------------------------------------------- */

var TabManager = Class.create();
TabManager.prototype = {

   initialize: function(container, options){
		var options = Object.extend({
			 claseContenedor : 'dialogo',
			 clasePestanas : 'pestanas',
			 quitarPrimerTab: false,
			 sortable: false,
			 tabsLimit: 100,
			 onLimit: function(){},
			 smartMode: false,
			 selectable: true
		}, options || {});
		this.container = $(container);
		this.tabs = new Array();
		this.lastActiveTab;
		this.activeTab;
		this.options = options;
		this.div_pestanas;
		this.ul_pestanas;
		this.div_contenedor;
		this._crearCampos();		
	},

   _crearCampos: function() {
	  //var clasePestana = this.options.clasePestanas
		this.div_pestanas = $E({
			tag: 'div', className: this.options.clasePestanas, children:{
				tag: 'ul'	
			}
		});
		
		this.ul_pestanas = this.div_pestanas.firstChild; // Compatibilidad
		
		this.div_contenedor = $E({
			tag: 'div', className: this.options.claseContenedor
		});
		
		$(this.container).appendChild(this.div_pestanas);
		$(this.container).appendChild(this.div_contenedor);
   },
   
   crearTab : function (ownerid, titlebar, options){
		var Tab = this.buscarTab(ownerid);
		if (Tab){
			this.mostrarTab(Tab);
		}
		else{
			if ( this.tabs.length < this.options.tabsLimit ){
				this.tabs.push( Tab = new TabManagerTab(this,ownerid,titlebar,options) );
			}
			else{
				this.options.onLimit(this);
				return false;
			}		
		}
		return(Tab);
   },
	
	crearTabExtendido : function (ownerid, titlebar, options){
		var Tab = this.buscarTab(ownerid);
		if (Tab){
			this.mostrarTab(Tab);
		}
		else{
			if ( this.tabs.length < this.options.tabsLimit ){
				this.tabs.push( Tab = new TabManagerTabExtendido(this,ownerid,titlebar,options) );
			}
			else{
				this.options.onLimit(this);
				return false;
			}	
		}	
		return(Tab);
   },
	
	actualizar: function(){
		if (this.options.sortable){
			Sortable.create(this.ul_pestanas, {constraint:'horizontal', overlap:'horizontal'});
		}
	}, 
	
	buscarTab: function(ownerid) {
		for (var i=0; i<this.tabs.length; i++){
			if ( this.tabs[i] != null )	{
				if ( this.tabs[i].ownerid == ownerid ){
					return this.tabs[i];
				}				
			}
		}
		return false;
	},
	
	mostrarTab: function(tab){
		for (var i=0; i<this.tabs.length; i++){
			if ( this.tabs[i] != null )	{
				this.tabs[i].esconder();				
			}
		}
		//alert(Object.inspect(tab));
		tab.mostrar();
		this.activeTab = tab;
	},
	
	quitarTab: function(tab) {
		for ( var i = 0; i<this.tabs.length; i++ ){
			if ( (this.tabs[i] === tab) && (this.tabs[i] != null) /* && (i != 0) */ ) {
				tabWasActive = ( this.tabs[i] == this.activeTab ? true : false);
				this.tabs[i].eliminar();
				this.tabs.splice(i, 1);
				if ( tabWasActive ){
					this.mostrarTab(this.tabs.last());
				}
			}			
			
			if ( (this.tabs[i] == tab) && (i == 0) && (this.options.quitarPrimerTab == true) ) {	
				this.tabs[i].eliminar();
				this.tabs[i] = null;
			}		
		}
	},
	
	limpiarTabs : function() {
		for ( var i = 0; i<this.tabs.length; i++ ){
			this.tabs[i].limpiar();
		}
	},
	
	actualizarLabelTab: function(owner_id, text){
		var Tab = this.buscarTab(owner_id);
		if ( Tab ){
			Tab.actualizarLabel(text);		
		}
		return;
	},
	
	getLabelTab: function(owner_id){
		var Tab = this.buscarTab(owner_id);
		if ( Tab ){				
			return(Tab.getLabel());		
		}
	}
};

/*--------------------------------------------------------------------------*/
var TabManagerTab = Class.create();
TabManagerTab.prototype = {
	initialize: function(tabmanager, ownerid, titlebar, options){
		var options = Object.extend({
			 claseContenidoTab: null,
			 contenido: null,
			 selected: true,
			 showCloseBtn: false,
			 onFocus: function(){},
			 onBlur: function(){},
			 onClose: function(){ return true }
		}, options || {});
		this.options = options;		
		this.tabmanager = tabmanager;
		this.ownerid = ownerid;
		this.titlebar = titlebar;
		this.displayed = false;		
		this.label;
		this.labelTitle;
		this.div_contenedor;
		this._crearCampos();
		if (this.options.selected == true){
			this.activar();
		}
	},
	
	_crearCampos: function() {						
		this.div_contenedor = $E({
			tag: 'div', className: (this.options.claseContenidoTab) ? this.options.claseContenidoTab : ''
		});
		
		this.label = $E({
			tag:'li',	children:{ 
				tag: 'a'
			}
		});
		
		this.labelTitle = this.label.firstChild;			
		
		this.labelTitle.innerHTML = this.titlebar;
		if ( this.tabmanager.options.selectable ){
			this.label.onclick = this.activar.bindAsEventListener(this);		
		}
		if (this.options.showCloseBtn){
			
			closeBtn = $E({tag: 'span', className: 'btn_cerrar_pestana'});
			closeBtn.onclick = this.quitar.bindAsEventListener(this);
			this.labelTitle.appendChild(closeBtn);
		}		
		this.esconder();
		$(this.tabmanager.ul_pestanas).appendChild(this.label);
		$(this.tabmanager.div_contenedor).appendChild(this.div_contenedor);
		if ($(this.options.contenido)){ // Si existe el Contenido
			$(this.div_contenedor).appendChild($(this.options.contenido));
		}
		this.tabmanager.actualizar();		
	},
	
	activar: function() {
		this.tabmanager.mostrarTab(this);
	},
	
	quitar: function(evt){		
		//this.tabmanager.quitarTab(this);
		
		// Se agrega para dar soporte a Firefox 3, Safari 3
		// detiene el evento click para no seleccionar el tab eliminado nuevamente
		Event.stop(evt); 
		var quitar = this.options.onClose(this);
		if ( quitar ){			
			//this.frame.src = '';
			this.tabmanager.quitarTab(this);					
		}
			
	},
	
	mostrar: function()	{
		this.label.className = 'current';
		Element.show(this.div_contenedor);		
		this.displayed = true;
		this.options.onFocus(this);
	},

	esconder: function() {
		this.label.className = '';
		Element.hide(this.div_contenedor);
		this.displayed = false;		
		this.options.onBlur(this);
	},
	
	eliminar: function(){
		Element.remove(this.div_contenedor);
		Element.remove(this.label);
		this.displayed = false;
	},
	
	limpiar: function(){
		this.div_contenedor.innerHTML = '';
	},
	
	actualizarLabel: function(texto){
		this.labelTitle.innerHTML = texto;	
	}	
};

/*--------------------------------------------------------------------------*/

var TabManagerTabExtendido = Class.create();
TabManagerTabExtendido.prototype = {

	initialize: function(tabmanager, ownerid, labelText, options){
		var options = Object.extend({
			 claseContenidoTab: null,
			 selected: true,
			 url: null,
			 usarAjax: false,
			 showCloseBtn: false,
			 iframeScrollBars: 'auto',
			 iframeHeightOffset: 35,
			 onFocus: function(){},
			 onBlur: function(){},
			 onClose: function(){return true}
		}, options || {});
		this.options = options;		
		this.tabmanager = tabmanager;
		this.ownerid = ownerid;
		this.displayed = false;		
		this._crearCampos(labelText);
		if (this.options.selected == true){
			this.activar();
		}		
	},
	
	_crearCampos: function(labelText) {
		this.div_contenedor = $E({
			tag: 'div', className: (this.options.claseContenidoTab) ? this.options.claseContenidoTab : ''
		});
		this.label = $E({
			tag:'li',	children:{ 
				tag: 'a', children:{
					tag: 'span', className: 'busy'								
				}
			}
		});
		this.labelTitle = this.label.firstChild;
		new Insertion.Bottom(this.labelTitle, labelText);
		
		if (this.options.showCloseBtn){
			closeBtn = $E({tag: 'span', className: 'btn_cerrar_pestana'});
			closeBtn.onclick = this.quitar.bindAsEventListener(this);
			closeBtn.innerHTML = "&nbsp;";
			this.labelTitle.appendChild(closeBtn);
		}		
		this.label.onclick = this.activar.bindAsEventListener(this);

		this.esconder();
		$(this.tabmanager.ul_pestanas).appendChild(this.label);
		$(this.tabmanager.div_contenedor).appendChild(this.div_contenedor);		
		
		var labelTitle = this.labelTitle;
		if (this.options.usarAjax){
			new Ajax.Updater(this.div_contenedor, this.options.url, {evalScripts: true, onComplete: function(){Element.hide(labelTitle.firstChild);}});	
		}
		else{
			var iframeHeightOffset = this.options.iframeHeightOffset;
			var iframeHeight = window.getHeight() - iframeHeightOffset;
			this.div_contenedor.innerHTML = '<iframe class="iframe_editar" src='+ this.options.url +' frameborder="0" scrolling="'+ this.options.showScrollBars +'" style="height:' + iframeHeight + 'px;"></iframe>';
			this.frame = this.div_contenedor.firstChild;
			var iframe = this.frame;
			Element.hide(iframe);
			Event.observe(window, 'resize', function(){var iframeHeight = window.getHeight() - iframeHeightOffset; iframe.style.height = iframeHeight + 'px';}, false);
			Event.observe(this.frame, 'load', function(){ 
				Element.hide(labelTitle.firstChild); 
				Element.show(iframe); 
				//Event.observe(iframe.contentWindow, 'click', function(){alert("aaa")}, true);
				Event.observe(iframe.contentWindow, 'click', top.Escritorio.Sesion.resetTimeout, true);			
			}, false);
		}
		this.tabmanager.actualizar();	
	},
	
	activar: function() {
		this.tabmanager.mostrarTab(this);
	},
	
	quitar: function(evt){
		// Se agrega para dar soporte a Firefox 3, Safari 3
		// detiene el evento click para no seleccionar el tab eliminado nuevamente
		Event.stop(evt); 
		
		var quitar = this.options.onClose(this);
		if ( quitar ){			
			//this.frame.src = '';
			this.tabmanager.quitarTab(this);					
		}
	},

	mostrar: function()	{
		this.label.className = 'current';
		Element.show(this.div_contenedor);
		this.displayed = true;
		this.options.onFocus(this);
	},

	esconder: function() {
		this.label.className = '';
		Element.hide(this.div_contenedor);
		this.displayed = false;
		this.options.onBlur(this);
	},
	
	eliminar: function(){
		Element.remove(this.div_contenedor);
		Element.remove(this.label);
		this.displayed = false;
	},
	
	limpiar: function(){
		this.div_contenedor.innerHTML = '';
	},
	
	getLabel: function(){
		return (this.labelTitle.childNodes[1]);
	},
		
	actualizarLabel: function(text){
		Element.remove(this.labelTitle.childNodes[1]);		
		new Insertion.After(this.labelTitle.childNodes[0], text);
	}
};