$().ready(function(){
	$('#map').mapTabs();
});

(function($){
	$.fn.extend({ 
		mapTabs: function() {	
			return this.each(function() {
				var obj = $(this);
				
				$(this).prepend('<ul class="nav"></ul>');
				$("h2", obj).each(function(){
					$("ul.nav").append('<li><a href="#' + $(this).parent().attr('id') + '">' + $(this).html() + '</a></li>')
				});
				
				switchMapTab(obj, '#' + $('.map-layer:first', obj).attr('id'));
				
				$(obj).children(".map-layer").each(function(){
					var id = "#" + $(this).attr('id');
					$("a[href=" + id + "]").click(function(){
						switchMapTab(obj, id)
						return false;
					});
				});
			
			});
		}
	});
	
	function switchMapTab(obj, id) {
		$(obj).children(".map-layer").hide();
		$(id).show();
		$("ul.nav li", obj)
			.removeClass('current')
			.children("a[href=" + id + "]").parent().addClass('current');
	};
})(jQuery);