/*
$Id: dropout_buttons_init.js,v 1.8.2.1 2009/11/10 07:51:03 max Exp $ 
vim: set ts=2 sw=2 sts=2 et:
*/

function initDropOutButton() {
  if (jQuery(this).hasClass('activated-widget'))
    return;

  jQuery(this).addClass('activated-widget');

  var dropOutBoxObj = jQuery(this).parent().find('.dropout-box');

  // Process the onclick event on a dropout button 
  jQuery(this).click(
    function(e) {
      e.stopPropagation();
      jQuery('.dropout-box').removeClass('current');
      dropOutBoxObj
        .toggle()
        .addClass('current');
      jQuery('.dropout-box').not('.current').hide();
    }
  );
   
  // Click on a dropout layer keeps the dropout content opened
  jQuery(this).parent().click(
    function(e) { 
      e.stopPropagation(); 
    }
  );
  
  // shift the dropout layer from the right hand side 
  // if it's out of the main area
  var borderDistance = (jQuery("#center-main").offset().left + jQuery("#center-main").outerWidth()) - (jQuery(this).offset().left + dropOutBoxObj.outerWidth());
  if (!isNaN(borderDistance) && borderDistance < 0) {
    dropOutBoxObj.css('left', borderDistance+'px');
  }
}

jQuery(document).ready(
  function() {

    jQuery('body').click(
      function() {
        jQuery('.dropout-box')
          .filter(function() { return jQuery(this).css('display') != 'none'; } )
          .hide();
      }
    );

    jQuery('div.dropout-container div.drop-out-button').each(initDropOutButton);
  }
);

