jQuery(document).ready(function($) {
	
	// #### Prevent click on active buttons #### \\
	
	$('a.active:not(#portfolio)').click(function(e){
		e.preventDefault();
	});
		
	// #### Min Nav Animation #### \\
	
	$('.btn-portfolio').hover(
		function(){
			$('.btn-portfolio-anime').animate({ top: '29px' }, 300);
			$(this).css('cursor', 'pointer');
		}, function(){
			$('.btn-portfolio-anime').animate({ top: '75px' }, 300);
	}).click(function() {
		window.location = '/webv1/portfolio/';
	});
	
	$('.btn-contactos').hover(
		function(){
			$('.btn-contactos-anime').animate({ top: '27px' }, 300);
		}, function(){
			$('.btn-contactos-anime').animate({ top: '75px' }, 300);
	});
	
	$('.btn-home').hover(
		function(){
			if($('.btn-home.active').length > 0) {
				return;
			} else {
				$('.btn-home-anime').animate({ top: '18px' }, 300);
			}
			//$('.btn-home-anime').animate({ top: '18px' }, 300);
		}, function(){
			if($('.btn-home.active').length > 0) {
				return;				
			} else {
				$('.btn-home-anime').animate({ top: '85px' }, 300);
			}
	}); 
	
	$('.btn-home.active').parents().find('.btn-home-anime').css({ top: '18px' });
	
	
	// #### Homepage #### \\
	var modules = $('.modulo');
	var h = 0;
	
	var buildHome = function () {
		
		modules.eq(h++).slideDown(300, function() {
			buildHome();
		});
	}
	
	$(window).bind('load', function() {	
		buildHome();
	});
		
	// #### Portfolio Lista #### \\
	var items = $('.list-item');
	var totalItems = items.length;	
	var buildportfolioLista = function(k) {
		
		var k = k;
		$('.list-item').eq(k-1).css('margin-right', 0);
		
		$('.list-item:lt('+k+')').filter(':not(:visible)').css({'display':'block'}).animate({ opacity: 1 }, 100, function() {
        	$(this).find('img').animate({ top: 0 }, 200, function(){
        		buildportfolioLista(k+5);
        	});
        });
	}
	
	var buildportfolioFilter = function () {
		
		var category = window.location.hash.slice(2);
		var items = $('.list-item');
		var j = 0;
		
		var itemsToHide = items.not('.'+category+'');
		var totalItemsToHide = items.not('.'+category+'').size();
		
		var listItem = $('.list-item.'+category+'');
		
		var animeFilter = function(k) {
			
			var k = k;
			
			$('.list-item.'+category+':lt('+k+')').filter(':not(:visible)').each(function(i){
			
				$(this).css({'display':'block'}).animate({ opacity: 1 }, 100, function() {
	        		$(this).find('img').animate({ top: 0 }, 200, function(){
	        		
	        			if ((i+1) === 5){
							animeFilter(k+5);
							$(this).parent().css('margin-right', 0);
						}						
	        		});
	        	}).find('.overlay').attr('title', category);
	        	
	        	
	        });
	        
		}
		
		$('#sub-nav a.active').removeClass('active');
		$('#sub-nav .'+category+' a').addClass('active');
		$('#category').val(category);
		
		items.each(function(i){
			
			var $this = $(this);
			
			$this.animate({ opacity: 0 }, 200, function() {
			
				$this.css('display', 'none').find('img').css({'top' : '144px'});
				if ((i+1) === totalItemsToHide){
					animeFilter(5);
				}
			}).css({'margin-right' : '4px'});
		});	
	}
	//
	
	var category = window.location.hash;
		
	if (category){
		buildportfolioFilter();
	} else {
		buildportfolioLista(5);
	}
	
	
	//
	$('#sub-nav a').click(function(e){
		e.preventDefault();
		
		var $this = $(this);
		var filter = $this.attr('href');
		
		$('#category').val(filter);
		window.location = '/webv1/portfolio/#/'+filter+'';
	});
	
	//
	$(window).bind('hashchange', function() {
		
		buildportfolioFilter();
	});
	//
	$('.list-item').each(function(){
		var $this = $(this);
		var overlay = $this.find('.overlay');
		
		$this.hover(
			function(){	
		 		overlay.animate({ top: 0, opacity: 1 }, 300); 
			},
			function(){
				overlay.animate({ top: '144px' }, 300);
			}
		);
	});
	//
	$('.overlay').click(function(){

		var _this = $(this);
		var url = _this.find('h2 a').attr('href');
		var _filter = _this.attr('title');
		
		if (_filter != '') {
			
			window.location = url+'#/'+_filter;

		} else {
			_filter = _this.parent().attr('class').split(' ')[2];
			window.location = url +'#/'+ _filter;
		}
	});
	
	
	// #### Portfolio Detalhe #### \\
	
	// add active class to selected category
	var category = window.location.hash.slice(2);
	
	if(category){		
		$('#sub-nav .'+category+' a').addClass('active');
	}
	
	// set prev project
	var prev = $('#none-'+category+' a[rel="next"]').attr('href');
	
	if (prev != undefined) {
		$('.wrap-nav .voltar').after('<li class="prev right"><a href="'+prev+'#/'+category+'" target="_self"><span>Anterior</span></a></li>');
	}
	
	// set next project
	var next = $('#none-'+category+' a[rel="prev"]').attr('href');
	
	if (next != undefined ) {
		$('.wrap-nav .voltar').after('<li class="next right"><a href="'+next+'#/'+category+'" target="_self"><span>Seguinte</span></a></li>');
	}
	
	//
	$('.voltar a').click(function(e){
		e.preventDefault();
		
		var category = $('#category').val();
		
		window.location = '/webv1/portfolio/#/'+category+'';
		// change when goes live window.location = '/portfolio/#/'+category+'';
		
	});
	
	// set name when roolover btns
	
	$('.wrap-nav ul a').each(function(){
		var $this = $(this);
		
		$this.hover(
			function(){
				var className = $this.parent().attr('class');
		 		
		 		$this.stop().animate({ opacity: 1 }, 300);
		 		
		 		if ($this.parent().hasClass('prev')) {
		 			$('#feedback').text('Projecto Anterior');
		 		} else if ($this.parent().hasClass('next')) {
		 			$('#feedback').text('Projecto Seguinte');
		 		} else {
		 			$('#feedback').text('Voltar aos Projectos');
		 		}
		 		
			},
			function(){
				$('#feedback').text('Projecto Seguinte');
				$this.stop().animate({ opacity: 0 }, 300);
			}
		);
	}).animate({ opacity: 0 }, 50);
	//
	
	
	//
	
});
