/* 
Author: Chris Barrow / Archive Studio
*/

var navTimer;

$(document).ready(function()
{
	if(!$('#content.home').length)
	{
		$('nav').mouseleave(function()
		{
			navTimer = setTimeout(hideNav,1000);
		}).mouseenter(function()
		{
			clearTimeout(navTimer);
			$(this).find('li:not(.selected) a').stop(true,true).fadeIn(200);
		});
		navTimer = setTimeout('hideNav()',1000);
	} else {
		
		$('#news h3 a').hover(function()
		{
			$(this).addClass('selected');
			$('#articles').fadeIn(400,function()
			{
				$('.article').wave(30,400);
			});
		});
		
		$('#news').mouseleave(function()
		{
			$('#news h3 a').removeClass('selected');
			$('#articles').fadeOut(400,function()
			{
				$('.article').hide();
			});
		});
		
	}
	
	$('.skills-list a').hover(function()
	{
		var c = $(this).attr('class');
		$('li.'+c).addClass('over');
	},function()
	{
		var c = $(this).attr('class');
		$('li.'+c).removeClass('over');
	});
	
	$('.post').hide().wave(30,400);
	
	$('#search input').focus(function()
	{
		if(this.value == 'Search') this.value = '';
	}).blur(function()
	{
		if(this.value == '') this.value = 'Search';
	});
	
	
	//$('#content').css('width',$(window).width() - 20);
	
	$('.section').each(function()
	{
		$(this).data('open',false);
		var link = $('#' + (this.id.replace('section-','section-link-')));
		var sectionID = this.id;
		var section = this;
		$(this).find('h3 a').click(function()
		{
			var o = $(section).data('open');
			$('nav ul li ul li,.section h3 a').removeClass('selected');
			$('nav ul li ul li a').fadeOut(600);
			$('.section[id!=' + sectionID + ']').each(function()
			{
				closeSection(this);
			});
			if(o) {
				$(this).removeClass('selected');
				closeSection(section);
			} else {
				openSection(section);
				$(this).addClass('selected');
				link.addClass('selected');
				link.children('a').fadeIn(600);
			}
		});
		link.find('a').click(function()
		{
			$('nav ul li ul li,.section h3 a').removeClass('selected');
			$('.section[id!=' + sectionID + ']').each(function()
			{
				closeSection(this);
			});
			openSection(section);
			$(this).parent().addClass('selected');
			$(this).fadeIn();
			$(section).find('h3 a').addClass('selected');
		});
	});
	
	$('.project,.related-project').hover(function()
	{
		$(this).find('.overlay').css('cursor','pointer').fadeIn();
	},function()
	{
		$(this).find('.overlay').fadeOut();
	});
	
	$('.alt .project').mouseenter(function()
	{
		var img = $(this).children('img');
		var newSrc = img.attr('src').replace('-grey.png','.jpg');
		img.attr('src',newSrc);
	}).mouseleave(function()
	{
		var img = $(this).find('img');
		var newSrc = img.attr('src').replace('.jpg','-grey.png');
		img.attr('src',newSrc);
	});
	
	$('.project').click(function(e)
	{
		var link = $(this).find('a');
		var href = link.attr('href');
		if(link.attr('target') != '_blank') location = href;
	});
	
	
	
	if($('#home ul li').length)
	{
		$('#home').gallery('li', {auto:true});
	}
	
	if($('.full-project').length)
	{
		//alert(projectOffset);
		var startLeft = -(projectOffset * 1120);
		pAnimating = false;
		$('#content').css('height','530px');
		var count = 0;
		$('.full-project').each(function(){
			/*if(count < projectOffset) l = -1120;
			else if(count == projectOffset) l = 0;
			else l = (count-2) * 1120;*/
			$(this).css({
				'position':'absolute',
				'left':startLeft + 'px',
				'top':'0px'
			});
			count ++;
			startLeft += 1120;
		});
		$('.gallery').gallery('li');
		currentProject = $('.full-project.selected').first();
		$('#next').click(function()
		{
			if(pAnimating) return false;
			pAnimating = true;
			if(currentProject.next('.full-project').length)
			{
				//update nav
				var link = $('#' + (currentProject.attr('id').replace('project-','project-link-')));
				link.removeClass('selected');
				link.children('a').fadeOut(600);
				link.next().addClass('selected');
				link.next().children('a').fadeIn(600);
				
				/*$('.full-project').animate({
					left:'-1120'
				},750,'easeOutExpo');*/
				$('.full-project').animate({
					'left':'-=1120'
				},750,'easeOutExpo',function()
				{
					pAnimating = false;
				});
				currentProject = currentProject.next();
				
			} else pAnimating = false;
		});
		
		$('#prev').click(function()
		{
			if(pAnimating) return false;
			pAnimating = true;
			
			var p = currentProject.prev('.full-project');
			if(p.length)
			{
				var link = $('#' + (currentProject.attr('id').replace('project-','project-link-')));
				link.removeClass('selected');
				link.children('a').fadeOut(600);
				link.prev().addClass('selected');
				link.prev().children('a').fadeIn(600);
				
				/*currentProject.animate({
					left:'+=1120'
				},750,'easeOutExpo');*/
				$('.full-project').animate({
					'left':'+=1120'
				},750,'easeOutExpo'	,function()
				{
					pAnimating = false;
				});
				currentProject = p;
			}else {
				pAnimating = false;
			}
		});
	}
	
	if($('.person').length)
	{
		var startLeft = -1120;
		pAnimating = false;
		$('#content').css('height','530px');
		var count = 0;
		$('.person').each(function(){
			if(count < projectOffset) l = -1120;
			else if(count == projectOffset) l = 0;
			else l = 1120;
			$(this).css({
				'position':'absolute',
				'left':l + 'px',
				'top':'0px'
			});
			count ++;
		});
		$('.gallery').gallery('li');
		currentPerson = $('.person.selected').first();
		$('#next').click(function()
		{
			if(pAnimating) return false;
			pAnimating = true;
			if(currentPerson.next('.person').length)
			{
				//update nav
				var link = $('#' + (currentPerson.attr('id').replace('person-','person-link-')));
				link.removeClass('selected');
				link.children('a').fadeOut(600);
				link.next().addClass('selected');
				link.next().children('a').fadeIn(600);
				
				currentPerson.animate({
					left:'-1120'
				},750,'easeOutExpo');
				currentPerson.next().animate({
					'left':'-=1120'
				},750,'easeOutExpo',function()
				{
					pAnimating = false;
				});
				currentPerson = currentPerson.next();
				
			} else pAnimating = false;
		});
		
		$('#prev').click(function()
		{
			if(pAnimating) return false;
			pAnimating = true;
			
			var p = currentPerson.prev('.person');
			if(p.length)
			{
				var link = $('#' + (currentPerson.attr('id').replace('person-','person-link-')));
				link.removeClass('selected');
				link.children('a').fadeOut(600);
				link.prev().addClass('selected');
				link.prev().children('a').fadeIn(600);
				
				currentPerson.animate({
					left:'+=1120'
				},750,'easeOutExpo');
				p.animate({
					'left':'+=1120'
				},750,'easeOutExpo'	,function()
				{
					pAnimating = false;
				});
				currentPerson = p;
			}else {
				pAnimating = false;
			}
		});
	}
	
	//open first/hashed section
	loadCat();
	
	$('#grid-icon').hover(function()
	{
		this.src = '/images/grid-icon-over.png';
	},function()
	{
		this.src = '/images/grid-icon.png';
	});
	
	initFeatured();
	
	$(window).resize(function()
	{
		$('.section').each(function()
		{
			var height = $(this).children('.section-content').height() + $(this).children('.section-header').height() + 19;
			if($(this).height() != height && $(this).data('open') && !$(this).is(':animated'))
			{
				$(this).animate({'height':height},400,'easeOutExpo');
			}
		});
		
		if(!$('.full-project').length && !$('.people').length) 
		{
			$('#content').css('width',$(window).width() - 20);
			//alert('r');
		} else $('#content').css('width','960px');
		var hWidth = $(window).width();
		if(hWidth < 993) hWidth = 993;
		$('header').css('width',hWidth);
	});
	
	$(window).trigger('resize');
});

// Functions
// auto load a section - detects from hash
loadCat = function()
{
	var section,h;
	h = location.hash.replace('#',''); // get the hash
	if(h.length && $('.'+h).length)
	{
		// section exists, open it
		section = $('.'+h);
	} else {
		// no section selected, open first one
		section = $('.section').first();
	}	
	setTimeout(function()
	{
		section.find('h3 a').click();
	},200);
}

// Navigation show/hide for timeout
hideNav = function()
{
	$('nav').find('li:not(.selected) a').stop(true,true).fadeOut(200);
}

sizeSection = function(section)
{
	
}

openSection = function(section)
{
	var height = $(section).children('.section-content').height() + $(section).children('.section-header').height() + 19;
	var hHeight = $(section).children('.section-header').height();
	$(section).data('totalHeight',height);
	$(section).find('.project.all').hide();
	var height = $(section).children('.section-content').height() + $(section).children('.section-header').height() + 19;
	
	// calculate heights for the papers section...
	if($(section).find('.show-papers').length)
	{
		$(section).find('.project').hide();
		$(section).find('.project.paper').show();
		var height = $(section).children('.section-content').height() + $(section).children('.section-header').height() + 19;
		$(section).data('paperHeight',height);
		
		$(section).find('.project').hide();
		$(section).find('.project.study').show();
		var height = $(section).children('.section-content').height() + $(section).children('.section-header').height() + 19;
		$(section).data('studyHeight',height);
		
		$(section).find('.project').hide();
		$(section).find('.project.press').show();
		var height = $(section).children('.section-content').height() + $(section).children('.section-header').height() + 19;
		$(section).data('pressHeight',height);
	}
	
	$(section).animate({'height':height},400,'easeOutExpo');
	$(section).data('minimisedHeight',height);
	$(section).find('.project').hide();
	$(section).children('.section-content').show();
	$(section).find('.project.featured').wave(40,400);
	$(section).find('.options').fadeIn(400);
	$(section).data('open',true);

	
	$(section).find('.view-all').click(function()
	{
		$(section).find('.featured-only').parent().removeClass('selected');
		$(this).parent().addClass('selected');
		$(section).find('.all').wave(40,400);
		$(section).animate({'height':$(section).data('totalHeight')},400,'easeOutExpo');
	});
	$(section).find('.featured-only').click(function()
	{
		$(section).find('.view-all').parent().removeClass('selected');
		$(this).parent().addClass('selected');
		$(section).find('.all').fadeOut();
		$(section).animate({'height':$(section).data('minimisedHeight')},400,'easeOutExpo');
	});
	
	if($(section).find('.show-papers').length)
	{
		var opn = $(section).find('.press');
		$(section).find('.show-papers').click(function()
		{
			$(section).find('.show-press,.show-studies').parent().removeClass('selected');
			$(this).parent().addClass('selected');
			opn.fadeOut(200,function()
			{
				$(section).find('.paper').wave(40,400);
				opn = $(section).find('.paper');
				$(section).stop().animate({'height':$(section).data('paperHeight')},400,'easeOutExpo');
			});
		});
		$(section).find('.show-press').click(function()
		{
			$(section).find('.show-papers,.show-studies').parent().removeClass('selected');
			$(this).parent().addClass('selected');
			opn.fadeOut(200,function()
			{
				$(section).find('.press').wave(40,400);
				opn = $(section).find('.press');
				$(section).stop().animate({'height':$(section).data('pressHeight')},400,'easeOutExpo');
			});
		});
		$(section).find('.show-studies').click(function()
		{
			$(section).find('.show-papers,.show-press').parent().removeClass('selected');
			$(this).parent().addClass('selected');
			opn.fadeOut(200,function()
			{
				$(section).find('.study').wave(40,400);
				opn = $(section).find('.study');
				$(section).stop().animate({'height':$(section).data('studyHeight')},400,'easeOutExpo');
			});
		});
	}
	
	
}

closeSection = function(section)
{
	var height = $(section).children('.section-header').height();
	$(section).animate({'height':height},400,'easeOutExpo');
	$(section).children('.section-content').fadeOut(400);
	$(section).find('.options').fadeOut(400);
	$(section).data('open',false);
}

// featured projects - services

initFeatured = function()
{
	
	$('.featured-project.click').hover(function()
	{
		$(this).css('cursor','pointer');	
	});
	$('.featured-project.click').click(function()
	{
		var link = $(this).find('a').attr('href');
		location = link;	
	});
	$('.section').each(function()
	{
		var sect = this;
		var starter = $(this).find('.starter');
		var selectedProject = $(this).find('.featured-project').first();
		selectedProject.addClass('selected');
		$(this).find('.related-project').click(function()
		{
			var isSkill = false;
			if($(this).hasClass('skill')) {
				var myProject = $(sect).find('.featured-skills');
				isSkill = true;
			} else {
				var myProject = $('#'+$(this).attr('id').replace('link-',''));
			}
			if(!myProject.hasClass('selected'))
			{
				myProject.fadeIn();
				if(!isSkill) {
					selectedProject.fadeOut('',function()
					{
						$(this).removeClass('selected');
						selectedProject = myProject;
						selectedProject.addClass('selected');
					});
					$(sect).find('.featured-skills').fadeOut();
				} else {
				
				}
			} else {
				$(sect).find('.featured-skills').fadeOut();
			}
		});
	});
	
}


// Plugins


