$(document).ready(function(){
    $('#form p').css({
        position: 'relative'
    });
    // stile di base dei baloons degli errori
    $('head').append('<style>div.error { -moz-border-radius:3px; -webkit-border-radius:3px; white-space:nowrap;position:absolute;}</style>')
    $('#form input[title],#form textarea[title]').each(function(){
        var posi = $(this).position();
        var desc = $(this).attr('title');
        var hei = $(this).height();
        var wid = $(this).width();
        var pad = $(this).outerWidth() - $(this).innerWidth();
        //alert(desc+' left:'+posi.left+' top:'+posi.top);
        $(this).after('<span class="fieldesc">' + desc + '</span>');
        $('~ .fieldesc', this).css({
            position: 'absolute',
            left: posi.left,
            top: posi.top,
            width: wid - pad,
            height: hei,
            display: 'block',
            whiteSpace: 'nowrap',
            overflow: 'hidden',
            padding: pad
        });

        $('.fieldesc').click(function(){
            $(this).prev().focus();
        });
        $(this).focus(function(){
            $('+ .fieldesc', this).hide();
        })
        $(this).blur(function(){
            if ($(this).val() == '') {
                $('+ .fieldesc', this).show();
            }
        });
    });
    $('#form').submit(function(){
        $('.fieldesc').hide();
    })
// termini privacy
    $('.terms').each(function(){
        $privacy = "Ai sensi del D.Lgs. 196/2003 si precisa che le informazioni inviate sono riservate ed a uso esclusivo del destinatario. Qualora il messaggio in parola Le fosse pervenuto per errore, La invitiamo ad eliminarlo senza copiarlo e a non inoltrarlo a terzi, dandocene gentilmente comunicazione. Grazie.";
        $css = "#termini{background:#fff;width:375px;height:158px;line-height:130%;padding:10px;color:#333}.close{color:#c81a11;text-decoration:none;font-weight:bold;text-transform:uppercase;float:right;width:10px}.window,#mask{position:absolute;left:0;top:0;display:none}.window{width:440px;height:200px;z-index:9999;padding:20px}#mask{background:#000;z-index:9000}";
        $('head').append('<style>' + $css + '</style>');
        $('body').append('<div id="termini" class="window"><a href="#" class="close">x</a>' + $privacy + '</div><div id="mask"></div>');
    });
    $('.terms').click(function(e){
        var id = '#' + $(this).attr('name');
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
        $('#mask').css({
            'width': maskWidth,
            'height': maskHeight
        });
        $('#mask').fadeIn(750);
        $('#mask').fadeTo(250, 0.8);
        var winH = $(window).height();
        var winW = $(window).width();
        $(id).css('top', winH / 2 - $(id).height() / 2);
        $(id).css('left', winW / 2 - $(id).width() / 2);
        $(id).fadeIn(1000);
        return false;
    });
    $('.window .close').click(function(e){
        //Cancel the link behavior
        e.preventDefault();
        $('#mask').hide();
        $('.window').hide();
    });
    $('#mask').click(function(){
        $(this).hide();
        $('.window').hide();
    });
    // valida il form
    $("#form").validate({
        errorElement: "div",
        rules: {
            nominativo: 'required',
            citta: 'required',
            telefono: {
                required: true,
                digits: true
            },
            operate: {
                required: true,
                email: true
            },
            richiesta: 'required',
            privacy: 'required'
        },
        messages: {
            nominativo: '&#8249; Inserire il nome',
            citta: '&#8249; Inserire la citt&agrave;',
            telefono: {
                required: '&#8249; Inserire un numero di telefono',
                digits: '&#8249; Inserire solo cifre'
            },
            operate: {
                required: '&#8249; Inserire un indirizzo email',
                email: '&#8249; Non &egrave; un indirizzo valido'
            },
            richiesta: '&#8249; Inserire una richiesta',
            privacy: '&#8249; Accettare la privacy policy'
        }
    });
    
});
