$(function() {
	
	var can_animate = true;
	var menu_cookie = $.cookie('_menustate') || '';
	var menu_states = menu_cookie.split(',');

	var save_state = function(category_key, state) {
    
	    // Obtain the attribute number.
	    $attribute_id = $container.attr('class').match(/a([0-9]+)/i, '')[1];
	    menu_states[$attribute_id] = state;
	    console.log('setting attribute menu #%d to %s', $attribute_id, (state == '1' ? 'Closed' : 'Open'));
    
	    // Save State.
	    $.cookie('_menustate', menu_states.join(','), { expires: 28, path: '/' });
	}
            
	var load_state = function() {
    
	    $('ul.categories').each(function() {
        
	        $class_key = $(this).attr('class').replace(/.*?\sa/ig, '');
        
	        // Only deal with numeric keys.
	        if(!!isNaN($class_key))
	            return;
        
	        //console.log('loading attribute menu #%d as %s', $class_key, (menu_states[$class_key] == '1' ? 'Closed' : 'Open'));
        
	        // Obtain parent and container classes.
	        state = (menu_states[$class_key] == null || menu_states[$class_key] == '0') ? 'up' : 'down';
	        pstate = (menu_states[$class_key] == null || menu_states[$class_key] == '0') ? 'shown' : 'hidden';
        
	        // Set the applicable classes.
	        $('ul.a' + $class_key).addClass(pstate);
	        $('ul.a' + $class_key).prev('a.cat-title').attr('class', 'cat-title ' + state);
	    });

	}

	$('#left-block-child a.close').live('click', function() {
	    return false;
	});

	$('#left-block-child a.down').live('click', function() {
    
	    if(!can_animate)
	        return false;
    
	    can_animate = false;
            
	    $operator = $(this);
	    $operator.removeClass('down').addClass('up');
	    $container = $(this).next('ul.categories:hidden');

	    save_state($container.attr('class').replace(/categories\s/ig, ''), 0);
    
	    $container.show('slide', { easing: 'easeInQuint', direction: 'up' }, 500, function() {
	        $(this).removeClass('hidden').addClass('shown');
	        can_animate = true;
	    });
    
	    return false;
	});

	$('#left-block-child a.up').live('click', function() {
    
	    if(!can_animate)
	        return false;

	    can_animate = false;
    
	    $operator = $(this);
	    $operator.removeClass('up').addClass('down');
	    $container = $(this).next('ul.categories:visible');
                    
	    save_state($container.attr('class').replace(/categories\s/ig, ''), 1);
    
	    $container.hide('slide', { easing: 'easeOutQuint', direction: 'up' }, 500, function() {
	        $(this).removeClass('shown').addClass('hidden');
	        can_animate = true;
	    });
    
	    return false;
	});

	$('select.filter').change(function() {
    
	    segment_array = window.location.href.toString().split('/');

	    if(position = $.inArray($(this).attr('id'), segment_array) > 0) {

	        // replace based on filter key.
	        var filter_regex = new RegExp('\\/(' + $(this).attr('id') + ')\\/([^\\/]*)', 'i');
	        new_location_uri = window.location.href.replace(filter_regex, '/$1/' + $(this).val());
	    }
	    else {
        
	        // glue together a new uri with new filter key.
	        new_location_uri = segment_array.join('/') + '/' + $(this).attr('id') + '/' + $(this).val();
	    }
    
	    // direct to new location.
	    window.location.href = new_location_uri;
	    return;
	});

	load_state();
});