(function($) {
/* Plugin Name:		
	 * Biber Tabs
	 * Programmed by:	Bircan Tuner
	 * Programmed for:	biber Ltd. of Istanbul, Turkiye.
	 * Licensed under:	GPL
	 * 
	 * version:			1.2
	 *					
	 *					1.2 fixes :
	 *						- Problem with the min-height of the first tab-content to be shown fixed.
	 * 
	 * 
	 * http://www.biberltd.com
	 */


$.fn.biberTabs = function(options){
	var defaults = {
		startWith:		null	// ID of the tab content that should be visible at the initialization. Default is the first element.	
	};
	
	var options = $.extend(defaults, options);
	
	return this.each(function() { 
		
		var cont = $(this);
		cont.tabsNav = null;
		cont.tabsArray = new Array();
		create(cont, options);
		
	}); 
}

/*
 *  Creates the tabs according to the title and id given to the tab-contents. 
 */

create = function(cont, options) {
	
	var containerID = cont.attr('id');
	var tabsContent = cont.children();
	
	cont.addClass('tabs-module');
	cont.wrapInner('<div class="tabs-content"></div>');
	cont.prepend('<div class="tabs-nav"><ul></ul></div>');
	cont.tabsNav = cont.find('.tabs-nav');
	
	var tabsNavInner = '';
	var i = 1;
	tabsContent.each(function(){
		
		// create the tab-navigation
		var tabID = containerID + '-tab-' + i;
		tabsNavInner += '<li><a id="' + tabID +'" href="#"><span>' + $(this).attr('title') + '</span></a>';
		
		// look if the container has already an ID 
		if($(this).attr('id').length == 0) {
			$(this).attr('id', containerID + '-tab-' + i + '-content');	
		}
		
		$(this).removeAttr('title');
		
		cont.tabsArray[tabID] = $(this).attr('id');
		
		i++;
		
	});
	
	cont.tabsNav.find('ul').append(tabsNavInner);
	
	setup(cont, options);
}

/*
 * Setup of the Tabs. It sets which tab-content should be visible and binds click-funtions to the tab-elements.
 */
setup = function(cont, options) {
	
	// which tab content is to be visible (Always add Class 'on' to the element)
	var showID = '';
	if(options.startWith == null){
		showID = '#' + cont.tabsArray[cont.tabsNav.find('ul').children('li:first').addClass('on').find('a').attr('id')];	
	}
	else {
		
		for(key in cont.tabsArray) {
			if(cont.tabsArray[key] == options.startWith) {
				showID = '#' + cont.tabsArray[key];	
				cont.tabsNav.find('a#' + key).parent('li').addClass('on')	
			}
		}
	}
	
	$(showID).show(function(){
		cont.find('.tabs-content').css('min-height', $(showID).height());	
	});
	
	cont.tabsNav.find('ul li a').bind('click',function(event){
		showTabContent(cont, $(this).attr('id'));
		event.preventDefault();
	})
}

/*
 *  Gets the tabID as parameter and shows the tab-content related to it
 */
showTabContent = function(cont, tabID) {
	var tab = $('#' + tabID);
	var tabContent = $('#' + cont.tabsArray[tabID]);
	
	if(tabContent.is(':hidden')){
		tabContent.siblings(':visible').fadeOut(500,function(){
			tabContent.fadeIn(500,function(){
						
				// get the min-height of the Tabs-container
				var temp = tabContent.parent('.tabs-content').css('min-height').split('p');
				var minHeight = parseInt(temp[0]);
				
				// set the min-height of the tab-content to the container, if its height is smaller 
				
				if(tabContent.height() > minHeight) {
					tabContent.parent('.tabs-content').css('min-height',tabContent.height() + 'px');
				}
			});
			tabContent.parent('.tabs-content').siblings('.tabs-nav').find('li.on').removeClass('on');
			tab.parent('li').addClass('on');
		});
	}
}

})(jQuery);
