var $j = jQuery.noConflict();
$j('document').ready(function() 
{                          
	// ----------------------------------------------------------------------------------------------------------------
	// FILTRO
	
	$j("#filtro-cliente").slideToggle(0);
	
	$j(".labelfiltro").hover(
		function () {
			$j(this).fadeTo('fast', 0.5);
		}, 
		function () {
			$j(this).fadeTo('fast', 1);
		}
	);
	
	// OPEN / CLOSE
	$j(".labelfiltro").click(
		function () {
			var idfiltro = $j(this).attr("id");
			var areaFiltro = idfiltro.substr(6);
			$j("#filtro-" + areaFiltro).slideToggle('fast', function(){
					$j("#label-" + areaFiltro).toggleClass("labelfiltro-up");
			});
		}
	);
	
	
	function disableClick()
	{
		$j("#filtro").animate({opacity: 0.2}, 200);
		$j("#ver_todos_projectos").unbind('click');
		$j(".filtro-content a").unbind('click');
	}
	
	function enableClick()
	{
		$j("#filtro").animate({opacity: 1}, 200);
			
		// SELECT TODOS
		$j("#ver_todos_projectos").click(
			function () {
				disableClick();
				nItems = nItemsInit;
				$j(".list_item").addClass("item_activo");
				onFiltroItem();
			}
		);
		
		// SELECT FILTRO
		$j(".filtro-content a").click(
			function () {
				disableClick();
				var toSearch = $j(this).text();
				onFiltroClick(unformatString(toSearch));
			}
		);
	}
	
	enableClick();
	
	// FILTRAR
	function onFiltroClick(keyword)
	{
		// Vars
		var query = $j("#mask_items").find("."+keyword);
		nItems = query.size();
		
		// esconde todos
		$j(".list_item").removeClass("item_activo");
		
		// Mostra filtrados
		query.addClass("item_activo");
		
		// Update Nav Proximos/anteriores
		onFiltroItem();
	}
	
	// ------------------------------------------------------------------------------------------------------------
	// Navegação Prev/Next
	
	jQuery.easing.def = "easeInOutQuint";
	
	// Quantidade de Items
	var maxItems = 10;
	var nItems = $j(".list_item").size();
	var nItemsInit = nItems;
	    
	
	// ------------------------------------------------------------------------------------------------------------
	// Intro List Item
	$j(".list_item").addClass("item_activo");
	$j(".list_item").width(0);
	$j(".list_item").animate({opacity: 0.6}, 1);
	
	var totalToLoad = 10;
	
	function onFiltroItem()
	{
		if(nItems > maxItems){
			totalToLoad = maxItems;
		}else{
			totalToLoad = nItems;
		}
		
		var inc = 0;
		$j(".item_activo").each(function(){
				if(inc < totalToLoad)
				{
					$j(this).stop(true, true).animate({width: 0, opacity: 0.6}, 1);
					$j(this).stop(true, true).delay(100 * inc).animate({width: $j(this).find("a").width() + 90 }, 1000);
				}else{
					$j(this).animate({opacity: 0.6, width: $j(this).find("a").width() + 90 }, 1);
				}
				inc++;
		});
		
		setTimeout(enableClick, 1000 + (100 * nItems));
		 
        // Activa Navegação
		if(nItems > totalToLoad)
		{
			// Estado Inicial das setas
			$j("#prev_nav").animate({opacity: 0.4}, 100);
			
			// Mostra Botões   
			$j("#prev_nav").css("visibility", "visible");
			$j("#next_nav").css("visibility", "visible");
		
			 
		}else{
			$j("#prev_nav").css("visibility", "hidden");
			$j("#next_nav").css("visibility", "hidden");
		}
		
		
		$j(".list_item").hide();
		$j(".item_activo").show();
	}
	
	onFiltroItem();
	 
	// ------------------------------------------------------------------------------------------------------------
	
	// Valor de referência
	var h = 400;
	var i = 0;
	var max = Math.floor((nItems-1) / maxItems) * -1;
			
	// Prev
	$j("#prev_nav").click(
		function () {
			if(i < 0){ i++; }
			$j("#list_items").animate({marginTop: h * i}, 500, function()
			{
				$j("#next_nav").animate({opacity: 1}, 100);
				if(i == 0){
					$j("#prev_nav").animate({opacity: 0.4}, 100);   	
				}else{
					$j("#prev_nav").animate({opacity: 1}, 100);
				}
			});
		}
	);
	
	// Next
	$j("#next_nav").click(
		function () {
			if(i > max){ i--; }
			$j("#list_items").animate({marginTop: h * i}, 500, function()
			{
				$j("#prev_nav").animate({opacity: 1}, 100);
				if(i == max){
					$j("#next_nav").animate({opacity: 0.4}, 100);   	
				}else{
					$j("#next_nav").animate({opacity: 1}, 100);
				}   	
			});
		}
	);
	
	
	// ------------------------------------------------------------------------------------------------------------
	// Hover List Item
	
	var largura = 0;
	$j(".list_item a").animate({textShadow: "#000 0px 0px 0"}, 1);
	
	$j(".list_item").hover(
		function ()
		{
			// Guarda largura inicial
			largura = $j(this).find("a span").width() + 90;
		
			// Expande barra
			$j(this).stop(true, true).animate({opacity: 1, width: '660px', height: '60px' }, 300);
			
			// Expande link
			$j(this).find("a").stop(true, true).animate({width: '570px', height: '40px', paddingTop: '20px', fontSize: 20, textShadow: "#000 1px 1px 0"}, 300);			   
		}, 
		function ()
		{
			// Recolhe barra
			$j(this).animate({opacity: 0.6, width: largura, height: '40px'}, 300);
			
			// Recolhe link
			$j(this).find("a").animate({width: largura-90, height: '30px', paddingTop: '10px', fontSize: 15, textShadow: "#000 0px 0px 0"}, 300);
		}
	);
	
	/*
	$j(".list_item").hover(
		function ()
		{
			// Inactiva novo hover até este estar terminado
			$j(this).unbind('mouseenter');
			
			// Guarda largura inicial
			largura = $j(this).find("a span").width() + 90;
			// Expande barra
			$j(this).animate({opacity: 1, width: '660px', height: '60px' }, 300);
			// Expande link
			$j(this).find("a").animate({width: '570px', height: '40px', paddingTop: '20px', fontSize: 20, textShadow: "#000 1px 1px 0"}, 300);
		   
		}, 
		function ()
		{
			// Recolhe barra
			$j(this).animate({opacity: 0.6, width: largura, height: '40px'}, 300, function(e)
			{
				$j(e).bind('mouseenter');  	
			});
			// Recolhe link
			$j(this).find("a").animate({width: largura-90, height: '30px', paddingTop: '10px', fontSize: 15, textShadow: "#000 0px 0px 0"}, 300);
		}
	);
	*/
	
	
	// Expande Mask
	$j("#mask_items").hover(
		function (){
			//if(nItems > maxItems){
				$j("#mask_items").animate({height: '420px'}, 300);
			//}
		}, 
		function (){
			//if(nItems > maxItems){
				$j("#mask_items").stop(true, true).animate({height: '400px'}, 300);
			//}
		}
	);
	
	
	// ------------------------------------------------------------------------------------------------------------
	// Novidades
	
	// Verifica quantos slides existem
	contentSlides = $j(".slideshow-content");
	contentSlides.each(function(i){
			totalHeight += this.clientHeight;
			totalSlides++;
	});
  
	// Adapta a altura do holder e do scroller
	$j("#slideshow-holder").height(totalHeight);
	$j("#slideshow-scroller").attr({scrollLeft: 0});
  
	// Navegação
	$j("#slideshow-previous").click(function() {
			clearInterval(alertTimerId);
			showPreviousSlide();
	});
	$j("#slideshow-next").click(function() {
			clearInterval(alertTimerId);
			showNextSlide();
	});
	
	$j(".slideshow-content a").hover(
		function () {
			clearInterval(alertTimerId);
		}, 
		function () {
			alertTimerId = setInterval("showNextSlide()", slideshowTimer );
		}
	);
});


// ------------------------------------------------------------------------------------------------------------
// Novidades
var totalSlides = 0;
var currentSlide = 1;
var contentSlides = "";
var totalHeight = 0;
var slideshowTimer = 4000;
var speed = 300;
var alertTimerId;

	
		
// Intervalo para loop
alertTimerId = setInterval("showNextSlide()", slideshowTimer );

// PREVIOUS
function showPreviousSlide()
{
	currentSlide--;
	if(currentSlide < 1){
	  currentSlide = totalSlides;
	}
	updateContentHolder();
};

// NEXT
function showNextSlide()
{
	currentSlide++;
	if(currentSlide > totalSlides){
	  currentSlide = 1;
	}
	updateContentHolder();
};

// TRANSIÇÃO
function updateContentHolder()
{
	var scrollAmount = 0;
	contentSlides.each(function(i){
	if(currentSlide - 1 > i) {
	  scrollAmount += this.clientHeight;
	}
	});
	$j("#slideshow-scroller").animate({scrollTop: scrollAmount}, {duration: 'slow', easing: 'easeOutQuad'});
};


