$(document).ready(function() {
	
	// jCarousel para o blog da home 
	$('#mycarousel').jcarousel({
		vertical: true,
		scroll: 2
	});
	////////////////////////////////
	
	// Ativa galeria de fotos e ou vídeos prettyPhoto
	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'dark_rounded'});	
	//////////////////////////////////////////
	
	// hover e click dos menus
	/*$('#nav1 li').hover(			
	function() {
		$(this).children('ul').show();
	},
	function() {
		$(this).children('ul').hide();
	});
	
	$('#nav1 li ul li').hover(			
	function() {
		$(this).children('a').css({color: '#000'});
	},
	function() {
		$(this).children('a').css({color: '#fff'});
	});
	
	
	if ($.browser.version == 532.5) {	
		$('.menor2').css({marginTop: '0px !important'});
		$('#cx_busca #form_busca2').css({marginTop: 40});
		$('#cx_cliente2').css({marginTop: 76});
	}
	
	$('#nav1 li').not('.agronegocio').click(function() {
		location = $(this).children('a').attr('href');
	});
	
	$('a.abre').click(function(e) {
		e.preventDefault();
	});
	*/
	///////////////////////////////
	
	// Slideshow	
	$('#cx_slide').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		speed:  1000, 
    	timeout: 3000,
		pager:  '#navslide'
	});
	///////////////////////////////
	
	// Cria as mascaras gerais
	$('<div id="mascara"></div>').css({position: 'absolute', top: 0, backgroundColor: '#000', zIndex: 20, opacity: 0.8, width:$(document).width(), height:$(document).height()}).appendTo('body').hide();
	///////////////////////////////
	
	// Busca futurista
	$('#futurista li a').click(function(e) {
		e.preventDefault();
		cont = $(this);
		$('#mascara').fadeIn(500);
		$.ajax({
			url: 'lib/futuristas.php',
			dataType: 'html',
			type: 'post',
			data: {futurista: $(cont).attr('href')},
			beforeSend: function() {
				$.alerts.dialogClass = 'futurista'; // set custom style class
				$.alerts.okButton = 'Fechar';
				jAlert('<img src="images/layout/ajax-loader.gif" id="busca1_load" style="margin-left:223px;" alt="" /><div id="resposta"></div>', $(cont).children('.titulo:first').text(), function() {										
					$.alerts.dialogClass = null; // reset to default
					$('#mascara').fadeOut(500);					
				});
			},
			complete: function() {
				$('#busca1_load').remove();
			},
			success: function(data, textStatus) {
				$('#popup_overlay').click(function(){
					$('#mascara').fadeOut(500);
					$.alerts._hide();
				});
				$('#resposta').html(data);
				
			},
			error: function(xhr, er) {
				jAlert('Error ' + xhr.status + ' - ' + xhr.statusText + ', Tente novamente', 'Atenção', function() {						
					$('#mascara').fadeOut(500);
				});
			}
		});
	});
	////////////////////////////////////////
	
	// Sistema de busca do topo
	$('#form_busca1').submit(function(e) {
		e.preventDefault();
		if ($('#busca1_cp').val().length < 3) {
			$('#mascara').fadeIn(800, function(){
				$.alerts.okButton = 'OK';
				jAlert('Sua busca deve conter pelo menos 3 caracteres!', 'Atenção', function() {
					$('#busca1_cp').focus();
					$('#mascara').fadeOut(800);
				});
				$('#popup_overlay').click(function(){
					$('#mascara').fadeOut(800);
					$.alerts._hide();
					$('#busca1_cp').focus();
				});
			});			
		}
		else {
			$('#mascara').fadeIn(1000);
			$.ajax({
				url: 'lib/busca1.php',
				dataType: 'html',
				type: 'post',
				data: {pesquisar: $('#busca1_cp').val()},
				beforeSend: function() {
					$.alerts.dialogClass = 'busca'; // set custom style class
					$.alerts.okButton = 'Fechar';
					jAlert('<img src="images/layout/ajax-loader.gif" id="busca1_load" style="margin-left:145px;" alt="" /><div id="resposta" style="width:350px"></div>', 'Buscado(s) com o termo: ' + $('#busca1_cp').val(), function() {					
						$.alerts.dialogClass = null; // reset to default
						$('#mascara').fadeOut(800);						
					});
				},
				complete: function() {
					$('#busca1_load').remove();
				},
				success: function(data, textStatus) {
					$('#popup_overlay').click(function(){
						$('#mascara').fadeOut(800);
						$.alerts._hide();
					});
					$('#resposta').html(data);
					
				},
				error: function(xhr, er) {
					jAlert('Error ' + xhr.status + ' - ' + xhr.statusText + ', Tente novamente', 'Atenção', function() {						
						$('#mascara').fadeOut(800);
					});
				}
			});
		}
	});
	///////////////////////////////
	
	
	// Sistema de busca direita
	$('#form_busca3').submit(function(e) {
		e.preventDefault();
		if ($('#bcliente3').val().length < 3) {
			$('#mascara').fadeIn(500, function(){
				$.alerts.okButton = 'OK';
				jAlert('Sua busca deve conter pelo menos 3 caracteres!', 'Atenção', function() {
					$('#bcliente3').focus();
					$('#mascara').fadeOut(500);
				});
				$('#popup_overlay').click(function(){
					$('#mascara').fadeOut(500);
					$.alerts._hide();
					$('#bcliente3').focus();
				});
			});			
		}
		else {
			$('#mascara').fadeIn(500);
			$.ajax({
				url: 'lib/busca2.php',
				dataType: 'html',
				type: 'post',
				data: {pesquisar: $('#bcliente3').val()},
				beforeSend: function() {
					$.alerts.dialogClass = 'busca'; // set custom style class
					$.alerts.okButton = 'Fechar';
					jAlert('<img src="images/layout/ajax-loader.gif" id="busca1_load" style="margin-left:145px;" alt="" /><div id="resposta" style="width:350px"></div>', 'Buscado(s) com o termo: ' + $('#bcliente3').val(), function() {					
						$.alerts.dialogClass = null; // reset to default
						$('#mascara').fadeOut(500);						
					});
				},
				complete: function() {
					$('#busca1_load').remove();
				},
				success: function(data, textStatus) {
					$('#popup_overlay').click(function(){
						$('#mascara').fadeOut(500);
						$.alerts._hide();
					});
					$('#resposta').html(data);
					
				},
				error: function(xhr, er) {
					jAlert('Error ' + xhr.status + ' - ' + xhr.statusText + ', Tente novamente', 'Atenção', function() {						
						$('#mascara').fadeOut(500);
					});
				}
			});
		}
	});
	///////////////////////////////
	
	// Combobox
	$("#bcliente1, #bcliente2").msDropDown();
	//////////////////////////////////////////
	
	$('#bcliente2_msdd').css({opacity: 0.3});	
				
	// Busca conteudo do campo2 da busca de conteudo
	$('#bcliente1_child a').live("click", function(e) {
		e.preventDefault();
		if ($(this).attr('rel') == '') {
			
			$('#bcliente2_msdd').css({opacity: 0.3});
			$('#bcliente2_titletext').text('');
			$('#bcliente2_child').empty();
			
		}
		else {
			
			$('#bcliente2_msdd').css({opacity: 1});
			$.ajax({
				url: 'lib/bpeca.php',
				dataType: 'html',
				type: 'post',
				data: {'campo1': $(this).attr('rel')},
				beforeSend: function() {
					$('#bcliente2').html('<option value="">Carregando...</option>');
					//$("#bcliente1, #bcliente2").msDropDown();
				},
				complete: function() {
					//$('.busca #bcampo2').empty();
				},
				success: function(data, textStatus) {
					//$('#bcliente2').html(data);
					if (data != '') {
						$('#bcliente2').html(data);
						$("#bcliente2").msDropDown();
					}
				},
				error: function(xhr, er) {
					$("#mascara").fadeIn(500, function(){
						jAlert('Error ' + xhr.status + ' - ' + xhr.statusText, "Atenção", function() {
							$("#mascara").fadeOut(500);		
						});
					});
				}
			});
			
		}		
	});
	// vai para o portfólio
	$('#bcliente2_child a').live("click", function(e) {
		e.preventDefault();
		location = $(this).attr('rel');								   
	});
	
	// Muda form contato para para solicite uma visita	
	$('.titulos6').live("click", function(e) {
		e.preventDefault();
		if ($('.titulos6 a').text() == 'Solicite uma visita') {
			$('.titulos6 a').text('Fale conosco');
				$('#form_contato').slideUp('slow', function() {
				$('#form_visita').slideDown('slow');										
			});	
		}
		else {
			$('.titulos6 a').text('Solicite uma visita');
			$('#form_visita').slideUp('slow', function() {
				$('#form_contato').slideDown('slow');									   
			});
		}
	});
	/////////////////////////////////
	
	// Envia contato
	$("#telefone").mask("(99) 9999-9999");
	$('#form_contato, #form_visita').submit(function(e){
		forms = $(this).attr('id');
		e.preventDefault();
		$.ajax({
			url: 'lib/' + forms +'.php',
			dataType: 'html',
			type: 'post',
			data: $(this).serialize(),
			beforeSend: function() {
				$('#'+forms+'_load').fadeIn(500);
			},
			complete: function() {
				$('#'+forms+'_load').fadeOut(500);
			},
			success: function(data, textStatus) {				
				if (data != 'enviado') {
					$("#mascara").fadeIn(500, function(){
						jAlert('O campo ' + data + ' deve ser preenchido corretamente!', "Atenção", function() {							
							$('#' + forms + ' #' + forms + '_' + data).focus();
							$("#mascara").fadeOut(500);		
						});
					});
				}				
				else {					
					$("#mascara").fadeIn(500, function(){
						jAlert("Mensagem enviada com sucesso!", "Atenção", function() {
							$("#mascara").fadeOut(500);
							$('#'+forms+' input[type="text"]').val('');
							$('#'+forms+' textarea').val('');
						});
					});
				}
				
			},
			error: function(xhr, er) {
				$("#mascara").fadeIn(500, function(){
					jAlert('Error ' + xhr.status + ' - ' + xhr.statusText, "Atenção", function() {
						$("#mascara").fadeOut(500);		
					});
				});
			}
		});
	});
	//////////////////////////////////////////
	
	// Envia seja um futurista
	$('#form_seja').submit(function(e){
		e.preventDefault();
		$.ajax({
			url: 'lib/mail_seja.php',
			dataType: 'html',
			type: 'post',
			data: $('#form_seja').serialize(),
			beforeSend: function() {
				$('#form_seja_load').fadeIn(500);
			},
			complete: function() {
				$('#form_seja_load').fadeOut(500);
			},
			success: function(data, textStatus) {				
				if (data != 'enviado') {
					$("#mascara").fadeIn(500, function(){
						jAlert('O campo ' + data + ' deve ser preenchido corretamente!', "Atenção", function() {
							if (data == 'cargo pretendido')
								$('#form_seja #carpre').focus();
							else
								$('#form_seja #' + data).focus();
							$("#mascara").fadeOut(500);		
						});
					});
				}				
				else {					
					$("#mascara").fadeIn(500, function(){
						jAlert("Mensagem enviada com sucesso!", "Atenção", function() {
							$("#mascara").fadeOut(500);
							$('#form_seja input[type="text"]').val('');
							$('#form_seja textarea').val('');
						});
					});
				}
				
			},
			error: function(xhr, er) {
				$("#mascara").fadeIn(500, function(){
					jAlert('Error ' + xhr.status + ' - ' + xhr.statusText, "Atenção", function() {
						$("#mascara").fadeOut(500);		
					});
				});
			}
		});
	});
	//////////////////////////////////////////
	
	// Busca img flickr
	requisitaFlickr = function() {
	iconCarregando = $('<img src="images/layout/ajax-loader.gif" class="icon" style="position:absolute; top:130px; left:140px;" />');
	  var numeroFotos = 6;
	  var url = 'http://api.flickr.com/services/feeds/photos_public.gne?id=43053115@N02&format=json&jsoncallback=?'
			  /*$('#resflickr').empty().ajaxStart(function() {
			  $(this).html(iconCarregando);
			  });*/
			  $('#resflickr').html(iconCarregando);
		  $.getJSON(url,
		  function(data) {
			  if ( (data.items).length == 0) {				  
			  $('#resflickr').empty();
			  $('#resflickr').html('Nenhuma foto encontra-se disponivel!'); 
			  return false;
			  };
		   $.each(data.items, function(i,obj){
		   $('<img class="fotos cx_img_flickr"/>').attr({ 
		   alt: obj.title,
		   src: obj.media.m.replace("_m.jpg", "_s.jpg"),
		   width: 68,  
		   height: 68//*3/4
		  }).appendTo('#resflickr').wrap('<a href="' + obj.link + '" target="_blank"></a>');
			  if ( i == numeroFotos-1 ) return false;
			});
		  });
		  $('#resflickr').ajaxComplete(function() {
		  $('.icon, .destaque', this).remove();
	  });
	}	
	//////////////////////////////////////////////
	
	// Google mapa
	$('#gmapa').hide().css({
		left:($(window).width()/2 - $('#gmapa').width() / 2),
		top:($(window).height()/2 - $('#gmapa').height() / 2)
	});
	
	$('#google').click(function(e) {
		e.preventDefault();
		$("#mascara").fadeIn(500, function(){
			$('#gmapa').slideDown('slow');
			loadMaps();
		});
		
	});
	
	$('#form_mapa').submit(function(e) {
		e.preventDefault();
		if ($('#form_mapa input[type="text"]').val() == '') {
			jAlert("O campo de endereço deve ser preenchido!", "Atenção", function() {
				$('#form_mapa input[type="text"]').focus();	
			});
		}
		else {
			tracarRota();
		}
	});
	
	$('#close1').click(function(e) {
		e.preventDefault();
		$('#gmapa').slideUp('slow');
		$('#route').empty();
		$("#mascara").fadeOut(500);
	});
	/////////////////////////////////////////////
	
	
}); // Fim do ready