(function (exports) {
'use strict';

/**
 * Module : Sparrow touch event
 * Author : Kvkens(yueming@yonyou.com)
 * Date	  : 2016-07-28 14:41:17
 */

var on = function(element, eventName, child, listener) {
	if(!element)
		return;
	if(arguments.length < 4) {
		listener = child;
		child = undefined;
	} else {
		var childlistener = function(e) {
			if(!e) {
				return;
			}
			var tmpchildren = element.querySelectorAll(child);
			tmpchildren.forEach(function(node) {
				if(node == e.target) {
					listener.call(e.target, e);
				}
			});
		};
	}
	//capture = capture || false;

	if(!element["uEvent"]) {
		//在dom上添加记录区
		element["uEvent"] = {};
	}
	//判断是否元素上是否用通过on方法填加进去的事件
	if(!element["uEvent"][eventName]) {
		element["uEvent"][eventName] = [child ? childlistener : listener];
		if(u.event && u.event[eventName] && u.event[eventName].setup) {
			u.event[eventName].setup.call(element);
		}
		element["uEvent"][eventName + 'fn'] = function(e) {
			//火狐下有问题修改判断
			if(!e)
				e = typeof event != 'undefined' && event ? event : window.event;
			element["uEvent"][eventName].forEach(function(fn) {
				try {
					e.target = e.target || e.srcElement; //兼容IE8
				} catch(ee) {}
				if(fn)
					fn.call(element, e);
			});
		};
		if(element.addEventListener) { // 用于支持DOM的浏览器
			element.addEventListener(eventName, element["uEvent"][eventName + 'fn']);
		} else if(element.attachEvent) { // 用于IE浏览器
			element.attachEvent("on" + eventName, element["uEvent"][eventName + 'fn']);
		} else { // 用于其它浏览器
			element["on" + eventName] = element["uEvent"][eventName + 'fn'];
		}
	} else {
		//如果有就直接往元素的记录区添加事件
		var lis = child ? childlistener : listener;
		var hasLis = false;
		element["uEvent"][eventName].forEach(function(fn) {
			if(fn == lis) {
				hasLis = true;
			}
		});
		if(!hasLis) {
			element["uEvent"][eventName].push(child ? childlistener : listener);
		}
	}

};

/**
 * Module : Sparrow dom
 * Author : Kvkens(yueming@yonyou.com)
 * Date	  : 2016-08-16 13:59:17
 */
/**
 * 元素增加指定样式
 * @param value
 * @returns {*}
 */
var addClass = function(element, value) {
	if(element){
		if(typeof element.classList === 'undefined') {
			if(u._addClass){
				u._addClass(element, value);
			}else{
				$(element).addClass(value);
			}

		} else {
			element.classList.add(value);
		}
	}

	return this;
};
/**
 * 删除元素上指定样式
 * @param {Object} element
 * @param {Object} value
 */
var removeClass = function(element, value) {
	if(element){
		if(typeof element.classList === 'undefined') {
			if(u._removeClass){
				u._removeClass(element, value);
			}else{
				$(element).removeClass(value);
			}

		} else {
			element.classList.remove(value);
		}
	}
	return this;
};
var makeDOM = function(htmlString) {
	var tempDiv = document.createElement("div");
	tempDiv.innerHTML = htmlString;
	var _dom = tempDiv.children[0];
	return _dom;
};

/**
 * Module : neoui-message
 * Author : Kvkens(yueming@yonyou.com)
 * Date      : 2016-08-02 19:40:59
 */

var messageTemplate = '<div class="u-message"><span class="u-msg-close uf uf-close"></span>{msg}</div>';

var showMessage = function (options) {
    var msg, position, width, height, showSeconds, msgType, template;
    //新增深色
    var darkType;
    if (typeof options === 'string') {
        options = {
            msg: options
        };
    }
    msg = options['msg'] || "";
    position = options['position'] || "bottom"; //center. top-left, top-center, top-right, bottom-left, bottom-center, bottom-right,
    //TODO 后面改规则：没设宽高时，自适应
    width = options['width'] || "";
    // height = options['height'] || "100px";
    msgType = options['msgType'] || 'info';
    //默认为当用户输入的时间，当用户输入的时间为false并且msgType=='info'时，默认显示时间为2s
    showSeconds = parseInt(options['showSeconds']) || (msgType == 'info' ? 2 : 0);

    darkType = options['darkType'] || "";

    template = options['template'] || messageTemplate;

    template = template.replace('{msg}', msg);
    var msgDom = makeDOM(template);
    addClass(msgDom, 'u-mes' + msgType);

    if (!darkType == "") {
        addClass(msgDom, darkType);
    }

    msgDom.style.width = width;
    // msgDom.style.height = height;
    // msgDom.style.lineHeight = height;
    if (position == 'bottom' || position == 'top' || position == 'center') {
			//msgDom.style.bottom = '10px';
			addClass(msgDom, 'u-mes-' + position);
	}
	
	if (position == 'topleft' || position == 'bottomleft') {
		if(width == ""){
			    msgDom.style.right = '2.4rem';
			    addClass(msgDom, 'u-mes-' + position);
		}else{
			    addClass(msgDom, 'u-mes-' + position);
			}	
	}
	if (position == 'topright' || position == 'bottomright') {
		if(width == ""){
				msgDom.style.left = '2.4rem';
				addClass(msgDom, 'u-mes-' + position);
		}else{
				addClass(msgDom, 'u-mes-' + position);
			}
		
	}	
    var closeBtn = msgDom.querySelector('.u-msg-close');
    //new Button({el:closeBtn});
    var closeFun = function () {
        removeClass(msgDom, "active");
        setTimeout(function () {
            try {
                document.body.removeChild(msgDom);
            } catch (e) {

            }
        }, 500);
    };
    on(closeBtn, 'click', closeFun);
    document.body.appendChild(msgDom);

    if (showSeconds > 0) {
        setTimeout(function () {
            closeFun();
        }, showSeconds * 1000);
    }

    setTimeout(function () {
        addClass(msgDom, "active");
    }, showSeconds * 1);

};

var showMessageDialog = showMessage;

exports.showMessageDialog = showMessageDialog;
exports.showMessage = showMessage;

}((this.bar = this.bar || {})));
