define(function (require, exports, module) {
    'use strict';

    var isIE6 = false;;
    if ($.browser['msie'] && $.browser['version'] === '6.0') {
        isIE6 = true;
    }


    var tmpl = _.template(require('./index.html'));

    var defaultConf = {

        containerId : 'box',
        containerClass : '',

        posTop : 200,
        posLeft : 200,

        title : '...',
        contents : '<p>...</p>',

        minHeight : '300',

        hasCancel : false, // 是否有取消按钮

        callbackAfterPopOut : function () {
            // alter('被关掉了！_(￣0￣)_[哦~]');
        },

        cbAfterCancel : function () {
            // 关闭或者取消的回调
            // console.log('关闭弹窗');
        }
    };

    /**
     * 遮罩层渲染方法
     * @return {[type]} [description]
     */
    var mask = {

        $el : $('#mask'),

        show : function () {
            if (this.$el.length === 0) {
                this.$el = $('<div id="mask"></div>');

                this.$el.css({
                    position : isIE6 ? 'absolute' : 'fixed',
                    top : '0',
                    left: '0',
                    height : $(window).height(),
                    width : $(window).width(),
                    'z-index' : 10
                });
                this.$el.appendTo($('body'));
            }

            this.$el.show();
        },

        hide : function () {
            this.$el.hide();
        }
    };



    var Pop = function (conf, eventsConf) {
        var me = this;

        me.$box = $('<div></div>');
        me.conf = _.extend(defaultConf, conf);

        me.init(me.conf, eventsConf);

        return me;
    };

    var popProto = Pop.prototype;

    var _eventsBinder= function (eventsConf, el) {

        if (!_.isObject(eventsConf)) {
            throw new Error('事件绑定参数错误');
        }

        if (el === undefined) {
            // 默认绑定到document上
            el = document;
        }

        var eventType, target, callback;
        for (var evnt in eventsConf) {
            if (eventsConf.hasOwnProperty(evnt)) {
                callback = eventsConf[evnt];
                target = evnt.split(' ');
                eventType = target.shift();
                target = target.join(' ');

                $(el).on(eventType, target, callback);
            }
        }

    };

    popProto.init = function (conf, events) {

        var me = this;

        me.events = _.extend({
            'click .pop-out-close' : function () {
                me.remove();
                conf.cbAfterCancel('CLOSE');
            },
            'click .pop-out-btn-confirm' : function () {
                me.remove();
            },
            'click .pop-out-btn-cancel' : function () {
                me.remove();
                conf.cbAfterCancel('CANCEL');
            },
            'keydown input' : function (e) {
                if (e.keyCode !== 13) return;
                me.$box.find('.pop-out-btn-confirm').click();
            }
        }, events);

        me.$box.attr({
            'id' : conf.containerId,
            'class' : conf.containerClass
        }).css({
            'display' : 'none'
        });

        var pop = _.clone(defaultConf, conf);

        me.$box.html(tmpl({
            pop : pop
        }));

        // 计算位置
        me.$box.appendTo('body');
        _eventsBinder(me.events, me.$box);
        me.show();
    };


    popProto.show = function () {
        var me = this;
        // 重新计算位置；
        me.$box.css({
            'position' : isIE6 ? 'absolute' : 'fixed',
            'top' : ($(window).height() - me.$box.outerHeight()) / 2,
            'left' : ($(window).width() - me.$box.outerWidth()) / 2,
            'z-index' : 20
        });

        if (!isIE6) {
            mask.show();
        }

        this.$box.show();
    };
    popProto.hide = function () {
        mask.hide();
        this.$box.hide();
    };
    popProto.remove = function () {
        mask.hide();
        this.$box.remove();
    };

    popProto.updateContent = function (contentsHtmlString) {
        this.$box.find('.pop-out-contents').html(contentsHtmlString);
    };


    module.exports = Pop;

});
