// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


/* ---------------------- Montrer/Cacher l'indicateur Ajax ------------ */

function ShowIndicator(opacity){
  CtrlIndicator = getBusyOverlay('viewport', {
    color: '#90b0de',
    opacity: typeof(opacity) == 'undefined' ? 0.2 : opacity,
    text: '',
    style: ''// : 'busy_indicator'
  }, {
    size: 70
  });
}


function HideIndicator(){
  if (typeof(CtrlIndicator) == 'object') {
    CtrlIndicator.remove();
    delete CtrlIndicator;
  }
  
}


/* ------------------------- Montrer/Cacher le panneau ------------------- */
var isExtended = 1;

function show_hide_left_panel(){
  if (isExtended == 0) {
    new Effect.Morph('contentcolumn', {
      style: 'margin-left:230px;',
      duration: 0.5,
      afterFinish: function(){
        $('leftcolumn').show();
        //$('link_left_panel').innerHTML = '<%= t(:'.hide_panel') %>';
        $('link_left_panel_hide').up().show();
        $('link_left_panel_show').up().hide();
        isExtended++;
      }
      
    });
  }
  else {
    $('leftcolumn').hide();
    new Effect.Morph('contentcolumn', {
      style: 'margin-left:10px;',
      duration: 0.5,
      afterFinish: function(){
        //$('link_left_panel').innerHTML = '<%= t(:'.show_panel') %>';
        $('link_left_panel_hide').up().hide();
        $('link_left_panel_show').up().show();
        isExtended = 0;
      }
      
    });
  }
}


function init(){
  if ($('link_left_panel_hide')) {
    Event.observe('link_left_panel_hide', 'click', show_hide_left_panel, true);
    Event.observe('link_left_panel_show', 'click', show_hide_left_panel, true);
  }
}


Event.observe(window, 'load', init, true);

/* ------------------------- Faire disparaitre les flashs ------------------- */
document.observe('dom:loaded', function(){
  setTimeout(hideFlashes, 25000);
  if ($('link_login')) {
    $('link_login').observe('click', function(event){
      event.stop();
      Modalbox.show(this.href, {
        title: (this.title == '' ? 'Login' : this.title),
        width: 500,
        afterLoad: function(){
          $('new_user_session').observe('submit', function(event){
            event.stop();
            this.request();
          })
        }
      });
    });
  }
  if ($('link_logout')) {
    $('link_logout').removeAttribute('onclick');
    $('link_logout').observe('click', function(event){
      event.stop();
      Modalbox.show('/user_sessions/confirm', {
        transitions: false,
        title: 'Confirmation',
        width: 300,
        afterLoad: function(){
          $('link_confirm_no').observe('click', function(event){
            event.stop();
            Modalbox.hide();
          })
        }
      });
    });
  }
});

var hideFlashes = function(){
  $$('p.notice', 'p.warning', 'p.error', 'div.notice', 'div.warning', 'div.error').each(function(e){
    if (e) 
      Effect.Fade(e, {
        duration: 1.5
      });
  })
}
