jQuery(function(){
  jQuery.setBlockTorrentsMode = function() {
	var n = 'block-torrents-downloadable', oldState = jQuery.cookie(n) || false;
	state = oldState ^ 1;
    jQuery.cookie(n, (state ? 1 : 0), { expires: 14, path: '/'});
  };
  jQuery('.change-block-torrents-mode').click(function( e ){
	  e.preventDefault();
	  jQuery.setBlockTorrentsMode();
	  window.location.href = '/';
	  return false;
  });
  
  jQuery.setBStateCookie = function(bid, state) {
	  var c = jQuery.cookie('blockstates');
	  if (typeof bid != 'undefined') {
		  var s = jQuery.evalJSON(c) || {};
		  s[bid] = state;
		  c = jQuery.toJSON(s);
	  }
	  jQuery.cookie('blockstates', c, { expires: 14, path: '/'});
  };
  jQuery.fn.setBStateTitle = function(init) {
    return this.each(function() {
      var o = jQuery(this), t = (o.hasClass('state_short-0') ? 'скрыть' : 'показать')+' содержимое';
      if (init) o.attr('title', t);
      else o.attr('tooltip', t);  
    });
  };
  jQuery('a.state-toggle').click(function(e) {
    $(this).parent().children('a.state_short-toggle').click();
    e.preventDefault();
  });
  jQuery('a.state_short-toggle').each(function(){
    var o = jQuery(this);
    o.setBStateTitle(true).click(function(e){
      var hide = o.hasClass('state_short-0');
      jQuery.setBStateCookie(o.attr('id'),hide ? 1 : 0)
      if (hide) {
        o.removeClass('state_short-0').addClass('state_short-1').setBStateTitle(false).parent().next().removeClass('state_short-0').addClass('state_short-1');
      } else {
        o.removeClass('state_short-1').addClass('state_short-0').setBStateTitle(false).parent().next().removeClass('state_short-1').addClass('state_short-0');
      }
      //alert('Функционал скрытия/отображения содержимого блоков - в разработке')
      e.preventDefault();
    });
  });
  jQuery.setBStateCookie();
  jQuery('a.state-toggle-next').click(function(e) {
    e.preventDefault();
    var a = $(this), href = a.attr('href'), ids = a.data('ids'), next = (href != '#' ? $( href ) : ( typeof ids !== 'undefined' ? $( ids ) : a.next() ));
    if (next.hasClass('state_short-0')) next.removeClass('state_short-0').addClass('state_short-1');
    else next.removeClass('state_short-1').addClass('state_short-0');
  });
});

