﻿var MooOverlay = new Class({
    Implements: [Options, Events],
    options:
    {
        container: '',
        overlayColor: '#fff'
    },
    initialize: function(options)
    {
        this.setOptions(options);
        this.container = this.options.container != "" ? document.id(this.options.container) : document.id(document.body);
        if(!this.container)
            return;
            
        this.overlay = new Element('div', {
            styles:
            {
                left: 0,
                top: 0,
                width: 0,
                height: 0,
                position: 'absolute',
                zIndex: 11002,
                opacity: 0,
                background: this.options.overlayColor
            }
        }).inject(this.container);   
    },
    show: function()
    {
        this.overlay.setStyles({
            width: this.container.getWidth(),
            height: this.container.getScrollSize().y
        });
        new Fx.Morph(this.overlay, {
            onComplete: function(){
                this.fireEvent('onShow', [this.overlay]);
            }.bind(this)
        }).start({opacity: .9});
        
    },
    hide: function()
    {
        new Fx.Morph(this.overlay, {
            onComplete: function(){
                this.fireEvent('onHide', [this.overlay]);
            }.bind(this)
        }).start({opacity: 0});
    },
    getOverlay: function()
    {
        return this.overlay;
    }
    
});    

var PulseFade = new Class({
      
  //implements
  Implements: [Options,Events],

  //options
  options: {
    min: 0,
    max: 1,
    duration: 200,
    times: 5
  },
  
  //initialization
  initialize: function(el,options) {
    //set options
    this.setOptions(options);
    this.element = $(el);
    this.times = 0;
  },
  
  //starts the pulse fade
  start: function(times) {
    if(!times) times = this.options.times * 2;
    this.running = 1;
    this.fireEvent('start').run(times -1);
  },
  
  //stops the pulse fade
  stop: function() {
    this.running = 0;
    this.fireEvent('stop');
  },
  
  //runs the shizzle
  run: function(times) {
    //make it happen
    var self = this;
    var to = self.element.get('opacity') == self.options.min ? self.options.max : self.options.min;
    self.fx = new Fx.Tween(self.element,{
      duration: self.options.duration / 2,
      onComplete: function() {
        self.fireEvent('tick');
        if(self.running && times)
        {
          self.run(times-1);
        }
        else
        {
          self.fireEvent('complete');
        }
      }
    }).start('opacity',to);
  }
});
