/**
 * infoTabs plugin for jQuery
 * @copyright 2009 info.nl
 * @author Jorn Luiten / Arno Wolkers
 * @version 1.0
 * 
 * ----------------------------------------
 * HOW TO USE
 * ----------------------------------------
 * $('div.infoTabs').infoTabs();
 * 
 *
 * ----------------------------------------
 * OPTIONS
 * ----------------------------------------
 * See at the bottom of this page for public settings
 * Use the following to set the default value for all the tabs at once:
 * $.fn.infoTabs.defaults.activeClass = 'active';
 *  
 * 
 */
(function($){
	$.fn.infoTabs = function(options) {
		var settings = $.extend({}, $.fn.infoTabs.defaults, options);
		
		// checks if an internal link anchor is set on the url
		var urlTab = location.href.lastIndexOf('#') > 0 ? location.href.substr(location.href.lastIndexOf('#')) : false;
		
		return this.each(function(i, elem) {
			// is the urlTab found, then this is the active tab
			var active_tab = $(elem).find('ul.'+ settings.tabsClass +':first li a[href='+ urlTab +']');
			
			// else is the active class set on a <li> then this is the active tab
			if (active_tab.length == 0) {
				active_tab = $(elem).find('ul.'+ settings.tabsClass +':first li.' + settings.activeClass +' a');
				// else the first <li> is the active tab
				if (active_tab.length == 0) {
					active_tab = $(elem).find('ul.'+ settings.tabsClass +':first li:first-child a');
				}
			}
			
			
			$(elem).find('ul.'+ settings.tabsClass +':first li a').each(function(){
				var tab_to_activate = $(this).attr('href');
				
				// set click functionality on the tabs
				$(this).unbind('.infoTabs').bind('click.infoTabs', function(){
					// remove all active states on the tabs
					$(elem).find('ul.' + settings.tabsClass + ':first li').removeClass(settings.activeClass);
					// set the active tab
					$(this).closest('li').addClass(settings.activeClass);
					
					// hide all content divs
					$(elem).find(settings.tabContentSeletor +'.'+ settings.tabContentClass).hide();
					// show the active content div
					$(tab_to_activate).show();
					
					// Check if the default link action should be stopped
					if( settings.cancelLinkAction )
						return false;
				});
				
				
				// set external links to change a tab state
				$('body').find('a[href='+ tab_to_activate +']').not('ul.'+ settings.tabsClass +' li a[href='+ tab_to_activate +']').bind('click.infoTabs.infoTabsExtLink', function(){
					// activate the active tab
					$('ul.'+ settings.tabsClass +' li a[href='+ tab_to_activate +']').trigger('click');
					
					// activate all the parent tabs of the active tab
					$(tab_to_activate).parents('div.'+ settings.tabContentClass +':hidden').each(function(){
						$('ul.'+ settings.tabsClass +' li a[href=#'+ $(this).attr('id') +']').trigger('click');
					});
					
					// Check if the default link action should be stopped
					if( settings.cancelLinkAction )
						return false;
				});					
				
			});
			
			// activate the active tab on init
			$(active_tab).trigger('click');
			
			// if a #anchor is set on the url then check if parent tabs should be set to visible
			if (urlTab) {
				var urlTabObj = $(elem).find('ul.'+ settings.tabsClass +':first li a[href='+ urlTab +']');
				if (urlTabObj.length != 0) {
					$(urlTabObj).parents('div.'+ settings.tabContentClass +':hidden').each(function(){
						$('ul.'+ settings.tabsClass +' li a[href=#'+ $(this).attr('id') +']').trigger('click');
					});	
				}
			}
		});
	};
	
	// plugin defaults 
	$.fn.infoTabs.defaults = {
		activeClass:		'active',
		tabsClass:			'tabs',
		tabContentSeletor:  '> div',
    	tabContentClass:	'tabContent',
    	cancelLinkAction:	true
	};

})(jQuery);