/** @package eFocus js lib */

/**
 * notification Class - creates and shows notification, hides automatically
 *
 * @author Mirjam <mirjam[AT]efocus.nl>
 * @since 1.0, 11 jan, 2010
 * @copyright eFocus
 * @uses MooTools 1.2.4 Core <http://www.mootools.net>
 */

var notification = new Class({

    Implements: Options,

    options: {
        text: 'Het product is toegevoegd aan de winkelwagen.',
        cssClass: 'shop_notification',
        duration: 3,
        fadetime: 0.5,
        position: 'right',
        offsetX: 10,
        offsetY: 0
    },

    initialize: function(options) {

        this.setOptions(options);
        this.create();

    },

    /**
    * creates notification, default opacity 0
    *
    * @author Mirjam <mirjam[AT]efocus.nl>
    * @since 1.0, 11 jan 2010
    *
    * @return void
    */
    
    create: function() {

        this.note = new Element('div', {
            'html': this.options.text,
            'class': this.options.cssClass,
            'opacity': '0'
        });

        this.note.inject(document.body);

    },

    /**
    * shows notification
    *
    * @author Mirjam <mirjam[AT]efocus.nl>
    * @since 1.0, 11 jan 2010
    *
    * @param sender, button or link that calls this function
    * @param options, specific options for this notification
    * @return void
    */
    
    show: function(sender, options) {

        this.sender = sender;
        this.setOptions(options);

        this.note.set('html', this.options.text);
        this.note.set('class', this.options.cssClass);
        
        if (this.myFx) {
            this.myFx.cancel();
            $clear(this.timer);
        }

        this.myFx = new Fx.Tween(this.note, { property: 'opacity', duration: 500 }).start(0).chain(function() {

            var newX = this.sender.getPosition().x;
            var newY = this.sender.getPosition().y;

            switch (this.options.position) {
                case 'center':
					
					newX = (window.getSize().x) / 2 - (this.note.getSize().x / 2);
                    newY = (window.getSize().y) / 2 - (this.note.getSize().y / 2);

                    break;
                case 'top':

                    newX += this.options.offsetX;
                    newY -= this.note.getSize().y + this.options.offsetY;

                    break;
                case 'bottom':

                    newX += this.options.offsetX;
                    newY += this.sender.getSize().y + this.options.offsetY;
                    
                    break;
                case 'left':

                    newX -= this.note.getSize().x + this.options.offsetX;
                    newY += this.options.offsetY;

                    break;
                case 'right':
                default:

                    newX += this.sender.getSize().x + this.options.offsetX;
                    newY += this.options.offsetY;

                    break;
            }

            this.note.setStyle('left', newX);
            this.note.setStyle('top', newY);

            this.myFx = new Fx.Tween(this.note, { property: 'opacity', duration: this.options.fadetime * 1000 }).start(1);
            this.myFx.addEvents({
                'complete': function() {
                    this.timer = this.hide.delay(this.options.duration * 1000, this);
                } .bind(this)
            });

        } .bind(this));

    },

    /**
    * hides notification
    *
    * @author Mirjam <mirjam[AT]efocus.nl>
    * @since 1.0, 11 jan 2010
    *
    * @return void
    */
    
    hide: function() {

        this.myFx = new Fx.Tween(this.note, { property: 'opacity', duration: this.options.fadetime * 1000 }).start(0);

    }

});