var conteudos = new Object()
$(document).ready(function(){
    resizeAll = function(){
        // posicionaRodape()
        //backgroundResize()
        decorResize()
        teste=function(){
            alert('696369')
        }
        $('.backg').css({
            width:'0px',
            height:'0px'
        })
        $('iframe').css({
            width:'0px',
            height:'0px'
        })
        $(".rodape").css({
            top : '0px'
        } );

        var alt = Math.max ($('html').innerHeight(),window.innerHeight)
        alt = Math.max(document.body.scrollHeight, alt)
        var larg = Math.max ($('html').innerWidth(),window.innerWidth)
        larg = Math.max ($(window).width(),window.innerWidth)
        //alert('larg:'+larg+' alt:'+alt)
        if ($.browser.msie){
            alt = $(document).height()
            larg = $(window).width();
        }
        $('.backg').css({
            width:larg,
            height:alt
        })
        $('iframe').css({
            width:larg,
            height:(alt-25)
        })
        $('iframe#mapaIframe').css({
            width:larg,
            height:alt
        })
        $(".rodape").css({
            top : alt-43,
            left: '0px'
        } );
    }
    //chama função para carregar imagens
    carregaImagens()
    //esconde iframe mapa google
    $('#mapaIframe').fadeOut(1500)

    $('.itemmenu').each(function(key,value){
        var x = $(value).find('a')
        $.get('paginas/'+x[0].id+'.php',function(data){
            conteudos[x[0].id]=data
        })
        $(x).click(function(){
            $('#conteudo').hide('fast',function(){
                $('#mapaIframe').fadeOut(1000)
                $('#conteudo').html(conteudos[x[0].id])
                $('#conteudo').show('slow')
                $('#deco').fadeOut(500,function(){
                    var ext = '.png'
                    if(x[0].id == 'quem_somos')
                        ext = '.gif'
                    $('#deco > img').attr('src','imagens/punch-decor-'+x[0].id+ext)
                    $('#deco').fadeIn(1000,resizeAll)
                })
                if(x[0].id == 'contactos'){
                    $('#mapaIframe').fadeIn(1000)
                }
            })
        })
    })
    $.get('paginas/apresentacao.php',function(data){
        conteudos['apresentacao']=data
        mostraApresentacao()
    })
    $('#home').click(function(){
        mostraApresentacao()
    })
    if(!applePhone){
        midground()
        foreground()
        background()
        }
    initnave()
    resizeAll()
    $("div.conteudo").resize(function(){
        resizeAll()
    })
    $(document).resize(function(){
        resizeAll()
    })
    $(window).resize(function(){
        resizeAll()
    })
})
var it=0
ValidaEmail = function (email){
    exp = /^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/
    if(!exp.test(email)){
        return false;
    }
    return true;
}
enviaSubNewsletter =function (){
    var email =document.getElementById("email").value
    if(ValidaEmail(email)){
        $.post('newsletter.php', 'email=' + email, function(data, res){
            switch(res){
                case 'success':
                    confirm(data);
                    break;
            }
        })
        document.getElementById("email").value='insira o seu email'
    }
    else{
        confirm("email inválido");
    }
}
mostraApresentacao = function(){
    $('#mapaIframe').fadeOut(1000)
    $('#conteudo').html(conteudos['apresentacao'])
    $('#conteudo').show('slow')
    $('#deco').fadeOut(500,function(){
        $('#deco > img').attr('src','imagens/punch-decor-apresentacao.png')
        $('#deco').fadeIn(1000,resizeAll)
    })
}
posicionaRodape = function(){
    $(".rodape").css({
        top : '0px'
    } );
    var topo = Math.max ($('html').innerHeight()-43,window.innerHeight-43)//(document.body.scrollHeight-43)+'px'
    topo = Math.max(document.body.scrollHeight-43, topo)
    if ($.browser.msie)
        topo = $(window).height()-43
    //confirm("$(html).innerHeight() "+ $('html').innerHeight()+'<br /> - window.innerHeight-43'+window.innerHeight+' <br /> document.body.scrollHeight '+document.body.scrollHeight)
    $(".rodape").css({
        top : topo,
        left: '0px'
    } );
// backgroundResize()
    
//    $(".rodape").animate({
//        top : '0px'
//    },'fast',function(){
//        var topo = Math.max ($('html').innerHeight()-43,window.innerHeight-43)//(document.body.scrollHeight-43)+'px'
//        topo = Math.max(document.body.scrollHeight-43, topo)
//        if ($.browser.msie)
//            topo = $(window).height()-43
//        //confirm("$(html).innerHeight() "+ $('html').innerHeight()+'<br /> - window.innerHeight-43'+window.innerHeight+' <br /> document.body.scrollHeight '+document.body.scrollHeight)
//        $(".rodape").animate({
//            top : topo,
//            left: '0px'
//        },'fast',backgroundResize);
//      //  backgroundResize()
//    });
    
}
decorResize = function() {
    var min = 850
    var max = 1000
    var newWindowWidth = $(window).width();
    var decoW = (newWindowWidth*250/(max))
    if(decoW < 250)
        $('#deco > img').animate({
            width:decoW+'px'
        },'fast')
}
backgroundResize = function(){
    //    $('.backg').animate({
    //        width:'500px',
    //        height:'500px'
    //    },function(){
    //        var alt = Math.max ($('html').innerHeight(),window.innerHeight-43)
    //        alt = Math.max(document.body.scrollHeight, alt)
    //        var larg = Math.max ($('html').innerWidth(),window.innerWidth)
    //        larg = Math.max ($(window).width(),window.innerWidth)
    //        //alert('larg:'+larg+' alt:'+alt)
    //
    //        $('.backg').css({
    //            width:larg,
    //            height:alt
    //        })
    //    });

    var alt = Math.max ($('html').innerHeight(),window.innerHeight-43)
    alt = Math.max(document.body.scrollHeight, alt)
    var larg = Math.max ($('html').innerWidth(),window.innerWidth)
    larg = Math.max ($(window).width(),window.innerWidth)
    //alert('larg:'+larg+' alt:'+alt)
    if ($.browser.msie){
        alt = $(window).height()-43
        larg = $(window).width();
    }
    $('.backg').css({
        width:larg,
        height:alt
    })
    $('iframe').css({
        width:larg,
        height:(alt+43-25)
    })
}
carregaImagens = function (){
    //carregar imagens para memoria
    var imgs= new Array()
    var i = 0
    if(!document.MM_p) document.MM_p= new Array();
    var  j=document.MM_p.length
    $('.itemmenu').each(function(key,value){
        var x = $(value).find('a')
        imgs[i]='imagens/punch-decor-'+x[0].id+'.png'
        i = i +1
    })
    imgs[i]='imagens/punch-decor-quem_somos.gif'
    imgs[imgs.length]='imagens/setaB.png'
    imgs[imgs.length]='imagens/setaD.png'
    imgs[imgs.length]='favicon.ico'
    for(i=0; i<imgs.length; i++)
        if (imgs[i].indexOf("#")!=0){
            document.MM_p[j]=new Image;
            document.MM_p[j++].src=imgs[i];
        }
}

/* Backgound a mover */
midground = function(){
    $('#midground').css({
        backgroundPosition: '0px 0px'
    });
    $('#midground').animate({
        backgroundPosition:"(-10000px -2000px)"
    }, 440000, 'linear',midground);
}
foreground = function(){
    $('#foreground').css({
        backgroundPosition: '0px 0px'
    });
    $('#foreground').animate({
        backgroundPosition:"(-10000px -2000px)"
    }, 320000, 'linear',foreground);
}
background = function(){
    $('#background').css({
        backgroundPosition: '0px 0px'
    });
    $('#background').animate({
        backgroundPosition:"(-10000px -2000px)"
    }, 520000, 'linear',background);
}
nave = function (){
    var pX = parseInt($(".nave").css('left')),
    pY = parseInt($(".nave").css('top')),
    x = aleatorio(0, 300) * ((aleatorio(0, 2)==0)? -1 : 1 ),
    y = aleatorio(0, 300) * ((aleatorio(0, 2)==0)? -1 : 1 ),
    newX = pX+x,
    newY = pY+y,
    size = aleatorio(30, 110)/100
    if(newX+100 > $(window).width() || newX-100 < 0)
        x = -x
    if(newY+100 > $(window).height() || newY < 0)
        y = -y
    if ($.browser.msie){
        var wNave
        size = aleatorio(30, 110)
        $(".nave").animate({
            left: "+="+x+"px",
            top: "+="+y+"px" ,
            width: size+'px'
        }, 6000,'linear',nave );
    }
    else
        $(".nave").animate({
            left: "+="+x+"px",
            top: "+="+y+"px" ,
            scale: size,
            rotate: ((x<0)? -15 : 15)+"deg"
        }, 6000,'linear',nave );
}
initnave = function(){
    var x = aleatorio(0, $(window).width())
    var y = aleatorio(0, $(window).height())
    $(".nave").css({
        left: "="+x+"px",
        top: "="+y+"px"
    })
    nave()
}
aleatorio = function (inferior,superior){
    var numPossibilidades = superior - inferior
    var aleat = Math.random() * numPossibilidades
    aleat = Math.floor(aleat)
    return parseInt(inferior) + aleat
}
