window.addEvent('domready', function(){
	// Activate tabs
	$$('.ds-tabbar-tabs-li a').each(function(link) {
		var link = link;
		
		link.addEvents({
			'click': function() {
				var parentBox = this.getParent('.ds-tabbar');
				
				// Switch tab itself
				parentBox.getElements('.ds-tabbar-tabs-li').each(function(el) {
					el.removeClass('active');
				});
				
				link.getParent().addClass('active');
				
				// Switch tab body
				parentBox.getElements('.ds-tabbar-bodies-li').each(function(el) {
					el.removeClass('active');
				});
				
				var body_id = link.getProperty('href').substring(1);
				
				$(body_id).addClass('active');
				
				return false;
			}
		});
	});
	
	// If address points to achor tag that is a tab, activate that tab
	
	var hash = window.location.hash;
	
	if (hash.length > 1) {
		var tabs = $$('li.ds-tabbar-tabs-li a');
		
		tabs.each(function(tab) {
			if (tab.get('href').indexOf(hash) >= 0) {
				tab.fireEvent('click');
			}
		});
	}
	
	// TODO: Fix inconsistent handling of the fact that browsers want to scroll down
	// to the element specified by the hash. For now, we've got this nasty hack:
	
	window.scrollTo(0, 0);
});