/* Class: vAlert
* 
* style alert to show a message
* 
* About:
* 
*     - Written by Victor de Carvalho Lima <victor@lojcomm.com.br> @ july 2011
*/
var vAlert = new Class({ 
    Implements: [Events, Options],
    options: {
        transition: 'Back:in:out',
        duration: 600
    },

    initialize: function(options) {
        this.setOptions(options);
        var that = this;

        // CREATE ELEMENTS INTO THE BODY
        that.wrapper = new Element('div', {'class':'vAlert-wrapper close' });
        that.wrapper.setStyles({ 
            'height': window.getScrollHeight(),
            'width' : window.getScrollWidth(),
            'opacity': 0.4
        });

        that.alert  = new Element('div', {'class': 'vAlert'});

        that.header = new Element('div', {'class': 'vAlert-header'});
        that.header.adopt(new Element('span', {'class':'btnClose close'}));
        that.main = new Element('div', {'class': 'vAlert-main'});
        that.msg = new Element('h4');
        that.main.adopt(that.msg);

        that.alert.adopt(that.header);
        that.alert.adopt(that.main);

        that.wrapper.inject(document.body);
        that.alert.inject(document.body);

        // Creating effects
        [that.wrapper, that.alert].each(function(item, key){
            item.set('morph', { duration: that.options.duration, transition: that.options.transition});
        });

        // Defines position origin top and left
        that.winWidth = window.getCoordinates().width;
        that.alert.setStyles({ 
            'left': (that.winWidth/2)-251,
            'top' : -600
        });

        // Events
        window.addEvents({
            'resize': function(){ 
                that.wrapper.setStyles({ 
                    'height': window.getScrollHeight(),
                    'width' : window.getScrollWidth()
                });
                that.fix();
            },
            'scroll': function(){ that.fix(); }
        });

        //alert close 
        $$('.close').each(function(item, key){
            item.addEvent('click', function(){
                that.close();
            });
        });

        that.active = false;
    },

    message: function(text){
        var that = this;
        that.active = true;
        that.msg.set('text', text)

        var newWinHeight = window.getCoordinates().height;
        that.wrapper.setStyles({'display':'block'});
        that.wrapper.morph({'opacity':0.7});
        that.alert.morph({ 'top': ((newWinHeight/2)-101)+window.getScrollTop(), 'opacity':1 });
    },

    close: function(){
        var that = this;
        that.active = false;

        that.wrapper.morph({'opacity':0.4});
        that.alert.morph({'top':-600, 'opacity':1});
        (function(){ that.wrapper.setStyle('display', 'none'); }).delay(that.options.duration);
    },

    fix: function(){
        var that = this;
        var newWinHeight = window.getCoordinates().height,
            newWinWidth  = window.getCoordinates().width;

        if (that.active == true){
            that.alert.morph({
                'top':((newWinHeight/2)-101)+window.getScrollTop(),
                'left':((newWinWidth/2)-251)
            });
        }else{
            that.alert.morph({
                'top': -600,
                'left':((newWinWidth/2)-251)
            });
        }
    }

});

