
$.fn.accordian = function( options ){

	var Plugin = function( options ){

		this.construct		= function(){

			this.setupVars();
			this.bind();
			return this;
		}
		
		this.setupVars		= function(){

			this.$this			= options.elem;
			this.$accordians	= this.$this.find(options.wrapper);
			
			for(var x in options ){
				this[x]	= options[x];
			}
		}

		this.bind			= function(){

			var inst		= this;

			this.$accordians.each(function( i ){
				var $wrapper = $(this);
				var $title = $wrapper.find(inst.title);
				var $content = $wrapper.find(inst.content);
				
				$wrapper.data('closed', true);
				
				$title.click(function( event ){
					event.preventDefault();
					if( $wrapper.data('closed') ){
						$wrapper.data('closed', false);//.removeClass(inst.closeClass);
						$title.addClass(inst.selectedClass);
						$content.slideDown(500, 'easeInOutQuart');
						inst.$accordians.not($wrapper).each(function () {
							var $otherWrapper = $(this);
							var $otherTitle = $otherWrapper.find(inst.title);
							var $otherContent = $otherWrapper.find(inst.content);
							
							$otherWrapper.data('closed', true);//.addClass(inst.closeClass);
							$otherTitle.removeClass(inst.selectedClass);
							$otherContent.slideUp(500, 'easeInOutQuart');
						});
					}else{
						$wrapper.data('closed', true);//.addClass(inst.closeClass);
						$title.removeClass(inst.selectedClass);
						$content.slideUp(500, 'easeInOutQuart');
					}
				});
			});
		}
	};

	return this.each( function(){

		var $this		= $(this);
		var opts = {
			wrapper : '.wrapper',
			title	: '.title',
			content	: '.content',
			closeClass : 'closed',
			selectedClass : 'selected'
		};
		
		$.extend( true, opts, options );
		opts.elem = $this;
		
		$this.data('Accordian', new Plugin(opts).construct() );

	});

}
