/*! jQuery & Zepto Lazy v1.7.4 - http://jquery.eisbehr.de/lazy - MIT&GPL-2.0 license - Copyright 2012-2016 Daniel 'Eisbehr' Kern */
!function(t,e){"use strict";function r(r,a,i,l,u){function f(){L=t.devicePixelRatio>1,c(i),a.delay>=0&&setTimeout(function(){s(!0)},a.delay),(a.delay<0||a.combined)&&(l.e=v(a.throttle,function(t){"resize"===t.type&&(w=B=-1),s(t.all)}),l.a=function(t){c(t),i.push.apply(i,t)},l.g=function(){return i=n(i).filter(function(){return!n(this).data(a.loadedName)})},l.f=function(t){for(var e=0;e<t.length;e++){var r=i.filter(t[e]);r.length&&s(!1,r)}},s(),n(a.appendScroll).on("scroll."+u+" resize."+u,l.e))}function c(t){var i=a.defaultImage,o=a.placeholder,l=a.imageBase,u=a.srcsetAttribute,f=a.loaderAttribute,c=a._f||{};t=n(t).filter(function(){var t=n(this),r=m(this);return!t.data(a.handledName)&&(t.attr(a.attribute)||t.attr(u)||t.attr(f)||c[r]!==e)}).data("plugin_"+a.name,r);for(var s=0,d=t.length;s<d;s++){var A=n(t[s]),g=m(t[s]),h=A.attr(a.imageBaseAttribute)||l;g==N&&h&&A.attr(u)&&A.attr(u,b(A.attr(u),h)),c[g]===e||A.attr(f)||A.attr(f,c[g]),g==N&&i&&!A.attr(E)?A.attr(E,i):g==N||!o||A.css(O)&&"none"!=A.css(O)||A.css(O,"url('"+o+"')")}}function s(t,e){if(!i.length)return void(a.autoDestroy&&r.destroy());for(var o=e||i,l=!1,u=a.imageBase||"",f=a.srcsetAttribute,c=a.handledName,s=0;s<o.length;s++)if(t||e||A(o[s])){var g=n(o[s]),h=m(o[s]),b=g.attr(a.attribute),v=g.attr(a.imageBaseAttribute)||u,p=g.attr(a.loaderAttribute);g.data(c)||a.visibleOnly&&!g.is(":visible")||!((b||g.attr(f))&&(h==N&&(v+b!=g.attr(E)||g.attr(f)!=g.attr(F))||h!=N&&v+b!=g.css(O))||p)||(l=!0,g.data(c,!0),d(g,h,v,p))}l&&(i=n(i).filter(function(){return!n(this).data(c)}))}function d(t,e,r,i){++z;var o=function(){y("onError",t),p(),o=n.noop};y("beforeLoad",t);var l=a.attribute,u=a.srcsetAttribute,f=a.sizesAttribute,c=a.retinaAttribute,s=a.removeAttribute,d=a.loadedName,A=t.attr(c);if(i){var g=function(){s&&t.removeAttr(a.loaderAttribute),t.data(d,!0),y(T,t),setTimeout(p,1),g=n.noop};t.off(I).one(I,o).one(D,g),y(i,t,function(e){e?(t.off(D),g()):(t.off(I),o())})||t.trigger(I)}else{var h=n(new Image);h.one(I,o).one(D,function(){t.hide(),e==N?t.attr(C,h.attr(C)).attr(F,h.attr(F)).attr(E,h.attr(E)):t.css(O,"url('"+h.attr(E)+"')"),t[a.effect](a.effectTime),s&&(t.removeAttr(l+" "+u+" "+c+" "+a.imageBaseAttribute),f!==C&&t.removeAttr(f)),t.data(d,!0),y(T,t),h.remove(),p()});var m=(L&&A?A:t.attr(l))||"";h.attr(C,t.attr(f)).attr(F,t.attr(u)).attr(E,m?r+m:null),h.complete&&h.trigger(D)}}function A(t){var e=t.getBoundingClientRect(),r=a.scrollDirection,n=a.threshold,i=h()+n>e.top&&-n<e.bottom,o=g()+n>e.left&&-n<e.right;return"vertical"==r?i:"horizontal"==r?o:i&&o}function g(){return w>=0?w:w=n(t).width()}function h(){return B>=0?B:B=n(t).height()}function m(t){return t.tagName.toLowerCase()}function b(t,e){if(e){var r=t.split(",");t="";for(var a=0,n=r.length;a<n;a++)t+=e+r[a].trim()+(a!==n-1?",":"")}return t}function v(t,e){var n,i=0;return function(o,l){function u(){i=+new Date,e.call(r,o)}var f=+new Date-i;n&&clearTimeout(n),f>t||!a.enableThrottle||l?u():n=setTimeout(u,t-f)}}function p(){--z,i.length||z||y("onFinishedAll")}function y(t,e,n){return!!(t=a[t])&&(t.apply(r,[].slice.call(arguments,1)),!0)}var z=0,w=-1,B=-1,L=!1,T="afterLoad",D="load",I="error",N="img",E="src",F="srcset",C="sizes",O="background-image";"event"==a.bind||o?f():n(t).on(D+"."+u,f)}function a(a,o){var l=this,u=n.extend({},l.config,o),f={},c=u.name+"-"+ ++i;return l.config=function(t,r){return r===e?u[t]:(u[t]=r,l)},l.addItems=function(t){return f.a&&f.a("string"===n.type(t)?n(t):t),l},l.getItems=function(){return f.g?f.g():{}},l.update=function(t){return f.e&&f.e({},!t),l},l.force=function(t){return f.f&&f.f("string"===n.type(t)?n(t):t),l},l.loadAll=function(){return f.e&&f.e({all:!0},!0),l},l.destroy=function(){return n(u.appendScroll).off("."+c,f.e),n(t).off("."+c),f={},e},r(l,u,a,f,c),u.chainable?a:l}var n=t.jQuery||t.Zepto,i=0,o=!1;n.fn.Lazy=n.fn.lazy=function(t){return new a(this,t)},n.Lazy=n.lazy=function(t,r,i){if(n.isFunction(r)&&(i=r,r=[]),n.isFunction(i)){t=n.isArray(t)?t:[t],r=n.isArray(r)?r:[r];for(var o=a.prototype.config,l=o._f||(o._f={}),u=0,f=t.length;u<f;u++)(o[t[u]]===e||n.isFunction(o[t[u]]))&&(o[t[u]]=i);for(var c=0,s=r.length;c<s;c++)l[r[c]]=t[0]}},a.prototype.config={name:"lazy",chainable:!0,autoDestroy:!0,bind:"load",threshold:500,visibleOnly:!1,appendScroll:t,scrollDirection:"both",imageBase:null,defaultImage:"data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==",placeholder:null,delay:-1,combined:!1,attribute:"data-src",srcsetAttribute:"data-srcset",sizesAttribute:"data-sizes",retinaAttribute:"data-retina",loaderAttribute:"data-loader",imageBaseAttribute:"data-imagebase",removeAttribute:!0,handledName:"handled",loadedName:"loaded",effect:"show",effectTime:0,enableThrottle:!0,throttle:250,beforeLoad:e,afterLoad:e,onError:e,onFinishedAll:e},n(t).on("load",function(){o=!0})}(window);

var easypower = function () {
	return {
		initialize: function () {
			$('select').chosen({'disable_search': true});

			// Lazy load, chainable: false added for instance use.
			var lazyInstance = $('img.lazy-image').lazy({threshold: 100, chainable: false});

			$('img[data-hover]').each(function(){
				var $that = $(this),
				defaultImage = $that.attr('src'),
				hoverImage = $that.data('hover');

				$that.on('mouseenter', function(){
					$that.attr('src', hoverImage);
				});

				$that.on('mouseleave', function(){
					$that.attr('src', defaultImage);
				});
			});

			$('.multi-col-table').each(function(){
				var $that = $(this),
				childIframes = $that.find('iframe');
				childIframes.each(function(){
					var $child = $(this);
					if ($child.attr('src').indexOf('wistia') > 0) {
						$child.closest('td').addClass('narrower');
						$parent = $child.parent();
						if ($parent.is('p')) $parent.addClass('adapt-video-wrapper');
						else {
							$newChild = $('<p/>', {'class': 'adapt-video-wrapper'}).append($child.clone());
							$child.replaceWith($newChild);
						}
					}
				})
			}).find('td').each(function(){
				if ($.trim($(this).text()) == '') {$(this).remove();}
			});

			$('body').find('iframe').each(function(){
				var $that = $(this);
				if ($that.attr('src').indexOf('wistia') > 0) {
					var thisSrc = $that.attr('src');
					//$that.attr('src', thisSrc+'?fullscreenButton=true');
					$that.attr('allowfullscreen', '');
					$that.attr('mozallowfullscreen', '');
					$that.attr('webkitallowfullscreen', '');
					$that.attr('msallowfullscreen', '');
				}
			});

			$('body').find('.wistia_responsive_padding').each(function(){$(this).parent('div').addClass('video-wrap-720');});

			$('.wys-content a[href*="#"]').on('click', function (e) {
	            var $that = $(this),
	            target = $that.attr('href');
	            if (target.length > 1) {
		            if ($(target).length) {
		            	e.preventDefault();
		            	e.stopPropagation();
		            	$('html, body').animate({
		                	scrollTop: $(target).offset().top - $('.navbar-fixed-top').height()
		                }, 1000);
		            }
		        }
	        });

			$('.wys-content table').each(function(){
				var $that = $(this);
				if (!$that.is('.multi-col-table') && !$that.is('.no-transform')) {
					$that.addClass('table table-bordered');
					$newEle = $('<p/>', {'class': 'table-responsive'}).append($that.clone());
					$that.replaceWith($newEle);
				}
			});

			if ($('#var-selector').length) {
				$('#var-selector').text('Loading VAR List...');
				// prepare reseller dropdown
				$.ajax({
					method: 'get',
					url: 'utils/reseller-list',
					dataType: 'json'
				}).done(function(data){
					var $dropdown = easypower.reseller.createDropdown(data),
					$infoContainer = $('<div/>', {'class': 'var-info-container table-responsive'});
					$dropdownWrap = $('<div/>', {'class': 'var-list-wrap'}).append($dropdown);
					$dropdownWrap.append($dropdown);
					$('#var-selector').replaceWith($dropdownWrap.add($infoContainer));
					$dropdown.chosen({'disable_search': true});
					$dropdown.on('change', function(){
						var $loader = $('<img/>', {'src': 'images/logos/spinning-circles.svg', 'class': 'var-loader'});
						$dropdownWrap.append($loader);
						$.ajax({
							url: 'utils/get-reseller/'+$(this).val(),
							dataType: 'json'
						}).done(function(info) {
							$loader.remove();
							$infoContainer.finish().slideUp(300, function(){
								$infoContainer.empty().append(easypower.reseller.updateList(info.reseller_info));
								$infoContainer.slideDown(300);
							});
						});
					});
				})
			}

			var resourceVIDs = $('main').data('vids');
			if (typeof resourceVIDs !== 'undefined') {
				var vids = resourceVIDs.split('|');
				for (var i = 0; i < vids.length; i++) {
					var videoInfoObj = window['wistiajsonp-/embed/medias/'+vids[i]+'.jsonp'];
					if (typeof videoInfoObj !== 'undefined') {
						var videoName = videoInfoObj['media']['name'],
						captions = videoInfoObj['media']['captions'],
						finalCaption = '';
						if (typeof captions !== 'undefined') {
							for (var j = 0; j < captions.length; j++) {
								var thisCaption = captions[j];
								if (thisCaption['language'] == 'eng') {
									finalCaption = thisCaption['text'];
								}
							}
							if (finalCaption == '') finalCaption = captions[0]['text'];

							$('.resource-article-content').append($('<h2/>', {'text': 'Full Transcript of the Video'}));
							var customAccordion = easypower.utils.createAccordion([{'header': videoName, 'content': finalCaption}]);
							$('.resource-article-content').append(customAccordion);
							customAccordion.find('.accordion-toggle').on('click', function(){
								var $that = $(this),
								href = $that.attr('href'),
								$foundContent = customAccordion.find(href);
								$foundContent.collapse('toggle');
							});
						}
					}
				}	
			}
			
			$('#all-videos-button').on('click', function(){
				document.getElementById('all-videos-button').style.color = 'black';
				document.getElementById('ezp-videos-button').style.color = '#969696';
				document.getElementById('xgs-videos-button').style.color = '#969696';
			    document.getElementById('db-videos-button').style.color = '#969696';
				var $select = $('#topics-select'),
				selectedTopic = $.trim($select.val());
				$('.category-item').add('.featured-item').add('.featured-block').add('.all-other-block').show();
				if (selectedTopic == '') {
					//$('.category-item').add('.featured-item').add('.featured-block').add('.all-other-block').show();
					$('.topic-title').text('All');
				}
				else {
					$('.category-item, .featured-item').each(function(){
						var $that = $(this),
						topics = $.trim($that.data('topics'));
						if (topics == '') $that.hide();
						else {
							var topicArray = topics.split('|');
							if ($.inArray(selectedTopic, topicArray) > -1) {$that.show();}
							else $that.hide();
						}
					});
				}

				if ($('.featured-item:visible').length) $('.featured-block').show();
				else $('.featured-block').hide();


				lazyInstance.update();
			});
			
			$('#ezp-videos-button').on('click', function(){
			    document.getElementById('all-videos-button').style.color = '#969696';
				document.getElementById('ezp-videos-button').style.color = 'black';
				document.getElementById('xgs-videos-button').style.color = '#969696';
			    document.getElementById('db-videos-button').style.color = '#969696';
				var $select = $('#topics-select'),
				selectedTopic = $.trim($select.val()) + 'EasyPower Software';
				$('.category-item').add('.featured-item').add('.featured-block').add('.all-other-block').show();
				if (selectedTopic == '') {
					//$('.category-item').add('.featured-item').add('.featured-block').add('.all-other-block').show();
					$('.topic-title').text('All');
				}
				else {
					$('.category-item').each(function(){
						var $that = $(this),
						topics = $.trim($that.data('topics'));
						if (topics == '') $that.hide();
						else {
							var topicArray = topics.split('|');
							if ($.inArray(selectedTopic, topicArray) > -1) {$that.show();}
							else $that.hide();
						}
					});
				}


				lazyInstance.update();
			});
			
			$('#xgs-videos-button').on('click', function(){
				document.getElementById('all-videos-button').style.color = '#969696';
				document.getElementById('ezp-videos-button').style.color = '#969696';
				document.getElementById('xgs-videos-button').style.color = 'black';
			    document.getElementById('db-videos-button').style.color = '#969696';
				var $select = $('#topics-select'),
				selectedTopic = $.trim($select.val()) + 'XGSLab';
				$('.category-item').add('.featured-item').add('.featured-block').add('.all-other-block').show();
				if (selectedTopic == '') {
					//$('.category-item').add('.featured-item').add('.featured-block').add('.all-other-block').show();
					$('.topic-title').text('All');
				}
				else {
					$('.category-item').each(function(){
						var $that = $(this),
						topics = $.trim($that.data('topics'));
						if (topics == '') $that.hide();
						else {
							var topicArray = topics.split('|');
							if ($.inArray(selectedTopic, topicArray) > -1) {$that.show();}
							else $that.hide();
						}
					});
				}


				lazyInstance.update();
			});
			
			$('#db-videos-button').on('click', function(){
				document.getElementById('all-videos-button').style.color = '#969696';
				document.getElementById('ezp-videos-button').style.color = '#969696';
				document.getElementById('xgs-videos-button').style.color = '#969696';
			    document.getElementById('db-videos-button').style.color = 'black';
				var $select = $('#topics-select'),
				selectedTopic = $.trim($select.val()) + 'DesignBase';
				selectedTopic = 'DesignBase';
				$('.category-item').add('.featured-item').add('.featured-block').add('.all-other-block').show();
				if (selectedTopic == '') {
					//$('.category-item').add('.featured-item').add('.featured-block').add('.all-other-block').show();
					$('.topic-title').text('All');
				}
				else {
					$('.category-item').each(function(){
						var $that = $(this),
						topics = $.trim($that.data('topics'));
						if (topics == '') $that.hide();
						else {
							var topicArray = topics.split('|');
							if ($.inArray(selectedTopic, topicArray) > -1) {$that.show();}
							else $that.hide();
						}
					});
				}


				lazyInstance.update();
			});

			$('#topics-select').on('change', function(){
				var $select = $(this),
				selectedTopic = $.trim($select.val());
				$('.category-item').add('.featured-item').add('.featured-block').add('.all-other-block').show();
				if (selectedTopic == '') {
					//$('.category-item').add('.featured-item').add('.featured-block').add('.all-other-block').show();
					$('.topic-title').text('All');
				}
				else {
					$('.topic-title').text(selectedTopic);
					$('.category-item, .featured-item').each(function(){
						var $that = $(this),
						topics = $.trim($that.data('topics'));
						if (topics == '') $that.hide();
						else {
							var topicArray = topics.split('|');
							if ($.inArray(selectedTopic, topicArray) > -1) {$that.show();}
							else $that.hide();
						}
					});
				}

				if ($('.featured-item:visible').length) $('.featured-block').show();
				else $('.featured-block').hide();

				if ($('.category-item:visible').length) $('.all-other-block').show();
				else $('.all-other-block').hide();

				lazyInstance.update();
			});

			if ($.trim($('#topics-select').val()) != '') $('#topics-select').trigger('change');
		},
		ellipsis: function (options) {
			if (typeof Ellipsis !== 'undefined' && !$('html').hasClass('oldIE')) {
				$.each(options, function(idx, val) {Ellipsis(val);});
			}
		},
		reseller: function () {
			return {
				createDropdown: function (resellers) {
					var $dropdown = $('<select/>', {'class': 'var-list'});
					$('<option/>', {'value': '', 'text': 'SELECT VAR'}).appendTo($dropdown);
					for (var i = 0; i < resellers.length; i++) {
						$('<option/>', {'value': resellers[i]['url_title'], 'text': resellers[i]['reseller_label']}).appendTo($dropdown);
					}
					return $dropdown;
				},
				updateList: function (infoList) {
					var $tbody = $('<tbody/>'),
					$table = $('<table/>', {'class': 'var-info-table table table-bordered'}).append($tbody);
					for (var i = 0; i < infoList.length; i++) {
						var $tr = $('<tr/>'),
						header = infoList[i]['info_header'],
						content = infoList[i]['info_content'],
						content_type = infoList[i]['info_content_type'];

						switch (content_type.toLowerCase()) {
							case 'bold':
								content = '<strong>'+content+'</strong>';
								break;
							case 'email':
								content = '<a href="mailto:'+content+'">'+content+'</a>';
								break;
							case 'link':
								contentUrl = easypower.utils.trimSlashes(content);
								if (contentUrl.indexOf('http') == -1) contentUrl = 'http://'+contentUrl;
								content = '<a href="'+contentUrl+'" target="_blank">'+content+'</a>';
						}

						var $hTd = $('<td/>', {'class': 'header-td', 'text': header}),
						$cTd = $('<td/>', {'class': 'content-td'}).html(content);

						$tr.append($hTd).append($cTd);

						$tbody.append($tr);
					}
					return $table;
				}
			}
		}(),
		video: function () {
			return {
				initialize: function () {}}
		}(),
		utils: function () {
			return {
				trimSlashes: function (str) {
    				return str.replace(/^\/+|\/+$/g, '');
				},
				createAccordion: function (arr) {
					var $panelGroup = $('<div/>', {'class': 'panel-group', 'role': 'tablist', 'aria-multiselectable': 'true', 'id': 'custom-accordion'});
					for (var i = 0; i < arr.length; i++) {
						var thisObj = arr[i];
						var $panel = $('<div/>', {'class': 'panel panel-default'}),
						$panelHeading = $('<div/>', {'class': 'panel-heading', 'role': 'tab', 'id': 'custom-accordion-heading-'+i})
							.append($('<h3/>', {'class': 'panel-title'}).append($('<div/>', {'class': 'accordion-toggle', 'role': 'button', 'href': '#collapse-'+i, 'aria-expanded': 'false', 'aria-controls': 'collapse-'+i}).html(thisObj.header))),
						$panelCollapse = $('<div/>', {'class': 'panel-collapse collapse', 'role': 'tabpanel', 'aria-labelledby': 'custom-accordion-heading-'+i, 'id': 'collapse-'+i}).append($('<div/>', {'class': 'panel-body'}).html(thisObj.content));
						$panelGroup.append($panel.append($panelHeading).append($panelCollapse));
					}
					return $panelGroup;
				}
			}
		}()
	}
}();

easypower.initialize();