var s;
var open;
var wh;
var mh;
var mhh;
var ch;
var comparatore;
var ism = '';

var homepage = 0;

var agent=navigator.userAgent.toLowerCase();
var ie = $.browser.msie;
var mozilla = $.browser.mozilla;
var safari = $.browser.safari;
var version = parseInt($.browser.version.slice(0,1), 10);

var is_iphone = ((agent.indexOf('iphone')!=-1));
var is_ipod = ((agent.indexOf('ipod')!=-1) || (agent.indexOf('android')!=-1));
var is_ipad = ((agent.indexOf('ipad')!=-1));
var c_os = parseInt(agent.indexOf('os'), 10);
var os = agent.charAt(c_os+3);

var addEvent = function(elem, type, eventHandle) {
    if (elem == null || elem == undefined) return;
    if ( elem.addEventListener ) {
        elem.addEventListener( type, eventHandle, false );
    } else if ( elem.attachEvent ) {
        elem.attachEvent( "on" + type, eventHandle );
    }
};
addEvent(window, "load", function() { res(); } );
addEvent(window, "orientationchange", function() { ochange(); } );

$(document).ready(function(){
    isMobile();
    $('.menu_azienda>li>a').hover(function(){
        clearTimeout(s);
        $('.sub').hide();
        $tn = $(this).attr('class');
        $('#'+$tn).show();
    }, function(){
        s = setTimeout(function(){ $('.sub').hide(); /*$(open).show();*/}, 500);
    });
    $('.sub').hover(function(){
        clearTimeout(s);
    }, function(){
        s = setTimeout(function(){ $('.sub').hide(); /*$(open).show();*/}, 500);
    });
    
    $('.privateA').click(function(){
        window.scrollTo( 0, 0) ;
        $('.privatePop').fadeIn(500);
        $('.closePrivatePop').click(function(){
            $('.privatePop').fadeOut(500);
        })
    })
});
var wst;
function off(){
    if(os<5){
        addEvent(window, "scroll", function() { off(); } );
        wst = $(window).scrollTop();
        $('.bkg').css('position','absolute');
        $('.menu').css('position','absolute');
        $('.top').css('position','absolute');
        $('.bkg').css('top',wst);
        $('.menu').css('top',wst+170);
        $('.top').css('top',wst);
    } else {
        $('.bkg').css('position','fixed');
        $('.menu').css('position','fixed');
        $('.top').css('position','fixed');
    }
}

function res(){
    ochange();
    if (homepage == 0){
        wh = $(window).height();
        $('.main').css({paddingBottom: wh+70});
        ch = $('.colonna').height()+10;
        mh = $('.main').height()-100;
        if(ch > mh){
            $('.colonna').css({paddingBottom: wh+170});
        }
        if(mh < wh-200){
            $('.footer').css({position: 'absolute', top: wh-130});
        }
        else{
            $('.footer').css({position: 'static', paddingTop: 60});
        }
    } else if (homepage == 1){
        wh = $(window).height();
        $('.mainH').css({paddingBottom: wh+70});
        mhh = $('.mainH').height();
        
        if(mhh < wh-60){
            $('.footer').css({position: 'absolute', top: wh-100});
        }
        else{
            $('.footer').css({position: 'static', paddingTop: 60});
        }
    }
    
}

function altrefoto(){
    $('.altre_foto').width($('.akufriend img').width());
}



function ochange() {
    var orientation = window.orientation;
    switch(orientation) {
        case 0:
            ism = '_v';
            isMobile();
            break;
        case 180:
            ism = '_v';
            isMobile();
            break;
        case -90:
            ism = '';
            isMobile();
            break;
        case 90:
            ism = '';
            isMobile();
            break;
    }
}

function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
    });
}


