/*!
 * in-view 0.6.1 - Get notified when a DOM element enters or exits the viewport.
 * Copyright (c) 2016 Cam Wiegert <cam@camwiegert.com> - https://camwiegert.github.io/in-view
 * License: MIT
 */
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.inView=e():t.inView=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var i=n[r]={exports:{},id:r,loaded:!1};return t[r].call(i.exports,i,i.exports,e),i.loaded=!0,i.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}var i=n(2),o=r(i);t.exports=o["default"]},function(t,e){function n(t){var e=typeof t;return null!=t&&("object"==e||"function"==e)}t.exports=n},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}Object.defineProperty(e,"__esModule",{value:!0});var i=n(9),o=r(i),u=n(3),f=r(u),s=n(4),c=function(){if("undefined"!=typeof window){var t=100,e=["scroll","resize","load"],n={history:[]},r={offset:{},threshold:0,test:s.inViewport},i=(0,o["default"])(function(){n.history.forEach(function(t){n[t].check()})},t);e.forEach(function(t){return addEventListener(t,i)}),window.MutationObserver&&addEventListener("DOMContentLoaded",function(){new MutationObserver(i).observe(document.body,{attributes:!0,childList:!0,subtree:!0})});var u=function(t){if("string"==typeof t){var e=[].slice.call(document.querySelectorAll(t));return n.history.indexOf(t)>-1?n[t].elements=e:(n[t]=(0,f["default"])(e,r),n.history.push(t)),n[t]}};return u.offset=function(t){if(void 0===t)return r.offset;var e=function(t){return"number"==typeof t};return["top","right","bottom","left"].forEach(e(t)?function(e){return r.offset[e]=t}:function(n){return e(t[n])?r.offset[n]=t[n]:null}),r.offset},u.threshold=function(t){return"number"==typeof t&&t>=0&&t<=1?r.threshold=t:r.threshold},u.test=function(t){return"function"==typeof t?r.test=t:r.test},u.is=function(t){return r.test(t,r)},u.offset(0),u}};e["default"]=c()},function(t,e){"use strict";function n(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var r=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),i=function(){function t(e,r){n(this,t),this.options=r,this.elements=e,this.current=[],this.handlers={enter:[],exit:[]},this.singles={enter:[],exit:[]}}return r(t,[{key:"check",value:function(){var t=this;return this.elements.forEach(function(e){var n=t.options.test(e,t.options),r=t.current.indexOf(e),i=r>-1,o=n&&!i,u=!n&&i;o&&(t.current.push(e),t.emit("enter",e)),u&&(t.current.splice(r,1),t.emit("exit",e))}),this}},{key:"on",value:function(t,e){return this.handlers[t].push(e),this}},{key:"once",value:function(t,e){return this.singles[t].unshift(e),this}},{key:"emit",value:function(t,e){for(;this.singles[t].length;)this.singles[t].pop()(e);for(var n=this.handlers[t].length;--n>-1;)this.handlers[t][n](e);return this}}]),t}();e["default"]=function(t,e){return new i(t,e)}},function(t,e){"use strict";function n(t,e){var n=t.getBoundingClientRect(),r=n.top,i=n.right,o=n.bottom,u=n.left,f=n.width,s=n.height,c={t:o,r:window.innerWidth-u,b:window.innerHeight-r,l:i},a={x:e.threshold*f,y:e.threshold*s};return c.t>e.offset.top+a.y&&c.r>e.offset.right+a.x&&c.b>e.offset.bottom+a.y&&c.l>e.offset.left+a.x}Object.defineProperty(e,"__esModule",{value:!0}),e.inViewport=n},function(t,e){(function(e){var n="object"==typeof e&&e&&e.Object===Object&&e;t.exports=n}).call(e,function(){return this}())},function(t,e,n){var r=n(5),i="object"==typeof self&&self&&self.Object===Object&&self,o=r||i||Function("return this")();t.exports=o},function(t,e,n){function r(t,e,n){function r(e){var n=x,r=m;return x=m=void 0,E=e,w=t.apply(r,n)}function a(t){return E=t,j=setTimeout(h,e),M?r(t):w}function l(t){var n=t-O,r=t-E,i=e-n;return _?c(i,g-r):i}function d(t){var n=t-O,r=t-E;return void 0===O||n>=e||n<0||_&&r>=g}function h(){var t=o();return d(t)?p(t):void(j=setTimeout(h,l(t)))}function p(t){return j=void 0,T&&x?r(t):(x=m=void 0,w)}function v(){void 0!==j&&clearTimeout(j),E=0,x=O=m=j=void 0}function y(){return void 0===j?w:p(o())}function b(){var t=o(),n=d(t);if(x=arguments,m=this,O=t,n){if(void 0===j)return a(O);if(_)return j=setTimeout(h,e),r(O)}return void 0===j&&(j=setTimeout(h,e)),w}var x,m,g,w,j,O,E=0,M=!1,_=!1,T=!0;if("function"!=typeof t)throw new TypeError(f);return e=u(e)||0,i(n)&&(M=!!n.leading,_="maxWait"in n,g=_?s(u(n.maxWait)||0,e):g,T="trailing"in n?!!n.trailing:T),b.cancel=v,b.flush=y,b}var i=n(1),o=n(8),u=n(10),f="Expected a function",s=Math.max,c=Math.min;t.exports=r},function(t,e,n){var r=n(6),i=function(){return r.Date.now()};t.exports=i},function(t,e,n){function r(t,e,n){var r=!0,f=!0;if("function"!=typeof t)throw new TypeError(u);return o(n)&&(r="leading"in n?!!n.leading:r,f="trailing"in n?!!n.trailing:f),i(t,e,{leading:r,maxWait:e,trailing:f})}var i=n(7),o=n(1),u="Expected a function";t.exports=r},function(t,e){function n(t){return t}t.exports=n}])});

// Study Pages Extensions
jQuery(function($) {
	var $anchornav = $('.study-anchornav'),
		pos = ($anchornav.offset() || {top:0}).top,
		$responsiveSlider = $('.responsive-slider'),
		$timeline = $('.study-timeline'),
		$barchart = $('.study-barchart');

	if($anchornav.length) {
		var $footer = $('footer');
		$(document).on('resize', function () {
			pos = $anchornav.offset().top;
		});

		$(document).on('scroll', function () {
			var scrollPos = $(document).scrollTop()+70,
				orientation = window.matchMedia("screen and (orientation: landscape)");

			if(orientation.matches) {
				var footerAmount = ($(document).scrollTop() + $(window).height()) - $footer.offset().top;
				$anchornav.css({
					top: 130 - Math.min($(document).scrollTop(), 90),
					bottom: Math.max(0, footerAmount)
				});
			}

			if($anchornav.offset().top > pos && !$anchornav.is('.affix')) {
				pos = $anchornav.offset().top;
			}

			if(pos <= scrollPos) {
				$anchornav.addClass('affix');
			}
			else {
				$anchornav.removeClass('affix');
			}

			var $links = $anchornav.find('li').removeClass('active').find('a').filter(function() {
				var href = $(this).attr('href');

				return !!(href.indexOf('#') == 0 && scrollPos+82 >= $(href).offset().top);
			});

			$links.eq(-1).parent().addClass('active');
			if($anchornav.find('li.active').length) {
				$anchornav.find('li.active').parent().scrollLeft(0);
				var offset = $anchornav.find('li.active').offset().left - $anchornav.find('li.active').offsetParent().offset().left;
				offset = Math.floor(offset - 84.4167);
				$anchornav.find('li.active').parent().scrollLeft(offset);
			}
		}).trigger('scroll');
	}

	if($responsiveSlider.length) {
		var getPrefixed = function(property) {
				const prefixes = ['', '-webkit-', '-moz-', '-ms-', '-o-'],
					div = document.createElement('div');

				for(var i in prefixes) {
					if (typeof div.style[prefixes[i] + property] !== 'undefined') {
						return prefixes[i] + property;
					}
				}
				return false;
			},
			slideTo = function(slider, index) {
				var css = {};
				css[prefixedTransform] = 'translateX('+(-103.25 * index)+'%)';

				$(slider).children().eq(0).css(css);
				$(slider).children().eq(-1).children().removeAttr('data-active').eq(index).attr('data-active', true);
			},
			disableClick = function(e) {
				e.stopImmediatePropagation();
				e.stopPropagation();
				return false;
			},
			removeSelection = function() {
				if (window.getSelection) {
					if (window.getSelection().empty) {  // Chrome
						window.getSelection().empty();
					} else if (window.getSelection().removeAllRanges) {  // Firefox
						window.getSelection().removeAllRanges();
					}
				} else if (document.selection) {  // IE?
					document.selection.empty();
				}
			},
			prefixedTransform = getPrefixed('transform'),
			mq = window.matchMedia("screen and (min-width:701px)");

		$(window).resize(function() {
			mq = window.matchMedia("screen and (min-width:701px)");
		});

		$responsiveSlider.each(function() {
			var $slider = $(this),
				$slides = $slider.find('.responsive-slider__item'),
				slideCount = $slides.length - 1,
				$nav = $slider.children().eq(-1),
				navChildren = $nav.find('li').get(),
				currentSlide = 0,
				isStarted = false,
				allowedTime = 50,
				startX, startTime, endX, endTime, lastY;

			$nav.on({
				click: function() {
					currentSlide = $.inArray(this, navChildren);
					slideTo($slider, currentSlide);
				}
			}, 'li');

			$slider.on({
				'touchstart mousedown': function(e) {
					if(mq.matches) return;

					isStarted = true;
					startX = (e.originalEvent.changedTouches || [e.originalEvent])[0].pageX;
					startTime = new Date().getTime();
					$slider.find('a').on('click', disableClick);
				},
				'touchend mouseup': function(e) {
					lastY = null;
					if(mq.matches || !isStarted) return;

					isStarted = false;
					endX = (e.originalEvent.changedTouches || [e.originalEvent])[0].pageX;
					endTime = new Date().getTime() - startTime;

					if(endTime >= allowedTime && $slider.width() < $slider.children().get(0).scrollWidth) {
						setTimeout(function() {
							$slider.find('a').off('click', disableClick);
						}, 1);

						var direction = (startX-endX < 0) ? -1 : 1,
							nextSlide = currentSlide + direction;

						if(nextSlide < 0) {
							nextSlide = slideCount;
						}
						else if(nextSlide > slideCount) {
							nextSlide = 0;
						}

						currentSlide = nextSlide;

						slideTo($slider, currentSlide);
					}
					else {
						$slider.find('a').off('click', disableClick);
					}
				},
				'touchmove mousemove': function(e) {
					if(mq.matches) return;

					var currentY = (e.originalEvent.touches || [e.originalEvent])[0].clientY;
					if(lastY && (currentY > lastY+30 || currentY < lastY-30)){
						isStarted = false;
						return true;
					}
					lastY = currentY;

					if(isStarted) {
						removeSelection();
						return false;
					}
				}
			});
		});
		$responsiveSlider.filter('.responsive-slider--carousel').each(function() {
			var $slider = $(this),
				$stage = $slider.children().eq(0),
				$slides = $slider.find('.responsive-slider__item'),
				$links = $slider.find('.responsive-slider__link');

			$slider.on({
				transitionend: function() {
					if(!mq.matches) return;

					var func = {
							left: 'appendTo',
							right: 'prependTo'
						},
						index = {
							left: 0,
							right: -1
						},
						dir = $slider.attr('data-animate');

					if(func[dir]) {
						$slides.eq(index[dir])[func[dir]]($stage);
						$slider.removeAttr('data-animate');
						$slides = $slider.find('.responsive-slider__item');
					}
				}
			});

			$links.on({
				click: function() {
					if(!mq.matches) return;

					$slider.attr('data-animate', $(this).is('.responsive-slider__link--next') ? 'left' : 'right');
				}
			})
		});
	}

	if($timeline.length) {
		$timeline.each(function() {
			var $this = $(this),
				days = $this.find('dt input').get(),
				$events = $this.find('dd');

			$this.on({
				change: function() {
					var key = $.inArray(this, days);
					$events.hide();
					$events.eq(key).show();
				}
			}, 'dt input');

			$(days[0]).change();
		});
	}

	if($barchart.length) {
		var mix = function(color_1, color_2, weight) {
			function d2h(d) { return d.toString(16); }  // convert a decimal value to hex
			function h2d(h) { return parseInt(h, 16); } // convert a hex value to decimal

			weight = (typeof(weight) !== 'undefined') ? parseInt(weight) : 50; // set the weight to 50%, if that argument is omitted
			color_1 = color_1.indexOf('#') > -1 ? color_1.substr(1, 6) : color_1;
			color_2 = color_2.indexOf('#') > -1 ? color_2.substr(1, 6) : color_2;

			var color = "#";

			for(var i = 0; i <= 5; i += 2) { // loop through each of the 3 hex pairs—red, green, and blue
				var v1 = h2d(color_1.substr(i, 2)), // extract the current pairs
					v2 = h2d(color_2.substr(i, 2)),

				// combine the current pairs from each source color, according to the specified weight
					val = d2h(Math.floor(v2 + (v1 - v2) * (weight / 100.0)));

				while(val.length < 2) { val = '0' + val; } // prepend a '0' if val results in a single digit

				color += val; // concatenate val to our new color string
			}

			return color; // PROFIT!
		};

		if (window.matchMedia('screen').matches) {
			inView('.study-barchart').once('enter', function(element) {
				var $chart = $(element),
					$bars = $chart.find('[data-percentage]').removeAttr('style');

				setTimeout(function() {

                    var maxpercentage = 0;
                    $bars.each(function() {
                        var mypercentage = $(this).data('percentage');
                        if (maxpercentage < mypercentage) {
                            maxpercentage = mypercentage;
                        }
                    });

                    if (maxpercentage < 50) {
                        $bars.each(function() {
                            var mypercentage = $(this).data('percentage')/maxpercentage * 100;
                            $(this).css({
                                width: mypercentage + '%',
                                backgroundColor: mix('#004488', '#22bbff', $(this).data('percentage'))
                            })
                        });
                    } else {
					$bars.each(function() {
                            var mypercentage = $(this).data('percentage');
						$(this).css({
                                width: mypercentage + '%',
							backgroundColor: mix('#004488', '#22bbff', $(this).data('percentage'))
						})
					});
                    }

				}, 1000);
			});
		}
	}


    /*$('.study-barchart').each(function (index, value) {
        var percentage = 0;
        var maxpercentage = 0;

        $('.study-barchart__progress', value).each(function(index2, value2) {
            var getpercentage = $('.study-barchart__progressbar .study-barchart__progressvalue',this).data('percentage');
            if (percentage < getpercentage) {
                maxpercentage = getpercentage;
            }
        });


        $('.study-barchart__progress', value).each(function(index2, value2) {

            var getpercentage = $('.study-barchart__progressbar .study-barchart__progressvalue',this).data('percentage');

            var newpercentage = 60;
            $('.study-barchart__progressbar .study-barchart__progressvalue',this).css('width', newpercentage + '%');;

        });


    });*/


});
