jQuery(function( $ ){	
	$('#portfolio ul#thumbs').jcarousel();
	
	
	$('#portfolio ul#thumbs li a').click(function() {
		var parent = $(this).parent('li');
		var siblings = $('ul#thumbs').children('li');
		var lrg = $(this).attr('title');
			
		if ( !$(parent).hasClass('current') ){ 
			// add current class to selected item
			$(siblings).removeClass('current');
			$(parent).addClass('current');
		
			// find matching large image
			$('#portfolio #large img').fadeOut(250).each(function() {
				$('#large h3').fadeOut(250);
				if ( $(this).parent('div').attr('id') == lrg ) {
					var self = this;
					var title = $(this).parent('div').children('h3:eq(0)');
					var source = $(this).attr('rel');
					
					setTimeout(function() {
						$(self).attr('src', source).fadeIn();
						$(title).animate({ 
						        opacity: 0.8,
						        height: "40px"
						      }, 1500 );
						
					}, 1250);
				}
			});
		}
		
		if ( $('ul#thumbs li:first').hasClass('current') ) {
			$('#portfolio p.controls a#prev').fadeOut(100);
		}
		else if ( $('ul#thumbs li:last').hasClass('current') ) {
			$('#portfolio p.controls a#next').fadeOut(100);
		}
		else { 
			$('#portfolio p.controls a').show();
		}
		
		return false;
	});
	$('#portfolio ul#thumbs li:eq(0) a').click();
	
	$('#portfolio p.controls a').click(function() {
		var self = $('ul#thumbs li.current').attr('jcarouselindex');
		var temp = parseInt(self);
		var next = (temp + 1);
		var prev = (temp - 1);

		if ( $(this).attr('id') == 'next' ) {
			$('ul#thumbs li').each(function() {
				if ( $(this).attr('jcarouselindex') == next )
					$('a', this).click();
			});
		}
		else if ( $(this).attr('id') == 'prev' ) {
			$('ul#thumbs li').each(function() {
				if ( $(this).attr('jcarouselindex') == prev )
					$('a', this).click();
			});
		}
		
		return false;
	});
});