function open_piva(doc){
    var win_dim = {
        'w':window.innerWidth,
        'h':window.innerHeight
    }

    var dim = {
        'w':369,
        'h':232
    };
    window.open(doc,'','status=no,toolbar=no,height='+dim['h']+',width='+dim['w']+',left='+((win_dim['w']-dim['w'])/2)+',top='+(win_dim['h']-dim['h'])/2);
}


function setAbility(){
    if(document.form1.accetto.checked){
        document.form1.invia.disabled = false;
    }
    else{
        document.form1.invia.disabled=true;
    }
}

function checkform() {

    switch(true){
        case(document.form1.nome.value==''):
            alert('Devi completare il campo "Nome"');
            document.form1.nome.focus();
            return false;
            break;
        case(document.form1.cognome.value==''):
            alert('Devi completare il campo "Cognome"');
            document.form1.cognome.focus();
            return false;
            break;
        case(document.form1.telefono.value==''):
            alert('Devi completare il campo "Telefono"');
            document.form1.telefono.focus();
            return false;
            break;
        case(document.form1.email.value==''):
            alert('Devi completare il campo "Email"');
            document.form1.email.focus();
            return false;
            break;
        case(document.form1.code.value==''):
            alert('Devi inserire il codice di sicurezza');
            document.form1.code.value='';
            document.form1.code.focus();
            return false;
            break;
        default:
            return true;
            break;
    }

    // checkcode(document.form1.code.value);

}

function submitBook(){
    document.checka.submit();
}

jQuery(
    function(){


        if(!jQuery('#formcontatti_n_privacy').attr('checked')){
            jQuery('#submit_formcontatti').attr("disabled", true);
        }else{
            jQuery('#submit_formcontatti').attr("disabled", false);
        }

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

                if(!jQuery(this).attr('checked')){
                    jQuery('#submit_formcontatti').attr("disabled", true);
                }else{
                    jQuery('#submit_formcontatti').attr("disabled", false);
                }

            }
        );


        var base = '';

        jQuery('#mainMenu').jmenu({speed:50});

        jQuery('#mainMenu>.ul>.li:first>a').css({borderLeft:'0px'});
//        jQuery('#mainMenu>.ul>.li:last>a').css({borderRight:'0px'});
        jQuery('#mainMenu .li .ul').each(
            function(){
                jQuery('.li:last>a',jQuery(this)).css({borderBottom:'0px'});
            }
        );

        var now = new Date();

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

                var that = this;
                var jthat = jQuery(this);
                var id = jthat.attr('class');

                jQuery('select[name=daysI]',jthat).val(now.getUTCDate()+1);
                jQuery('select[name=monthsI]',jthat).val(now.getUTCMonth()+1);
                jQuery('select[name=yearI]',jthat).val(now.getUTCFullYear());

            }
        );
        jQuery('#partenzaWr').each(
            function(){

                var that = this;
                var jthat = jQuery(this);
                var id = jthat.attr('class');

                jQuery('select[name=daysS]',jthat).val(now.getUTCDate()+3);
                jQuery('select[name=monthsS]',jthat).val(now.getUTCMonth()+1);
                jQuery('select[name=yearS]',jthat).val(now.getUTCFullYear());

            }
        );

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

                var that = this;
                var t = false;
                var l = jQuery('img',jQuery(this)).length;
                var timeout = 5000;

                that.initFade = function(){
                    that.display(1);
                }

                that.display = function(i){

                    jQuery('img',jQuery(that)).fadeOut(200);
                    jQuery('img:eq('+i+')',jQuery(that)).fadeIn(200);

                    t = window.setTimeout(
                        function(){
                            that.display((i+1)%l);
                        },
                        timeout
                    );

                }

                jQuery('img:first',jQuery(this)).show();

                if(l>1){
                    window.setTimeout(
                        function(){
                            that.initFade();
                        },
                        timeout
                    );
                }

            }
        );

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

                var that = jQuery(this);
                var current = 0;

                jQuery('.next',that).click(
                    function(){
                        if(current<jQuery('.news',that).length - 3){
                            current++;
                        }
                        jQuery('.ovrHid',that).stop(false,true).scrollTo(jQuery('.news:eq('+current+')',that),200);
                    }
                );

                jQuery('.prev',that).click(
                    function(){
                        if(current>0){
                            current--;
                        }
                        jQuery('.ovrHid',that).stop(false,true).scrollTo(jQuery('.news:eq('+current+')',that),200);
                    }
                );

            }
        );


        var imgzoom = 1.25;
        var zoomspeed = 500;
        
        jQuery('#homeBoxes .image').each(
            function(){

                var img = jQuery('img',jQuery(this));

                var oh = img.height();
                var ow = img.width();
                var ot = 0;
                var ol = 0;

                var nh = img.height()*imgzoom;
                var nw = img.width()*imgzoom;
                var nt = -1*(nh-img.height())/2;
                var nl = -1*(nw-img.width())/2;

//                var txt = jQuery('.imageDescription:not(.oneline)',jQuery(this)).text();
//                txt = '<span class="fs22">'+txt.substr(0,1)+'</span>'+txt.substr(1);
//                jQuery('.imageDescription:not(.oneline)',jQuery(this)).html(txt);

                jQuery(this).hover(
                    function(){

                        jQuery('.imageDescription',jQuery(this)).stop(false,true).animate(
                            {
//                                opacity: 0.0,
                                bottom: -64
                            },
                            zoomspeed,
                            'linear'
                        );

                        img.stop(false,true).animate(
                            {
                                height:nh,
                                width:nw,
                                top:nt,
                                left:nl
                            },
                            zoomspeed,
                            'linear'
                        );

                    },
                    function(){

                        img.stop(false,true).animate(
                            {
                                height:oh,
                                width:ow,
                                top:ot,
                                left:ol
                            },
                            zoomspeed,
                            'linear'

                        );

                        jQuery('.imageDescription',jQuery(this)).stop(false,true).animate(
                            {
//                                opacity: 1,
                                bottom: 0
                            },
                            zoomspeed,
                            'linear'
                        );

                    }
                );

            }
        );


        jQuery('#footer .left .logo').each(
            function(){

                jQuery('img',jQuery(this)).load(
                    function(){
                        jQuery(this).parent().css({
                            width: jQuery(this).width(),
                            height: jQuery(this).height()
                        });
                    }
                );
                

                var osrc = jQuery('img',jQuery(this)).attr('src');
                var nsrc = osrc.replace(/\/([\w]+)\.jpg/ig,'/color-$1.jpg');

                jQuery('img',jQuery(this)).load(
                    function(){
          // se tolgo questo load va in errore qtip, chissà perché?
                    }
                );

                jQuery(this).hover(
                    function(){
                        //jQuery('img',jQuery(this)).stop(false,true).fadeOut(200).attr('src',nsrc);
                        jQuery('img',jQuery(this)).attr('src',nsrc);
                    },
                    function(){
                        //jQuery('img',jQuery(this)).stop(false,true).fadeOut(200).attr('src',osrc);
                        jQuery('img',jQuery(this)).attr('src',osrc);
                    }
                );

            }
        );



        jQuery('#content .gallery').each(
            function(){

                var parent = jQuery(this);
                var images = [];
                var that = this;

                jQuery('img',parent).each(
                    function(){
                        images.push(jQuery(this));
                    }
                );

                parent.html(''+
                    '<div class="big"></div>'+
                    '<div class="thumbs">'+
                    '   <div class="top">&nbsp;</div>'+
                    '   <div class="scrollable"></div>'+
                    '   <div class="bottom">&nbsp;</div>'+
                    '</div>'+
                    '');
                
                jQuery('.big',parent).append('<img src="/Thumb/phpThumb.php?src='+base+images[0].attr('src')+'&q=100&w=425&h=258" alt="big" />');
                jQuery('.big img',parent).load(
                    function(){

                        jQuery('.big .loading',parent).remove();
                        jQuery(this).fadeIn(200);

                    }
                );

                jQuery.each(images,function(k,v){
                    jQuery('.thumbs .scrollable',parent).append('<img src="/Thumb/phpThumb.php?src='+base+v.attr('src')+'&q=100&w=80" alt="'+v.attr('alt')+'" />');
                });

                jQuery('img',parent).show();

                jQuery('.thumbs .scrollable img',parent).click(
                    function(){

                        var i = jQuery(this).index();
                        jQuery('.big img',parent).stop(false,true).fadeOut(200).attr('src','/Thumb/phpThumb.php?src='+base+images[i].attr('src')+'&q=100&w=425&h=258');
                        jQuery('.big',parent).append('<div class="loading">Loading&hellip;</div>');

                    }
                );

                that.minTop = -1*(jQuery('.thumbs .scrollable',parent).height()-jQuery('.thumbs',parent).height()+30);
                that.maxTop = 30;
                that.stop = false;
                that.active = jQuery('.thumbs .scrollable img',parent).first();
                that.active = 0;

                that.moveTop = function(){
                    if(that.active>0){
                        that.active--;
                        jQuery('.thumbs .scrollable',parent).scrollTo(jQuery('.thumbs .scrollable img:eq('+that.active+')',parent),200);
                    }
                }

                that.moveBottom = function(){
                    if(that.active<jQuery('.thumbs .scrollable img',parent).length-3){
                        that.active++;
                        jQuery('.thumbs .scrollable',parent).scrollTo(jQuery('.thumbs .scrollable img:eq('+that.active+')',parent),200);
                    }
                }

                jQuery('.top',parent).click(
                    function(){
                        that.moveTop();
                    }
                );

                jQuery('.bottom',parent).click(
                    function(){
                        that.moveBottom();
                    }
                );

            }
        );

        jQuery('#footer .left img[alt]').qtip(
            {
                content: {
                    text: false
                },
                position: {
                    corner: {
                        target: 'topMiddle',
                        tooltip: 'bottomLeft'
                    }
                },

//                style : 'blue'

                style: {
                    background: '#EFEEED',
                    color: 'black',
                    border: {
                        width: 2,
                        radius: 2,
                        color: '#142A62'
                    },
                    name: 'blue'
                }

            }
        );

    }
);

