﻿
var f = {
    init: function () {

        f.fontEmbed();

    },
    //funcoes da home
    initHome: function () {
        $('#slider').nivoSlider({ controlNav: false, randomStart: true });
        f.exp();
        f.form();
        f.navs();
        f.formField();
        f.lsProjetos();
    },
    // funcoes projetos
    initProjetos: function () {
        f.detProjetos();
        f.scroll();


    },
    //Ação mover para o topo nos projetos
    scroll: function () {
        $('.navBlt li , .btTop').click(function (e) {
            $("html:not(:animated),body:not(:animated)").animate({ scrollTop: "116px" });
            e.preventDefault();
        });
    },
    // MASCARAS DE INPUT
    formField: function () {
        $('input[type="text"] , textarea').addClass("idleField");
        $('input[type="text"] , textarea').focus(function () {
            $(this).removeClass("idleField").addClass("focusField").parent().addClass('formAtv');
            if (this.value == this.defaultValue) {
                this.value = '';
            }
            if (this.value != this.defaultValue) {
                this.select();
            }
        });
        $('input[type="text"] , textarea').blur(function () {
            $(this).removeClass("focusField").addClass("idleField").parent().removeClass('formAtv');
            if ($.trim(this.value) == '') {
                this.value = (this.defaultValue ? this.defaultValue : '');
            }
        });
    },
    // FONT EMBED
    fontEmbed: function () {
        $white = '1px 1px rgba(255, 255, 255, 0.6)';
        $darkBlue = '1px 1px rgba(33, 100, 140, 1.0)';
        $black = '1px 1px rgba(0, 0, 0, 0.4)';
        $font = 'Gill Sans MT';
        $fontLight = 'Gill Sans Light';
        $cBlue = '#609BBF';
        $cLightBlue = '#79C4F2';
        $cDarkBlue = '#23648C';
        $cWhiteBlue = '#ADC8D9';
        $cWhite = '#f2f2f2';

        Cufon.replace('.nav a , , .exp .canvas > a , .navProjeto a', { textShadow: $darkBlue, hover: true, fontFamily: $font });
        Cufon.replace('.exp h1 , .contato h1 , .contato h2 ', { textShadow: $black, hover: false, fontFamily: $fontLight, fontStretch: '105%', color: '-linear-gradient(#FFF ,' + $cWhiteBlue + ' )' });
        Cufon.replace('.exp h2', { textShadow: $darkBlue, hover: false, fontFamily: $font, color: '-linear-gradient(' + $cWhite + ',' + $cWhiteBlue + ' )' });
        Cufon.replace('.bltLst li , .btPdf', { textShadow: $darkBlue, hover: true, fontFamily: $font });
        Cufon.replace('.main h1 , .main h2', { textShadow: $white, hover: false, fontFamily: $fontLight, fontStretch: '105%', color: '-linear-gradient(' + $cBlue + ',' + $cDarkBlue + ' )' });
        Cufon.replace('.listInfo li ', { textShadow: $black, hover: false, fontFamily: $font });
        Cufon.replace('.listInfo .ttl', { textShadow: $black, hover: false, fontFamily: $font, color: '-linear-gradient(#FFF ,' + $cWhiteBlue + ' )' });
        Cufon.replace('.frameHover > a > span', { textShadow: $black, hover: true, fontFamily: $font });
        Cufon.replace('.contato .canvas > a , .copyright , .lsCompartilhe li ', { textShadow: $white, hover: true, fontFamily: $font });
        Cufon.replace('.compartilhe h2 ', { textShadow: $darkBlue, hover: false, fontFamily: $font, fontStretch: '105%' });
        Cufon.replace('.lsDados li ', { textShadow: $white, hover: false, fontFamily: $fontLight, color: '-linear-gradient(' + $cLightBlue + ' , ' + $cDarkBlue + ' )' });
        Cufon.replace('.lsDados li span', { textShadow: $white, hover: false, fontFamily: $fontLight, color: '-linear-gradient(' + $cBlue + ' , ' + $cDarkBlue + ' )' });


    },
    //EXPANSIVEL
    exp: function () {
        $('.nav .it01').click(function () {
            $e = $('.exp'), $this = $(this), $bt = $('.exp .btClose'), v = $e.css('height'), $s = $("html:not(:animated),body:not(:animated)");
            // validação da acao
            if (v == "0px") {
                $(this).addClass('it01atv');
                $e.animate({ height: "511px" }, function () { $(this).css('overflow', 'visible'); $s.animate({ scrollTop: "115px" }); setTimeout(function () { $bt.animate({ opacity: 'show', bottom: "-31px" }); }, 300) });
            } else {
                close();
            }
            // botao de fechar
            $bt.click(function () {
                close();
            });
            // acao de fechar
            function close() {
                $bt.animate({ opacity: 'hide', bottom: "-15px" }, function () { $e.css('overflow', 'hidden').animate({ height: "0" }, function () { $s.animate({ scrollTop: "0px" }, function () { $('.nav .it01').removeClass('it01atv'); setTimeout(function () { Cufon.refresh('.nav .it01'); }, 20) }); }) })
            }

        })
    },
    //Ação mover para o topo
    navs: function () {
        $('.btTopo').click(function () {
            $("html:not(:animated),body:not(:animated)").animate({ scrollTop: "0px" });
        });

        $('.nav .it02').click(function () {
            var offset = $('.btTopo').offset();

            $("html:not(:animated),body:not(:animated)").animate({ scrollTop: offset.top });
        });

    },
    // pega variavel da url
    gup: function (name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regexS = "[\\?&]" + name + "=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.href);
        if (results == null)
            return "";
        else
            return results[1];
    },
    // alerta de formulario enviado
    form: function () {
        v = f.gup("form");
        if (v == "ok") {
            alert("Formulário Enviado com sucesso.")

        }

    },
    // slider 
    slider: function () {
        $('.flexslider').flexslider({
            slideshow: false,
            animation: 'slide',
            directionNav: false,
            manualControls: '.navBlt li',
            controlsContainer: ".navProjeto"
        });
        f.scroll();
    },
    /////////////////////////////////////////////////////////////////////////////////////////////
    //XML
    ////////////////////////////////////////////////////////////////////////////////////////////
    // função do ajax para carregar os valores do xml 
    // _url = página do xml a ser passada 
    // _loadData = carrega dados do xml
    ajaxData: function (_url, _loadData, _complete) {
        $.ajax({
            type: "GET",
            url: _url,
            dataType: "xml",
            success: _loadData,
            complete: function () { _complete }
        })
    },
    // finaliza acao do ajax de carregar os dados.
    close: function () {
        setTimeout(function () {
            Cufon.refresh();
            $('.load').animate({ opacity: "hide", top: '-50%' });
        }, 2000);

        setTimeout(function () {
            $(".frame .true").contents().find("a").attr("href", "javascript:;");
        }, 2000);


    },
    // lista projetos
    // carrega lista de categoria de projetos no bottom
    lsProjetos: function () {
        f.ajaxData("_xml/dados.xml",
            function (xml) {
                //variaveis
                var $t
                , nome
                , dados = ""
                , dadosItem = ""
                , id
                , thumb
                , tecnologias = ""
                , restrito = ""
                , lock
                , key = f.gup("key");

                // troca a frase do topo dos projetos
                if (key == "ok") {
                    $('.main h2').html("Conhecimento, agilidade e descrição, qualidades indispensáveis para um projeto de sucesso. ")
                }
                //filtra dados
                $(xml).find('dados').find('projeto').each(function () {
                    $t = $(this);
                    thumb = $t.find('thumb').text();
                    nome = $t.find('nome').text();
                    restrito = $t.find('restrito').text();
                    id = $t.find('id').text();
                    tecnologias = "";
                    $t.find('tecnologia').find('item').each(function () {
                        tecnologias += "<li>" + $(this).text(); +"</li>";
                    });
                    // validação dos projetos restritos
                    if (key != "ok" && restrito == "sim") {
                        lock = "<li style='display:none'>";
                    } else {
                        lock = "<li>";

                    }


                    //monta string do item
                    dadosItem = lock;
                    dadosItem += "<div class='frame' style='background:url(_img/projetos/" + thumb + ".jpg) no-repeat;'>";
                    dadosItem += "<div class='frameHover'>";
                    dadosItem += "<ul class='listInfo'>";
                    dadosItem += "<li class='ttl'>" + nome + "</li>";
                    dadosItem += tecnologias;
                    dadosItem += "</ul>";
                    dadosItem += "<a href='" + id + "/projetos.aspx' title='acessar o projeto' class='btLupa'><span>Acessar o projeto</span></a>";
                    dadosItem += "</div>"
                    dadosItem += "</div>"
                    dadosItem += "<div class='shadow'><!-- --></div>"
                    dadosItem += "</li>"
                    dados += dadosItem;

                })
                // aplica dados no html
                $("#lsProjeto").append(dados)
            }
        // finaliza loading
        , f.close()
        )
    },
    // carrega lista de categoria de projetos no bottom
    detProjetos: function () {
        f.ajaxData("../_xml/dados.xml",
        function (xml) {
            //variaveis
            var $t
            , qs = window.location.pathname

            , id
            , telas = ""
            , descript = ""
            , $sel = $(xml).find('dados').find('projeto')
            , nav
            , info
            , tec
            , face

            // pega o valor pela url amigavel
            qs = qs.split("/"); //quebra o endeço de acordo com a / (barra)

            //qs = qs[2]; // local
            qs = qs[1]; // producao

            $sel.each(function () {
                id = $(this).find('id').text();

                if (id == qs) {// localiza o projeto
                    $t = $(this)
                    c = 1;
                    blockUrl = $t.find('blockUrl').text();
                    desc = $t.find('description').text();
                    //descrip = $('meta[property="og:title"]').attr("content", desc);
                    //descrip = $('meta[property="og:description"]').attr("content", desc);
                    //$('meta[property="og:url"]').attr("content", 'http://www.alexandrelenz.com.br/' + id + '/projetos.aspx');
                    //face = "<fb:like href='http://www.alexandrelenz.com.br/" + id + "/projetos.aspx' send='false' layout='button_count' width='160' show_faces='false' action='recommend' font='tahoma'></fb:like>"
                    nav = "";
                    mobile = $t.find('mobile').text();

                    if (desc == "null") { $('#dados .compartilhe').hide(); }




                    $t.find('infos').find('lk').each(function () {
                        h = $(this).parent().find('frameHeight').text();

                        if (mobile == "sim") {
                            console.debug('entro')
                            frame = "<iframe scrolling='no' class='" + blockUrl + "' width='350' frameborder='0' src='../projetosDados/" + qs + "/" + $(this).text() + "' height='" + h + "' ></iframe>"

                        } else {
                            frame = "<iframe scrolling='no' class='" + blockUrl + "' width='1000' frameborder='0' src='../projetosDados/" + qs + "/" + $(this).text() + "' height='" + h + "' ></iframe>"
                            // frame = '1';
                        }

                        telas += "<li>"
                        telas += "<div class='frame'>"
                        telas += frame
                        telas += "</div>"
                        telas += "</li>"
                        nav += "<li><a href='#' title='Exemplo " + c + "'>Tela 0" + c + "</a></li>";
                        c++;
                    })

                    info = $t.find('dados').find('tipo').text();
                    tec = $t.find('dados').find('info').text();

                }
            });

            //filtra dados
            $sel.find('infos').find('lk').each(function () {

            })
            // aplica dados no html
            //$("#lkFace").append(face);
            $(".navBlt").append(nav)
            $("#i01 span").append(info);
            $("#i02 span").append(tec);
            $(".slides").append(telas)
            l = $('.frame iframe').css('width');
            $('.frame .true').contents().find('a').attr('href', 'javascritp:;');
            $('.frame').css('width', l);
            $('.flexslider').css('width', l);
            f.slider();




        }
        // finaliza loading
        , f.close());
    }
}

$(function () {
    f.init();
    

 });
