function is_valid_email (email){
    return /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i .test(email);
}


jQuery(function(){
    jQuery('#homeFade .images').each(function(){

        var that = jQuery(this);
        var nImgs = jQuery('img',that).length;
        var to = 1;
        var ft = 1000;

        function fade(t){
            jQuery('img',that).fadeOut(ft);
            jQuery('img:eq('+t+')',that).fadeIn(ft);
            to++;
        }

        jQuery('img:eq(0)',that).fadeIn(ft);
        window.setInterval(function(){
            fade(to%nImgs);
        },6000);

    });

    jQuery('#submitHomeForm').click(function(){

        switch(true){
            case (jQuery('#hNome').val()==''):
                alert('compilare il campo nome');
                return false;
                break;
            case (jQuery('#hCognome').val()==''):
                alert('compilare il campo cognome');
                return false;
                break;
            case (!is_valid_email(jQuery('#hEmail').val())):
                alert('inserire un indirizzo email valido');
                return false;
                break;
            case (!jQuery('#hPrivacy').attr('checked')):
                alert('prendere visione note sulla privacy');
                return false;
                break;
        }

        jQuery('#homeForm form').submit();
        return true;
    });

    jQuery('#popup').each(function(){

        var that = jQuery(this);
        that
        .before('<div id="popupOverlay"></div>')
        .wrap('<div id="popupWrapper"></div>')
        .show();
        var wr = jQuery('#popupWrapper');
        var ov = jQuery('#popupOverlay');
        wr.css({
            position: 'absolute',
            top: '100px',
            left: parseInt((parseInt(jQuery(document).width()) - 1041) / 2)+'px',
            opacity: 1,
            zIndex: 6000,
            cursor: 'pointer'
        })
        .hide()
        .click(function(){
            wr.slideUp(1000,function(){
                ov.fadeOut(1000);
                jQuery('#hNome').focus();
            });
        });
        ov.css({
            position: 'absolute',
            top: 0,
            left: 0,
            width: jQuery(document).width(),
            height: jQuery(document).height(),
            zIndex: 5000,
            opacity: 0.8,
            background: '#000000'
        });//.hide();

        ov.fadeIn(1000,function(){
            wr.slideDown(1000);
        });

    });

});
