﻿var Wefit = {}

Wefit.isLocal = !/wefit.com.br/.test(window.location.host);

Wefit.carregarVideo = function (videoID) {
    
    if( Wefit.isLocal ){ //ambiente de produção
        $tag = $("<video/>", {
            'class': 'video-js',
            'width': '782px',
            'height': '441px',
            'controls': 'controls',
            'id': 'the_video',
            'autoplay': 'true',
            'src': 'videos/' + videoID + '.mp4'
        });    
    }else{
        $tag = "<iframe src=\"http://player.vimeo.com/video/" + videoID + "?title=0&byline=0&portrait=0&autoplay=1\" width=\"782\" height=\"441\" frameborder=\"0\" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>";
    }

    $player = $('.player-placeholder');
    $player.html($tag);
    
    if( Wefit.isLocal ){
        var the_video = VideoJS.setup("the_video");
        the_video.play();
    }
}

Wefit.bindarNavVideos = function () {
    $player = $('.player-placeholder');
    $itens = $('.video-nav a');

    $itens.click(function (e) {
        e.preventDefault();

        var videoID = $(this).attr('data-videoId');

        Wefit.carregarVideo(videoID);
    });
}

Wefit.slideBodyTo = function (target) {
    //html e body são necessários!
    $('html, body').stop().animate({
        scrollTop: target 
    }, 1000);
}

Wefit.bindarLinksToTop = function () {
    //Scroll Top
    $('.toTop, .logotipo').click(function (e) {
        e.preventDefault();

        Wefit.scrollToTop();
    });
}

Wefit.scrollToTop = function () {
    var target = $('header').offset().top;

    Wefit.slideBodyTo(target);
}

Wefit.scrollToSection = function (section) {
    var margin = 50;
    var target = $("." + section).offset().top - 50;

    Wefit.slideBodyTo(target);

    Wefit.trocarTituloPagina(section);
}

Wefit.bindarLinkTecnologiaEstrategica = function () {
    $("h1 img").hover(
        function () {
            $(this).animate({ left: '-231px' }, 100);
        },
	    function () {
	        $(this).animate({ left: '0' }, 100);
	    }
    );
}

Wefit.bindarNavPrincipal = function () {
    //Scroll Anchor
    $('nav a').click(function (e) {
        e.preventDefault();

        var section = $(this).attr("href");

        Wefit.scrollToSection(section);
    });
}

Wefit.mostrarMenuFixo = function () {
    $(".header-fixed").fadeIn(200);
}

Wefit.esconderMenuFixo = function () {
    $(".header-fixed").fadeOut(200);
}

Wefit.bindarLabelsInline = function () {

    var itens = [
        { nome: 'nome', title: 'NOME' },
        { nome: 'email', title: 'EMAIL' },
        { nome: 'mensagem', title: 'MENSAGEM' },
        { nome: 'nome', title: 'NOME' }
    ]

    for (var i = 0; i < itens.length; i++) {

        $(".formulario input." + itens[i].nome)
        .focus(function () {
            if ($(this).val() == itens[i].title) {
                $(this).val('');
            }
        })
        .focusout(function () {
            if ($(this).val() == '') {
                $(this).val(itens[i].title);
            }
        });
    }

//    $.fn.labels = function () {
//        return this.each(function () {
//            $(this)
//            .focus(function () {
//                if ($(this).val() == $(this).attr("title")) {
//                    $(this).val('');
//                }
//            })
//            .focusout(function () {
//                if ($(this).val() == '') {
//                    $(this).val($(this).attr("title));
//                }
//            });
//        });
//    }

//    $(".formulario input[type=text]").labels();

    //    //PLACEHOLDER CONTATO
    //    //NOME
    //    $(".formulario input.nome").focus(function () {
    //        if ($(".formulario input.nome").val() == "NOME") {
    //            $(".formulario input.nome").val('');
    //        }
    //    }).focusout(function () {
    //        if ($(".formulario input.nome").val() == "") {
    //            $(".formulario input.nome").val("NOME");
    //        }
    //    });
    //    //EMAIL
    //    $(".formulario input.email").focus(function () {
    //        if ($(".formulario input.email").val() == "EMAIL") {
    //            $(".formulario input.email").val('');
    //        }
    //    });
    //    $(".formulario input.email").focusout(function () {
    //        if ($(".formulario input.email").val() == "") {
    //            $(".formulario input.email").val("EMAIL");
    //        }
    //    });
    //    //MENSAGEM
    //    $(".formulario textarea.mensagem").focus(function () {
    //        if ($(".formulario textarea.mensagem").val() == "MENSAGEM") {
    //            $(".formulario textarea.mensagem").val('');
    //        }
    //    });
    //    $(".formulario textarea.mensagem").focusout(function () {
    //        if ($(".formulario textarea.mensagem").val() == "") {
    //            $(".formulario textarea.mensagem").val("MENSAGEM");
    //        }
    //    });
}

Wefit.tratarMenuFixo = function () {
    //Scroll Top Fixed
    $(window).scroll(function () {
        var x = $(this).scrollTop();

        if (x > 75) {
            Wefit.mostrarMenuFixo();
            //$("body").css('background-position', '0% ' + parseInt(-x / 10) + 'px');
        } else {
            Wefit.esconderMenuFixo();
        }
    });
}

Wefit.bindarNavCases = function () {
    $('#paraQuem .filter li.active').css({ color: '#000', backgroundColor: '#FDD100', backgroundImage: 'none' });

    $('#paraQuem .filter li').hover(
	    function () {
	        $(this).not('.active').css('color', '#FFF');
	    },
	    function () {
	        $(this).not('.active').css('color', '#FDD100');
	    }
    );

    $('#paraQuem .filter li').click(function () {
        $(this).siblings('li').removeClass('active');
        $(this).siblings('li').css({ color: '#FFD100', backgroundColor: 'transparent', backgroundImage: 'url(img/black.5.png)' });
        $(this).addClass('active').css({ color: '#000', backgroundColor: '#FDD100', backgroundImage: 'none' });
        var label = $(this).attr('data-label');
        switch (label) {
            case 'all':
                $('#paraQuem .thumbs li').stop().fadeTo(500, 1);
                break;
            case 'web':
                $('#paraQuem .thumbs .mobile').stop().fadeTo(500, .2);
                $('#paraQuem .thumbs .web').stop().fadeTo(500, 1);
                break;
            case 'mobile':
                $('#paraQuem .thumbs .web').stop().fadeTo(500, .2);
                $('#paraQuem .thumbs .mobile').stop().fadeTo(500, 1);
                break;
        }
    });
    // --- cases filter ---

    // --- cases nav ---
    $('.case').hide();

    // function to be triggered when ajax load ends
    function ajaxLoad() {
        $('.case .nav .active').stop().fadeTo(200, 0);

        $('.case .info div').hover(
		    function () {
		        $(this).css({ color: '#000', backgroundColor: '#FDD100', backgroundImage: 'none' });
		        $(this).children('span').children('img').attr('src', 'img/cases/closeOver.png');
		    },
		    function () {
		        $(this).css({ color: '#FFF', backgroundColor: 'transparent', backgroundImage: 'url(img/black.5.png)' });
		        $(this).children('span').children('img').attr('src', 'img/cases/close.png');
		    }
    	);

        $('.case .info div').click(function () {
            $('.case').slideUp(500);
            $('#paraQuem .thumbs li').removeClass('active');
            $('#paraQuem .thumbs li').children('.over').stop().fadeTo(500, 1);
        });

        $('.case .nav li').click(function () {
            $(this).siblings().removeClass('active').fadeTo(200, 1);
            $(this).addClass('active').fadeTo(500, 0);
            var scrollDist = ($(this).attr('data-nav')) * -468;
            $('.mainImg img').stop().animate({ top: scrollDist }, 500);
        });

        Wefit.scrollToSection('paraquem');
    };

    $('#paraQuem .thumbs li').mouseover(function () {
        $(this).children('.over').stop().fadeTo(500, 0);
    });

    $('#paraQuem .thumbs li').mouseout(function () {
        $(this).not('.active').children('.over').stop().fadeTo(500, 1);
    });

    $('#paraQuem .thumbs li').click(function () {
        if ($(this).is('.active')) {
            $(this).removeClass('active');
            $('.case').stop().slideUp(500);
        } else {
            var project = $(this).attr('data-case');
            $(this).siblings().removeClass('active');
            $(this).addClass('active');
            $(this).siblings().children('.over').stop().fadeTo(500, 1);
            $('.case').slideDown(500);
            $('.case').html('<img src="img/cases/loading.gif" alt="carregando case" class="loading">');
            
            if( Wefit.isLocal ){
                console.log('rodando local');
                $('.case').html( Cases[project] );
                ajaxLoad();
            }else{
                $('.case').load('cases/' + project + '.html', ajaxLoad);
            }  
        }
    });
}

Wefit.trocarTituloPagina = function (section) {
    var title = null;
    var sufixo = " - Wefit - Tecnologia Estratégica";

    switch (section) {
        case 'porque':
            title = "Porque";
            break;

        case 'o-que':
            title = "O que";
            break;

        case 'como':
            title = "Como";
            break;

        case 'paraquem':
            title = "Para quem";
            break;

        case 'contato':
            title = "Contato";
            break;

        default:
            title = "Home";
            break;
    }

    document.title = title + sufixo;
    window.location.hash = "/" + section;
}

Wefit.autoloadSection = function () {

    if (window.location.hash != "") {
        section = window.location.hash.replace("#/", "");
        
        Wefit.scrollToSection(section);
    }
}
