$(document).ready(function() {
	var storeClick;
	var imgAlt;
	$('.thumb').mouseenter(function () {
		$(this).addClass('over');
		openThumb();
	});
	$('.thumb').mouseleave(function() {
		$('.thumb').removeClass('over');
		closeThumb();
	});
	
	$('.thumb').click(function() {
		storeClick = $(this).children().attr('src');
		imgAlt = $(this).children().attr('alt');
		clickThumb();
		
	});
	
	$('#large-image').mouseenter(function () {
		$('#large-caption').stop().animate({marginTop:'-55px'});
	});
	
	$('#large-image').mouseleave(function () {
		$('#large-caption').stop().animate({marginTop:'0px'});
	});
	
	function openThumb() {
		$('.thumb').stop().animate({
			'height':'75px'
		});
		if ( $('.thumb').hasClass("over") ){
			$('.over').stop().animate({
				'height':'150px'
			});
		} 	
	};
	
	function closeThumb() {
		$('.thumb').stop().animate({'height':'99px'})
	};
	
	function clickThumb() {		
		storeClick= storeClick.replace('small','large');
		$('#large-image a').children('img').fadeOut(500);
		setTimeout(loadLarge,500);
	};
	
	function loadLarge() {
		$('#large-caption').css({'display':'none'}).html(imgAlt);
		$('#large-image a').children('img').attr({'src':storeClick}).load(function(){
			$('#large-image a').children('img').fadeIn();
			$('#large-caption').css({'display':'block'});
		});
	};
	
	var portfolioWidth;
	var currLeft;
	var animation=0;
	
	$('.portfolio-slide').css({'top':'-135px'});
	$('.portfolio-slide').each(function() {
		var count = $(this).children('img').length;
		$(this).css({'width':(count*920)+'px'});
	});
	$('.portfolio-description p').hide();
	$('.portfolio-description p.read-more').show();
	$('.portfolio-item').each(function() {
		$(this).append('<div class="move-left"></div><div class="move-right"></div>');
		$(this).children('.portfolio-description').append('<p class="read-more">Read More >></p>');
	});
	
	$('.move-right').click(function(){
		portfolioWidth = $(this).parent().children('.portfolio-slide').width();
		currLeft = $(this).parent().children('.portfolio-slide').css('left').replace('px','');
		if(currLeft <= 0 && currLeft > -(portfolioWidth - 920) && animation==0){
			animation =	1;
			$(this).parent().children('.portfolio-slide').animate({'left':'-='+'920px'}, function() {
				animation =0;
			});
		} else {
			if(currLeft==-(portfolioWidth - 920) && animation==0){
			animation =	1;
			$(this).parent().children('.portfolio-slide').animate({'left':'0px'}, function() {
				animation =0;
			});
			}
		};
	});
	
	$('.move-left').click(function(){
		portfolioWidth = $(this).parent().children('.portfolio-slide').width();
		currLeft = $(this).parent().children('.portfolio-slide').css('left').replace('px','');
		if(currLeft < 0 && currLeft >= -(portfolioWidth - 920) && animation==0){
			animation =	1;
			$(this).parent().children('.portfolio-slide').animate({'left':'+='+'920px'}, function() {
				animation =0;
			});
		}else{
			if(currLeft==0 && animation==0){
			animation =	1;
			$(this).parent().children('.portfolio-slide').animate({'left':-(portfolioWidth - 920)+'px'}, function() {
				animation =0;
			});
			}
		};
	});
	
	$('.portfolio-item').mouseenter(function(){
		$(this).stop().animate({'height':'431px'}).children('div.portfolio-slide').stop().animate({'top':'0px'});
		$(this).children('.move-left').css({'opacity':'0'}).stop().animate({'height':'431px','opacity':'.8'});
		$(this).children('.move-right').css({'opacity':'0'}).stop().animate({'height':'431px','opacity':'.8'});
		$(this).children('.portfolio-description').children('h3').stop().animate({'marginTop':'0px'});
		$(this).children('.portfolio-description').children('p.read-more').fadeOut();
		$(this).children('.portfolio-description').children('p:first').fadeIn();
		$(this).children('.portfolio-description').stop().animate({
			'height':'60px',
			'width':'874px',
			'top':'341px',
			'left':'10px'
		});
	}).mouseleave(function() {
		$(this).stop().animate({'height':'120px'}).children('div.portfolio-slide').stop().animate({'top':'-135px'});
		$(this).children('.move-left').stop().animate({'height':'120px','opacity':'0'});
		$(this).children('.move-right').stop().animate({'height':'120px','opacity':'0'});
		$(this).children('.portfolio-description').children('h3').stop().animate({'marginTop':'18px'});
		$(this).children('.portfolio-description').children('p.read-more').fadeIn();
		$(this).children('.portfolio-description').children('p:first').fadeOut();
		$(this).children('.portfolio-description').stop().animate({
			'height':'80px',
			'width':'180px',
			'top':'10px',
			'left':'704px'
		});
	});
	
});
