function getScrollTop(){
    if(typeof pageYOffset!= 'undefined'){
        //most browsers
        return pageYOffset;
    }
    else{
        var B= document.body; //IE 'quirks'
        var D= document.documentElement; //IE with doctype
        D= (D.clientHeight)? D: B;
        return D.scrollTop;
    }
}


function alert(msg) {
    Sexy.alert(msg);
    return;
}

$(function(){
    
	/* animazione vetrine */
	/*$('.viewport').mouseenter(function(e) {
            $(this).children('a').children('img').animate({ height: '170', left: '0', top: '0', width: '280'}, 70);
            //$(this).children('a').children('span').fadeIn(200);
    }).mouseleave(function(e) {
            $(this).children('a').children('img').animate({ height: '151', left: '-20px', top: '-20px', width: '231'}, 70);
            //$(this).children('a').children('span').fadeOut(200);
    });*/
    
    $("#top-menu > .main > li").mouseenter(function() {
        $(this).addClass("current-dynamic");
        $(this).find(".sub").animate({
            height: "show"
        },100);
        return false;
    }).mouseleave(function(){
        $(this).removeClass("current-dynamic");
        $(this).find(".sub").animate({
            height: "hide"
        },100);
        return false;
    });
    
    $('#top-menu > .main > li:last').css({
        border:'none' 
    });
    
    $('#fader,#RuotaAzienda').fader();
    $('#details-gallery').fader({
        squares: false,
        squaresCaptions:'alt'
    });





/* 

$('#RuotaAziendaNoNav ').fader({
	speed: 800,
        squares: true,
        squaresCaptions:'alt',
	controls:true
    });

*/




    $('.tiptip').tipTip({
        maxWidth:'300px'
    });
    
    $('#contenuto .homebox .boxBackground .puntini').each(function(){
        var zanzara = $('.nome',$(this).parent()).outerWidth();
        $(this).css({
            width:''+(273-zanzara)+'px',
            left:''+(zanzara+13)+'px'
        });
    });
            
    $('#areaRiservata .content .openArea').bind({
        click: function(){
            if(!$('#areaRiservata').hasClass('active')){
                $('#areaRiservata').animate({
                    top:'0px'
                },500);
                $('#areaRiservata').addClass('active');
                $('.tic',this).attr('src', template_url+'assets/images/bg/closeArea.png');
            }
            else{
                $('#areaRiservata').animate({
                    top:'-160px'
                },500);
                $('#areaRiservata').removeClass('active');
                $('.tic',this).attr('src', template_url+'assets/images/bg/downArrow.png');
            }
        } 
    });
    
    $('.homebox').bind({
        mouseenter: function(){
            $('.img-to-zoom',this).stop(false,true).animate({
                width: '+=15',
                height: '+=15',
                top: '-=7px',
                left: '-=7px'
            }, 200);
        },
        mouseleave: function(){
            $('.img-to-zoom',this).stop(false,true).animate({
                width: '-=15',
                height: '-=15',
                top: '+=7px',
                left: '+=7px'
            }, 200
            );
        }
    });
    
    var bind_submit_newsletter = function(){
        $('#newsletter-submit').click(function(){
            $.ajax({
                type:'post',
                url:$('#boxIscrizione form').attr('action'),
                dataType:'html',
                data:$('#boxIscrizione form').serialize(),
                success: function(data){
                    $('#boxIscrizione .content').html(data);
                    bind_submit_newsletter();
                },
                async: false
            });
        });
    }
    
    $('#widget').bind({
        mouseenter: function(){
            if(!$('#boxIscrizione').hasClass('active')){
                $(this).stop(false,true).animate({
                    paddingLeft:'+=10px'
                }, 200
                );
            }
        },
        click: function(){
            if(!$('#boxIscrizione').hasClass('active')){
                $.ajax({
                    url:$('.apri',$('#widget')).attr('href'),
                    dataType:'html',
                    success: function(data){
                        $('#boxIscrizione .content').html(data);
                        bind_submit_newsletter();
                    },
                    async: false
                });
                $('#boxIscrizione').fadeIn(500);
                $('#boxIscrizione').addClass('active');
            }
            else{
                $('#boxIscrizione').removeClass('active');
                $('#boxIscrizione').fadeOut(500);
            }
            return false;
            
        },
        mouseleave: function(){
            if(!$('#boxIscrizione').hasClass('active')){
                $(this).stop(false,true).animate({
                    paddingLeft:'-=10px'
                }, 200
                );
            }
        }
    });
    
    $('#widget .close').click(function(){
        $('#boxIscrizione').removeClass('active');
        $('#boxIscrizione').fadeOut(500);
    })
    
    $('#boxIscrizione').click(function(e){
        if (!e) var e = window.event;
        e.cancelBubble = true;
        if (e.stopPropagation) e.stopPropagation();
    });
    
    $('#widget').css({
        top:getScrollTop()+20+200
    });
    
    $(window).scroll(function(){
        $('#widget').stop(false,true).animate({
            top:getScrollTop()+20+200
        },500,'easeOutQuad');
    });
  

    $('#sloganInput').val($('#slogan').text());

    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);
    }
    
    hs.creditsText = '';
	
    $('#read-privacy,#newsletterprivacy,.hs-privacy').live('click',function(){
        return hs.htmlExpand(this, {
            contentId: 'highslide-html'
        } );
    });
    
    $('#datisoc').click(function(){
        var win_dim = {
            'w':window.innerWidth,
            'h':window.innerHeight
        }

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

    });

    $('#form form').submit(function(){
        $('input,textarea',$(this)).removeClass('error');
        switch(true){
            case ($('#name').val()==''):
                alert('Compilare il campo "nome"');
                $('#name').addClass('error');
                return false;
                break;
            case ($('#surname').val()==''):
                alert('Compilare il campo "cognome"');
                $('#surname').addClass('error');
                return false;
                break;
            case (!is_valid_email($('#email').val())):
                alert('Inserire un indirizzo email valido');
                $('#email').addClass('error');
                return false;
                break;
            case ($('#phone').val()==''):
                alert('Compilare il campo "numero di telefono"');
                $('#phone').addClass('error');
                return false;
                break;
            case (!$('#privacy-check').is(':checked')):
                alert('Prendere visione note sulla privacy');
                $('#privacy-check').addClass('error');
                return false;
                break;
        }
        return true;
    });


jQuery('#products-list ul li:nth-child(4)').css('margin-right','0px');
jQuery('#products-list ul li:nth-child(8)').css('margin-right','0px');

});

function open_piva(doc){
    var win_dim = {
        'w':window.innerWidth,
        'h':window.innerHeight
    }
    var dim = {
        'w':369,
        'h':300
    };
    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);
}

