
/*
 * Widget encargado de establecer la propiedad de collapse/expand a una capa
 * @param gestEl Id o elemento del elemento encargado de gestionar el efecto
 * @param el Id o elemento que sufre el efecto
 * @param config Objeto que de configuracion para el collapsable
 *    - elExpandClassName: Nombre de la clase que se asigna al elemento cuando estamos
 *                         ante el efecto expandido
 *    - elCollapseClassName: Nombre de la clase que se asigna al elemento cuando estamos
 *                           ante el efecto colapsado
 *    - expand Valor booleano que indica si el elemento originalmente se debe mostrar expandido
 *    - anim Si se debe realizar o no una animacion sobre el evento
 */
GALILEO.widget.Collapsable = function(gestEl, el, config) {

	if (el && gestEl) {
		
		this._el = null;
		this._animHeight = null;
		this._gestEl = null;
		this._animCollapse = null;
		this._animExpand = null;

		this._elExpandClassName = null;	
		if (config.elExpandClassName) {
			this._elExpandClassName = config.elExpandClassName;
		}
		this._elCollapseClassName = null;
		if (config.elCollapseClassName) {
			this._elCollapseClassName = config.elCollapseClassName;
		}
		
		if (typeof gestEl == "string") {
			this._gestEl = YAHOO.util.Dom.get(gestEl);
		}
		else {
			this._gestEl = gestEl;
		}
					
		if (typeof el == "string") {
			this._el = YAHOO.util.Dom.get(el);
		}
		else {
			this._el = el;
		}
		
		if (config.anim) {	
			var parent = this._el.parentNode;
			var divAnimacion = document.createElement('div');
			divAnimacion.id = 'anim-' + this._el.id;
			parent.appendChild(divAnimacion);
			divAnimacion.appendChild(this._el);
			YAHOO.util.Dom.setStyle(divAnimacion, 'overflow', 'hidden');
			YAHOO.util.Dom.setStyle(divAnimacion, 'display', 'none');
			YAHOO.util.Dom.setStyle(this._el, 'display', 'block');

			var s = divAnimacion.style;
			s.height = "0px";
			s.display = "block";
			this._animHeight = divAnimacion.scrollHeight;
			
			if (config.expand) {
				s.height = this._animHeight + "px"; 		
			} 
			
			var attCollapse = {
				height: {from: this._animHeight, to: 0}
			};
			var attExpand = {
				height: {from: 0, to: this._animHeight}
			};
			
			this._animCollapse = new YAHOO.util.Anim(divAnimacion, attCollapse, 0.75, YAHOO.util.Easing.easyOut);
			this._animExpand = new YAHOO.util.Anim(divAnimacion, attExpand, 0.75, YAHOO.util.Easing.easyOut);			
		}
		
		if (config.expand) {
			if (this._elCollapseClassName) {
				YAHOO.util.Dom.addClass(this._gestEl, this._elCollapseClassName);
			}

			this._status = 1;
		}
		else {
		
			this._precollapse();		
			if (!config.anim) {
				YAHOO.util.Dom.setStyle(this._el, 'display', 'none');
			}
		}
		
		YAHOO.util.Event.addListener(this._gestEl, 'click', this.expandCollapse, this);
	}
};

/*
 * Definicion de variables privadas utilizadas por el elemento
 */
GALILEO.widget.Collapsable._el = null;
GALILEO.widget.Collapsable._status = 0;
GALILEO.widget.Collapsable._animHeight = null;
GALILEO.widget.Collapsable._animCollapse = null;
GALILEO.widget.Collapsable._animExpand = null;
GALILEO.widget.Collapsable._gestEl = null;
GALILEO.widget.Collapsable._elExpandClassName = null;
GALILEO.widget.Collapsable._elCollapseClassName = null;

GALILEO.widget.Collapsable.prototype._precollapse = function() {
	if ((this._elCollapseClassName) && (YAHOO.util.Dom.hasClass(this._gestEl, this._elCollapseClassName))) {
		YAHOO.util.Dom.removeClass(this._gestEl, this._elCollapseClassName);
	}
	if (this._elExpandClassName) {
		YAHOO.util.Dom.addClass(this._gestEl, this._elExpandClassName);
	}
	
	this._status = 0;
};

/*
 * Definicion del metodo encargado de collapsar la capa
 */
GALILEO.widget.Collapsable.prototype.collapse = function() {
	
	this._precollapse();
	
	if (this._animCollapse) {
		this._animCollapse.animate();
	}
	else {
		YAHOO.util.Dom.setStyle(this._el, 'display', 'none');
	}
};

/*
 * Definicion del metodo encargado de expandir la capa
 */
GALILEO.widget.Collapsable.prototype.expand = function() {
	if ((this._elExpandClassName) && (YAHOO.util.Dom.hasClass(this._gestEl, this._elExpandClassName))) {
		YAHOO.util.Dom.removeClass(this._gestEl, this._elExpandClassName);
	}
	if (this._elCollapseClassName) {
		YAHOO.util.Dom.addClass(this._gestEl, this._elCollapseClassName);
	}
	
	YAHOO.util.Dom.setStyle(this._el, 'display', 'block');
	this._status = 1;
	if (this._animExpand) {
		this._animExpand.animate();
	}
};

/*
 * Definicion del metodo encargado de collapsar la capa
 */
GALILEO.widget.Collapsable.prototype.expandCollapse = function(e, obj) {

    if (obj._status == 1) {
    	obj.collapse();
    }
	else if (obj._status == 0) {
		obj.expand();
	}
};

