var   CategoriesTree  =  new(function(){

    /**
     * Текущая категория 1-го уровня
     */
    this.currentCategory;

    this.init  =  (function(currentCategory){
        this.currentCategory  =  currentCategory;

        $('#categories').ready(function(){
            $('span.switch').click(function(){
                CategoriesTree.switcher(this);
                return  false;
            });

            CategoriesTree.openCurrent(CategoriesTree.currentCategory);
        });
    });


    /**
     * Включаем/Выключаем отображение категории
     */
    this.switcher  =  (function(switchSpan){

        var  categoryCid  =  $($($(switchSpan).parent()).parent()).attr('id');

        if($('#'+categoryCid).attr('class') == 'current'){
            this.close(categoryCid);
        }
        else{
            if(!$('#'+this.currentCategory+' #'+categoryCid).get(0)){
                this.close(this.currentCategory);
                this.currentCategory  =  categoryCid;
            }
            else{
                $('#'+this.currentCategory+' li').removeClass('current');
                $('#'+this.currentCategory+' li ul').hide('fast');
                $('#'+this.currentCategory+' li span.switch').text('+');
            }

            this.open(categoryCid);
        }

        return  false;
    });


    /**
     * Открываем ветку категорий
     */
    this.open  =  (function(cid){
        $('#'+cid).addClass('current');
        $('#'+cid+' > ul').show('fast');
        $('#'+cid+' > a > span').text('-');
    });


    /**
     * Закрываем ветку категорий
     */
    this.close  =  (function(cid){
        $('#'+cid).removeClass('current');
        $('#'+cid+' ul').hide('fast');
        $('#'+cid+' a span').text('+');
    });


    /**
     * Разворачиваем дерево до "текущей" категории(рекурсивно)
     */
    this.openCurrent  =  (function(cid){

        var  parentUL  =  $($('#'+cid).parent());
        this.open(cid)

        if(parentUL.attr('id') == 'categories'){
            this.currentCategory  =  cid;
        }
        else{
            this.openCurrent($(parentUL.parent()).attr('id'));
        }
    });


    /**
     * Получаем Id категории, к которой привязан "переключатель"
     */
    this.getCategoryId  =  (function(switcherJ){

    });
});
