jQuery(function(){

    var base = '/';
    jQuery('#menu').jmenu({
        speed:50
    });
	
	jQuery('#formnewsletter_email').click(function(){
        jQuery(this).val('');
    });

    jQuery('#menu .li>.ul>.li:last-child>a').css({
        borderBottom:'0px'
    });

    jQuery('.contLoghi a img').each(
        function(){
            var that = jQuery(this);
            var l = jQuery('.contLoghi').width();

            jQuery(that).css({
                margin:'0 '+parseInt((((l/3)-jQuery(that).width())/2)-1)+'px'
            });
        }
    );


    jQuery('#fadeHome').each(function(){
        var that = jQuery(this);
        var i = false;
        var timeout = 4000;
        var speed = 1000;
        var cur = 0;
        var l = jQuery('img:not(.control)',that).length;
        jQuery('img:not(.control)',that).hide().css({
            visibility:'visible'
        });
        var prev = jQuery('<img>');
        var next = jQuery('<img>');
        prev.attr('src',base+'images/icons/arrow-left.jpg').addClass('control');
        next.attr('src',base+'images/icons/arrow-right.jpg').addClass('control');
        prev.css({
            visibility:'visible',
            position:'absolute',
            top:'50%',
            marginTop:'-22px',
            left:'0px',
            cursor:'pointer',
            opacity:'0.5'
        });
        next.css({
            visibility:'visible',
            position:'absolute',
            top:'50%',
            marginTop:'-22px',
            left:'920px',
            cursor:'pointer',
            opacity:'0.5'
        });
        that.append(prev).append(next);
        var init = function(){
            jQuery('img:not(.control):eq('+((cur+l-1)%l)+')',that).fadeOut(speed);
            jQuery('img:not(.control):eq('+cur+')',that).fadeIn(speed,function(){
                start();
            });
        }
        var start = function(){
            i = window.setInterval(function(){
                cur = (cur+1)%l;
                jQuery('img:not(.control):eq('+((cur+l-1)%l)+')',that).fadeOut(speed);
                jQuery('img:not(.control):eq('+cur+')',that).fadeIn(speed);
            },timeout);
        }
        var stop = function(){
            if(i !== false){
                window.clearInterval(i);
            }
            i = false;
        }
        next.hover(function(){
            jQuery(this).stop(false,true).animate({
                opacity:'1'
            },200,'linear');
        },function(){
            jQuery(this).stop(false,true).animate({
                opacity:'0.5'
            },200,'linear');
        }).click(function(){
            cur = (cur+1)%l;
            jQuery('img:not(.control):eq('+((cur+l-1)%l)+')',that).fadeOut(speed);
            jQuery('img:not(.control):eq('+cur+')',that).fadeIn(speed);
        });
        prev.hover(function(){
            jQuery(this).stop(false,true).animate({
                opacity:'1'
            },200,'linear');
        },function(){
            jQuery(this).stop(false,true).animate({
                opacity:'0.5'
            },200,'linear');
        }).click(function(){
            cur = (cur+l-1)%l;
            jQuery('img:not(.control):eq('+((cur+l-1)%l)+')',that).fadeOut(speed);
            jQuery('img:not(.control):eq('+cur+')',that).fadeIn(speed);
        });
        init();
    });

    jQuery('#homeBoxes #news.box').each(function(){
        var p = jQuery(this);
        var cur = 0;
        var l = jQuery('.news',p).length;
        var speed = 500;
        jQuery('.scrollable',p).scrollTo(0);
        jQuery('.next',p).click(function(){
            if((cur)<(l-2)){
                cur++;
                if(cur>0){
                    jQuery('.prev',p).show();
                }
                if((cur)==(l-2)){
                    jQuery('.next',p).hide();
                }
                jQuery('.scrollable',p).stop().scrollTo(jQuery('.news:eq('+cur+')'),speed);
            }
        });
        jQuery('.prev',p).hide().click(function(){
            if(cur>0){
                cur--;
                if(cur==0){
                    jQuery('.prev',p).hide();
                }
                if((cur)<(l-2)){
                    jQuery('.next',p).show();
                }
                jQuery('.scrollable',p).stop().scrollTo(jQuery('.news:eq('+cur+')'),speed);
            }
        });
    });

    jQuery('.suggestion').each(function(){
        var suggestion = jQuery(this).val();
        jQuery(this).focus(function(){
            if(jQuery(this).val()==suggestion){
                jQuery(this).val('');
            }
        }).blur(function(){
            if(jQuery(this).val()==''){
                jQuery(this).val(suggestion);
            }
        });
    });

    jQuery('#homeBoxes #contract.box .customers').each(function(){
        var that = jQuery(this);
        var cur = 0;
        var l = jQuery('a',that).length;
        var i = false;
        var start = function(){
            i = window.setInterval(function(){
                cur = (cur+1)%(l-1);
                that.scrollTo(jQuery('a:eq('+cur+')',that),500);
            },3000);
        }
        var stop = function(){
            window.clearInterval(i);
        }
        that.hover(function(){
            stop();
        },function(){
            start();
        });
        start();
    });

    jQuery('#who_we_are_photo .photo').each(function(){
        var that = jQuery(this);
        var speed = 1000;
        var c = undefined;
        if(jQuery('.caption',that).length > 0){
            c = jQuery('.caption',that);
            var pw = parseInt(that.width());
            c.css({
                left: '5px',
                bottom: -1*c.outerHeight(true),
                width: (pw-30)+'px'
            });
        }
        that.hover(function(){
            // caption?
            if(c){
                c.stop(false,true).animate({
                    bottom:'5px'
                },300,'easeOutCirc').fadeIn(500);
            }
            // range -5/5
            var rx = Math.floor(Math.random()*11)-5;
            var ry = Math.floor(Math.random()*11)-5;
            // sprite corretta
            rx = (rx%2==0) ? rx+1 : rx;
            ry = (ry%2==0) ? ry+1 : ry;
            // anima XD!!
            that.stop().animate({
                backgroundPosition:(that.width()*rx)+'px '+(that.height()*ry)+'px'
            },speed,'easeInOutQuad');
        },function(){
            // rimettiamo tutto apposto
            if(c){
                c.stop(false,true).fadeOut(500,function(){
                    c.css({
                        bottom:-1*c.outerHeight(true)
                    });
                });
            }
            that.stop().animate({
                backgroundPosition:'0 0'
            },speed,'easeOutCirc');

        });
    });

    jQuery('.scroll').each(function(){
        var that = jQuery(this);
	var th = 0;
	that.children().each(function(){
		th += jQuery(this).outerHeight(true);
	});
        var h = /h[0-9]+/i.exec(that.attr('class')).pop().substr(1);
	var html = that.html();
        that.html('<div class="scrollable">'+
            '     <div class="scroller">'+html+'</div>'+
            '</div>'+
            '<div class="scrollbar">'+
            '     <div class="bar"></div>'+
            '</div>')
        .css({
            position: 'relative'
        });
        jQuery('.scrollable',that).css({
            height: h+'px',
            overflow: 'hidden'
        });
        jQuery('.scrollbar').css({
            position: 'absolute',
            top: '0',
            right: '-10px',
            width: '15px',
            height: h+'px'
        });
        jQuery('.bar',that).draggable({
            axis: 'y',
            containment: 'parent',
            cursor: 'move',
            drag: function(e,ui){
                var perc = Math.floor((ui.position.top)*100/(h-jQuery('.bar',that).height()));
                jQuery('.scrollable',that).scrollTo(perc+'%',{
                    axis:'y',
                    easing:'linear'
                })
            }
        });
	if(th<=h){
	        jQuery('.bar',that).hide();
	}


    });
    jQuery('#gal').jSquareSlide({
        easing:'easeInOutCirc',
        captions: jQuery('.caption',jQuery('#gal').parent()),
        autoslide: ((jQuery('.caption',jQuery('#gal').parent()).length==0) ? false : true),
        squares: false
    });
    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);
        }

    });

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

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

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

    });

    jQuery('.contentdx .content').each(
    function(){

        var that = jQuery(this);
        var isImage = jQuery('#gal').children().length;

       if(isImage>0){
           jQuery('#content .dx .contentdx .content').css({
              marginTop:''+parseInt((jQuery('#gal .squares').height())+15)+'px'
           });
       }
       else{
           console.debug('no');
       }
    });

    jQuery('[id^="n_"]').wrapAll('<div class="posizione" />');



});

