$(function(){
	
	$('a[rel=external]').attr('target','_blank');
	
	if($('article').length){
		$('body').addClass('blur');	
	}

	$('article').animate({top:0},1500);

	$('.floater').click(function(){
		$(this).fadeOut();
	})

	$('#form-contato').submit(function(e){
		if($(this).find('input#name').val() != '' && $(this).find('input#email').val() != '' && $(this).find('input#phone').val() != '' && $(this).find('textarea#message').val() != ''){
			$.post('process.php',$('#form-contato').serialize(),function(){
				alert('Mensagem enviada com sucesso.');
				$(this)[0].reset();
			});	
		}else{
			alert('Todos os campos são obrigatórios!');
		}
		e.preventDefault();
		e.preventDefault();
	})

	//mapas
	$('.enderecos a').click(function(e){
		var path = $(this).attr('href');
		$('.mapa iframe').attr('src',path);
		path = path.slice(0,-13);
		$('.mapa a[rel=external]').attr('href',path);
		e.preventDefault();
	})
	
	//galeria
	if($('.full-page-gallery')[0]){
		
		var infoProdutos = [];
		var categoriaSelecionada = $('#categoria_produto option:selected').val();

		$('#categoria_produto').change(function(){
			window.location  = 'http://www.estudiocaju.com.br/demo/gmag/produtos/?id='+$(this).val();
		})
		
		$.getJSON('produtos.php', function(data) {
			for(var i = 0;i<data.produtos.length;i++){
				if(data.produtos[i].categoria == categoriaSelecionada){
					infoProdutos.push(data.produtos[i]);
				}
			}
		});

		$('.full-page-gallery .ampliar').attr('target','_blank');

		function fotos(carousel) {
			jQuery('#proximas-fotos').bind('click', function() {
				carousel.next();
				return false;
			});
			jQuery('#fotos-anteriores').bind('click', function() {
				carousel.prev();
				return false;
			});
		};
		$(document).ready(function(){
			jQuery("#thumbs").jcarousel({scroll:6,initCallback:fotos,buttonNextHTML:null,buttonPrevHTML:null});
		})
		$('.full-page-gallery #thumbs a').click(function(e){

			var helper = $(this).attr('href');
			$('.full-page-gallery .thumbs span').hide();
			$(this).parent().find('span').show();
			$('.full-page-gallery .crop img').attr('src',helper);
			//helper = helper.replace("media-", "grande-");
			//helper = helper.replace(".png",".jpg");
			//$('.full-page-gallery .ampliar').attr('href',helper);
			
			//recuperando as informaceos do produto no json
			var indice = parseInt($(this).attr('rel')) - 1;
			$('.full-page-gallery .ampliar').attr('href',infoProdutos[indice].grande);
			var nome = infoProdutos[indice].nome;
			nome = nome.toUpperCase();
			$('.titulo-produtos').html(nome);
			$('.informacoes-produto span.altura').html(infoProdutos[indice].catacteristicas);
			// $('.informacoes-produto span.comprimento').html(infoProdutos[indice].comprimento);
			// $('.informacoes-produto span.profundidade').html(infoProdutos[indice].profundidade);
			
			e.preventDefault();
		})
	}

	//solicitar-orcamento
	$('.solicitar-orcamento').click(function(){
		$('#form-solicitar-orcamento').fadeIn();
	})

	$('#form-solicitar-orcamento .close').click(function(){
		$('#form-solicitar-orcamento').fadeOut();
	})

	
	$('#form-solicitar-orcamento').submit(function(e){
		if($(this).find('input#name').val() != '' && $(this).find('input#email').val() != '' && $(this).find('input#phone').val() != ''){
			$.post('process.php',$('#form-solicitar-orcamento').serialize(),function(){
				alert('Solicitação enviada com sucesso.');
				$('#form-solicitar-orcamento').fadeOut();
				$(this)[0].reset();
			});	
		}else{
			alert('Todos os campos são obrigatórios!');
		}
		e.preventDefault();
	})

	//controle do encosto
	$('.forracao-encosto a').click(function(e){
		
		var nome_encosto = $(this).prop('rel');
		var novo_encosto = $(this).prop('href');
		$('#encosto img').attr('src',novo_encosto);
		
		$('input#nome-encosto').val(nome_encosto);
		
		e.preventDefault();
	})

	//controle do acento
	$('.forracao-acento a').click(function(e){
		var nome_acento = $(this).prop('rel');
		var novo_acento = $(this).prop('href');
		$('#acento img').attr('src',novo_acento);

		$('input#nome-acento').val(nome_acento);
		
		e.preventDefault();
	})
	
	//controle do sofa
	$('.thumbs-sofas a').click(function(e){
		
		$('input#nome-encosto,input#nome-acento').val('');
		$('h2.sofa-selecionado').html($(this).prop('title'));
		
		var path_atual = $('.forracao-encosto a').prop('rel');
		var novo_path = $(this).prop('rel')
		var novo_encosto = $(this).prop('href');
		$('#acento img, #encosto img').attr('src','../assets/images/misc/blank.gif');

		$('#sofa, #acento, #encosto').attr('class',novo_path);

		$('.forracao-encosto a').each(function(i){
			i++;
			$(this).attr('href','../assets/images/sofas/textures/top/'+novo_path+'/'+i+'.png');
		})

		$('.forracao-acento a').each(function(i){
			i++;
			$(this).attr('href','../assets/images/sofas/textures/bottom/'+novo_path+'/'+i+'.png');	
		})
		
		$('#sofa img').attr('src','../assets/images/misc/blank.gif');
		$('#sofa img').attr('src',novo_encosto);
		$('input#nome-sofa').val($(this).prop('title'));
		
		e.preventDefault();
	})
});

