jQuery(function($) { 
	
	$(document).ready(function() {
		$('.gallery a').click(function() {
			return false;
		});
	});
	
	var active = '#'+jQuery.url.attr("anchor");
	var activeCookie = jQuery.url.attr("anchor");
	
	
	$('.toggleMainCategory').each(function() {
		if($.cookie($(this).prev('.mainCategory').attr('id')) == 'collapsed' || $.cookie($(this).prev('.mainCategory').attr('id')) == null) {
			$(this).hide();
			$(this).prev('.mainCategory').children('.maxMin').removeClass('active');
		};
		
		if($.cookie($(this).prev('.mainCategory').attr('id')) == 'expanded') {
			$(this).prev('.mainCategory').children('.maxMin').addClass('active');
		};
	});
	if ($(active).children('.maxMin').hasClass('active')) {
		$.scrollTo(active, 500, {offset: {top:-20} });
	} else {
		$(active).children('.maxMin').addClass('active');
		$.cookie(activeCookie, 'expanded', { path: '/', expires: null });
		$(active).next('.toggleMainCategory').slideToggle(500, function () {
			$.scrollTo(active, 500, {offset: {top:-20} });
		});
	}		
	$('.maxMin').click(function(){
		if ($(this).hasClass('active')) {
			$.cookie($(this).parent().attr('id'), 'collapsed', { path: '/', expires: null });
		 	$(this).removeClass('active');
			$(this).parent().next('.toggleMainCategory').slideToggle(500);
		} else  {
			var whereTo = this;
			$(this).addClass('active');
			$.cookie($(this).parent().attr('id'), 'expanded', { path: '/', expires: null });
			$(this).parent().next('.toggleMainCategory').slideToggle(500, function () {
				$.scrollTo(whereTo, 500, {offset: {top:-20} });
			});
		}
		return false;
	});
	$('.menu a').click(function(){
		if ($(this.hash).children('.maxMin').hasClass('active') ) {
			$.scrollTo( this.hash, 500, {offset: {top:-20} });
		} else  {
			var whereTo = this.hash;
			$(this.hash).children('.maxMin').addClass('active');
			$.cookie($(this.hash).attr('id'), 'expanded', { path: '/', expires: null });
			$(this.hash).next('.toggleMainCategory').slideToggle(500, function () {
				$.scrollTo(whereTo, 500, {offset: {top:-20} });
			});
		}
		return false;
	});
});
