$(document).ready(function() {
	$(".tabs").each(function() {
		var options = $.extend({
			selectedClass: "selected"
		}, {});
		var self = this;
		var tabs = $("li>a", this);
		var current = undefined;
		
		if (tabs.length < 1) return;
		
		var showTab = function(elem) {
			if (elem.tab && elem.tab.target_elem && !elem.tab.disabled) {
				$(elem.tab.target_elem).show();
				elem.tab.current = true;
				$(elem).addClass(options.selectedClass);
			}
		};
		
		var hideTab = function(elem) {
			if (elem.tab && elem.tab.target_elem && !elem.tab.disabled) {
				$(elem.tab.target_elem).hide();
				elem.tab.current = false;
				$(elem).removeClass(options.selectedClass);
			}
		};
		
		var tab_onclick = function() {
			if (this.tab && !this.tab.current) {
				hideTab($(current)[0]);
				showTab(this);
				current = this;
			}
			return false;
		};
		
		tabs.each(function(idx) {
			var href = this.href,
				disabled = true,
				target_elem = undefined;
				
			if (href != "") {
				var m = href.match(/#(.+)/);
				if (m.length > 0) {
					var container = $("#"+m[1]);
					if (container.length) {
						target_elem = container[0];
						disabled = false;
					}
				}	
			}
			
			this.tab = {
				tab_idx: idx,
				target_elem: target_elem,
				disabled: disabled,
				current: false
			};
			
			if (!this.tab.disabled) {
				
				$(this.tab.target_elem).hide();
				$(this).bind("click", tab_onclick);
			}
		});
		
		current = tabs.filter("."+options.selectedClass);
		if (!current.length) {
			current = $(tabs[0]);
		}
		
		showTab($(current)[0]);
	});
});
