/**
 * Binds basic animation functionality to a hideable object
 * @version 1.0
 * @notes 
 */

$.widget("ui.collapsible", {
	_init: function(){
		this._bindControls();
	},
	destroy: function(){
		$.widget.prototype.apply(this, arguments); // Default destroy
	},
	/* Public Methods */
	hide: function(){
		this._hide();
	},
	show: function(){
		this._show();
	},
	/* Private Methods */
	
	/**
	 * Bind the optional collapsible controls
	 */
	_bindControls: function(){
		if(this.options.collapseControl){
			var that = this;
			$(this.options.collapseControl).toggle(function(){
				that._hide();
			}, function(){
				that._show();
			});
		}
	},
	/**
	 * Hides the container object and updates the hide/show controller (when provided)
	 */
	_hide: function(){
		var container = this.options.containerControl || this.element;
		
		if($(container).not(":hidden") && $(container).not(":animated")){
			
			var that = this;
			$(container).slideUp(this.options.animationSpeed, function(){
				if(that.options.collapseControl){
					var collapse = that.options.collapseControl; 
					
					if(that.options.showText) $(collapse).text(that.options.showText);
					if(that.options.hideClass) $(collapse).removeClass(that.options.hideClass).addClass(that.options.showClass);
				}
			});
		}
	},
	/**
	 * Shows the container object and updates the hide/show controller (when provided)
	 */
	_show: function(){
		var container = this.options.containerControl || this.element;
		
		if($(container).is(":hidden") && $(container).not(":animated")){
			
			var that = this;
			$(container).slideDown(this.options.animationSpeed, function(){
				if(that.options.collapseControl){
					var collapse = that.options.collapseControl; 
					
					if(that.options.hideText) $(collapse).text(that.options.hideText);
					if(that.options.showClass) $(collapse).removeClass(that.options.showClass).addClass(that.options.hideClass);
				}
			});
		}
	}
});

$.extend($.ui.collapsible, {
	getter: "",
	defaults: {
		animationSpeed: "slow",
		hideText: "",
		showText: "",
		hideClass: "",
		showClass: "",
		collapseControl: null,
		containerControl: null
	}
});
