$(document).ready(function(){
	$('form input, form, textarea')
		.focusin(function(){
			thisId = $(this).attr('id');
			$('label[for='+thisId+']').addClass('invisible');
		})
		.focusout(function(){
			thisId = $(this).attr('id');
			if ($(this).val() == '') {
				$('label[for='+thisId+']').removeClass('invisible');
			}
		})
		
	$('a[href*=#]').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
        && location.hostname == this.hostname) {
            var $target = $(this.hash);
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
            if ($target.length) {
                var targetOffset = $target.offset().top;
                $('html,body').animate({scrollTop: targetOffset}, 1500);
                return false;
            }
        }
    });
    
    $('#noticias h2 a').click(function(){
    	if ($('#noticias').hasClass('closed')) {
    		openNoticias();
    	} else {
    		closeNoticias();
    	}
    })
    
    $('#noticias a.fechar').click(function(){
    	if (!$('#noticias').hasClass('closed')) {
	    	closeNoticias();
    	}
    });
    
    $('#metodo .fluxograma h3')
    	.hover(function(){
	    	$('#metodo .fluxograma div.active').removeClass('active');
    	}, function(){})
	    .click(function(){
	    	parentDiv = $(this).parents('div:first');
	
	    	$('#metodo .fluxograma div.active').removeClass('active');
	    	$(this).parents('div:first').toggleClass('active');
	    })
    
    $('#portifolio .clientes .lista a').click(loadClientPortifolio).filter(':first').click();
    $('.trabalhos a.linkSite').click(loadClientPortifolio);
    
    //prepareGalleryNavigation();
})

function loadClientPortifolio()
{
	tgt = $(this).attr('href').replace('#','');
	path = 'getCliente.php?id='+tgt;
	
	if ($(this).hasClass('linkSite')) $('nav .portifolio a').click();
	
	divDetalhe = $('#portifolio .clientes .detalhe');
	divDetalhe.html("<p class='loading'><img src='pub/img/loading.gif'></p>");
	divDetalhe.load(path, {}, function(){
		prepareGalleryNavigation();
		$('.galeria ul li a:first', divDetalhe).addClass('active');
	});
	
	return false;
}

function openNoticias()
{
	$('#noticias').removeClass('closed');
	$('#noticias').animate({
		left: '+=363'
	}, 500, function() {});
}

function closeNoticias()
{
	$('#noticias').addClass('closed');
	$('#noticias').animate({
		left: '-=363'
	}, 500, function() {
	});
}

function prepareGalleryNavigation()
{
    
    $('#portifolio .clientes .galeria img').load(hidePortifolioLoading);
	
	$('#portifolio .clientes .galeria a').click(function(){
		$('#portifolio .clientes .galeria a.active').removeClass('active');
		
    	imgSrc = $(this).attr('href');
    	targetElement = $('#portifolio .clientes .galeria img');
    	
    	if (imgSrc != targetElement.attr('src'))
    	{
    		$(this).addClass('active');
	    	showPortifolioLoading();
	    	$('#portifolio .clientes .galeria img').attr('src', imgSrc);
    	}
    	
    	return false;
    })
}

function showPortifolioLoading()
{
	$('#portifolio .clientes .detalhe').prepend("<p class='loading'><img src='pub/img/loading.gif'></p>");
}

function hidePortifolioLoading()
{
	$('#portifolio .clientes .detalhe .loading').remove();
}
