$(document).foundation();

$(document).ready(function(){

	var request, PATH = baseUrl();



	//Mobnav

	$('.fi-list').click(function(){

		var m = $('.menu').css('display');



		if(m == "none") {

			$('.menu').slideDown('fast');

			$('.header').css({

				height: '100%',

				top: '0',

				'overflow-y': 'auto'

			})

		} else {

			$('.menu').slideUp('fast');

			$('.header').css({

				height: '80px',

				'overflow-y': 'inherit'

			})

		}

	});



	//Nav Scroll

	var w = $(window).width();



	$('#main li a, a.pipo').click(function(){

		var d = $(this).data('sec');



		if(w >= 750) {

			$('html, body').animate({

				'scrollTop': $('#'+d).offset().top-60

			}, 1000, 'easeInOutQuint');

		} else if(w <= 749) {

			$('html, body').animate({

				'scrollTop': $('#'+d).offset().top-60

			}, 1000, 'easeInOutQuint');



			$('.menu').fadeOut('fast');

			$('.header').css({

				height: '80px',

				'overflow-y': 'inherit'

			})

		}

	});



	//Header fix

	$(window).scroll(function() {

		var t = $(this).width();



	    if ($(this).scrollTop() > 150) {

	        $('header').addClass('fix');

	    } else {

	        $('header').removeClass('fix');

	    }

	});



	//Instagram

	var w = $(window).width();



	if(w > 900) {

		var limit = 15;

		var iw = '20%';

	} else if(w > 640 && w < 800) {

		var limit = 12;

		var iw = '50%';

	} else if(w > 300 && w < 600) {

		var limit = 6;

		var iw = '50%';

	}



	$(window).resize(function(){

		var w = $(window).width();



		if(w > 900) {

			var limit = 15;

			var iw = '20%';

		} else if(w > 640 && w < 800) {

			var limit = 12;

			var iw = '50%';

		} else if(w > 300 && w < 600) {

			var limit = 6;

			var iw = '50%';

		}



		$('.il-item').css('width', iw);

	});



	$('.instagram-lite').instagramLite({

	    clientID: '24b27711273a45869866405d4733b552',

	    username: 'pipoderani',
		
		vlimit : limit,
	    limit: limit + 10,

	    videos: false,

	    list: true,

	    urls: true,

	    success: function(){

	    	$('.il-item').css('width', iw);

	    }

	});



	//News

	$('.opennews').click(function(){

		var d = $(this).data('id');



		$.ajax({

			type: "POST",  

			data: {id:d},

			url: PATH+"noticias/",

			success: function(data){

				$('#cont-noticia').html(data);

			}

		});

	});



	$("#vermais-noticias").on("click", function() {

	    	var $data = { offset: $('.itemnoticia').length };



	    	$.ajax({

	    		"url":baseUrl()+"ver-mais-noticias/",

	    		"data": $data,

	    		// "async": false,

	    		"dataType": "json",

	    		"type": "POST",

	    		"success": function( data ) {

	    			var itens = $( data.divs ).css("display","none");

	    			itens.appendTo( "#news_row" ).slideDown(400);



	    			if (data.botao == 0){

	    				$('.vermais-noticias').css("display","none");

	    			}

	    		}

	    	});

	    });



	//Gallery

	$('.gal_thumbs img').click(function(){

		var d = $(this).data('zoom');



		$('#modgallery img').attr('src', d);

	});



	//videos

	$('.assistir_video').click(function(){

		var d = $(this).data('video');

		$('#video-pipo').html('<iframe width="853%" height="480" frameborder="0" src="http://www.youtube.com/embed/'+d+'" allowfullscreen></iframe>' );

	});



	//Resultados

	$('.results').click(function(){

		$('.tab1').fadeOut('fast', function(){

			$('.tab2').fadeIn('fast');

		});

	});



	$('.back').click(function(){

		$('.tab2').fadeOut('fast', function(){

			$('.tab1').fadeIn('fast');

		});

	});



	//Owl Carousel

	var owl = $('.owl-main');

	var owl2 = $('.owl-achievements');

	var owl3 = $('.owl-videos');

	var owl4 = $('.owl-parceiros');



	owl.owlCarousel({

	    loop: true,

	    autoplay: true,

	    animateOut: 'fadeOut',

        items: 1,

        dots: true,

        margin: 0,

        responsiveClass: true

	});



	owl2.owlCarousel({

	    loop: false,

	    autoplay: false,

	    animateOut: 'fadeOut',

        dots: true,

        margin: 15,

        responsiveClass: true,



        autoHeight : true,

        responsive: {

        	0: {

        		items: 1

        	},

        	700: {

        		items: 4

        	}

        }

	});



	owl3.owlCarousel({

	    loop: false,

	    autoplay: false,

	    animateOut: 'fadeOut',

        dots: true,

        margin: 15,

        responsiveClass: true,

        autoHeight : true,

        responsive: {

        	0: {

        		items: 1

        	},

        	700: {

        		items: 4

        	}

        }

	});



	owl4.owlCarousel({

	    loop: false,

	    autoplay: false,

	    animateOut: 'fadeOut',

        dots: true,

        margin: 15,

        responsiveClass: true,

        responsive: {

        	0: {

        		items: 1

        	},

        	700: {

        		items: 3

        	}

        }

	});



	//Validate

	var obrigatorio;

	var valida_email;

	if (window.is_pt == true){

		obrigatorio = 'Obrigatório';

		valida_email = 'E-mail inválido';

	} else {

		obrigatorio = 'Required';

		valida_email = 'Invalid email address';

	}

	$("#formNewsletter").validate({

		messages:{

			iptNome:{

				required: obrigatorio,

			},

			iptMail:{

				required: obrigatorio,

				email: valida_email,

			},

			txtMsg:{

				required: obrigatorio,

			}

		},

		submitHandler: function(form) {

			var dados = $( form ).serialize();

			

			$.ajax({

				type: "POST",  

				data: dados,

				url: PATH+"newsletter",

				success: function(data){

					$('#alert-newsletter').html(data.msg);

					$('#alert-newsletter').fadeIn(1000);

					setTimeout(function() {

					      $('#alert-newsletter').fadeOut(1000);

					}, 5000);

					$(form)[0].reset();

				}

			});

		}

	});



	$("#formContato").validate({

		messages:{

			slcAssunto:{

				required: obrigatorio,

			},

			iptNome:{

				required: obrigatorio,

			},

			iptMail:{

				required: obrigatorio,

				email: valida_email,

			},

			txtMsg:{

				required: obrigatorio,

			}

		},

		submitHandler: function(form) {

			var dados = $( form ).serialize();

			

			$.ajax({

				type: "POST",  

				data: dados,

				url: PATH+"enviar-contato",

				success: function(data){

					$('#alert-contato').html(data.msg);

					$('#alert-contato').fadeIn('fast');

					setTimeout(function() {

					      $('#alert-contato').fadeOut(1000);

					}, 5000);

					$(form)[0].reset();

				}

			});

		}

	});



});



function baseUrl() {

	var pathname = location.pathname.split('/');

	var url = [ window.location.protocol, '//', window.location.host, '/', pathname[1], '/' ];

	return url.join('');

}