$(function(){

/* ----- CUFON ----- */

	/* Replace slider headings */
	Cufon.replace('.nivo-info strong', {
		color: '-linear-gradient(#f6e77c, #ab6c06, #f6e77c, #ab6c06)',
		textShadow: '#000 1px 1px 1px',
		fontSize: '110px'
	});

	/* Replace inner page top info headings */
	Cufon.replace('#top-info h1, #top-info h2, #top-info h4', { fontSize: '42px' });
	$('#top-info h1, #top-info h2, #top-info h3, #top-info h4').css('letter-spacing', '-1px');
	
	/* Replace main page info blocks headings 	
	Cufon.replace('.info-box h2', { fontSize: '30px' });
	$('.info-box h2').css({
		lineHeight: "1em",
		fontSize: "20px"
	});
	*/
	Cufon.now();

	/* This function allow to set any line-height in cufon */
	function fixLineHeight($replaced, lineHeight, clearFloat) {
	
		$replaced.each(function(){
			var $current = $(this);
			if (clearFloat==null) {
				clearFloat = false;
			}
	
			var clearFloatClassname = 'fltbox';
			var $cufonWords = $current.children('cufon')
			var $cufonSiblings = $current.children(':not(cufon)');
	
			if ($(this).css('display')=='inline') {
				$cufonWords.css('display','inline-block');
			} else {
	
				$cufonWords.css('float','left');
				$cufonWords.css('display','inline');// IE floats margin bug
	
				$cufonSiblings.each(function(){
					$sib = $(this);
					if (!($sib.css('float')=='left' || $sib.css('float')=='right') ) {				
						$sib.css('float','left');			
						$sib.css('display','inline');// IE floats margin bug			
					}
				});
	
				if (clearFloat && !$current.hasClass(clearFloatClassname)) { //
					$current.addClass(clearFloatClassname);
				}
			}
	
			$current.css('line-height', lineHeight);
			$cufonWords.css('height', lineHeight);
		});
	}
	
	if ($.browser.mozilla && (parseFloat($.browser.version) < 1.9)) {
		$('.info-box h2').css({	lineHeight: "37px" });
	} else {
		fixLineHeight($('.info-box h2'),'37px');
	}



/* ----- NIVO SLIDER ----- */
	
	$('.info-content:first').show();
	
	var nSlider = $(".slider-container").nivoSlider({
		effect:'fade', //Specify sets like: 'fold,fade,sliceDown'
		slices:1,
		animSpeed:500,
		pauseTime:8000,
		startSlide:0, //Set starting Slide (0 index)
		directionNav:true, //Next & Prev
		directionNavHide:false, //Only show on hover
		controlNav:false, //1,2,3...
		controlNavThumbs:false, //Use thumbnails for Control Nav
		controlNavThumbsFromRel:false, //Use image rel for thumbs
		controlNavThumbsSearch: '.jpg', //Replace this with...
		controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
		keyboardNav:true, //Use left & right arrows
		pauseOnHover:true, //Stop animation while hovering
		manualAdvance:false, //Force manual transitions
		captionOpacity:1, //Universal caption opacity
		beforeChange: function(){
			var oldSlide = $(".slider-container img").index(nSlider.data("nivo:vars").currentImage);
			$('.info-content:eq(' + oldSlide + ')').fadeOut(500);
		},
		afterChange: function(){
			var newSlide = $(".slider-container img").index(nSlider.data("nivo:vars").currentImage);
			var imageParentTag = $('.slider-container img:eq(' + newSlide + ')').parent().get(0).tagName;
			if(imageParentTag == 'A') {
				var href = $('.slider-container img:eq(' + newSlide + ')').parent().attr('href');
				$('.whole-link').attr('href', href);
			} else {
				$('.whole-link').hide();
			}
		},
		slideshowEnd: function(){} //Triggers after all slides have been shown
	});



/* ----- IMAGES, GALLERIES ----- */

	/* catch alone images for prettyPhoto */
	$("a[href$='.jpg'], a[href$='.png'], a[href$='.gif']").attr("rel", "prettyPhoto");

	/* catch gallery images for prettyPhoto */
	$(".gallery, .gallery-large").each(function(i) {
		$("a[href$='.jpg'], a[href$='.png'], a[href$='.gif']", this).attr("rel", 'prettyPhoto[' + i + ']');
	});
	
	/* prettyPhoto initialization */
	$("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'light_rounded' /* light_rounded / dark_rounded / light_square / dark_square / facebook */
	});




/* ----- REORDER BLOG --------*/
	$('ul#lista-categoria a').live('click', function(){
		filterBlog($(this).attr('title'));
	});

	/*
    $('ul#lista-categoria a').click(function(e) {
      $.get( $(this).attr('title'), function(data) {
          $('div.content-filter').quicksand( $(data).find('li'), { adjustHeight: 'dynamic' } );
      });  
      e.preventDefault();  
    });
	*/
	
	$('#retorna-blog').click(function(){
		window.location.href= 'blog.php';
	});

/* ---- VALIDATION ---- */
	$("#contactForm").validationEngine({
		success :  function() {
				liberaForm('S');
		},
		failure : function() {
				liberaForm('N');
		}
	});

/* ---- CORNER ----*/
	/*$('.corner').corner(10);*/


/* ============================= FORMS ================================ */

	$('#celular_news').mask("99-9999-9999");
	
	$('#formCadastro').validationEngine({
		success :  function() {
				liberaCadNews('S');
		},
		failure : function() {
				liberaCadNews('N');
		}
	});


	function alignParceiros(){
		var wdt = '-' + ( $('div.parceiros').width() / 2) + 'px';
		$('div.parceiros').css({'left':'50%', 'margin-left': wdt}).fadeIn(1500);
		//alert(wdt);
	}
	alignParceiros()
	


});


function liberaCadNews(sts)
{
	var txt ='';
	
	if(sts == 'S')
	{
		$.jGrowl('Enviando cadastro! Aguarde...', { header: 'Processando Formulário:', life: 2000 });
			
		$.ajax({
			url: 'ajaxSubmit.php?action=cadNews',
			dataType: 'json',
			type: 'POST',
			data: $('#formCadastro').serialize(),
			
			success: function(data)
			{
				txt = (data.valido == 'S')?'Cadastrado com <b class="sucesso">sucesso</b>.' : 'Sua cadastro <b class="falha">não</b> foi enviado!';
				$.jGrowl(txt, { header: 'Processo:', sticky: true });
				(data.valido == 'N')? $.jGrowl(data.status, { header: 'Status:', sticky: true }) : false;
				setTimeout( function(){
					location.href = 'index.php';
				}, 5000);

			},
			
			error: function(data)
			{
				txt = 'Erro <b class="falha">AJAX</b>!! Sua mensagem <b class="falha">não</b> foi processada!';
				$.jGrowl(txt, { header: 'Status do Processo:', sticky: true });
				setTimeout( function(){
					location.href = 'index.php';
				}, 5000);
				
			}
		});
			
		
	}
	else
	{
		txt = 'Preencha todos os campos corretamente!';
		$.jGrowl(txt, { sticky: true });
	}

}


function liberaForm(sts)
{
//$.jGrowl("Sticky notification with a header", { header: 'A Header', sticky: true, life: 1000 });
	var txt ='';
	
	if(sts == 'S')
	{
		$.jGrowl('Sua mensagem está sendo enviada! Aguarde...', { header: 'Processando Formulário:', life: 2000 });
			
		$.ajax({
			url: 'ajaxSubmit.php?action=contact',
			dataType: 'json',
			type: 'POST',
			data: $('#contactForm').serialize(),
			
			success: function(data)
			{
				txt = (data.valido == 'S')?'Sua mensagem foi enviada com <b>sucesso</b>.' : 'Sua mensagem <b>não</b> foi enviada!';
				$.jGrowl(txt, { header: 'Status do Processo:', life: 5000 });
				$.jGrowl('Aguarde, reiniciando...', { sticky: true });
				setTimeout( function(){
					location.href = 'contact.php';
				}, 5000);

			},
			
			error: function(data)
			{
				txt = 'Erro <b>AJAX</b>!! Sua mensagem <b>não</b> foi processada!';
				$.jGrowl(txt, { header: 'Status do Processo:', life: 5000 });
				$.jGrowl('Aguarde, reiniciando...', { sticky: true });
				
				setTimeout( function(){
					location.href = 'contact.php';
				}, 5000);
				
			}
		});
			
		
	}
	else
	{
		txt = 'Preencha todos os campos corretamente!';
		$.jGrowl(txt, { sticky: true });
	}

}


function filterBlog(f){
	//alert(f);
	var filtro = '.' + f;
	
	if(filtro == '.all')
	{
		$('div.content-filter .post').slideDown(900);
	}
	else
	{
		$('div.content-filter .post').slideUp(900);
			$('div.content-filter').find(filtro).each(function(){
				$(this).slideDown(900);
		});
	}
}


