/**
* Allow collapsing containers easily
* Written by: Nathan Newport
* April 1, 2011
*/
(function( $ )
{    
    var methods = {
    
            init : function( options )
            {
				//specify the default settings
                var data = {
						clickSelector: 'a',
						collapseSelector: 'div',
						//collapseTag: '', //Not implemented yet
						//expandTag: '', //Not implemented yet
						collapseClass: "closed",
						expandClass: "open",
						defaultState: 'closed'
                }

				//override defaults if options are provided
                if( options )
                {
                    $.extend( data, options);
                }
				$(this).data('collapsible', data);
				
				$(data.clickSelector, this).mouseenter(function(){ $(this).css("cursor", "pointer"); }).mouseleave(function(){ $(this).css("cursor", "default"); });
				
				$(this).each(function()
				{
	                var $this = $(this);
					
					if(data.defaultState == 'closed')
					{
						if(!($(data.clickSelector, this).hasClass(data.expandClass)))
						{
							$(data.clickSelector, this).addClass(data.collapseClass);
							$(data.collapseSelector, this).css("display","none");
						}
					}
					else if(data.defaultState == 'open')
					{
						if(!($(data.clickSelector, this).hasClass(data.collapseClass)))
						{
							$(data.clickSelector, this).addClass(data.expandClass);
						}
					}
						
					$(data.clickSelector, this).click(function() 
					{
						$this.collapsible('toggle');
					});
				});
                return $(this);
            },
            
			/**
			 * Toggle the collapseSelector container
			 */
			toggle: function()
			{
				 var $this = $(this),
				 data = $(this).data('collapsible'),
				 collapsed = ($(data.collapseSelector, $this).css("display") == "none");
				
				if(collapsed)
				{
					if(data.expandClass != "")
					{
						if(data.collapseClass != "") {
							$(data.clickSelector, this).removeClass(data.collapseClass);
						}
						$(data.clickSelector, this).addClass(data.expandClass);
					}
				}
				else
				{
					if(data.collapseClass != "")
					{
						if(data.expandClass != "") {
							$(data.clickSelector, this).removeClass(data.expandClass);
						}
						$(data.clickSelector, this).addClass(data.collapseClass);
					}
				}
				
				$(data.collapseSelector, this).slideToggle();
				return $(this);
			},
			
            stop: function( options )
            {
                $(this).stop(true);
                return $(this);
            }
    }
    
    //declare the plugin and allow method calling
     $.fn.collapsible = function( method )
     {
            // Method calling logic
            if ( methods[method] ) {
              return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
            } else if ( typeof method === 'object' || ! method ) {
              return methods.init.apply( this, arguments );
            } else {
              $.error( 'Method ' +  method + ' does not exist on jQuery.collapsible' );
            }
     };
    
})( jQuery );
