
var Tabs = {
	change_tab : function($el){
		var activeTab = $el.attr("href"); //Find the href attribute value to identify the active tab + content
		var $li = $el.parents('li:first');
		
		$li.siblings('li').removeClass("active"); //Remove any "active" class
		$li.addClass("active"); //Add "active" class to selected tab
		$(".h-tab_content, .v-tab_content").hide(); //Hide all tab content
		
		$(activeTab).fadeIn(); //Fade in the active ID content
		
		//Tabs.set_hash(activeTab.replace('#',''));
	},
	
	hash_tab : function(){
		// # hash navigation
		var hash = Tabs.get_hash();
		if(hash){
			if($('#tab_'+Tabs.get_hash()).length > 0){
				Tabs.change_tab($('#tab_'+hash));
				$('.v-tab_container form, .h-tab_container form').each(function(c){
					var $form = $(this);
					var action = $form.attr('action');
					if(action.indexOf('#') != -1){
						action = action.replace(action.substr(action.indexOf('#'), action.length), '');
					}
					$form.attr('action',action+'#'+hash);
				});
			}
		} else {
			Tabs.change_tab($("ul.h-tabs li:first a, ul.v-tabs li:first a")); //Activate first tab
		}
	},
	
	get_hash : function(){
		return $.param.fragment();
	},
	
	init : function(){
		
		// this bind is not needed - events are triggered with hashchange event
		/*$("ul.tabs li a").click(function() {
			change_tab($(this));
			return false;
		});*/
		
		Tabs.hash_tab();
			
		$(window).bind( 'hashchange', function(e) {
			Tabs.hash_tab();
		});
	}
};

$(function(r){
	Tabs.init();
});
