/**
 * Klasa wyświetlania wiadomości w małym wysuwanym u góry ekranu okienku.
 * Wiadomość może sama znikać, po zakończeniu odliczania lub ręcznym zamknięciu może 
 * dojść do przeładowania obecnej strony, lub przeniesienia pod inny adres
 */
var Api_message2 = Class.create({
  div: null,
  
  default_options: {
    timeout: 0,
    duration: 0.5,
    reload_after_close: false
  },

  initialize: function(options)
  {
    this.options = Object.extend(Object.extend({ }, this.default_options), options || { });
    this.create_html();
    this.install_actions();
    this.positione();
  },
  
  set_message: function(message)
  {
    this.div.down('.content').update(message);
  },
  
  show_hide: function(show)
  {
    if(show) {
      this.div.down('.action_close').show();
    }
    else {
      this.div.down('.action_close').hide();
    }
  },

  /**
   * Metoda tworzy dynamicznie kod html
   */
  create_html: function()
  {
	msg_window = Builder.node('div', { className: 'api_msg_wraper', style: 'display: none' }, [
      Builder.node('div', { className: 'api_msg' }, [
        Builder.node('div', { className: 'content' }),
        Builder.node('div', { className: 'status' }, [
          Builder.node('div', { className: 'counter' }),
          Builder.node('div', { className: 'closer' }, [
            Builder.node('span'),
            Builder.node('a', { className: 'action_close', href: '' }, [ 'zamknij /close' ])
          ])
        ]),
        Builder.node('div', { className: 'footer' }, [
          Builder.node('div', { className: 'cl' }),
          Builder.node('div', { className: 'bb' }),
          Builder.node('div', { className: 'cr' })
        ])
      ])
    ]);
    
    $$('body')[0].insert(msg_window);
    this.div = $(msg_window);
    this.div.down('.closer').down('span').update("&nbsp;");
    this.div.down('.action_close').hide();
  },  

  /**
   * Instaluje akcje na okienku, w tej chwili tylko zamykanie
   */
  install_actions: function()
  {
    t = this;
    t.div.select('.action_close').each(function(action) {
      action.observe('click', t.action_close.bindAsEventListener(t));
    });
  },
  
  /**
   * Ustawiamy na środku ekranu, na samej górze
   */
  positione: function()
  {
    desktop = $$('body')[0];
    desktopWidth = desktop.getWidth();
    desktopOffset = desktop.cumulativeOffset();
    elementWidth = this.div.getWidth();
    //scrollOffset = document.viewport.getScrollOffsets();
    
    this.div.setStyle({
      left: ((desktopWidth - elementWidth) / 2 + desktopOffset.left) + "px"
    });
  },
  
  /**
   * Jeśli jest ustawiony timeout to uruchamia odliczanie
   */
  auto_hide: function(timeout)
  {
    if(timeout > 0) {
      this.counter = this.div.down('.counter');
      this.counter.update(timeout);
      this.remaind = timeout;
      this.interval = setInterval(this.counting.bind(this) ,1000);     
    }
  },
  
  show_cancel: function(show)
  {
	  if(show == true || show == undefined) {
		  this.div.down('.action_close').show();		  
	  }
	  else {
		  this.div.down('.action_close').hide();
	  }
  },

  /**
   * Metoda wywoływana przez timer w celu aktualizacji licznika
   */
  counting: function()
  {
    this.remaind = this.remaind - 1;
    this.counter.update(this.remaind);
    if(this.remaind <= 0) { 
      clearInterval(this.interval);
      this.hide();
    }
  },  

  /**
   * Efektowanie pokazuje wiadomość 
   */
  show: function()
  {
    t = this;
    t.isToggling = true;
    Effect.SlideDown(t.div, { duration: t.options.duration, afterFinish: function() {
      t.isToggling = false;
    }});
  },

  /**
   * Efektowanie chowa wiadomość
   */
  hide: function()
  {
    if(t.options.reload_after_close) {
      window.location.reload();
    }
    else {
      t = this;
      t.isToggling = true;
      Effect.SlideUp(t.div, { duration: t.options.duration, afterFinish: function() {
        t.isToggling = false;
        t.div.remove();
      }});
    }
  },

  /**
   * Obsługa akcji zamykającej okienko
   */
  action_close: function(event)
  {
    event.stop();
    if(!this.isToggling) this.hide();
  }
  
});

