!function ($) {
	$.waJumpTo = function(target){
		target = Boolean(target) ? target : 0;

		var $target = target instanceof jQuery ? target : typeof target == 'string' && isNaN(Number(target)) ? $(target) : null;
		var targetScrollTop = $target && $target.length ? $target.offset().top : typeof target == 'number' || (typeof target == 'string' && !isNaN(Number(target))) ? Number(target) : null;
		
		if (targetScrollTop == null) return;

		var maxScrollTop = $(document).height() - $(window).height()
		var scrollTop = Math.min(targetScrollTop, maxScrollTop);

		$('html, body').animate({scrollTop: scrollTop}, {
			duration: 500,
			easing: 'easeOutCubic'
		});
		return false;
	}
	$.fn.waJumpToAnchor = function(){
		var getTargetSelector = function(href){
			return href + ', a[name = ' + href.replace(/^#(.*)$/, '$1') + ']';
		}

		this.click(function(){	
			$.waJumpTo(getTargetSelector($(this).attr('href')));
		});
	}
	/* BEM */
	$.Bem = function(blockName) {
		var that = this;
		this.c = function(elem) { return blockName + '__' + elem; }	/* class */
		this.s = function(elems) {
			var res = '';
			$.each(elems.split(','), function(i, e) {
				res += '.' + that.c(e.replace(' ','')) + ', ';
			});
			return res.replace(/, $/, '');
		}
		this.j = function(elem) { return $(that.s(elem)); } /* jQuery object */
	}
	/* BEM2 */
	$.Bem2 = function(blockName) {
		return {
			c: function() { return blockName; },
			s: function() { return '.' + blockName; },
			j: function() { return $('.' + blockName); },

			e: function(elems) {
				return {
					c: function(index) {
						if( index === undefined ) index = 0;
						return blockName + '__' + elems.split(',')[index].replace(' ', '');
					},
					s: function() {
						var res = '';
						$.each(elems.split(','), function(i, e) {
							res += '.' + blockName + '__' + e.replace(' ','') + ', ';
						});
						return res.replace(/, $/, '');
					},
					j: function() {
						return $($this2.s());
					}
				}
			}
		}
	}
}(window.jQuery);
