var open_cats = [];
var cat_of_selected_subcat = null;
var prev_selected_cat = null;
var showed_cats = {};

function category_up(id) {
  if(Prototype.Browser.IE) {
    $('subcats_of_' + id).fade({ duration: 0.1 });
  } else {
    $('subcats_of_' + id).blindUp();
  }
}

function category_down(id) {
  if(Prototype.Browser.IE) {
    $('subcats_of_' + id).appear({ duration: 0.1 });
  } else {
    $('subcats_of_' + id).blindDown();
  }
}

function close_open_cats() {
  $(open_cats).each(function(i){ 
    //$('subcats_of_' + i).blindUp();
    category_up(i);
    $('category_state_selector_' + i).src = '/stylesheets/tree/closed.gif';
  });
}

function click_on_cat(el, id, cat_of_the_subcat){
  var ul = $('subcats_of_' + id);
  if(!ul) return;

  if((cat_of_selected_subcat != undefined) && (cat_of_selected_subcat == id)) {
    // close this category          
    el.src='/stylesheets/tree/closed.gif';
    //$('subcats_of_' + cat_of_selected_subcat).blindUp();
    category_up(cat_of_selected_subcat);
    cat_of_selected_subcat = null;
    close_open_cats();
    open_cats = [];
  } else if(open_cats.indexOf(id) == -1) {
    el.src='/stylesheets/tree/open.gif';

    // if this is not subcategory then close all open categories
    if(cat_of_the_subcat == undefined) { 
      close_open_cats();
      // if we close the other cat then cat of the opened subcat
      if((cat_of_selected_subcat != undefined) && (cat_of_selected_subcat != id)) {
        //$('subcats_of_' + cat_of_selected_subcat).blindUp();
        category_up(cat_of_selected_subcat);
        $('category_state_selector_' + cat_of_selected_subcat).src = '/stylesheets/tree/closed.gif';
      }
      cat_of_selected_subcat = null; // and asure that subcat selector is null
    } else {
      cat_of_selected_subcat = cat_of_the_subcat;
    }

    // open subcats of the selected category
    //$('subcats_of_' + id).blindDown();
    category_down(id);

    // store only its id
    open_cats = [id];
  } else {
    // close this category          
    el.src='/stylesheets/tree/closed.gif';
    //$('subcats_of_' + id).blindUp();
    category_up(id);
    open_cats = [];
  }
}

// without closing open categories
function click_on_cat_simple(el, id, cat_of_the_subcat){
  var ul = $('subcats_of_' + id);
  if(!ul) return;

  if((cat_of_selected_subcat != undefined) && (cat_of_selected_subcat == id)) {
    // close this category          
    el.src='/stylesheets/tree/closed.gif';
    category_up(cat_of_selected_subcat);
    cat_of_selected_subcat = null;
    open_cats = [];
  } else if(open_cats.indexOf(id) == -1) {
    el.src='/stylesheets/tree/open.gif';
    // open subcats of the selected category
    category_down(id);

    // store only its id
    open_cats = [id];
  } else {
    // close this category          
    el.src='/stylesheets/tree/closed.gif';
    //$('subcats_of_' + id).blindUp();
    category_up(id);
    open_cats = [];
  }
}

function request_new_category(el, url) {
  new Ajax.Request(url, {method: 'get', onSuccess: function(transport){$('content_column').innerHTML = transport.responseText} });
  if(prev_selected_cat != null) {
    prev_selected_cat.parentNode.style.backgroundColor = '#fff';
    prev_selected_cat.style.color = '#000';
  }

  el.parentNode.style.backgroundColor = '#777';    
  el.style.color = '#fff';    

  prev_selected_cat = el;
}

function show_cis_from_category(el, id) {
  if(showed_cats[id]) {
    $('place_for_cis_'+id).hide();
    showed_cats[id] = false;
  } else {
    new Ajax.Request('/admin/categories/show_cis_from_category/'+id, {
      method: 'get', 
      onSuccess: function(transport){
        $('place_for_cis_'+id).innerHTML = transport.responseText; 
        $('place_for_cis_'+id).show(); 
        sortable_lists.push('category_'+id); 
        create_sortable_lists(); 
        category_page['category_'+id] = 1; 
        showed_cats[id] = true;
      } 
    });
  }
}

function create_sortable_lists() {
  sortable_lists.each(function(id) {
    Sortable.create(id, {
      dropOnEmpty: true,
      containment: sortable_lists,
      constraint: false,
      onUpdate: function(list) {
        new Ajax.Request('/admin/categories/set_category_items_order/' + window.category_page[id], {method: 'get', parameters: Sortable.serialize(list)});
      }
    });
  });
}
