$(function() {

$('.parceiros').fancybox({ fitToView : false });
$('.imglight').fancybox();
$('.imgallery').fancybox({
	maxWidth	: 800,
	maxHeight	: 600,	
 });
 
$('#parallex-estrela').css('display','none');


/*------------------
  GALERIA
-------------------*/

var cat = [ ['Edifícios e condominios residenciais', 'galeria1.html'], ['Hotéis restaurantes, bares e similares' , 'galeria2.html'], ['Lojas' , 'galeria3.html'], ['Residenciais' , 'galeria4.html'] ];
var place = [ ['1º Lugar', ' #first'], ['2º Lugar', ' #second'], ['3º Lugar', ' #third'] ];
var pos_count = 0;
var cat_count = 0;
$('a.prev').fadeTo('fast', 0.1);

function loadgallery() {

	$('#slide').empty();
	$(document.createElement("div"))
  		.attr('class', 'slider')
  		.appendTo($('#slide')
  	);
  	
	$(".slider").load(cat[cat_count][1]+place[pos_count][1],function(){
	  $('.navigation').children('p').html(place[pos_count][0]+'<span>'+cat[cat_count][0]+'</span>');
	  $('.description').html($(this).find('span').text());
      $(this).easySlider();
  	});
}

$('.categorias li').click(function(e) {
	
	cat_count = $(this).children('a').attr('href').substring(1,2);
	pos_count = 0;
	loadgallery();
	
	$('.categorias li').removeClass('active');
  	$(this).addClass('active');

	e.preventDefault();
});

$('a.next').click(function() {
	
	if($(this).css('opacity') != 0.1) {
	  pos_count++;
	  loadgallery();
	  
	  $('a.prev').fadeTo('fast', 1);
	  
	  if(pos_count == place.length-1) {
		$(this).fadeTo('fast', 0.1);
	  } 
	 }
	 
	 e.preventDefault();
});

$('a.prev').click(function() {

	if($(this).css('opacity') != 0.1) {
	  pos_count--;
	  loadgallery();
	  
	  $('a.next').fadeTo('fast', 1);
	  
	  if(pos_count == 0) {
		$(this).fadeTo('fast', 0.1);
	  } 
	 }
		

	 e.preventDefault();

});


loadgallery();

/*------------------
  NAVEGAÇÃO
-------------------*/

	$(window).scroll(function(){
		var scrollTop = $(window).scrollTop();
		if(scrollTop != 0)
			$('nav').stop().animate({'opacity':'0.5'},400);
		else
			$('nav').stop().animate({'opacity':'1'},400);
	});

	$('nav').hover(
		function (e) {
			var scrollTop = $(window).scrollTop();
			if(scrollTop != 0){
				$('nav').stop().animate({'opacity':'1'},400);
			}
		},
		function (e) {
			var scrollTop = $(window).scrollTop();
			if(scrollTop != 0){
				$('nav').stop().animate({'opacity':'0.2'},400);
			}
		}
	);
	
	$('nav ul li a, .anchor').bind('click',function(event){
	
        var $anchor = $(this);
        
        $('html, body').stop().animate({
            scrollTop: $($anchor.attr('href')).offset().top
        }, 1500,'easeInOutExpo');
        
        event.preventDefault();
    });
    
    
/*------------------
  ABAS
-------------------*/

 $('.box').each(function() {
        var content = $(this).find('.content'),
            tab     = $('> ul li', this);
        $('div.section', content).eq(0).show();
        tab.click(function () {
            tab.removeClass('active');
            $(this).addClass('active');
            $('div.section', content).hide().eq($(this).index()).show();
            if($(this).hasClass('regulamento')) {
            	$('#parallex-estrela').css('display','none');
            } else {
            	$('#parallex-estrela').css('display','none');
            }
        });
    });
    
    
    

/*------------------
  SCROLLING
-------------------*/

	$('#parallex-bola').scrollingParallax({
	        staticSpeed : 1.6,
			staticScrollLimit : false,
			disableIE6 : true
	});
	 
	$('#parallex-bolasmall').scrollingParallax({
	        staticSpeed : 0.7,
			staticScrollLimit : false,
			disableIE6 : true
	});
	
	$('#parallex-fita').scrollingParallax({
	        staticSpeed : 1.1,
			staticScrollLimit : false,
			disableIE6 : true
	});
	
	$('#parallex-fita2').scrollingParallax({
	        staticSpeed : 1.1,
			staticScrollLimit : false,
			disableIE6 : true
	});
	
	$('#parallex-estrela').scrollingParallax({
	        staticSpeed : 1.1,
			staticScrollLimit : false,
			disableIE6 : true
	});
	
	$('#parallex-estrela2').scrollingParallax({
	        staticSpeed : 0.6,
			staticScrollLimit : false,
			disableIE6 : true
	});
	
	
/*------------------
  VALIDAÇÃO
-------------------*/

	$("#inscricao").validate({
    rules: {
      nome: "required",    
      email: {             
      	required: true,
      	email: true
      },
      rg: "required",
      endereco: "required",
      telefone: "required",
      fotos1: "required",
      fotos2: "required",
      fotos3: "required",
      fotos4: "required",
      termos: "required"
    },
    messages: {
    	nome: "Preencha o Nome",
      	rg: "Preencha o RG",
      	email: "Preencha o Email",
      	endereco: "Preencha seu endereço",
      	telefone: "Preencha seu telefone",
      	fotos1: "No minimo 4 fotos",
      	fotos2: "No minimo 4 fotos",
      	fotos3: "No minimo 4 fotos",
      	fotos4: "No minimo 4 fotos",
      	termos: ""
    },
    errorPlacement: function(error, element) {
		if ( element.is(":checkbox") )
			error.appendTo( element.parent());
		else 
			error.appendTo( element.parent());
	}
  });
  
 /*------------------
  VOTAÇÃO
-------------------*/

	function enqueteAjax($id_categoria, $id_inscrito) {

		$.ajax({
			url: 'ajaxVotacao.php',
			data: {
				'id_inscrito': $id_inscrito
			},
			dataType: 'html',
			type: 'post',
			success: function (r) {
				var c = $.parseJSON(r);
				
				if(c.status == 200) {
					alert('Voto computado com sucesso! Obrigado.');
					$('.categoria'+$id_categoria).addClass('disabled');
				}
				else {
					alert('Oops... Não conseguimos computar seu voto, tente novamente.');
					$('.categoria'+$id_categoria).removeClass('disabled');
					$.cookie('categoria'+$id_categoria, null);
				}
			}
		});
		
	}
	
	$('.votar').click(function () {
	
		var idcategoria = $(this).data('categoria');
		var idinscrito = $(this).data('id');
		
		if (!$.cookie('categoria'+idcategoria)) {
			$.cookie('categoria'+idcategoria, 'voted', {
				expires: 25,
				path: '/'
			});
			
			enqueteAjax(idcategoria, idinscrito);

		}
		
		return false;
		
	});

});

