(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);
		}
	}

};

var off = function(element, eventName, listener) {
	//删除事件数组
	if(listener) {
		if(element && element["uEvent"] && element["uEvent"][eventName]) {
			element["uEvent"][eventName].forEach(function(fn, i) {
				if(fn == listener) {
					element["uEvent"][eventName].splice(i, 1);
				}
			});
		}
		return;
	}
	var eventfn;
	if(element && element["uEvent"] && element["uEvent"][eventName + 'fn'])
		eventfn = element["uEvent"][eventName + 'fn'];
	if(element.removeEventListener) { // 用于支持DOM的浏览器
		element.removeEventListener(eventName, eventfn);
	} else if(element.removeEvent) { // 用于IE浏览器
		element.removeEvent("on" + eventName, eventfn);
	} else { // 用于其它浏览器
		delete element["on" + eventName];
	}
	if(u.event && u.event[eventName] && u.event[eventName].teardown) {
		u.event[eventName].teardown.call(element);
	}

	if(element && element["uEvent"] && element["uEvent"][eventName])
		element["uEvent"][eventName] = undefined;
	if(element && element["uEvent"] && element["uEvent"][eventName + 'fn'])
		element["uEvent"][eventName + 'fn'] = undefined;

};

/**
 * Module : Sparrow dom
 * Author : Kvkens(yueming@yonyou.com)
 * Date	  : 2016-08-16 13:59:17
 */

/**
 * Module : Sparrow extend enum
 * Author : Kvkens(yueming@yonyou.com)
 * Date	  : 2016-07-27 21:46:50
 */

var enumerables = true;
var enumerablesTest = {
		toString: 1
	};
for(var i in enumerablesTest) {
	enumerables = null;
}
if(enumerables) {
	enumerables = ['hasOwnProperty', 'valueOf', 'isPrototypeOf', 'propertyIsEnumerable',
		'toLocaleString', 'toString', 'constructor'
	];
}

/**
 * Module : Sparrow extend
 * Author : Kvkens(yueming@yonyou.com)
 * Date	  : 2016-07-27 21:46:50
 */

/**
 * 复制对象属性
 *
 * @param {Object}  目标对象
 * @param {config} 源对象
 */
var extend = function(object, config) {
	var args = arguments,
		options;
	if(args.length > 1) {
		for(var len = 1; len < args.length; len++) {
			options = args[len];
			if(object && options && typeof options === 'object') {
				var i, j, k;
				for(i in options) {
					object[i] = options[i];
				}
				if(enumerables) {
					for(j = enumerables.length; j--;) {
						k = enumerables[j];
						if(options.hasOwnProperty && options.hasOwnProperty(k)) {
							object[k] = options[k];
						}
					}
				}
			}
		}
	}
	return object;
};

if(!Object.assign){
	Object.assign = extend;
}

/**
 * Module : neoui-collapse
 * Author :  yaoxinc(yaoxinc@yonyou.com)
 * Date   : 2016/11/30
 *
 */

/* COLLAPSIBLE PLUGIN DEFINITION
 *
 */


function Collapse(element, options) {

    this.$element = $(element); //数值是下面的class等，且只执行一次
    this.options = $.extend({}, {
        toggle: true
    }, options); //数值是Oject{toggle:collapse}
    this.options.toggle && this.toggle(); //undefined

}

Collapse.prototype = {
    constructor: Collapse,
    dimension: function() {
        /*  console.log(this.$element)*/
        var hasWidth = this.$element.hasClass('width'); //false
        return hasWidth ? 'width' : 'height'; //返回height
    },
    show: function() {
        var dimension = this.dimension(), //数值是height
            scroll = $.camelCase(['scroll', dimension].join('-')), //数值scrollHeight
            actives = this.$parent && this.$parent.find('.in'), //数值undefined
            hasData;

        if (actives && actives.length) {
            hasData = actives.data('collapse');
            actives.collapse('hide');
            hasData || actives.data('collapse', null);
        }
        this.$element[dimension](0); //数值是下面的class等
        this.transition('addClass', 'show', 'shown');
        this.$element[dimension](this.$element[0][scroll]);

    },
    transition: function(method, startEvent, completeEvent) { //'addClass', 'show', 'shown'
        var that = this,
            complete = function() {
                if (startEvent == 'show')
                    that.reset();
                that.$element.trigger(completeEvent);
            };
        this.$element.trigger(startEvent)[method]('in');

        $.support.transition && this.$element.hasClass('collapse') ? this.$element.one($.support.transition.end, complete) : complete();

    },
    reset: function(size) {
        var dimension = this.dimension(); //数值一直是height
        this.$element.removeClass('collapse')[dimension](size || 'auto')[0].offsetWidth; //1200
        this.$element.addClass('collapse');
    },
    hide: function() {
        var dimension = this.dimension();
        this.reset(this.$element[dimension]());
        this.transition('removeClass', 'hide', 'hidden');
        this.$element[dimension](0);
    },

    toggle: function() {
        this[this.$element.hasClass('in') ? 'hide' : 'show']();
    }

};


var showCollapse = u.BaseComponent.extend({

    /* COLLAPSIBLE DATA-API
     * ==================== */
    init: function() {
        off(this.element, 'click');
        on(this.element, 'click', function(e) {
            var $this = $(this);

            var href;
            var target = (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') || $this.attr('u-data-toggle') || e.preventDefault(); //strip for ie7

            var option = $(target).data('collapse') ? 'toggle' : $this.data();
            var $this_down = $(target);

            var data = $this_down.data('collapse'),
                options = typeof option == 'object' && option;

            if (!data) {
                $this_down.data('collapse', data = new Collapse(target, options));
            }
            if (typeof option == 'string') {
                data[option]();
            }
        });

    }

});


if (u.compMgr)
    u.compMgr.regComp({
        comp: showCollapse,
        compAsString: 'u.collapse.updown',
        css: 'u-collapse-updown'
    });

exports.showCollapse = showCollapse;

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