/**
 * jQuery Tabs
 * @version 0.01 - Initial build of the plugin. 
 *
 * For deeplinks use the item-id
 * 
 * <div class="tabContainer">
 * 		<ul class="tabs">
 *  		<li><a href="#a">Item A</a></li>
 * 			<li><a href="#b">Item B</a></li>
 * 			<li><a href="#c">Item C</a></li>
 * 		</ul>
 *
 * 		<div id="tabdata_a" class="tabdata">Content A</div>
 * 		<div id="tabdata_b" class="tabdata">Content B</div>
 * 		<div id="tabdata_c" class="tabdata">Content C</div>
 * </div>
 */

var tab = 'ul.tabs';
var tabContainer = '.tabContainer';
var activeClass = 'selected';

$(function(){
	$(tab).each(function() {
		$(this).find('a').live('click', function() {
			if ($(this).hasClass('disabled')) {
				return;
			}
	
			var id = this.hash.split('#')[1];
	
			$(this).parent()
				.closest(tab)
					.find('li')
						.removeClass(activeClass)
						.end()
					.end()
				.addClass(activeClass);
			
			$(this).closest(tabContainer).find('.tabdata').hide().filter( '#tabdata_' + id ).show();
	
		    return false;
		});

		$(this).find('a:first').click();
	});


	// Deeplink
	if(window.location.hash) {
		var hash = window.location.hash;

		if($a = $(tab + ' a[href=' + hash + ']')) {
			$a.click();
		}
	}
	
	
	// Observe anchors (starts with #)
	$('#content a[href^=#]').live('click', function() {
		var hash = $(this).attr('href');
		
		if(hash.length > 1) {
			// if tab content does exists
			if($a = $(tab + ' a[href=' + hash + ']')) {
				$('html, body').animate({
			    	scrollTop: $a.offset().top - 100
			  	}, 500, function() {
			    	$a.click();
			  	});
		
				return false;
			}
		}
	});
});
