jQuery.fn.extend({
	buttons: function(){
		$('<div id="buttons-content"></div>').appendTo('div.content');
		var items = $('div.item');
		
		for (var i = 0; i < items.length; i++) 
			$(items[i]).appendTo($('div#buttons-content')).hide();
		
		
		jQuery.extend({
			buttnz: this
		});
		
		
		
		var hash = document.location.hash || '';
		
		
		$('ul#subnav').find('li').each(function(i, obj){
			var anchor = $(obj).find('a');
			anchor.attr('title', anchor.attr('href'));
			anchor.attr('href', 'javascript:;');
			
			var h = anchor.attr('name');
			//anchor.attr('href', '#'+h);
			if ('#' + h == hash) {
				$.buttnz.find('li').each(function(i, obj){
					$(obj).removeClass('active');
				});
				$.buttnz.find('li').eq(i).addClass('active');
				$('div.item').removeClass('active').eq(i).addClass('active');
			}
			anchor.click(function(e){
				$.buttnz.find('li').removeClass('active');
				$.buttnz.find('li').eq(i).addClass('active');
				$('div.item').removeClass('active').eq(i).addClass('active');
				var rel = $(this).parent().parent().parent().attr('rel');
				$('body').animate({
					'scrollTop': '0'
				}, 500, function(){
					$('body').css({
						'backgroundImage': 'url(' + rel + ')'
					});
					$('div.item').not('.active').hide();
					$('div#buttons-content>div.active').fadeIn("slow");
				});
				//document.location.hash = '#'+$(this).attr('name');
				return false;
			});
		});
		
		if (typeof (relaxButtons)=='undefined') {
			$('div#buttons-content').find('.active').show();
			var rel = $.buttnz.find('li.active').attr('rel');
			$('body').animate({
				'scrollTop': '0'
			}, 500, function(){
				$('body').css({
					'backgroundImage': 'url(' + rel + ')'
				});
				$('div.item').not('.active').hide();
			});
		}
	}
});

$(document).ready(function(){
	$('ul#subnav').buttons();
});

