$(document).ready(function() { init(); });

$(window).load(function() {
	$('.loading').remove();
	$('.diorama').removeClass('hidden');
	$('.hide').fadeOut(1400);
	$('.bg-mid').height($('.content').height()+56);
	$('.primary .border, .single .border').height($('.content').height());
	$('#gallery .primary .border').height($('.video-gallery .inner').outerHeight());
});

function init()
{
	//$('.entry p a img').parent().addClass('fancybox');
	//$('.entry p a img').parent().attr('rel','single');

	if ($('.primary .item').length == 1) { $('.primary .divider').remove(); }
	
	$('.fancybox').fancybox({
		nextEffect	: 'none',
		prevEffect	: 'none'
	});
	
	$('.primary iframe').each(function()
	{
		$(this).attr('src', $(this).attr('src')+'?wmode=transparent');
	});
	
	gallery();
	gallery2();
	form();
	filters();
	diorama();
	adjustText();
	
	if ($('li').hasClass('active') && $('#home').length != 0) { $('.active').removeClass('active'); }
	
	$('.entry p:last-child').addClass('last');
	
	function diorama()
	{
		$('body').append('<div class="loading"></div>');
		$('.diorama').append('<div class="hide"></div>');
	
		var dir;
		var bgDist = 100;
		var fgDists = [640,640,640,640,640];
		var tweenTime = 1400;
		var layers = $('.background .holder div');		
		var baileys = $('.foreground .holder img');
		var quotes = $('.diorama .quote');
		var widths = [];
		var heights = [];
		var ratios = ['.1','.4','.6','.8','1.2','1.6'];
		var i, j;
		var curr = 2;
		var oldCurr = curr;
		var ratio = 0.75;
		var w;
		var h;
		var inTween = false;
		var faded;
		var diff = (915-(915*ratio))*0.5;
		var first = true;
		var timer;
		
		$('.diorama .arrow-left').click(function() { if (curr == 0) { dir = -4; toggle(); } else if (curr != 0 && !inTween) { dir = 1; toggle(); } });
		$('.diorama .arrow-right').click(function() { if (curr == 4) { dir = 4; toggle(); } else if (curr != $(baileys).length-1 && !inTween) { dir = -1; toggle(); } });

		$(baileys).each(function()
		{
			w = $(this).width();
			h = $(this).height();
			widths.push(w);
			heights.push(h);
			$(this).css('bottom',80);
			$(this).width(w*ratio);
			$(this).css('left',diff);
		});
		
		$(baileys[curr]).width(widths[curr]);
		$(baileys[curr]).css('bottom',0);
		$(baileys[curr]).css('left',0);
	
		var directions = [-2,-1,0,1,2];
		dir = Math.ceil(Math.random()*4);
		dir = directions[dir];
		
		toggle();
		
		function toggle()
		{
			clearInterval(timer);
			timer = setInterval(function() {
				if (curr != $(baileys).length-1) { dir = -1; } else { dir = 4 }
				toggle();
			}, 7000);
			
			if (first) { tweenTime = 0; first = false; } else { tweenTime = 1400; }
			inTween = true;
			i = 0;
			$(layers).each(function()
			{
				$(this).animate({
					left : '+='+dir*(bgDist*ratios[i])
				}, tweenTime, 'easeInOutQuad', function() { inTween = false; } );
				i++;
			});
			
			j = 0;
			
			curr = curr-dir;
			
			$(baileys).each(function()
			{
				if (j == oldCurr && oldCurr != curr)
				{
					$(this).animate({
						left : diff,
						marginLeft : '+='+(dir*fgDists[oldCurr]),
						bottom : 80,
						width :  widths[oldCurr]*ratio
					}, tweenTime, 'easeInOutQuad');
				}
				else if (j == curr)
				{
					$(this).animate({
						left : 0,
						marginLeft : '+='+(dir*fgDists[curr]),
						bottom : 0,
						width :  widths[curr]
					}, tweenTime, 'easeInOutQuad');
				}
				else
				{
					$(this).animate({
						left : diff,
						marginLeft : '+='+(dir*fgDists[j])
					}, tweenTime, 'easeInOutQuad');
				}
				j++;
			});
			
			$(quotes[oldCurr]).fadeOut(200);
			
			oldCurr = curr;
			
			$(quotes[curr]).delay(700).fadeIn();
			
			if (curr == 0 && curr < 4)
			{
				$('.diorama .arrow-left').addClass('circle-left');
				faded = true;
			}
			else
			{
				if (faded)
				{
					$('.diorama .arrow-left').removeClass('circle-left');
				}
			}
			if (curr == 4 && curr > 0)
			{
				$('.diorama .arrow-right').addClass('circle-right');
				faded = true;
			}
			else
			{
				if (faded)
				{
					$('.diorama .arrow-right').removeClass('circle-right');
				}
			}
		}
	}
	
	function gallery()
	{
		var numPhotos = $('.photos-wrapper img').length;
		var num = 0;
		var faded2;
		
		$('.photos-wrapper .arrow-right').click(function(){
			if (num < numPhotos-9)
			{
				$('.photos-wrapper .photos').animate({
					left : '-=90'
				}, 500);
				num++;
			}
			fadeBtn();
		});
		
		$('.photos-wrapper .arrow-left').click(function(){
			if (num > 0)
			{
				$('.photos-wrapper .photos').animate({
					left : '+=90'
				}, 500);
				num--;
			}
			fadeBtn();
		});
		
		fadeBtn();
		
		function fadeBtn()
		{
			if (num == 0)
			{
				$('.photos-wrapper .arrow-left').fadeOut();
				faded2 = true;
			}
			else
			{
				if (faded2)
				{
					$('.photos-wrapper .arrow-left').fadeIn();
				}
			}
			
			if (num == numPhotos-9 && num > 0)
			{
				$('.photos-wrapper .arrow-right').fadeOut();
				faded2 = true;
			}
			else
			{
				if (faded2)
				{
					$('.photos-wrapper .arrow-right').fadeIn();
				}
			}
		}
	}
		
	function gallery2()
	{
		var numPhotos2 = $('.gallery-wrapper a:not(:hidden)').length;
		var num = 0;
		var maxNum = 30;
		var faded3;
		
		$('.arrow-down').click(function()
		{
			if (num < numPhotos2-3)
			{
				$('.gallery-wrapper .gallery').animate({
					top : '-=160'
				}, 500);
				num = num+3;
			}
			fadeBtn();
		});
		
		$('.arrow-up').click(function()
		{
			if (num >= 3)
			{
				$('.gallery-wrapper .gallery').animate({
					top : '+=160'
				}, 500);
				num = num-3;
			}
			fadeBtn();
		});
		
		fadeBtn();
		
		function fadeBtn()
		{
			if (num == 0)
			{
				$('.arrow-up').fadeOut();
				faded3 = true;
			}
			else
			{
				if (faded3)
				{
					$('.arrow-up').fadeIn();
				}
			}
			
			if (num > numPhotos2-maxNum)
			{
				$('.arrow-down').fadeOut();
				faded3 = true;
			}
			else
			{
				if (faded3)
				{
					$('.arrow-down').fadeIn();
				}
			}
		}
		
		$('.filters a:not(.all)').click(function()
		{
			$('.filters a').removeClass('active');
			var year = $(this).attr('class');
			$('.gallery-wrapper a.'+year+'').fadeIn();
			$('.gallery-wrapper a:not(.'+year+')').fadeOut(0);
			$(this).addClass('active');
			numPhotos2 = $('.gallery-wrapper a:not(:hidden)').length;
			fadeBtn();
			$('.gallery-wrapper .gallery').animate({
					top : '0'
				}, 500);
				num = 0;
		});
		
		$('.filters a.all').click(function()
		{
			$('.filters a').removeClass('active');
			$('.gallery-wrapper a').fadeIn();
			$(this).addClass('active');
			numPhotos2 = $('.gallery-wrapper a:not(:hidden)').length;
			fadeBtn();
			$('.gallery-wrapper .gallery').animate({
					top : '0'
				}, 500);
				num = 0;
		});
	}
	
	function form()
	{
		$('input.name').focus(function() 	{ if ($(this).attr('value') == "Name") 	{ $(this).attr('value',''); } });
		$('input.name').blur(function() 	{ if ($(this).attr('value') == '') 		{ $(this).attr('value','Name'); } });
		$('input.email').focus(function() 	{ if ($(this).attr('value') == "Email") { $(this).attr('value',''); } });
		$('input.email').blur(function() 	{ if ($(this).attr('value') == '') 		{ $(this).attr('value','Email'); } });
		$('textarea').focus(function()		{ if ($(this).text() == "Message") 		{ $(this).text(''); } });
		$('textarea').blur(function()		{ if ($(this).text() == "") 			{ $(this).text('Message'); } });
	}
	
	function filters()
	{
		
	}
	
	function adjustText()
	{
		$('.columnize').columnize({
			width : 200,
			columns: 3,
			columnFloat: "left",
			buildOnce : true,
			lastNeverTallest : true
		});
	}
	
	$('.categories li').slice(4,7).wrapAll('<div class="col" style="left: 120px" />');
	$('.categories li').slice(8,11).wrapAll('<div class="col" style="left: 240px" />');
	$('.categories li').slice(12,15).wrapAll('<div class="col" style="left: 360px" />');
}

(function() {
	var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
	po.src = 'https://apis.google.com/js/plusone.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
