$(document).ready(function(){

    var ketchupOn = false;

    function fieldRequired(form, el, value) {
        var type = el.attr('type').toLowerCase();

        if(type == 'checkbox' || type == 'radio') {
            return (el.attr('checked') == true);
        } else {
            return (value.length != 0);
        }
    }
    
    function fieldEmail(form, el, value) {
        return this.isEmail(value);
    }

    $.ketchup
        .validation('required_pt', 'Este campo é obrigatório.', 
            fieldRequired)
        .validation('required_es', 'Este campo es obligatorio.',
            fieldRequired)
        .validation('email_pt', 'Deve ser um email válido.', 
                fieldEmail)
        .validation('email_es', 'Debe ser un email válido.', 
                fieldEmail);

    $("form#contact-form")
        .bind('formIsInvalid', function(event, form) {
            ketchupOn = true;
        })
        .ketchup()
        .submit(function(ev) {
        
        });

	/*- Bubbles -*/
	$('#welcomeMsg').append('<span class="bubble" id="bubble1"></span><span class="bubble" id="bubble2"></span><span class="bubble" id="bubble3"></span><span class="bubble" id="bubble4"></span>');	

	/*- Current Seccion on Scroll -*/
	$(window).scroll(function() {    

        if (ketchupOn) {
            $(".ketchup-error").hide();
            ketchupOn = false;
        }

	    $('#menu li').removeClass('currentSection');
	    var scroll = $(window).scrollTop();
	    if (scroll <= 500) {
	        $('#menu li:first').addClass('currentSection');
	    }
	    else if (scroll <= 1200) {
	        $('#menu li:nth-child(2)').addClass('currentSection');
	    }
	    else if (scroll <= 3250) {
	        $('#menu li:nth-child(3)').addClass('currentSection');
	    }
	    else if (scroll <= 4200) {
	        $('#menu li:nth-child(4)').addClass('currentSection');
	    }
	    else {
	        $('#menu li:last').addClass('currentSection');
	    }
	});
	
	/*- Smooth scrolling -*/
	$('#menu a, .s-tools li a').click(function(e){
		var scrollAnchor = $(this).attr('href');
		e.preventDefault();
		$('html, body').animate({
			scrollTop: $(scrollAnchor).offset().top
		}, 900);
	});
	
	/*- Twitter polls Slider -*/
	$('#pollSlider').anythingSlider({
		autoPlay: false,
		animationTime: 800,
		easing: 'linear'
	});
	
	/*- Contact Lightbox -*/
	$('.contactCTA').click(function(e){
		e.preventDefault();
		$('#contactLbox').fadeIn();
	});

	$('.closeBtn').click(function(e){
		e.preventDefault();
        $('.ketchup-error').hide();
		$(this).parents('.lBox').fadeOut();
	});
	
	/*- External links -*/
	$('a[rel="external"]').attr('target', '_blank');
	
});

