/*-- Home --*/
(function($){
	"use strict";
	$(document).ready(function(){
		if ($("body").hasClass('home')) {
			$(window).load(function(){
				if ($(":root").scrollTop() > 0) {
					$(":root").scrollTop(0);
				}
			});
			$("#wrapper").scroll(function(){
				$("[data-scroll-trigger]").not('.go').each(function(){
					var myTop = $(this).offset().top;
					var percentOnPage = 1 - (0.01 * parseInt($(this).data('scroll-trigger'))); // 40% on page = 60% from top
					var pixelsFromTop = $(window).height() * percentOnPage;	// pixels of 60% window height
					if (myTop < pixelsFromTop) {
						$(this).addClass('go');
					}
				});
			});
			window.rotatingBanners = [];
			$(".rotating-banners").each(function(){
				window.rotatingBanners.push( new window.rotatingBanner(this) );
			});
		}
	});
	window.rotatingBanner = function(element) {
		if (element[0] !== undefined) {
			element = element[0];
		}
		element.Banner = this;
		$.extend(this,{
			element: element,
			nav: null,
			banners: [],
			currentIndex: 0,
			total: 0,
			isAnimating: false,
			rotateTimer: 0,
			rotationDelay: 7000,
			transitionSpeed: 500,
			init: function() {
				var $this = this;
				var $element = $($this.element);
				$element.find('.banners-banner').each(function(){
					var banner = [this];
					if ($element.find('.banners-content[data-banner-id='+$(this).data('banner-id')+']').length) {
						banner.push($element.find('.banners-content[data-banner-id='+$(this).data('banner-id')+']')[0]);
					}
					$this.banners.push(banner);
				});
				$this.setTimer();
				setTimeout(function(){
					$this.total = $this.banners.length;
					$($this.banners[0]).addClass('current');
				},0);
				$this.nav = $($this.element).find('[data-nav]').each(function(){
					this.Banner = $this;
				});
				$this.nav.filter('[data-go-next]').click(function(){
					this.Banner.goNext();
				});
				$this.nav.filter('[data-go-prev]').click(function(){
					this.Banner.goPrev();
				});
			},
			setTimer: function() {
				var $this = this;
				if (window.location.hash.match(/\#pause/) === null) {
					$this.rotateTimer = setTimeout(function(){
						$this.goNext();
					}, $this.rotationDelay + $this.transitionSpeed);
				}
			},
			done: function() {
				var $this = this;
				return new Promise(function(resolve) {
					var timer = setInterval(function(){
						if (!$this.isAnimating) {
							clearInterval(timer);
							resolve();
						}
					},100);
				});
			},
			nextIndex: function() {
				if (this.currentIndex + 1 >= this.total) {
					return 0;
				}
				return this.currentIndex + 1;
			},
			prevIndex: function() {
				if (this.currentIndex === 0) {
					return this.total - 1;
				}
				return this.currentIndex - 1;
			},
			goNext: function() {
				var $this = this;
				if (!$this.isAnimating) {
					clearTimeout($this.rotateTimer);
					$this.isAnimating = true;
					for(var b=0;b < $this.banners.length;b++) {
						$($this.banners[b]).removeClass('next prev show moving');
					}
					var $current = $($this.banners[$this.currentIndex]).addClass('show');
					var $nextIndex = $this.nextIndex();
					var $next = $($this.banners[$nextIndex]).addClass('next show');
					setTimeout(function(){
						$current.addClass('moving').removeClass('current').addClass('prev');
						$next.addClass('moving').removeClass('next').addClass('current');
						setTimeout(function(){
							$this.currentIndex = $nextIndex;
							$current.removeClass('moving prev next show current');
							$next.removeClass('moving prev next show');
							$this.isAnimating = false;
							$this.setTimer();
						}, $this.transitionSpeed);
					}, 50);
				}
				return $this;
			},
			goPrev: function() {
				var $this = this;
				if (!$this.isAnimating) {
					clearTimeout($this.rotateTimer);
					$this.isAnimating = true;
					for(var b=0;b < $this.banners.length;b++) {
						$($this.banners[b]).removeClass('next prev show moving');
					}
					var $current = $($this.banners[$this.currentIndex]).addClass('show');
					var $prevIndex = $this.prevIndex();
					var $prev = $($this.banners[$prevIndex]).addClass('prev show');
					setTimeout(function(){
						$current.addClass('moving').removeClass('current').addClass('next');
						$prev.addClass('moving').removeClass('prev').addClass('current');
						setTimeout(function(){
							$this.currentIndex = $prevIndex;
							$current.removeClass('moving prev next show current');
							$prev.removeClass('moving prev next show');
							$this.isAnimating = false;
							$this.setTimer();
						}, $this.transitionSpeed);
					}, 50);
				}
				return $this;
			}
		});
		return this.init();
	};
}(jQuery));

/*-- FAQs Page --*/
(function($){
	"use strict";
	window._faqItems = window._faqItems || [];
	window.faqClickHander = function(){
		var tag = $(this).parent().children('.faq-expand');
		var answer_mask = $(this).parent().children('.faq-answer-mask');
		var height_needed = $(this).parent().find('.faq-answer').css('height');
		var is_closed = answer_mask.css('height') === '0px';
		if(is_closed){
			answer_mask.animate({
				height: height_needed,
			}, {	
				duration:500,
				complete: function() {
					// Animation complete.
					tag.css('background-position','center -11px');
				},
				progress: function(){ 
					fixAllHeights(); 
					if(typeof FaqOnExpand === "function"){
						FaqOnExpand();
					}
				}				
			});
		} else {
			answer_mask.animate({
				height: '0px'
			}, {	
				duration:500,
				complete: function() {
					// Animation complete.
					tag.css('background-position','center top');
				},
				progress: function(){ 
					fixAllHeights(); 
					if(typeof FaqOnCollapse === "function"){
						FaqOnCollapse();
					}
				}	
			});
		}
	}
	$(window).load(function(){
		$('.faq-question').click(window.faqClickHander);
		$('.faq-icon').click(window.faqClickHander);
		$('.faq-items-clean .faq-item.covered').each(function(){
			window._faqItems.push(new window._faqItem(this));
		});
	});
	
	window._faqItem = function(element){
		var faq = this;
		element.faq = faq;
		faq.e = $(element);
		faq.a = faq.e.find('.faq-answer');
		faq.isCovered = faq.e.hasClass('covered');
		faq.isUnCovered = faq.e.hasClass('uncovered');
		faq.totalHeight = 0;
		faq.initialHeight = 0;
		faq.init = function(){
			faq.initialHeight = faq.a.outerHeight();
			faq.setHeights();
			$(window).resize(function(){
				faq.setHeights();
			});
			faq.e.click(function(){
				this.faq.expand();
			});
			return faq;
		};
		faq.expand = function(){
			if (!faq.isUnCovered) {
				var distance = faq.totalHeight - faq.initialHeight;
				var speed = Math.floor(distance * (10/3));
				faq.a.animate({
					'height': faq.totalHeight+'px'
				}, speed, 'linear', function(){
					$(this).css('height','auto');
				});
			}
			faq.e.addClass('uncovered').removeClass('covered');
			faq.isUnCovered = true;
			faq.isCovered = false;
			return faq;
		};
		faq.setHeights = function(){
			if (!faq.isUnCovered) {
				faq.a.css({
					'max-height':'none',
					'height':'auto'
				});
				faq.totalHeight = faq.a.outerHeight();
				// if the faq doesn't need to be collapsed, remove the height constraint
				if (faq.totalHeight < faq.initialHeight * 1.1){
					faq.e.removeClass('covered');
					faq.isCovered = false;
				}else{
					faq.e.addClass('covered');
					faq.a.height(faq.initialHeight);
					faq.isCovered = true;
				}
			}
			return faq;
		};
		return faq.init();
	};
}(jQuery));

/*-- Minisite --*/
(function($){
	"use strict";
	$(document).ready(function(){
		// Minisite Sidebar Multilevel
		$(".minisite--nav > li").filter('.current,.section').addClass('open');
		$(".minisite--nav .minisite--control").each(function(){
			$.extend(this, {
				_minisite_dropdown : $($(this).data('target'))[0],
				_li : $(this).parents('li').first()[0]
			});
			$(this).unbind('click').click(function(){
				if($(this._li).hasClass('open')){
					$(this._li).removeClass('open').addClass('closed');
					$(this._minisite_dropdown).slideUp(300);
				}else{
					$(this._li).removeClass('closed').addClass('open');
					$(this._minisite_dropdown).slideDown(300);
				}
			});
		});
	});
}(jQuery));

/*-- Tabs --*/
(function($){
	"use strict";
	window.IQTabs = function(selector) {
		return $(selector).find('.tabs-button').each(function(){
			var element = this;
			if (element._tabs === undefined) {
				element._tabs = {
					btn: element,
					selector: selector,
					buttons: $(selector).find('.tabs-button').not(this),
					content: {},
					initialized: false,
					init: function(){
						if (!this.initialized) {
							if ($(this.btn).data('tab')) {
								this.content = $($(this.btn).data('tab'));
							}
							if (!this.content.length) {
								this.content = $('[data-tab="'+$(this.btn).data('tab')+'"]');
							}
							$(this.btn).click(function(e){
								e.preventDefault();
								this._tabs.choose();
							});
							this.initialized = true;
						}
						return this;
					},
					show: function() {
						$(this.btn).addClass('current');
						$(this.content).addClass('current');
						return this;
					},
					hide: function() {
						$(this.btn).removeClass('current');
						$(this.content).removeClass('current');
						return this;
					},
					choose: function (){
						$(this.buttons).each(function(){
							this._tabs.hide();
						});
						this.show();
						return this;
					}
				};
			}
			this._tabs.init();
		});
	};
}(jQuery));

/*-- Tabbed Page --*/
(function($){
	"use strict";
	$(document).ready(function(){
		window._tabs = window._tabs || [];
		$(".tabs-tabs").each(function(){
			window._tabs.push(new window.IQTabs(this));
		});
	});
}(jQuery));

/*-- Staff Page --*/
(function($){
	"use strict";
	$(document).ready(function(){
//		window.staffTabs= new window.IQTabs('.staff-departments');
	});
}(jQuery));







