define([], function() {
    function Notify() {

    }
    Notify.prototype = {
        init: function(message) {
            con = document.createElement('section');
            p = document.createElement('p');
            i = document.createElement('i');
            confirm = document.createElement('a');
            refuse = document.createElement('a');
            con.style.cssText = "opacity:0;position:fixed;top:0px;height:100px;width:100%;text-align:center;background:#2d9cee;z-index:2;";
            p.style.cssText = "padding-left:20px;color:white;font-size:16px;text-align:left;height:50%;;line-height:50px;";
            confirm.style.cssText = "display:none;cursor:pointer;margin-right:5px;border-radius:3px;padding:5px 10px;font-size:14px;color:white;background:#7FDBFF;line-height:normal;";
            refuse.style.cssText = "display:none;cursor:pointer;margin-right:5px;border-radius:3px;padding:5px 10px;font-size:14px;color:white;background:#FF4136;line-height:normal;";
            i.style.cssText = "cursor:pointer;font-size:30px;position:absolute;right:0px;top:50%;color:white;width:30px;height:30px;";
            p.textContent = message;
            confirm.textContent = "yes";
            refuse.textContent = 'cancle';
            i.className = "fa fa-times";
            con.appendChild(i);
            con.appendChild(p);
            con.appendChild(confirm);
            con.appendChild(refuse);
            document.body.appendChild(con);
            return {section:con,confirm:confirm,refuse:refuse,close:i};
        },
        fadeIn: function(dom, time, func) {
            var handler, opacity,
                offset = 50 / (3 * time),
                g = 0.005;
            func = func ? func : function() {};

            function fadein() {
                opacity = Number(dom.style.opacity);
                offset += g;
                if (opacity >= 1) {
                    window.cancelAnimationFrame(handler);
                    func();
                } else {
                    dom.style.opacity = opacity + offset;
                    handler = window.requestAnimationFrame(fadein);
                }
            }
            fadein();
        },
        fadeOut: function(dom, time, func) {
            var handler, opacity,
                offset = 50 / (3 * time),
                func = func ? func : function() {},
                g = 0.005;

            function fadeout() {
                opacity = Number(dom.style.opacity);
                offset += g;
                if (opacity <= 0) {
                    window.cancelAnimationFrame(handler);
                    document.body.removeChild(dom);
                    func();
                } else {
                    dom.style.opacity = opacity - offset;
                    handler = window.requestAnimationFrame(fadeout);
                }
            }
            fadeout();
        },
        type: function(o) {
            var s = Object.prototype.toString.call(o);
            return s.match(/\[object (.*?)\]/)[1].toLowerCase();
        },
        notify: function(message, color, afterAnimation) {
            var that = this;
            if(this.type(arguments[1])=="function") {
                afterAnimation = arguments[1];
            } 
            if(this.type(arguments[1])=="string") {
                afterAnimation = afterAnimation?afterAnimation:function(){};
            }
            var color = color ? color : "#2d9cee",
            con = this.init(message);
            con.section.style.background = color;
            this.fadeIn(con.section, 500, function() {
                setTimeout(function() {
                    that.fadeOut(con.section, 500, afterAnimation);
                }, 2000)
            });
        },
        notifyError: function(message, afterAnimation) {
            this.notify(message, "#FF4136", afterAnimation);
        },
        notifySuccess: function(message, afterAnimation) {
            this.notify(message, "#2ECC40", afterAnimation);
        },
        notifyConfirm: function(message, confirm, refuse) {
            var that = this,
                confirm = confirm ? confirm : function() {},
                refuse = refuse ? refuse : function() {},
                con = this.init(message);
            con.confirm.style.display = 'inline-block';
            con.refuse.style.display = 'inline-block';
            this.fadeIn(con.section, 500);
            con.confirm.addEventListener('click', function(event) {
                event.preventDefault();
                confirm();
                that.fadeOut(con.section, 500);
            })
            con.refuse.addEventListener('click', function(event) {
                event.preventDefault();
                that.fadeOut(con.section, 500);
                refuse();
            })
            con.close.addEventListener('click', function(event) {
                event.preventDefault();
                that.fadeOut(con.section, 500);
            })
        }
    }
    return new Notify();
})