/**
 * Tabbr v1.0
 *
 * Author: Matthew Kingston <matthew@clickcreative.com.au>
 *         Copyright: 2011 © Click Creative Pty Ltd
 * 
 */
(function(window, $, undefined)
{
	
	
	var Tabbr = window.Tabbr = {
		
		/**
		 * The current version of Mappr.
		 *
		 * @type String
		 */
		version: "1.0",
		
		/**
		 * Event to trigger display of tabbr panel
		 */
		triggerEvent: 'click',
		
		/**
		 * Class name of the tabbr panels
		 *
		 * @type String
		 */
		panelClass: 'tabbrPanel',
		
		/**
		 * Class name to append to an active tabbr panel
		 */
		activePanelClass: 'tabbrActive',
		
		/**
		 * Class name of the tabbr menu container
		 *
		 * @type String
		 */
		menuClass: 'tabbrMenu',
		
		/**
		 * Class name of the tabbr menu link items
		 *
		 * @type String
		 */
		menuLinkClass: 'tabbrLink',
		
		/**
		 * Class name of the active tabbr menu link
		 */
		activeMenuLinkClass: 'tabbrActive',
		
		
		hideMethod: 'hide',
		
		
		hideMethodDuration: 1000,
		
		
		showMethod: 'fadeIn',
		
		showMethodDuration: 1000
	}
	
	
	Tabbr.initialize = function()
	{
		var self = $(this);
		var menuLinks = $('.' + Tabbr.menuClass + ' .' + Tabbr.menuLinkClass, self);
		var panels = $('.' + Tabbr.panelClass, self);
		
		// Hide all tabbr panels
		$(panels, self).hide();
		
		// Add click event handlers to the tabbr menu links
		$(menuLinks, self).bind(Tabbr.triggerEvent, function(e)
		{
			e.preventDefault();
			
			if(!$(this).hasClass(Tabbr.activeMenuLinkClass))
			{
				// Remove all active classes from the current tabbr link menu
				$(menuLinks, self).removeClass(Tabbr.activeMenuLinkClass);
				$(menuLinks, self).parent().removeClass(Tabbr.activeMenuLinkClass);
				
				// Add an active ("tabbrActive") class to selected tab
				$(this).addClass(Tabbr.activeMenuLinkClass);
				$(this).parent().addClass(Tabbr.activeMenuLinkClass);
				
				// Hide all tabbr panels
				if(Tabbr.hideMethod == 'hide')
				{
					$(panels, self).hide();
				}
				else
				{
					$(panels, self)[Tabbr.hideMethod](Tabbr.hideMethodDuration);
				}
				
				//Find the href attribute value to identify the active tab + content
				var panel = $(this).data("tabbr-target");
				
				// Show the active tabbr panel
				if(Tabbr.hideMethod == 'show')
				{
					$(panel).show();
				}
				else
				{
					$(panel)[Tabbr.showMethod](Tabbr.showMethodDuration);
				}
			}
			
			return false;
		});
		
		if(!self.activeTab)
		{
			var activeTab = $(menuLinks).filter('[class~="' + Tabbr.activeMenuLinkClass + '"]');
			var firstTab = $(menuLinks).first();
			
			self.activeTab = activeTab.length > 0 ? activeTab : firstTab;
		}
		
		self.activeTab.removeClass(Tabbr.activeMenuLinkClass);
		self.activeTab.trigger(Tabbr.triggerEvent);
	}
	
	
	/**
	 * jQuery element prototype function that allows any jQuery
	 * element collection to call this method using the following:
	 *
	 * <code>
	 *     $('div').Tabbr();
	 * </code>
	 *
	 */
	$.fn.Tabbr = function()
	{
		return this.each(function()
		{
			Tabbr.initialize.apply(this);
		});
	}
	
	
	$(function()
	{
		if($('*[class~="tabbr"]').length)
		{
			$('*[class~="tabbr"]').Tabbr();
		}
	});
	
	
})(window, jQuery);
