var carregaClientes = 1;
var lastWorks = 1;

$(document).ready(function() {
    var displayArea = $(window).height() - 70;
    var scrolled = $(window).scrollTop();

    $(window).scroll(function () {
        scrolled = $(window).scrollTop();
    //$(".scroll").text(scrolled)
    });

    if ("home" == module || "ultimostrabalhos" == module) {
        // AJAX Carregar mais Tweets
        var current_page = 0;
        $("#tweets .carregar-mais").click(function() {
            current_page++;
            $.ajax({
                url : base_url,
                data : {
                    'page' : current_page
                },
                dataType: "json",
                success : function(response) {
                    if (false == response.hasMore) {
                        $("#tweets .carregar-mais").remove();
                    }

                    $("div#twitter").append(response.body);
                    $("#twitter div").removeClass("ultimoTwett");
                    $("#twitter div").last().addClass("ultimoTwett");

                    $('html,body').animate({
                        scrollTop: $('.ultimoTwett').offset().top - 370
                    }, 400);

                    blankLinks();
                }
            });
            return false;
        });

        // Scroll do Home
        $(".menu-home").click(function() {
            $('html,body').animate({
                scrollTop: $('#home').offset().top - 70
            }, 400);
            window.location.hash = "";
        });

        $(".menu-cases").click(function(){
            $('html,body').animate({
                scrollTop: $('#cases').offset().top - 70
            }, 400);
        });
        $(".menu-clientes").click(function(){
            $('html,body').animate({
                scrollTop: $('#clientes').offset().top - 70
            }, 400);
        });

        // Hover dos VIDEOS da home
        $("#home img").hover(function () {
            $(this).attr("src", $(this).attr("src").split('.png')[0] + "_hover.png");
        }, function () {
            $(this).attr("src", $(this).attr("src").split('_hover.png')[0] + ".png");
        });

        if (scrolled < 1059) {
            $(".menu-main li:eq(0)").addClass("selected");
        }

        $(window).scroll(function () {
            if (scrolled < 459) {
                $(".menu-main li:eq(0)").addClass("selected");
                $(".menu-main li:eq(1)").removeClass("selected");
                $(".menu-main li:eq(2)").removeClass("selected");
                $(".menu-main li:eq(3)").removeClass("selected");
                $(".menu-main li:eq(4)").removeClass("selected");
            }

            if (scrolled > 460 && scrolled < 1248) {
                $(".menu-main li:eq(0)").removeClass("selected");
                $(".menu-main li:eq(1)").removeClass("selected");
                $(".menu-main li:eq(2)").removeClass("selected");
                $(".menu-main li:eq(3)").removeClass("selected");
                $(".menu-main li:eq(4)").removeClass("selected");
            }
            if (scrolled > 1249 && scrolled < 1698) {
                $(".menu-main li:eq(0)").removeClass("selected");
                $(".menu-main li:eq(1)").removeClass("selected");
                $(".menu-main li:eq(2)").addClass("selected");
                $(".menu-main li:eq(3)").removeClass("selected");
                $(".menu-main li:eq(4)").removeClass("selected");
            }

            if (scrolled >= 1699 && scrolled < 2078) {
                $(".menu-main li:eq(2)").removeClass("selected");
                $(".menu-main li:eq(0)").removeClass("selected");
                $(".menu-main li:eq(1)").removeClass("selected");
                $(".menu-main li:eq(3)").removeClass("selected");
                $(".menu-main li:eq(4)").removeClass("selected");
            }

            if (scrolled >= 2079) {
                $(".menu-main li:eq(0)").removeClass("selected");
                $(".menu-main li:eq(1)").removeClass("selected");
                $(".menu-main li:eq(2)").removeClass("selected");
                $(".menu-main li:eq(3)").addClass("selected");
                $(".menu-main li:eq(4)").removeClass("selected");
            }

            if (true == isScrolledIntoView($(".capp-clientes"))) {
                $(".capp-clientes li:eq(0)").delay(200).fadeIn();
                $(".capp-clientes li:eq(1)").delay(400).fadeIn();
                $(".capp-clientes li:eq(2)").delay(600).fadeIn();
                $(".capp-clientes li:eq(3)").delay(800).fadeIn();
                $(".capp-clientes li:eq(4)").delay(1000).fadeIn();
                $(".capp-clientes li:eq(5)").delay(1200).fadeIn();
                $(".capp-clientes li:eq(6)").delay(1400).fadeIn();
                $(".capp-clientes li:eq(7)").delay(1600).fadeIn();
                $(".capp-clientes li:eq(8)").delay(1800).fadeIn();
                $(".capp-clientes li:eq(9)").delay(2000).fadeIn();
                $(".capp-clientes li:eq(10)").delay(2200).fadeIn();
                $(".capp-clientes li:eq(11)").delay(2400).fadeIn();
                $(".capp-clientes li:eq(12)").delay(2600).fadeIn();
                $(".capp-clientes li:eq(13)").delay(2800).fadeIn();
                $(".capp-clientes li:eq(14)").delay(3000).fadeIn();
                $(".capp-clientes li:eq(15)").delay(3200).fadeIn();
                $(".capp-clientes li:eq(16)").delay(3400).fadeIn();
                $(".capp-clientes li:eq(17)").delay(3600).fadeIn();
                $(".capp-clientes li:eq(18)").delay(3800).fadeIn();
                $(".capp-clientes li:eq(19)").delay(4000).fadeIn();
            }
        });

        //Slide Prêmios
        $(function carregaSelos() {
            $(".awwwards-1").delay(5000).animate({
                right: '-80'
            }, function() {
                $(".awwwards-2").animate({
                    right: '0'
                }, function() {
                    $(".awwwards-2").delay(5000).animate({
                        right: '-80'
                    }, function() {
                        $(".awwwards-1").animate({
                            right: '0'
                        }, function() {
                            carregaSelos();
                        });
                    });
                });
            });
        });

        // Banner rotativo - HOME
        $("#controller").jFlow({
            slides: "#slide",
            width: "960px",
            height: "740px",
            duration: 600
        });

        // Vitrine Últimos Trabalhos
        $(".jFlowNext").click(function() {
            lastWorks++;

            if (lastWorks == 10) {
                lastWorks = 1;
            }

            if (lastWorks < 10) {
                $(".bg-trabalhos").animate({
                    opacity: 0
                }, function() {
                    switch (lastWorks) {
                        case 1:
                            currentWork = "url('images/cases/rede_da_conquista/bg_ultimos_trabalhos.jpg')";
                            break;

                        case 2:
                            currentWork = "url('images/cases/bol/bg_ultimos_trabalhos.jpg')";
                            break;

                        case 3:
                            currentWork = "url('images/cases/cappuccino/bg_ultimos_trabalhos.jpg')";
                            break;

                        case 4:
                            currentWork = "url('images/cases/ajudar/bg_ultimos_trabalhos.jpg')";
                            break;

                        case 5:
                            currentWork = "url('images/cases/sazon/bg_ultimos_trabalhos.jpg')";
                            break;

                        case 6:
                            currentWork = "url('images/cases/danix/bg_ultimos_trabalhos.jpg')";
                            break;

                        case 7:
                            currentWork = "url('images/cases/batepapouol/bg_ultimos_trabalhos.jpg')";
                            break;

                        case 8:
                            currentWork = "url('images/cases/amd/bg_ultimos_trabalhos.jpg')";
                            break;

                        case 9:
                            currentWork = "url('images/cases/pharmaton/bg_ultimos_trabalhos.jpg')";
                            break;
                    }

                    $(".bg-trabalhos").css("background-image", currentWork);

                    $(".bg-trabalhos").animate({
                        opacity: 1
                    });
                });
            }
        });

        $(".jFlowPrev").click(function() {
            lastWorks--;

            if (lastWorks == 0) {
                lastWorks = 9;
            }

            if (lastWorks > 0) {
                $(".bg-trabalhos").animate({
                    opacity: 0
                }, function() {
                    switch (lastWorks) {
                        case 1:
                            currentWork = "url('images/cases/rede_da_conquista/bg_ultimos_trabalhos.jpg')";
                            break;
                        case 2:
                            currentWork = "url('images/cases/bol/bg_ultimos_trabalhos.jpg')";
                            break;
                        case 3:
                            currentWork = "url('images/cases/cappuccino/bg_ultimos_trabalhos.jpg')";
                            break;
                        case 4:
                            currentWork = "url('images/cases/ajudar/bg_ultimos_trabalhos.jpg')";
                            $(".play-video-home").html("<img src="+ base_url + "images/cases/ajudar/thumb_video.jpg />");
                            break;
                        case 5:
                            currentWork = "url('images/cases/sazon/bg_ultimos_trabalhos.jpg')";
                            break;

                        case 6:
                            currentWork = "url('images/cases/danix/bg_ultimos_trabalhos.jpg')";
                            break;

                        case 7:
                            currentWork = "url('images/cases/batepapouol/bg_ultimos_trabalhos.jpg')";
                            break;

                        case 8:
                            currentWork = "url('images/cases/amd/bg_ultimos_trabalhos.jpg')";
                            break;

                        case 9:
                            currentWork = "url('images/cases/pharmaton/bg_ultimos_trabalhos.jpg')";
                            break;
                    }

                    $(".bg-trabalhos").css("background-image", currentWork);

                    $(".bg-trabalhos").animate({
                        opacity: 1
                    });
                });
            }
        });

        $(".veja-projeto").hover(function(){
            $(this).animate({
                opacity: 0.50
            });
        }, function(){
            $(this).animate({
                opacity: 1
            });
        });

        $(".play-video-home").click(function(){
            $(this).html("<iframe width='610' height='410' src='http://www.youtube.com/embed/rwWu5VO3Ve0' frameborder='0' allowfullscreen></iframe>");

        });

    } // Fim module Home

    $(".ir-topo").click(function() {
        $('html,body').animate({
            scrollTop: $("body").offset().top - 70
        }, 400);
    });

    // Texto Alternativo dos idiomas
    $(".english").hover(function() {
        $(".idiomas-alt").text("english version");
    }, function() {
        $(".idiomas-alt").text("");
    });

    $(".portugues").hover(function() {
        $(".idiomas-alt").text("versão português");
    }, function() {
        $(".idiomas-alt").text("");
    });

    // Hover dos CASES
    $(".hover-up, .hover-up-mini").hover(function () {
        $(this).attr("src", $(this).attr("src").split('.png')[0] + "_hover.png");
    }, function () {
        $(this).attr("src", $(this).attr("src").split('_hover.png')[0] + ".png");
    });

    // Hover setas avançar slide
    $(".jFlowPrev, .jFlowNext").hover(function () {
        $(this).attr("src", $(this).attr("src").split('.png')[0] + "_hover.png");
    }, function () {
        $(this).attr("src", $(this).attr("src").split('_hover.png')[0] + ".png");
    });

    // Parar em CASE ou CLIENTES quando vier de outra página
    switch (window.location.hash) {
        case '#cases':
            $('html,body').animate({
                scrollTop: $('#cases').offset().top - 70
            }, 400);
            break;
        case '#clientes':
            $('html,body').animate({
                scrollTop: $('#clientes').offset().top -70
            }, 400);
            break;
    }

    if ('contato' == window.module) {
        $("#contato_telefone").mask("(99) 9999-9999");

        // Limpar Input quando clicar
        $("input:text, textarea").each(function () {
            var changed = false;

            if ("" == $(this).val()) {
                $(this).val($(this).attr("rel"));
                changed = false;
            }

            if ($(this).val() != $(this).attr("rel")) {
                changed = true;
            }

            $(this).focusin(function () {
                if (false == changed) {
                    $(this).val("");
                }
            });

            $(this).focusout(function () {
                if ("" == $(this).val()) {
                    $(this).val($(this).attr("rel"));
                    changed = false;
                } else {
                    changed = true;
                }
            });
        });

        $("form").submit(function () {
            $("input:text, textarea").each(function () {
                if ($(this).val() == $(this).attr("rel")) {
                    $(this).val("");
                }
            });
            return true;
        });

        $("#myController").jFlow({
            slides: "#mySlides",
            width: "99%",
            height: "200px",
            duration: 400
        });
    }


    // Páginas em branco
    function blankLinks() {
        $("a.blank").unbind();
        $("a.blank").click(function() {
            window.open($(this).attr('href'));
            return false;
        });
    }
    blankLinks();

    $("#twitter div").eq(0).addClass("last twitter");

    $(".footer-right img").hover(function () {
        $(this).attr("src", $(this).attr("src").split('.png')[0] + "_hover.png");
    }, function () {
        $(this).attr("src", $(this).attr("src").split('_hover.png')[0] + ".png");
    });

    // Selected outras páginas
    if ("cases" == module) {
        $(".menu-main li:eq(2)").addClass("selected");

        $(".compartilhe-p img").hover(function () {
            $(this).attr("src", $(this).attr("src").split('.png')[0] + "_hover.png");
        }, function () {
            $(this).attr("src", $(this).attr("src").split('_hover.png')[0] + ".png");
        });

        // Banner rotativo - pagina Cases
        $("#controller").jFlow({
            slides: "#slide",
            width: "940px",
            height: "460px",
            duration: 600
        });

    }
    if ("joy" == module) {
        $(".menu-main li:eq(1)").addClass("selected");
    }
    if ("contato" == module) {
        $(".menu-main li:eq(4)").addClass("selected");
    }

    function isScrolledIntoView(elem) {
            var docViewTop = $(window).scrollTop();
            var docViewBottom = docViewTop + $(window).height();

            var elemTop = $(elem).offset().top;
            var elemBottom = elemTop + $(elem).height();

            return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
    }
});

