
$().ready(function(){
	$('.gallery').each(function(){
		if($('.gallery-images img', this).length > 0)
		{
			$(this).gallery();
		}else{
			$(this).hide();
		}
	});
	$('small').hide();
	$('iframe').attr('width', '810');
	$('iframe').attr('height', '600');
	
	$('#portfolio-select').change(function(){
		search = $(this).val();
		if($(this).val() != "")
		{
			$('.portfolio-portfoliothumb').hide();
			$('.portfolio-portfoliothumb').each(function(){
				var tags = $(this).attr('tags');
				if(tags.search(search) != -1)
				{
					$(this).show();
				}
			});
		}else{
			$('.portfolio-portfoliothumb').show();
		}
		
	});
});

$.fn.gallery = function(){
	return this.each(function(){
		var obj = $(this);
		var current = 1;
		$('.gallery-images img', obj).hide();
		$('.gallery-images img:nth-child(1)', obj).show();
		$('.gallery-caption', obj).html($('.gallery-images img:nth-child(1)', obj).attr('alt'));
		var c = $('.gallery-images img', obj).length;
		$('.gallery-images img', obj).each(function(){
			if(c==$('.gallery-images img', obj).length)
			{	$('.gallery-controls', obj).html('<a rel="'+c+'" class="gallery-num" href="#">'+c+'</a>'+$('.gallery-controls', obj).html());
				
			}else{
				if(c == 1)
				{
					$('.gallery-controls', obj).html('<a rel="'+c+'" class="gallery-num current" href="#">'+c+'</a> - '+$('.gallery-controls', obj).html());
				}else{
					$('.gallery-controls', obj).html('<a rel="'+c+'" class="gallery-num" href="#">'+c+'</a> - '+$('.gallery-controls', obj).html());
				}
			}
			c--;
		});
		$('.gallery-controls', obj).html('<img src="/resources/LeftArrow.jpg" class="leftarrow" />Jump to image: '+$('.gallery-controls', obj).html()+'<img src="/resources/RightArrow.jpg" class="rightarrow" /><br /><img src="/resources/Play.png" class="play" />');
		$('.gallery a', obj).removeClass('current');
		$('.gallery-controls a:nth-child(1)', obj).addClass('current');
		
		$('.gallery-images img:nth-child(1)', obj).load(function(){
			$('.gallery-images', obj).css('height', $(this).height()+'px');
		});
		
		
		$('.gallery-num', obj).click(function(){
			var n = $(this).html();
			$('.gallery-images img', obj).fadeOut(1000);
			$('.gallery-images img:nth-child('+$(this).html()+')', obj).fadeIn(1000);
			$('.gallery-images', obj).animate({height: $('.gallery-images img:nth-child('+$(this).html()+')', obj).height()}, 1000);
			$('.gallery-controls a', obj).removeClass('current');
			$(this).addClass('current');
			$('.gallery-caption', obj).html($('.gallery-images img:nth-child('+current+')', obj).attr('alt'));
			current = $(this).html();
		});
		
		$('.gallery-controls .leftarrow', obj).click(function(){
			if(current != 1)
			{
				current--;
				$('.gallery-images img', obj).fadeOut(1000);
				$('.gallery-images img:nth-child('+current+')', obj).fadeIn(1000);
				$('.gallery-images', obj).animate({height: $('.gallery-images img:nth-child('+current+')', obj).height()}, 1000);
				$('.gallery-controls a', obj).removeClass('current');
				$('.gallery-controls a[rel="'+current+'"]', obj).addClass('current');
				$('.gallery-caption', obj).html($('.gallery-images img:nth-child('+current+')', obj).attr('alt'));
			}
			
		});
		
		$('.gallery-controls .rightarrow', obj).click(function(){
			if(current != $('.gallery-images img', obj).length)
			{
				current++;
				$('.gallery-images img', obj).fadeOut(1000);
				$('.gallery-images img:nth-child('+current+')', obj).fadeIn(1000);
				$('.gallery-images', obj).animate({height: $('.gallery-images img:nth-child('+current+')', obj).height()}, 1000);
				$('.gallery-controls a', obj).removeClass('current');
				$('.gallery-controls a[rel="'+current+'"]', obj).addClass('current');
				$('.gallery-caption', obj).html($('.gallery-images img:nth-child('+current+')', obj).attr('alt'));
			}
			
		});
		
		
		$('.play', obj).click(function(){
			$('body').prepend('<div id="gallery-overlay"><div id="gallery-overlay-holder"><img src="/resources/Close.png" id="overlay-close" /></div></div>');
			$('#gallery-overlay-holder').append('<div id="gallery-overlay-title"></div>');
			$('#gallery-overlay-holder').append('<div id="gallery-overlay-c"><div id="gallery-overlay-cl"><img id="overlay-leftarrow" src="/resources/LeftArrow2.jpg" /></div><div id="gallery-overlay-cc"></div><div id="gallery-overlay-cr"><img id="overlay-rightarrow" src="/resources/RightArrow2.jpg" /></div></div>');
			
			$('#gallery-overlay').css('height', $('html').height()+80).hide().fadeIn();
			
			
			var imagehtml = $('.gallery-images', obj).html();
			$('#gallery-overlay-cc').html(imagehtml);
			$('#gallery-overlay-cc img').each(function(){
				src = $(this).attr('src');
				src = src.replace('w/390', 'w/810');
				src = src.replace('w/522', 'w/810');
				src = $(this).attr('src', src);
			});
			$('#gallery-overlay-cc img').hide();
			$('#gallery-overlay-cc img:nth-child(1)').show();
			$('#gallery-overlay-cc img:nth-child(1)').load(function(){
				$('#gallery-overlay-cc').css('height', $('#gallery-overlay-cc img:nth-child(1)').height()+'px');
				$('#overlay-leftarrow').animate({'margin-top': ($('#gallery-overlay-cc img:nth-child(1)').height() / 2) - 17 +'px'}, 600);
				$('#overlay-rightarrow').animate({'margin-top': ($('#gallery-overlay-cc img:nth-child(1)').height() / 2) - 17 +'px'}, 600);
			});
			$('#gallery-overlay-holder').append('<div id="gallery-overlay-caption">'+$('#gallery-overlay-cc img:nth-child(1)').attr('alt')+'</div>');
			$('#gallery-overlay-holder').append('<div id="gallery-overlay-controls"></div>');
			$('#gallery-overlay-holder').append('<img id="gallery-overlay-playback" src="/resources/Play.png" alt="" />');
			$('#gallery-overlay-playback').hide();
			var c2 = $('#gallery-overlay-cc img').length;
			
			$('#gallery-overlay-cc img').each(function(){
				if(c2==$('#gallery-overlay-cc img').length)
				{	
					$('#gallery-overlay-controls').html('<a rel="'+c2+'" class="gallery-num" href="#">'+c2+'</a>'+$('#gallery-overlay-controls').html());
				}else{
					if(c2 == 1)
					{
						$('#gallery-overlay-controls').html('Jump to image: <a rel="'+c2+'" class="gallery-num current" href="#">'+c2+'</a> - '+$('#gallery-overlay-controls').html());
					}else{
						$('#gallery-overlay-controls').html('<a rel="'+c2+'" class="gallery-num" href="#">'+c2+'</a> - '+$('#gallery-overlay-controls').html());
					}
				}
				c2--;
			});
			
			$('#gallery-overlay-controls .gallery-num').click(function(){
				current2 = $(this).html();
				$('#gallery-overlay-cc img').fadeOut(1000);
				$('#gallery-overlay-cc img:nth-child('+current2+')').fadeIn(1000);
				$('#gallery-overlay-cc').animate({height: $('#gallery-overlay-cc img:nth-child('+current2+')').height()}, 600);
				$('#gallery-overlay-controls a').removeClass('current');
				$('#gallery-overlay-controls a[rel="'+current2+'"]').addClass('current');
				$('#overlay-leftarrow').animate({'margin-top': ($('#gallery-overlay-cc img:nth-child('+current2+')').height() / 2) - 17 +'px'}, 600);
				$('#overlay-rightarrow').animate({'margin-top': ($('#gallery-overlay-cc img:nth-child('+current2+')').height() / 2) - 17 +'px'}, 600);
				$('#gallery-overlay-caption').html($('#gallery-overlay-cc img:nth-child('+current2+')').attr('alt'));
				$('#gallery-overlay-playback').fadeIn();
				clearInterval(timing);
			});
			
			var current2 = 1;
			$('#overlay-leftarrow').click(function(){
				if(current2 != 1)
				{
					current2--;
					$('#gallery-overlay-cc img').fadeOut(1000);
					$('#gallery-overlay-cc img:nth-child('+current2+')').fadeIn(1000);
					$('#gallery-overlay-cc').animate({height: $('#gallery-overlay-cc img:nth-child('+current2+')').height()}, 600);
					$('#gallery-overlay-controls a').removeClass('current');
					$('#gallery-overlay-controls a[rel="'+current2+'"]').addClass('current');
					$('#overlay-leftarrow').animate({'margin-top': ($('#gallery-overlay-cc img:nth-child('+current2+')').height() / 2) - 17 +'px'}, 600);
					$('#overlay-rightarrow').animate({'margin-top': ($('#gallery-overlay-cc img:nth-child('+current2+')').height() / 2) - 17 +'px'}, 600);
					$('#gallery-overlay-caption').html($('#gallery-overlay-cc img:nth-child('+current2+')').attr('alt'));
					$('#gallery-overlay-playback').fadeIn();
					clearInterval(timing);
					//$('.gallery-caption', obj).html($('.gallery-images img:nth-child('+current+')', obj).attr('alt'));
				}
				
			});
			
			$('#overlay-rightarrow').click(function(){
				if(current2 != $('.gallery-images img', obj).length)
				{
					current2++;
				}else{
					current2 = 1;
				}
				$('#gallery-overlay-cc img').fadeOut(1000);
				$('#gallery-overlay-cc img:nth-child('+current2+')').fadeIn(1000);
				$('#gallery-overlay-cc').animate({height: $('#gallery-overlay-cc img:nth-child('+current2+')').height()}, 600);
				$('#gallery-overlay-controls a').removeClass('current');
				$('#gallery-overlay-controls a[rel="'+current2+'"]').addClass('current');
				$('#overlay-leftarrow').animate({'margin-top': ($('#gallery-overlay-cc img:nth-child('+current2+')').height() / 2) - 17 +'px'}, 600);
				$('#overlay-rightarrow').animate({'margin-top': ($('#gallery-overlay-cc img:nth-child('+current2+')').height() / 2) - 17 +'px'}, 600);
				$('#gallery-overlay-caption').html($('#gallery-overlay-cc img:nth-child('+current2+')').attr('alt'));
				$('#gallery-overlay-playback').fadeIn();
				clearInterval(timing);
				//$('.gallery-caption', obj).html($('.gallery-images img:nth-child('+current+')', obj).attr('alt'));
			});
			
			var timing;
			
			$('#gallery-overlay-playback').click(function(){
				timing = setInterval(function(){
					if(current2 != $('.gallery-images img', obj).length)
					{
						current2++;
					}else{
						current2 = 1;
					}
					$('#gallery-overlay-cc img').fadeOut(1000);
					$('#gallery-overlay-cc img:nth-child('+current2+')').fadeIn(1000);
					$('#gallery-overlay-cc').animate({height: $('#gallery-overlay-cc img:nth-child('+current2+')').height()}, 600);
					
					$('#gallery-overlay-controls a').removeClass('current');
					$('#overlay-leftarrow').animate({'margin-top': ($('#gallery-overlay-cc img:nth-child('+current2+')').height() / 2) - 17 +'px'}, 600);
					$('#overlay-rightarrow').animate({'margin-top': ($('#gallery-overlay-cc img:nth-child('+current2+')').height() / 2) - 17 +'px'}, 600);
					$('#gallery-overlay-caption').html($('#gallery-overlay-cc img:nth-child('+current2+')').attr('alt'));
					$('#gallery-overlay-controls a[rel="'+current2+'"]').addClass('current');
				}, 5000);
				$('#gallery-overlay-playback').fadeOut();
			});
			
			$('#gallery-overlay-playback').trigger('click');
			
			
			$('#overlay-close').click(function(){
				clearInterval(timing);
				$('#gallery-overlay').fadeOut('slow', function(){
					$('#gallery-overlay').remove();
				});
			});
			
		});
		
	});
}
