(function($) {

	/**
	 * Motionlab Tabs
	 * @author Joe Critchley, Motionlab
	 * @date 2010-03-02
	 */

	$.fn.ml_tabs = function(options) {
	
		var defaults = {
			panels: '.itemContent',
			tabs: '.tabs'
		};
		
		options = $.extend(defaults, options);
		
		return this.each(function() {
		
			/**
			 * Selector caching
			 */
			var $obj = $(this),
				$tabs = $(options.tabs, $obj),
				$tabLinks = $('a', $tabs),
				$tabPanels = $(options.panels, $obj);
				
				

			/**
			 * Dynamics
			 */
			
			
		
			/**
			 * Init
			 */
		
			function init() {
				
				// Hide em all to start with (except the first).
				//$tabPanels.not(':first').hide();
							
				// Bind it up.
				$tabLinks.bind('click', switchPanel);
				
				//$livePanel = $('#tabName').val();
				$('#li_tabItem_1').removeClass('current');
						$('#li_tabItem_2').removeClass('current');
						$('#li_tabItem_3').removeClass('current');
						$('#li_tabItem_4').removeClass('current');
						$('#li_tabItem_5').removeClass('current');
						
						$('#li_'+$('#tabID').val()).addClass('current');
						
						//console.debug('#li_'+$('#tabID').val());
						
				$tabPanels.each(function(i) {	
						$item = '#'+$('#tabID').val();
						//console.debug($item+' : #'+$(this).attr('id'));	
						//console.debug('li_'+$('#tabID').val());
						
						
						
					if('#'+$('#tabID').val() == '#'+$(this).attr('id')) {
						
						//$(event.target).parent('li').addClass('current');
						$(this).show();
					}
					else {
						//$(event.target).parent('li').removeClass('current');
						//$('li_'+$('#tabID').val()).removeClass('current');
						$(this).hide();
					}	
				});
				
			}
			
			function switchPanel(event) {
				
				$tabLinks.parent('li').removeClass('current');
				
				$tabPanels.each(function(i) {																 
					if($(event.target).attr('href') == '#'+$(this).attr('id')) {
						$('#tabName').val($(event.target).html());
						$('#tabID').val($(this).attr('id'));
						$(event.target).parent('li').addClass('current');
						$('.thumbs:first li',$(this)).trigger('click');
						$(this).show();
						//$innerSlideshow.trigger('slide', [(currentIndex)]);

					}
					else {
						$(this).hide();
					}	
				});
			
				return false;
			
			}

			init();
		
		});
			
	};

})(jQuery);