var numImages = 0;
var currentImage = 0;
var prevActive = false;
var nextActive = false;
var overLink = false;
var overDrop = false;
var splashAnimSpeed = 3000;
var selectedTeam;
var textPage = 1;

var nextPage = function()
{
	$('#page-'+textPage).fadeOut('slow',function()
	{
		textPage++;
		$('#current-page').html(textPage);
		$('#page-'+textPage).fadeIn();
		$('#prevPage').show();
		// check for next page
		if(!$('#page-'+(textPage+1)).length) {
			$('#nextPage').hide();
			$('#pageSep').hide();
		} else {
			$('#pageSep').show();
		}
	});	
}

var prevPage = function()
{
	$('#page-'+textPage).fadeOut('slow',function()
	{
		textPage--;
		$('#current-page').html(textPage);
		$('#page-'+textPage).fadeIn();
		$('#nextPage').show();
		// check for next page
		if(!$('#page-'+(textPage-1)).length) {
			$('#prevPage').hide();
			$('#pageSep').hide();
		} else {
			$('#pageSep').show();
		}
	});	
}

scrollTeam = function(id)
{
	var o = $('#section-'+id).position();
	newLeft = -o.left;
	$('.team-gallery').animate({left:newLeft});
}

highlightMembers = function(path)
{
	// hide all first
	$('.blue').hide();
	$('.grey').show();
	var a = path.split('/');
	var h = a[a.length-1];
	$('.'+h).each(function()
	{
		$(this).children('.grey').hide();
		$(this).children('.blue').show();
	});
	selectedTeam = h;
}

addScroll = function(){
	$('li.selected ul li a').each(function()
	{
		var h = $(this).attr('href');
		for(var i=0;i < parent_items.length; i++)
		{
			if(parent_items[i][0] == h)
			{
				var id = parent_items[i][1];
				$(this).click(function(e)
				{
					e.preventDefault();
					scrollTeam(id);
					highlightMembers(h);
					return false;
				});
			}
		}
	});
}

check_form = function(f)
{
	if(f.name.value == '') { alert('Please enter your name'); return false; }
	if(f.email.value == '') { alert('Please enter your email'); return false; }
	return true;
}

var hide_gallery_nav = function()
{
	if($('.prev').is(':visible') && !overLink) $('.prev').fadeOut();
	if($('.next').is(':visible') && !overLink) $('.next').fadeOut();
}

var show_columns = function()
{
	$('.column').show('slide',{direction:'up',easing:'easeOutQuart'},500);
}

nextImage = function()
{

		$('.prev').fadeIn();
	var n = currentImage.next();
	if(n.length)
	{
	currentImage.animate({left:-970},500,'easeOutCirc');
	n.animate({left:0},500,'easeOutCirc',function()
	{
		currentImage = n;
		if(currentImage.is(':last-child')) $('.next').fadeOut();
	});
	}
}

prevImage = function()
{

		$('.next').fadeIn();
	var n = currentImage.prev();
	if(n.length)
	{
	currentImage.animate({left:970},500,'easeOutCirc');
	n.animate({left:0},500,'easeOutCirc',function()
	{
		currentImage = n;
		if(currentImage.is(':first-child')) $('.prev').fadeOut();
	});
	}
}

nextDropImage = function()
{
	//alert('showing nxt image');
	var timer = 500;
	$('.drop-banner').each(function()
	{
		var c = $(this).data('currentImage');
		var n = c.next('.drop-image');
		if(!n.length) n = $(this).children('.drop-image:first-child');
		//if(!c.next('.drop-image').length) n = $(this).children('.drop-image:first-child');
		//else n = c.next('.drop-image');
		//console.log(n.attr('class'));
		setTimeout(function() { c.fadeOut(700); }, timer);
		setTimeout(function() { n.fadeIn(700); }, timer);
		timer += 500;
		//console.log($(this).find('img').attr('src'));
		$(this).data('currentImage',n);
	});
	setTimeout('nextDropImage()',splashAnimSpeed)
}

nextNewsImage = function()
{
	//alert('showing nxt image');
	var timer = 500;
	$('.recent-project').each(function()
	{
	if($(this).children('.news-image').length > 1) {
		var c = $(this).data('currentImage');
		var n = c.next('.news-image');
		if(!n.length) n = $(this).children('.news-image').first();
		//if(!c.next('.drop-image').length) n = $(this).children('.drop-image:first-child');
		//else n = c.next('.drop-image');
		//console.log(c.attr('class'));
		setTimeout(function() { c.fadeOut(700); }, timer);
		setTimeout(function() { n.fadeIn(700); }, timer);
		timer += 500;

		$(this).data('currentImage',n);
	}
	});
	setTimeout('nextNewsImage()',splashAnimSpeed)
}

var show_member = function(id)
{
	$('#member-'+id).fadeIn();
}

var hide_member = function(id)
{
	$('#member-'+id).fadeOut();
}

var navHidden = false;

$(document).ready(function()
{
	//$('.column').css('display','none');
	//setTimeout('show_columns()',500);
	
	$('a.blank').each(function()
	{
		$(this).attr('target','_blank');
	});
	
	if($('.project .pagination').length)
	{
		$('.project').css('height',560);
	}
	
	$('.member').hover(function(){
		$(this).children('.member-desc').fadeIn();
		if(!$(this).hasClass(selectedTeam)) $(this).children('.blue').show();
	},function()
	{
		$(this).children('.member-desc').fadeOut();
		if(!$(this).hasClass(selectedTeam)) $(this).children('.blue').hide();
	});
	
	$('.drop-banner').each(function()
	{
		$(this).data('currentImage',$(this).children('.drop-image:first-child'));
	});
	
	setTimeout('nextDropImage()',splashAnimSpeed);
	
	$('.recent-project').each(function()
	{
		if($(this).children('.news-image').length > 1) {
			$(this).data('currentImage',$(this).children('.news-image').first());
		}
	});
	
	setTimeout('nextNewsImage()',splashAnimSpeed);
	
	
	$('#close-nav a').click(function()
	{
		if(!navHidden)
		{
			$('.column').slideUp();
			$(this).html('Show all navigation');
			$(this).css('background-image','url(/images/shownav.png)');
			$('.next,.prev').css('visibility','visible');
			navHidden = true;
		} else {
			$('.column').slideDown();
			$(this).html('Hide all navigation');
			$(this).css('background-image','url(/images/hidenav.png)');
			$('.next,.prev').css('visibility','hidden');
			navHidden = false;
		}
			
	});
	
	if($('.gallery').length)
	{
		$('.gallery').swipe({
			enableMouse:true,
			swipeLeft: function() { nextImage(); },
			swipeRight: function() { prevImage(); }
		});
	}
	
	$('#search-text').focus(function()
	{
		if($(this).attr('value') == 'Search') {
			$(this).attr('value','');
		}
	})
	.blur(function()
	{
		if($(this).attr('value') == '') {
			$(this).attr('value','Search');
		}
	});
	
	$('.drop-banner').mouseenter(function()
	{
		//$(this).children('.drop-text').show('slide',{direction:'up',easing:'easeOutQuad'},700);
		$(this).children('.drop-text').slideDown();
	}).mouseleave(function()
		{
			//$(this).children('.drop-text').stop().hide('slide',{direction:'up',easing:'easeOutQuart'},500);
			$(this).children('.drop-text').slideUp();
			
		
	});
	
	numImages = $('.gallery ul li').length;
	
	if(numImages > 1)
	{
		currentImage = $('.gallery ul li').first();
		$('.next').fadeIn();
	}
	
	if(numImages > 1)
	{
		prevActive = 1;
	}
	/*
	$('.gallery').mouseout(function()
	{
		setTimeout('hide_gallery_nav()',750);
	});
	*/
	// setup rollover show/hide
	/*
	$('.gallery').mousemove(function(e)
	{
		var offset = $(this).offset();
		var mousePos = e.pageX - offset.left;
		if(mousePos < 200 && !$('.prev').is(':visible') && !currentImage.is(':first-child'))
		{
			// show the previous option
			$('.prev').fadeIn();
		} else if(mousePos > 200 && $('.prev').is(':visible'))
		{
			// hide the previous option
			$('.prev').fadeOut();
		}
		
		if(mousePos > 770 && !$('.next').is(':visible') && !currentImage.is(':last-child'))
		{
			// show the previous option
			$('.next').fadeIn();
		} else if(mousePos < 770 && $('.next').is(':visible'))
		{
			// hide the previous option
			$('.next').fadeOut();
		}
	});
	*/
	$('.gallery ul li').each(function()
	{
		var i = $(this).children('img');
		if(!$(this).is(':first-child'))
		{
			$(this).css({
				position:'absolute',
				top:0,
				left:i.width()
			});
		} else {
		}
	});
	
	$('#thumbnails div.thumb').click(function()
	{
		var num = parseInt($(this).attr('id').replace('small-',''));
		var cnum = parseInt(currentImage.attr('id').replace('large-',''));
		var total = $('#thumbnails div').length;
		var l = $('#large-' + num);
		
		if(num > cnum) {
			currentImage.animate({left:-970},500,'easeOutCirc');
			l.css('left',970);
			l.animate({left:0},500,'easeOutCirc',function()
			{
				currentImage = l;
			});
		} else {
			currentImage.animate({left:970},500,'easeOutCirc');
			l.css('left',-970);
			l.animate({left:0},500,'easeOutCirc',function()
			{
				currentImage = l;
			});
		}
		
		if(num < total) $('.next').fadeIn();
		else $('.next').fadeOut();
		if(num > 1) $('.prev').fadeIn();
		else $('.prev').fadeOut();

	});
	
	currentGroup = $('.thumb-group').first();
	if($('.thumb-group').length > 1) $('.next-thumb').show();
	
	$('.next-thumb a').click(function()
	{
		var n = currentGroup.next('.thumb-group');
		if(n.length)
		{
			$('.prev-thumb').fadeIn();
			n.css('left',920);
			n.css('display','block');
			currentGroup.animate({left:-920},500,'easeOutQuad');
			n.animate({left:0},500,'easeOutQuad');
			currentGroup = n;
			if(!currentGroup.next('.thumb-group').length) $('.next-thumb').hide();
		}
	});
	
	$('.prev-thumb a').click(function()
	{
		var n = currentGroup.prev('.thumb-group');
		if(n.length)
		{
			$('.next-thumb').fadeIn();
			n.css('left',-920);
			n.css('display','block');
			currentGroup.animate({left:920},500,'easeOutQuad');
			n.animate({left:0},500,'easeOutQuad');
			currentGroup = n;
			if(!currentGroup.prev('.thumb-group').length) $('.prev-thumb').hide();
		}
	});
	
	$('.next,.prev').hover(function()
	{
		overLink = true;
	},function()
	{
		overLink = false;
	});
	
	$('.next a').click(function()
	{
		nextImage();
	});
	
	$('.prev a').click(function()
	{
		prevImage();
	});
	
});
