﻿(function($)
{
	$.fn.CreateTabs = function(settings)
	{		
		// Config Array
		var config = $.extend(
		{
			// ID for current tab item
			// This needs to get passed as a parameter when creating the tabs
			tabsID:	 '',
			
			// Tab CSS Class
			tabsClass:			'.tabs',
			tabsContentClass:	'.tabs-content',
			tabSelected:		'.selected',
			
			// Tab Items HTML Tag
			tabsTag:	'li'
			
		}, settings || {});
		
	//  =============
	//  Global Object
		var global = 
		{
			currentItem: null,
			currentContent: null,	
			prevItem: null,
			prevContent: null,
			
			controlsVisible: true
		};
		
	//  =================
	//  Initiate Function
		var jQueryTabs = function() 
		{
			// Find the ID for the first content object
			// Will be used to set the previous content element
			var firstContentID = $(config.tabsContentClass, '#' + config.tabsID).children(':first-child').attr('id');
			
			// Set the first Item to set as default previous item
			global.prevItem = $(config.tabsTag + ':first-child', '#' + config.tabsID);
			global.prevContent = $('#' + firstContentID);
			
			// Create a new tab item with each tab
			$(config.tabsClass + ' > ' + config.tabsTag, '#' + config.tabsID).each(function()
			{
				var elem = tabItem($('a:first-child', this));
				elem.removeLinks();
				elem.enableClick();
			});
		};
		
	//  =================
	//  Tab Item Function
		var tabItem = function(elem)
		{
			var methods = {};
			
			// Split the href value and add the value as a 'rel' attribute
			// This value will be use to toggle the appropriate content holder
			methods.removeLinks = function()
			{
				var href 	= elem.attr('href');
				var id_arr 	= href.split('=');
				var id		= id_arr[id_arr.length - 1];
				
				// Ensure the element gets the hand cursor
				elem.css({cursor: 'pointer'});
				elem.attr('rel', id);
			};
			
			// Enable click event on the tab item
			// This results in a call to the handleClick function
			methods.enableClick = function()
			{
				elem.bind('click', { btn: elem }, methods.handleClick);
			};
			
			// Handling Click Event
			methods.handleClick = function(event)
			{
				// Store temporary variable to hold the clicked tab
				// Information is passed from the enableClick function
				var currentTabItem = $(event.data.btn);
				global.currentItem = currentTabItem.parent(config.tabsTab);
				
				// This test ensures the tab clicked is NOT the currently selected one
				if (global.currentItem.attr('class') != 'selected')
				{
					if ($('.slider-controls', '.home-feature-slider').length > 0)
					{
						if (global.currentItem.attr('class') == '')
						{
						    global.prevContent.parent().css({ border: '1px solid #DBDBDB' });
							$('.slider-controls', '.home-feature-slider').css({ display: 'none' });
						}
						else
						{
							global.prevContent.parent().css({ border: '1px solid #F2F2F0' });
							$('.slider-controls', '.home-feature-slider').css({ display: 'block' });
						}
					}
					
					
					// Assign the selected value to the proper tab item
					global.prevItem.removeClass('selected');
					global.currentItem.addClass('selected');
					
					// Retrieve the rel attribute assign in the removeLinks function
					// and update the currentContent variable
					var id = currentTabItem.attr('rel');
					global.currentContent = $('#tab-' + id);
					
					// Toggle the content
					global.currentContent.slideDown();//.addClass('show').removeClass('hide');
					global.prevContent.slideUp();//addClass('hide').removeClass('show');
					
					// Update the previous elements
					global.prevContent = global.currentContent;
					global.prevItem = global.currentItem;
				}
				
				// Prevent the link from firing
				return false;
			};
			
			return methods;
		};
		
		// Initiate the tabs
		jQueryTabs();
	};
})(jQuery);
