$(document).ready(function() {

$('input, textarea').placeholder();

if ( $( window ).width() > 640 ) {

	$('.slides').magnificPopup({
		delegate: 'a',
		type: 'image',
		fixedContentPos: false,
		tLoading: 'Loading image #%curr%...',
		mainClass: 'mfp-img-mobile',
		gallery: {
			tCounter: '',
			enabled: true,
			navigateByImgClick: true,
			preload: [0,1] // Will preload 0 - before current, and 1 after the current image
		},
		image: {
			tError: '<a href="%url%">The image #%curr%</a> could not be loaded.',
			titleSrc: function(item) {
				return '<div id="svfw" class="svfw-loaded"><div><a href="" class="social-icon facebook" target="_blank"></a><a href="" class="social-icon twitter" target="_blank"></a><a href="" class="social-icon google" target="_blank"></a><a href="" class="social-icon pinterest" target="_blank"></a></div></div>';
			}
		},
		callbacks: {
		    close: function() {
	      		document.location.hash = '';
		    }
  		}
	});
	
	$('.slides a').on('click', function(){
		document.location.hash = '#!/'+$(this).parent().index();
	});

} else {
	$('.slides a').click(function(event) {
	  event.preventDefault();
	});
}

	$('body').on('click','a', function(){

		if($(this).parents('.mfp-title').length){

			var url = '';
			var href = document.location.href.replace(/#/, '%23');

	  		if($(this).hasClass('facebook')){
				url = 'https://www.facebook.com/sharer/sharer.php?u=' + href;
			}else if($(this).hasClass('twitter')){
				url = 'https://twitter.com/home?status=' + href;
			}else if($(this).hasClass('google')){
				url = 'https://plus.google.com/share?url=' + href;
			}else if($(this).hasClass('pinterest')){
				url = 'https://pinterest.com/pin/create/button/?url=https://twitter.com/home?status=' + href + '&media=' + $('.mfp-img').attr('src') + '&description=';
			}

			if (navigator.userAgent.match(/(iPod|iPhone|iPad|Android)/)) {
				document.location.href = url;
			}else{
				window.open(url)
			}

			return false;

		}
		
	});

	var hash = document.location.hash;
	if(hash.length > 1){
		var value = document.location.hash.split('/')[1];
		$('.slides li').eq(value).find('a').trigger('click');
	}

});


$(function(){

	if ($('body').hasClass('innerPage')) {
		top_section_inner_page();

		$(window).resize(function(){
			top_section_inner_page();
		});
	}

	$('.failsworth-slider').flexslider({
		animation: "slide",
		animationLoop: false,
		itemWidth: 300,
		controlNav: false,
		slideshow: false,
		useCSS: false
	});

	if($('.history-nav').length){
		$(".history-nav a").click(function () {
			var elementClick = $(this).attr("href");
			var destination = $(elementClick).offset().top - 70;
			jQuery("html:not(:animated),body:not(:animated)").animate({scrollTop: destination}, 800);
			return false;
		});

		$(window).scroll(function() {
			fixed_nav();
		});

		fixed_nav();
	}

	// accordion
	$('.accordion .accordTitle').on('click', function() {
		var el = this;
		$('.accordCont').slideUp('500');
		$('.accordTitle').removeClass('iconOpen').addClass('iconClose');
		var accLine = $(this).next();
		if($(accLine).css('display') == 'block') {
			// $('html, body').animate({scrollTop: $(el).offset().top+$(accLine).outerHeight()}, 500);
			$(accLine).slideUp('500');
			$(this).removeClass('iconOpen').addClass('iconClose');
		} else {
			$(accLine).slideDown('500', function() {$('html, body').animate({scrollTop: $(el).offset().top-$('#header').outerHeight()-2}, 500)});
			$(this).removeClass('iconClose').addClass('iconOpen');
		};
	});


});


function fixed_nav(){
	var scroll = $(document).scrollTop();
	var navPosition = $('section.nav').offset().top - 73;

	if(scroll > navPosition){
		$('#navbar-history').addClass('fixed');
	} else {
		$('#navbar-history').removeClass('fixed');
	}
}


function top_section_inner_page(){

	var wh = 560;
	var ww = $wnd.width();
	$('.homepage-section .title-wrap').css('height', wh);
	var squ = Math.floor(ww/4);
	if (ww<481) {
		squ = Math.floor(ww);
	} else if (ww<800) {
		squ = Math.floor(ww/3);
	}

	function canSee($block) {
		var c = {
			top: $wnd.scrollTop(),
			left: $wnd.scrollLeft()
		};
		c.right = c.left + $wnd.width();
		c.bottom = c.top + $wnd.height();
		var a = $block.offset();
		a.right = a.left + $block.outerWidth();
		a.bottom = a.top + $block.outerHeight();
		return (!(c.right < a.left || c.left > a.right || c.bottom < a.top || c.top > a.bottom));
	}

	$('.homepage-section .title-wrap').each(function () {
		var $sect = $(this);
		var speed = 2;
		$sect.css({
			backgroundAttachment: "fixed",
			backgroundPosition: "50%" + Math.floor(($sect.offset().top - $wnd.scrollTop()) / speed) + "px"
		});
		$wnd.on('scroll.parallax', function () {
			if (canSee($sect)) {
				var d = Math.floor(($sect.offset().top - $wnd.scrollTop()) / speed);
				var pos = "50% " + d + "px";
				$sect.css({
					backgroundPosition: pos
				});
			}
		});
	});
}