$(function(){
    $('.cloudes-menu a').click(function(event){
        event.preventDefault();
        $this = $(this);
        $this.parents('.cloudes').find('.cloud').hide(400);
        
        var id = $this.attr('href').substring(1);
        
        $(this).parent('li').addClass('active');
        var treadth = this;
        $(this).parents('.cloudes').find('.cloudes-menu a').each(function(){
            if ($(this)[0] != treadth) {
                $(this).parent().removeClass("active");
            }
        });
        $container = $('#' + id);
        if(!$('ul', $container).length){
        	params = id.split('-');
        	if(params[0] != 'actor'){
        		$container.html('<img src="/img/ajax-loader-white.gif" />');
        		$container.load('/catalog/tags/?table=' + params[0] + '&type=' + params[1] + '&cloud=' + params[2]);
        	}
        }
        $container.show(400);
    });

    var current_choice_title = '';
    $('.search-choice-title').click(function(event){
		event.preventDefault();
		if(current_choice_title){
			$('.values').hide();
			$('.search-choice-title').removeClass('active');
		}
		
		$this = $(this);
		var id = $this.attr('href').substring(1);
		
		if(id != current_choice_title){
			$this.addClass('active');
			current_choice_title = id;
			$container = $('#' + id);
			if(!$('ul', $container).length){
				params = id.split('-');
				if(params[0] != 'actor'){
					$container.html('<img src="/img/ajax-loader.gif" />');
					$container.load('/catalog/tags/?table=' + params[0] + '&type=' + params[1]);
				}
			}
			$container.show();
		} else {
			current_choice_title = '';
		}
    });
    
    $('.actor-letter').click(function(event){
    	event.preventDefault();
    	$this = $(this);
    	var currentLetter = $this.text();
    	$('.actor-letter', $this.parents('div:first')).removeClass('current');
    	$this.addClass('current');
    	var letter = $this.attr('href').substring(1);
    	var cloud = $this.hasClass('cloud');
    	var $container = $('.list', $this.parents('div:first'));
    	$container.show();
    	$container.html('<img src="/img/ajax-loader-white.gif" />');
    	$container.load('/catalog/tags/?table=actor&type=1&letter=' + letter + '&cloud=' + (cloud ? "1" : "0"));
    });
});
