$(function() {
	$('.category ul li').each(function(){
		var $this = $(this);
		var hover = $this.find('.hover');
		var desc  = $this.find('span');
		
		hover.fadeTo(0,0);
		//desc.css({top: '-45px'});
		
		$this.hover(function(){
			desc.stop().animate({top:'0'},200);
			hover.stop().fadeTo(300,1);
		},function(){
			desc.stop().animate({top: '-45px'},200);
			hover.stop().fadeTo(300,0);
		});
	});
	
	var opened;
	
	function hideItem(item) {
		item.removeClass('active');
		item.find('ul').slideUp();
		item.find('.bg').stop().fadeTo(200,0);
	}
	
	$('.category').each(function(){
		var $this = $(this);
		var $h2   = $this.find('h2');
		var $bg   = $this.find('.bg');
		
		$bg.css({opacity:0});
		
		$this.click(function(){
			if ($this.hasClass('active')) {
				hideItem($this);
				opened = null;
				
			} else {
				if (opened) {
					hideItem(opened);
				}
				
				$this
					.addClass('active')
					.find('ul').slideDown();
				
				$bg.stop().fadeTo(200,1);
				
				opened = $this;
			}
		});
		
		$this.hover(function(){
			$bg.stop().fadeTo(200,1);
		},function(){
			if (!$this.hasClass('active')) {
				$bg.stop().fadeTo(200,0);
			}
		});
		
		$h2.hover(function(){
			if ($this.hasClass('active')) {
				$bg.addClass('bg-rotate');
			}
		},function(){
			$bg.removeClass('bg-rotate');
		});
	});
	
	if (window.location.hash) {
		current = parseInt(window.location.hash.substr(2));
		
		$($('.category')[current]).each(function(){
			$this = $(this);
			$this.addClass('active');
			$this.find('ul').slideDown(0);
			$this.find('.bg').fadeTo(0,1);
			
			opened = $this;
		});
	}
});

