$(document).ready(function() {
	
	
	/* =TAB CLICK
	-------------------------------------------------------------- */	
	
	$('.portfolio-intro .trigger').click(function(ev) {
		ev.preventDefault();
		var target = $('.pf-'+$(this).attr('href').substr(1));
		
		if(!target.hasClass('top') && !$('.portfolio-list').is(':animated')) {
			$('.portfolio-intro .trigger.down').removeClass('down');
			$('.portfolio-lists .top').fadeOut(300, function() {
				$(this).removeClass('top');
				target.fadeIn(300, function() {
					$(this).addClass('top');
				});
			});
			$(this).addClass('down');
		}
	});
	
	
	/* =LOAD PORTFOLIO-LISTING
	-------------------------------------------------------------- */	
	
	$('.portfolio-list ul a, .selected-work-wrapper a').click(function(ev) {
		ev.preventDefault();
		
		if(!$(this).hasClass('active')) {
			var id = $(this).attr('rel');
			
			$('.portfolio-list a.active').removeClass('active');
			$(this).addClass('active');
			
			$.ajax({
				async: false,
				url: "/v6/what/renderPFTitle.php?c="+id,
				success: function(data) {
					$('.portfolio-intro .trigger.down').removeClass('down');
					$('.portfolio-list.top').fadeOut(300, function() {
						$(this).removeClass('top');
						
						$('.anchor-portfolio .portfolio-title').html(data);
						$('.anchor-portfolio .portfolio-title').fadeIn(300, function() {
							$(this).addClass('top');
						});
					});
				}
			});
			
			if($('.pf-listing').length > 0) {
				$('.pf-listing').children('.zorro').animate({'opacity':'1'}, 300, function() {
					$('.pf-listing').remove();
					loadPortfolio(id);
				});
			} else {
				loadPortfolio(id);
			}
		}
	});
		
		function loadPortfolio(id) {
			$.ajax({
				async: false,
				url: "/v6/what/renderPortfolio.php?c="+id,
				success: function(data) {
					$('.anchor-portfolio').after(data);
					$('.pf-loading .zorro').css({'opacity':'1'});
				}
			});
			
			$('.pf-loading').removeClass('pf-loading').children('.zorro').animate({'opacity':'0.7'}, 300);
			
			enableSlideshow();
		}
	
});


function enableSlideshow() {

	/*******************************************
	 * Project Slideshows
	 ******************************************/

	var lock,
		count = 1,
		adjust = 0,
		scrollStart = 0,
		scrollBottom = 0,
		imageScroll,
		captions,
		imgHeight = 353;
	
	$('#gallery-wrapper').addClass('js-enabled');
	
	$('.slideshow-panel .buttons').delegate('a', 'click', function(ev) {
		ev.preventDefault();
		
		imageScroll = $(this).parent('.buttons').parent('.slideshow-panel').siblings('.image-scroll');
		if (!lock) {
			lock = true;
			// arrow up
			if ($(this).hasClass('slide-up')) {
				// if at the top, 'bounce' the slide
				if (parseInt($(imageScroll).css('top')) == 0) {
					$(imageScroll).animate({'top':'40px'}, 150, function() {
						$(this).animate({'top':'0'}, 400, function() {
							lock = false;
						});
					});
				} else {
					$(imageScroll).animate({'top':(parseInt($(imageScroll).css('top')) + imgHeight)+'px'}, 400, function() {            
						count = parseInt($(this).siblings('.slideshow-panel').find('.count').html()) - 1;
						$(this).siblings('.slideshow-panel').find('.count').html(count);
						captions = $(this).siblings('.slideshow-panel').find('p.caption');
						changeCaption(captions, (count - 1));
						lock = false;
					});
					
				}
			// arrow down
			} else if ($(this).hasClass('slide-down')) {
				scrollBottom = ($(imageScroll).find('img').length - 1) * (-1 * imgHeight);
				
				// if at the bottom, 'bounce' the slide
				if (parseInt($(imageScroll).css('top')) == scrollBottom) {
					$(imageScroll).animate({'top':(scrollBottom - 40)+'px'}, 150, function() {
						$(this).animate({'top':scrollBottom+'px'}, 400, function() {
							lock = false;
						});
					});
				} else {
					$(imageScroll).animate({'top':(parseInt($(imageScroll).css('top')) - imgHeight)+'px'}, 400, function() {
						count = parseInt($(this).siblings('.slideshow-panel').find('.count').html()) + 1;
						$(this).siblings('.slideshow-panel').find('.count').html(count);
						captions = $(this).siblings('.slideshow-panel').find('p.caption');
						changeCaption(captions, (count - 1));
						lock = false;
					});
				}
			}
		}
	});
	
		function changeCaption(cap, cnt) {
			$(cap).each(function() {
				if ($(this).hasClass('top')) {
					$(this).animate({'opacity':'0'}, 150, function() {
						$(this).removeClass('top');
						$(cap).eq(cnt).animate({'opacity':'1'}, 150, function() {
							$(this).addClass('top');
						});
					});
				}
			});
		}
	
		
	/*******************************************
	 * Draggable slideshow
	 ******************************************/
	
	var scrollTop;
	
	$('.image-scroll').addClass('ui-draggable');
	
	$('.image-scroll').draggable({ axis: 'y' ,
		start: function(event, ui) {
			scrollStart = parseInt($(this).css('top'));
		} , stop: function(event, ui) {
			scrollTop = parseInt($(this).css('top'));
			
			// if moving up or down
			if (scrollTop > scrollStart) {
				adjust = (Math.floor((scrollTop + 308) / imgHeight)) * imgHeight;
			} else {
				adjust = (Math.floor((scrollTop + 80) / imgHeight)) * imgHeight;
			}
			
			// if over the top or bottom
			if (adjust > 0) {
				adjust = 0;
			} else if (adjust <= ($(this).find('img').length * (-1 * imgHeight))) {
				adjust = ($(this).find('img').length - 1) * (-1 * imgHeight);
			}
			
			// 'snap' to the 'closest' slide
			$(this).animate({'top':(adjust)+'px'}, 400, function() {
				count = (parseInt($(this).css('top')) / (-1 * imgHeight)) + 1;
				if (count != parseInt($(this).siblings('.slideshow-panel').find('.count').html())) {
					$(this).siblings('.slideshow-panel').find('.count').html(count);
					captions = $(this).siblings('.slideshow-panel').find('p.caption');
					changeCaption(captions, (count - 1));
				}
			});
		}
	});
	
}
