/*!
 * Collapse plugin for jQuery
 * http://github.com/danielstocks/jQuery-Collapse/
 *
 * @author Daniel Stocks (http://webcloud.se)
 * @version 0.9.1
 * @updated 17-AUG-2010
 * 
 * Copyright 2010, Daniel Stocks
 * Released under the MIT, BSD, and GPL Licenses.
 */
 
(function($) {
    $.fn.extend({
        collapse: function(options) {
            
            var defaults = {
				bypass: false,
                // Default function for showing content
                show: function() { 
                    this.show();
                },
                // Default function for hiding content
                hide: function() { 
                    this.hide();
                }
            };
            var op = $.extend(defaults, options);
            
            // Default CSS classes
            var active = "active",
                inactive = "inactive";
            
            return this.each(function() {
                var obj = $(this);
    
                // Bind event for showing content
                obj.bind("show", function(e, options) {
                    var obj = $(e.target),
						opt = $.extend({}, op, options);
                    
					// skip if the object is already active
					if (obj.hasClass(active)) {
						if (opt.complete) {
							opt.complete.call();
						}
						return;
					}
					
                    if(opt.bypass) {
                        obj.show();
                    } else {
                        opt.show.call(obj, options);
                    }
					// Update attributes after starting the transition,
					// so that the callback sees the correct properties
					// at the start
                    obj.attr('aria-hidden', false)
                    obj.removeClass(inactive)
                        .addClass(active);
                });

                // Bind event for hiding content
                obj.bind("hide", function(e, options) {
                    var obj = $(e.target),
						opt = $.extend({}, op, options);
					
					// skip if the object is already hidden
                    if (obj.hasClass(inactive)) {
						if (opt.complete) {
							opt.complete.call();
						}
						return;
					}
					
                    if(opt.bypass) {
                        obj.hide();
                    } else {
                        opt.hide.call(obj, options);
                    }
					// Update attributes after starting the transition,
					// so that the callback sees the correct properties
					// at the start
                    obj.attr('aria-hidden', true);
                    obj.removeClass(active)
						.addClass(inactive);
                });
				
				// Toggle event for switching
				obj.bind("toggle", function(e, options) {
                    var obj = $(e.target);
						
                    if (obj.hasClass(active)) {
						obj.trigger('hide', options);
					} else if (obj.hasClass(inactive)) {
						obj.trigger('show', options);
					}
				});                                
            });
        }
    });
})(jQuery);
