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').each(function(el){
    el.select('.boxbutton').each(function(el2) { el2.remove(); });

    var header = el.select('.leftnavheader')[0]
    if(!header) return;

    // {{{ Add minimize
    var i = new Element('img', {
      src:    '/graphics/minimize.gif',
      height: 11,
      width:  10
    });

    var a = new Element('a', {
      src:   '#',
      title: 'Close this Sidebar Item',
      id: 'close' + el.getAttribute('id')
    });
    a.addClassName('boxbutton');

    Event.observe(a, 'click', function(ev){
      Event.stop(ev);
      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.parentNode.parentNode;
      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, {
         beforeStart: function() {
           sb_lock.set(el.getAttribute('id'), 1);
         },

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

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

      // {{{ Otherwise blindup to hide:
      else {
        Effect.BlindUp(div_bottom, {
         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;
    }, true);

    header.insert({after:a});
    a.appendChild(i);
    // }}}

  });
} // }}}

// {{{ 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);
      });
    });

    Cookie.set('position',  pos.toJSON());
    Cookie.set('hidden', hidden.toJSON());
  }, // }}}

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

    if(hpos) hpos = new Hash(hpos.evalJSON());
    else     hpos = new Hash();

    if(hhid) hhid = new Hash(hhid.evalJSON());
    else     hhid = new Hash();


    [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 = hpos.get(el.id);
        el = el.remove();
        if(pos)
          arr[pos] = el;
        else
          arr.push(el);
      });

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

          var ishidden = 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();
  }
}, false);
