'use strict';

Object.defineProperty(exports, "__esModule", {
	value: true
});
/**
 * 是否是移动端
 */
var hasTouch = exports.hasTouch = 'ontouchstart' in window;
/**
 * 绑定事件
 */
var addEvent = exports.addEvent = function addEvent(el, type, fn, capture) {
	el.addEventListener(type, fn, { passive: false, capture: !!capture });
};
/**
 * 移除事件
 */
var removeEvent = exports.removeEvent = function removeEvent(el, type, fn, capture) {
	el.removeEventListener(type, fn, { passive: false, capture: !!capture });
};
// - start 处理样式前缀 
var elementStyle = document.createElement('div').style;
var vendor = function () {
	var transformNames = {
		webkit: 'webkitTransform',
		Moz: 'MozTransform',
		O: 'OTransform',
		ms: 'msTransform',
		standard: 'transform'
	};

	for (var key in transformNames) {
		if (elementStyle[transformNames[key]] !== undefined) {
			return key;
		}
	}

	return false;
}();
var prefixStyle = function prefixStyle(style) {
	if (vendor === false) {
		return false;
	}

	if (vendor === 'standard') {
		return style;
	}

	return vendor + style.charAt(0).toUpperCase() + style.substr(1);
};
// 是否启用硬件加速
var hasPerspective = exports.hasPerspective = prefixStyle('perspective') in elementStyle;
// 是否使用CSS3的Transform属性
var hasTransform = exports.hasTransform = prefixStyle('transform') in elementStyle;
// 是否使用CSS3的Transition属性，否则使用requestAnimationFram代替
var hasTransition = exports.hasTransition = prefixStyle('transition') in elementStyle;

var style = exports.style = {
	transform: prefixStyle('transform'),
	transitionTimingFunction: prefixStyle('transitionTimingFunction'),
	transitionDuration: prefixStyle('transitionDuration'),
	transitionDelay: prefixStyle('transitionDelay'),
	transformOrigin: prefixStyle('transformOrigin'),
	transitionEnd: prefixStyle('transitionEnd')
};
// - end 处理样式前缀 

/**
 * 获取元素在容器内的位置
 */
var getRect = exports.getRect = function getRect(el) {
	if (el instanceof window.SVGElement) {
		var rect = el.getBoundingClientRect();
		return {
			top: rect.top,
			left: rect.left,
			width: rect.width,
			height: rect.height
		};
	} else {
		return {
			top: el.offsetTop,
			left: el.offsetLeft,
			width: el.offsetWidth,
			height: el.offsetHeight
		};
	}
};
/**
 * 计算元素所在位置偏移值 负值
 */
var offset = exports.offset = function offset(el) {
	var left = 0;
	var top = 0;

	while (el) {
		left -= el.offsetLeft;
		top -= el.offsetTop;
		el = el.offsetParent;
	}

	return {
		left: left,
		top: top
	};
};

/**
 * 去除默认事件判断，在exceptions内不去除
 */
var preventDefaultException = exports.preventDefaultException = function preventDefaultException(el, exceptions) {
	for (var i in exceptions) {
		if (exceptions[i].test(el[i])) {
			return true;
		}
	}
	return false;
};

var ease = exports.ease = {
	// easeOutQuint
	swipe: {
		style: 'cubic-bezier(0.23, 1, 0.32, 1)',
		fn: function fn(t) {
			return 1 + --t * t * t * t * t;
		}
	},
	// easeOutQuard
	swipeBounce: {
		style: 'cubic-bezier(0.25, 0.46, 0.45, 0.94)',
		fn: function fn(t) {
			return t * (2 - t);
		}
	},
	// easeOutQuart
	bounce: {
		style: 'cubic-bezier(0.165, 0.84, 0.44, 1)',
		fn: function fn(t) {
			return 1 - --t * t * t * t;
		}
	}
};

/**
 * 常量判断事件类型 
 */
var TOUCH_EVENT = exports.TOUCH_EVENT = 1;
var MOUSE_EVENT = exports.MOUSE_EVENT = 2;
var eventType = exports.eventType = {
	touchstart: TOUCH_EVENT,
	touchmove: TOUCH_EVENT,
	touchend: TOUCH_EVENT,

	mousedown: MOUSE_EVENT,
	mousemove: MOUSE_EVENT,
	mouseup: MOUSE_EVENT
};

var getNow = exports.getNow = function getNow() {
	// navigationStart 可以理解为第一次打开网址的时间
	// performance.now()可以理解为打开网页后到现在的时间，以微秒（百万分之一秒）为单位的时间
	return window.performance && window.performance.now ? window.performance.now() + window.performance.timing.navigationStart : +new Date();
};

var tap = exports.tap = function tap(e, eventName) {
	var ev = document.createEvent('Event');
	ev.initEvent(eventName, true, true);
	ev.pageX = e.pageX;
	ev.pageY = e.pageY;
	e.target.dispatchEvent(ev);
};

var click = exports.click = function click(e) {
	var target = e.target;

	if (!/(SELECT|INPUT|TEXTAREA)/i.test(target.tagName)) {
		var ev = document.createEvent(window.MouseEvent ? 'MouseEvents' : 'Event');
		// cancelable 设置为 false 是为了解决和 fastclick 冲突问题
		ev.initEvent('click', true, false);
		ev._constructed = true;
		target.dispatchEvent(ev);
	}
};

var momentum = exports.momentum = function momentum(current, start, time, lowerMargin, wrapperSize, options) {
	var distance = current - start;
	var speed = Math.abs(distance) / time;

	var deceleration = options.deceleration,
	    itemHeight = options.itemHeight,
	    swipeBounceTime = options.swipeBounceTime,
	    wheel = options.wheel,
	    swipeTime = options.swipeTime;

	var duration = swipeTime;
	var rate = wheel ? 4 : 15;

	var destination = current + speed / deceleration * (distance < 0 ? -1 : 1);

	if (wheel && itemHeight) {
		destination = Math.round(destination / itemHeight) * itemHeight;
	}

	if (destination < lowerMargin) {
		destination = wrapperSize ? lowerMargin - wrapperSize / rate * speed : lowerMargin;
		duration = swipeBounceTime;
	} else if (destination > 0) {
		destination = wrapperSize ? wrapperSize / rate * speed : 0;
		duration = swipeBounceTime;
	}

	return {
		destination: Math.round(destination),
		duration: duration
	};
};

var DEFAULT_INTERVAL = 100 / 60;

var requestAnimationFrame = exports.requestAnimationFrame = function () {
	return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame ||
	// if all else fails, use setTimeout
	function (callback) {
		return window.setTimeout(callback, (callback.interval || DEFAULT_INTERVAL) / 2); // make interval as precise as possible.
	};
}();

var cancelAnimationFrame = exports.cancelAnimationFrame = function () {
	return window.cancelAnimationFrame || window.webkitCancelAnimationFrame || window.mozCancelAnimationFrame || window.oCancelAnimationFrame || function (id) {
		window.clearTimeout(id);
	};
}();

var createScrollbar = exports.createScrollbar = function createScrollbar(direction) {
	var scrollbar = document.createElement('div');
	var indicator = document.createElement('div');

	scrollbar.style.cssText = 'position:absolute;z-index:9999;pointerEvents:none';
	indicator.style.cssText = 'box-sizing:border-box;position:absolute;background:rgba(0,0,0,0.5);border:1px solid rgba(255,255,255,0.9);border-radius:3px;';

	indicator.className = 'bscroll-indicator';

	if (direction === 'horizontal') {
		scrollbar.style.cssText += ';height:7px;left:2px;right:2px;bottom:0';
		indicator.style.height = '100%';
		scrollbar.className = 'bscroll-horizontal-scrollbar';
	} else {
		scrollbar.style.cssText += ';width:7px;bottom:2px;top:2px;right:1px';
		indicator.style.width = '100%';
		scrollbar.className = 'bscroll-vertical-scrollbar';
	}

	scrollbar.style.cssText += ';overflow:hidden';
	scrollbar.appendChild(indicator);

	return scrollbar;
};