(function( $ ){
	
	var settings = {
		tabs: [
//		       {plugin: $('#selector'), method: $('#selector').someMethod}
		       ]
    };
	
	var methods = {
		init : function (options) {
			if ( options ) { 
				$.extend( settings, options );
			}
			
			return this.each(function(i, el){
		    	var _this = this;
		    	var $this = $(this);
		    	var items = [];
		    	var opened = null;
		    	
		    	function _openTab (evt) {
		    		var el = $(this).parents('.accordionItem').get(0);
		    		if (opened == el) return;
		    		if (opened != null) {
		    			$(opened).removeClass('opened');
		    			$(opened).css('height', $(opened).data('height'));
		    			try {$(opened).easySearch('onCloseTab');} catch (e) {}
			    		try {$(opened).advancedSearch('onCloseTab');} catch (e) {}
		    			try {$(opened).designerSearch('onCloseTab');} catch (e) {}
		    		}
		    		opened = el;
		    		var h = $this.height();
		    		for (var i=0;i<items.length;i++) {
		    			if (items[i].item != opened) {
		    				h -= $(items[i].item).data('height');
		    			}
		    		}
		    		$(opened).addClass('opened');
		    		$(opened).css('height', h);
				}
		    	
		    	function closeAllTabs () {
		    		$('#easySearch').easySearch('onCloseTab');
		    		$('#advancedSearch').advancedSearch('onCloseTab');
		    		$('#designerSearch').designerSearch('onCloseTab');
		    	}
		    	
		    	var i = 0;
		    	$('.accordionItem').each(function(i, el) {
		    		var ih = $('.accordionItemHead', el).get();
		    		items.push({item: el, head: ih});
		    		$(el).height($(ih).outerHeight());
		    		$(el).data('height', $(ih).outerHeight());
		    		$('a', ih).bind('click', _openTab);
		    		i++;
		    	});
		    	
		    	closeAllTabs();
		    });
		},
		
		openTab : function (i) {
			$($('.accordionItemHead a', this).get(i)).click();
		}
	}
	
	$.fn.fvtvAccordion = function( method ) {
	    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.fvtvAccordion' );
	    }
	}	
})( jQuery );
