var state = new Array();
var prev=0;
var current_id;
var auto_open=false;
function close_children(){
  $('.ycat1_').each(function(){
    var test = $('[id="cat_'+$(this).attr('id')+'"] [id='+current_id+']').length;
    if(!test&&current_id!=$(this).attr('id')){
      $(this).toggleClass('ycat1').toggleClass('ycat1_');
      $('#cat_'+$(this).attr('id')).slideUp();
      //state[$(this).attr('id')]=2;
    }
  });
  $('.ycat_').each(function(){
    var test = $('[id="cat_'+$(this).attr('id')+'"] [id="'+current_id+'"]').length;
    if(!test&&current_id!=$(this).attr('id')){
      $(this).toggleClass('ycat').toggleClass('ycat_');
      $('#cat_'+$(this).attr('id')).slideUp();
      //state[$(this).attr('id')]=2;
    }
  });
}

function init_second_level(){
  $('.ycat1 a').unbind('click');
    $('.ycat1 a').click(function(){
      var id=$(this).parent().attr('id');
      current_id = id;
      if(!auto_open){
        close_children();
        }
      if(!state[id]){
        $('<div class="ycato" id="cat_'+id+'"></div>').insertAfter($(this).parent());
        $('#cat_'+id).load('/catalog/get_children',{id:id},function(){
          init_second_level();
        });
        state[id]=1;
      }else{
        if(state[id]!=2){
          $('#cat_'+id).slideToggle('fast');
        }
        state[id]=1;
/*        if(state[id]==1){
          $('#cat_'+id).slideUp('fast');
          state[id]=2;
        }else{
          $('#cat_'+id).slideDown('fast');
          state[id]=1;
        }*/
      }
    prev=id;
    $(this).parent().toggleClass('ycat1_');
    $(this).parent().toggleClass('ycat1');
    return false;
    });
    $('.b-catalog-footnote-link').unbind('click');
    $('.b-catalog-order-add').unbind('click');
    $('.b-catalog-order-add_count').unbind('keyup');
    $('.b-catalog-footnote-link').click(function(/* Event */ event){
      event.preventDefault();
      var position = $(this).position();
      $('#catalog_footnote').css({left: (position.left + 10) + 'px', top: (position.top + 10) + 'px'}).toggle();
    });
      // Проверяем данные о количестве заказываемых товаров
  $('.b-catalog-order-add_count').keyup(function() {
    var count = parseInt( $(this).val() );
    if ( count > 1000 ) {
      $(this).val(1000);
    }
  });
  
  // Добавлем заказ
  $('.b-catalog-order-add').click(function(event) {
    event.preventDefault();
    var button = $(this);
    var posId  = parseInt( $(this).attr('pos_id') );
    var count  = $(this).parent().prev().children('.b-catalog-order-add_count').val();
    
    $.ajax({
      type: "GET",
      url: "/order/add/",
      data: { pos_id: posId, count: count },
      dataType: "text",
      success: function(data) {
        if ( !data ) return false;
        button.parent().prev().text('');
        button.parent().html('<span><a href="/order/">перейти в заказ</a></span>');
        $('.b-order-count-info').text(data);
      }
    });
  });
}
var timer;
function open_rec(){
  var count_ycat = $('.ycat').length;
  if(count_ycat){
    $('.ycat a').click();
    clearTimeout(timer);
    timer = window.setTimeout(function(){open_rec();},500);
    return;
  }
  var count_ycat_ = $('.ycat1').length;
  if(count_ycat_){
    $('.ycat1 a').click();
    clearTimeout(timer);
    timer = window.setTimeout(function(){open_rec();},500);
    return;
  }
  clearTimeout(timer);
  auto_open=false;
}
$(document).ready(function(){
  $('.ycat a').click(function(){
    var id=$(this).parent().attr('id');
    current_id = id;
    if(!auto_open){
      close_children();
    }
    if(!state[id]){
      $('<div class="ycato" id="cat_'+id+'"></div>').insertAfter($(this).parent());
      $('#cat_'+id).load('/catalog/get_children',{id:id},function(){
        init_second_level();
      });
      state[id]=1;
      }else{
        if(state[id]!=2){
          $('#cat_'+id).slideToggle('fast');
        }
        state[id]=1;
      }
      prev = id;
      $(this).parent().toggleClass('ycat_');
      $(this).parent().toggleClass('ycat');
      return false;
  });
  $('.ymenu2 a').click(function(){
    clearTimeout(timer);
    auto_open=false;
    $('.ycat1_').toggleClass('ycat1').toggleClass('ycat1_');
    $('.ycat_').toggleClass('ycat').toggleClass('ycat_');
    $('.ycato').slideUp('fast');
    return false;
  });
  $('.ymenu1 a').click(function(){
    auto_open=true;
    open_rec();
    return false;
  });
  init_second_level();
});
