var sb_lock = new Hash();

// {{{ fixup_sidebars()
function fixup_sidebars(col_id){
  if(!col_id) return;

  var asortable = $('frontpage_col' + col_id);
  if(!asortable) return;

  Sortable.create(asortable, {
    tag:'div',
    only:'sidebaritem',
    constraint:'vertical',
    onChange: function(){ SBPos.save(); }
  });

  asortable.select('.sidebaritem .boxbutton').each(function(minbutton) {
    minbutton.observe('click', opencloseSBW);
  });
} // }}}

// {{{ opencloseSBW(ev)
function opencloseSBW(ev) {
  ev.stop();
  var el = Event.element(ev);

  // Although the event is hooked on the A we the IMG overlays is so we
  // get the IMG instead, so pass up tree to parent (A element)
  if(el.tagName == 'IMG' && el.parentNode.tagName == 'A')
    el = el.parentNode;

  var sb_box = el.up('.sidebaritem');
  var div_content = sb_box.select('.sidebarcontent')[0];
  var div_bottom  = sb_box.select('.sidebarbottom')[0];

  // Check if we're busy:
  if(sb_lock.get(el.getAttribute('id'))) return;

  // {{{ If hidden, blinddown to display:
  if(div_content.getStyle('display') == 'none'){
    Effect.BlindDown(div_content, {
     afterUpdate: function() {
       if(balanceSidebars)
         balanceSidebars();
     },

     beforeStart: function() {
       sb_lock.set(el.getAttribute('id'), 1);
     },

     afterFinish: function() {
       sb_lock.unset(el.getAttribute('id'));
       el.select('img')[0].setAttribute('src', '/graphics/minimize.gif');
       SBPos.save();
     }
    });

    Effect.BlindDown(div_bottom);
  } // }}}

  // {{{ Otherwise blindup to hide:
  else {
    Effect.BlindUp(div_bottom, {
     afterUpdate: function() {
       if(balanceSidebars)
         balanceSidebars();
     },

     beforeStart: function() {
       sb_lock.set(el.getAttribute('id'), 1);
     }
    });

    Effect.BlindUp(div_content, {
     afterFinish: function() {
       sb_lock.set(el.getAttribute('id'), 0);
       el.select('img')[0].setAttribute('src', '/graphics/maximize.gif');
       SBPos.save();
     }
    });
  } // }}}

  return true;
} // }}}

// {{{ SBPos
var SBPos = {

  // {{{ save()
  save: function(){
    var pos= new Hash();
    var hidden= new Hash();

    [1, 3].each(function(col) {
      var i=1;
      $$('#frontpage_col' + col +' .sidebaritem').each(function(el){
        pos.set(el.id, i++);
        var content = el.select('.sidebarcontent');
        if(content && content[0] && content[0].getStyle('display') == 'none')
          hidden.set(el.id, 1);
      });
    });

    // Only save if saving something :p
    if(pos.size()) {
      Cookie.set('position',  pos.toJSON());
    } else {
      Cookie.erase('position');
    }

    if(hidden.size()) {
      Cookie.set('hidden', hidden.toJSON());
    } else {
      Cookie.erase('hidden');
    }
  }, // }}}

  // {{{ load()
  load: function(){
    var hpos = Cookie.get('position');
    var hhid = Cookie.get('hidden');

    SBPos.hpos = new Hash(hpos ? hpos.evalJSON() : {});
    SBPos.hhid = new Hash(hhid ? hhid.evalJSON() : {});

  }, // }}}

  // {{{ process()
  process: function() {
    [1, 3].each(function(col_id) {

      var arr = new Array(10);
      var col = $('frontpage_col' + col_id);

      // Column may have been administratively hidden
      if(!col) return;

      col.select('.sidebaritem').each(function(el){
        var pos = SBPos.hpos.get(el.id);
        el = el.remove();
        if(pos)
          arr[pos] = el;
        else
          arr.push(el);
      });

      arr.each(function(el) {
        if(el){

          var ishidden = SBPos.hhid.get(el.id);
          var cont = el.select('.sidebarcontent')[0];
          var bot  = el.select('.sidebarbottom')[0];
          var boxbut = el.select('.boxbutton img');
          if(boxbut && boxbut.length) boxbut = boxbut[0];

          if(ishidden) {
            if(bot)   bot.hide();
            if(cont) cont.hide();
            if(boxbut) boxbut.setAttribute('src', '/graphics/maximize.gif');
          } else {
            if(bot)   bot.show();
            if(cont) cont.show();
            if(!boxbut) alert(el.id);
            if(boxbut) boxbut.setAttribute('src', '/graphics/minimize.gif');
          };

          col.appendChild(el);
        }
      });
    });

  } // }}}

}; // }}}

Event.observe(window, 'load', function(){
  if($('fpbody')){
    fixup_sidebars(1);
    fixup_sidebars(3);
    SBPos.load();
    SBPos.process();
    if(balanceSidebars)
      balanceSidebars();
  }
});
