$(document).ready(function(){
    $.easing.elastic = function(x, t, b, c, d, s) {
        if (s == undefined) s = 0.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
    };
    $('img.lazy').lazyload({
        effect: "fadeIn"
    });

    $nome = $('span.nome');

    $input = $('#name');
    $input.select();

    $botao = $('#inicio').find('.button');

    $botao.click(function(){
        if ( $input.val() == "" || $input.val() == "Digite aqui" ){
            $input.addClass('error');
        }else{
            $nome.html( $input.val() );
        }
        return false;
    });

    $('#inicio form').submit(function(){
        return false;
    });

    $input.focus(function(){
        if (this.value == "Digite aqui"){
            this.value = "";
        }
    }).blur(function(){
        if (this.value == ""){
            this.value = "Digite aqui";
        }
    }).keyup(function(e){
        $(this).removeClass('error');
        if (e.keyCode == 13){
            $botao.click();
        }
    });

    $links = $('.menu a');

    $.localScroll({
        target: 'body',
        axis: 'x',
        duration: 1500,
        easing: 'elastic',
        filter: '.animated',
        onBefore: function(event, element, target){
            var id = $(element).attr('id');
            $links.removeClass('active').filter('.'+id).addClass('active');
            if ( $(element).hasClass('frame2') ){
                if ( $input.val() == "" || $input.val() == "Digite aqui" ){
                    return false;
                }
            }
        }
    });

    $modal = $('.modal');
    $gfoto = $('.gfoto');
    $gvideo = $('.gvideo');

    $cadastro = $('#modal_cadastro');

    $premiacao = $('#premiacao');

    $modal.hide();
    $cadastro.hide();

    $('a.modal_Gfoto').click(function(){
        $gfoto.show();
        block();
        return false;
    });

    $('a.modal_Gvideo').click(function(){
        $gvideo.show();
        block();
        return false;
    });

    $('.modal_close').click(function(){
        unblock();
        $modal.hide();
        return false;
    });

    $('.ver-video').click(function(){
        $gfoto.hide();
        $gvideo.show();
        return false;
    });

    $('.ver-fotos').click(function(){
        $gvideo.hide();
        $gfoto.show();
        return false;
    });

    $('.cadastrese').click(function(){
        block();
        $cadastro.fadeIn();
        return false;
    });

    $block = $('#block').css('opacity', 0);

	function block(){
        $block.stop().show().animate({
            opacity: 0.6
        }, 300 ).click(function(){
            unblock();
            $modal.hide();
            $cadastro.hide();
        });
    }

    function unblock(){
        $block.stop().animate({
            opacity: 0
        }, 300, function(){
           $(this).hide();
        });
    }

    $('#cpf').mask('999.999.999-99');
    $img = $('#modal_Gfoto').find('.foto_destaque').find('img');

    $('#slideshow').serialScroll({
        items:'li',
        prev:'#prev',
        next:'#next',
        offset:-150, //when scrolling to photo, stop 230 before reaching it (from the left)
        start:0, //as we are centering it, start at the 2nd
        duration:500,
        lazy:true,
        stop:true,
        lock:false,
        cycle:false, //don't pull back once you reach the end
        jump: true, //click on the images to scroll to them
        onBefore: function(event, target, element, itens, position){
            $targetImg = $(target).find('img');
            $img.fadeOut(function(){
                $img.attr('src', $targetImg.attr('src')).fadeIn();
            });
        }
    });

    //adiciona validacao para CPF válido
    jQuery.validator.addMethod(
        "cpf", function(value, element) {
            value = value.replace('.','');
            value = value.replace('.','');
            cpf = value.replace('-','');
            while(cpf.length < 11) cpf = "0"+ cpf;
            var expReg = /^0+$|^1+$|^2+$|^3+$|^4+$|^5+$|^6+$|^7+$|^8+$|^9+$/;
            var a = [];
            var b = new Number;
            var c = 11;
            for (i=0; i<11; i++){
                a[i] = cpf.charAt(i);
                if (i < 9) b += (a[i] * --c);
            }
            if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
            b = 0;
            c = 11;
            for (y=0; y<10; y++) b += (a[y] * c--);
            if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
            if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10]) || cpf.match(expReg)) return false;
            return true;
        }, "Informe um CPF válido."
    );

    $('#modal_cadastro').find('form').validate({
		onfocusout: false,
		onkeyup:false,

        highlight: function(element, errorClass) {
            if( $(element).attr('id') == 'captchastring' ){
                $('a.captcha').trigger('click');
            }
            $(element).addClass(errorClass);
        },
        errorLabelContainer: "#errorContainer",
        rules:{
            cpf: {
                required: true,
				cpf: true,
				remote: {
					url: 'lib/proxy.php?proxy_url=http://www.nc.sebrae-sc.com.br/portais/negociocerto/concurso/checkcpf.php',
					type: 'post',
					data: {
					  cpf: function() {
						return $("#cpf").val();
					  }
					}
				}
            },
            captchastring: {
				required: true,
				remote: "lib/validaCaptcha.php"
			}
        },
		messages:{
            cpf:{
                required:'Por favor informe seu CPF',
                cpf:'CPF inválido, tente novamente',
				remote:'CPF já cadastrado no concurso'

            },
            captchastring: 'Código não confere'
        },
		submitHandler: function(form) {
			$('#cpf').val($('#cpf').val().replace('.','').replace('.','').replace('-',''));
			form.submit();
		}



    });







});