﻿(function ($) {
    $.fn.hypeUtil = function () {

        // COMPONENTES DE TELA
        var _root = $('#main');
        var _mosaico = _root.find('.mosaico-wrapp .stage .mosaico-lista');

        // VARIÁVEIS
        var movimentoAtivo = false;
        var filtroAtivo;
        var pageWidth;
        var pageHeight;

        // LISTENER DE RESIZE
        $(window).resize(function () {
            pageWidth = $(window).width();
            pageHeight = $(window).height();
            setLarguraMosaicoStage();

            _root.find('.mosaico-wrapp .stage').stop();
            ajustaProporcoesMosaico();

        });

        // LISTENER DE SCROLL
        $(window).scroll(function () {
            var objLogo = $('.logo-wrapp .content');
            if (!objLogo.hasClass('locked')) {
                posicionaLogo();
            }
        });

        function getPageScroll() {
            var yScroll;
            if (self.pageYOffset) { yScroll = self.pageYOffset; }
            else if (document.documentElement && document.documentElement.scrollTop) { yScroll = document.documentElement.scrollTop; }
            else if (document.body) { yScroll = document.body.scrollTop; }
            return yScroll
        }

        function getRandom(minNumber, maxNumber) {
            var adjustedHigh = (parseFloat(maxNumber) - parseFloat(minNumber)) + 1;
            var numRand = Math.floor(Math.random() * adjustedHigh) + parseFloat(minNumber);
            return numRand;
        }

        /************************************************************************************/
        /*[ CONTROLE DO LOGO ]***************************************************************/
        function posicionaLogo() {
            var objLogo = $('.logo-wrapp .content');
            var mosaicoStage = _root.find('.mosaico-wrapp .stage');

            if (!objLogo.hasClass('locked')) {
                var targetSroll = (getPageScroll() + 45);
                if (!objLogo.hasClass('inicializado')) {
                    objLogo.animate({ top: 45 }, "slow", "easeInOutExpo", function () { objLogo.addClass('inicializado'); });
                }
                else {
                    var margem = (mosaicoStage.height() - (getPageScroll() + 10)) - $('.logo-wrapp .content').height();
                    if (margem <= 0) {
                        if (!objLogo.hasClass('fixada')) {
                            objLogo.addClass('fixada');

                            objLogo.stop();
                            objLogo.animate({ top: (mosaicoStage.height() + 90) }, "fast", "easeOutExpo", function () {
                                objLogo.find('.fb-like').fadeIn('medium');
                            });
                        }
                    }
                    else {
                        if (objLogo.hasClass('fixada')) {
                            objLogo.stop();
                            objLogo.find('.fb-like').fadeOut('fast');
                            objLogo.find('#logo-zeppelin').fadeIn('medium');
                            objLogo.find('#logo-zeppelin-vermelho').fadeOut('medium');
                            objLogo.animate({ top: targetSroll }, "fast", "easeOutExpo", function () { objLogo.removeClass('fixada'); });
                        }
                        else {
                            objLogo.css('top', targetSroll);
                        }
                    }
                }
            }
            else {
                objLogo.stop();
                objLogo.animate({ top: (mosaicoStage.height() + 90) }, "fast", "easeOutExpo", function () { });
            }
        }

        _root.find('.logo-wrapp #logo-zeppelin').click(function () {
            var _this = $(this);
            var objLogo = $('.logo-wrapp .content');
            var mosaicoStage = _root.find('.mosaico-wrapp .stage');

            objLogo.addClass('fixada');
            objLogo.addClass('locked');
            _this.fadeTo('fast', 1);

            objLogo.animate({ top: (mosaicoStage.height() + 90) }, "fast", "easeOutExpo", function () {
                objLogo.find('.fb-like').fadeIn('medium');
            });

        });

        _root.find('.logo-wrapp #logo-zeppelin').mouseenter(function () {
            var _this = $(this);
            var objLogo = $('.logo-wrapp .content');

            if (!objLogo.hasClass('locked')) {
                if (!objLogo.hasClass('fixada')) {
                    _this.fadeTo('fast', 0.1);
                    $('.logo-wrapp .content #logo-zeppelin-vermelho').fadeTo('fast', 0.2);
                    $('.logo-wrapp .content #logo-fixa').fadeIn('fast');
                }
            }
        });

        _root.find('.logo-wrapp #logo-zeppelin').mouseleave(function () {
            var _this = $(this);
            var objLogo = $('.logo-wrapp .content');

            if (!objLogo.hasClass('locked')) {
                if (!objLogo.hasClass('fixada')) {
                    _this.fadeTo('fast', 1);
                }
            }
        });


        //-[ ANIMAÇÃO DE HOVER NO MOSAICO ]------------------------------------------------------------------------------------------
        setLarguraMosaicoStage();

        _root.find('ul.categorias-lista').mouseenter(function () {
            var _this = $(this);
            _this.find('li.categorias').stop();
            _this.find('li.categorias').animate({ width: (_this.find('.stage').width() - 30), opacity: 1 }, "medium", "easeInOutExpo");
        });

        _root.find('ul.categorias-lista').mouseleave(function () {
            var _this = $(this);
            _this.find('li.categorias').stop();
            _this.find('li.categorias').animate({ width: 0, opacity: 0.1 }, "medium", "easeInOutExpo");
        });


        //-[ AÇÃO DE FILTRO NO MOSAICO ]------------------------------------------------------------------------------------------
        _root.find('ul.categorias-lista li[rel]').click(function () {
            var _this = $(this);
            if (filtroAtivo != _this.attr('rel')) {
                _root.find('ul.categorias-lista li').removeClass('selected');
                _this.addClass('selected');
                filtroAtivo = _this.attr('rel');

                filtraMosaico(_this.attr('rel'));
                configuraMenuCategorias(_this.text());
            }
        });

        function filtraMosaico(filtro) {
            var _mosaicoStage = _root.find('ul.mosaico-lista');

            if (filtro == "todos") {
                _mosaicoStage.find('li[rel]').show();
                _mosaicoStage.find('li[rel]').animate({ width: 196, height: 126, margin: 1 }, "fast", "easeOutExpo", function () { });
            }
            else {

                _mosaicoStage.find('li[rel!="' + filtro + '"]').animate({ width: 0, height: 0, margin: 0 }, "fast", "easeOutExpo", function () { $(this).hide(); });
                _mosaicoStage.find('li[rel="' + filtro + '"]').show();
                _mosaicoStage.find('li[rel="' + filtro + '"]').animate({ width: 196, height: 126, margin: 1 }, "fast", "easeOutExpo", function () { });

            }
            _mosaico.animate({ top: 0 }, "medium", "easeOutExpo", function () { ajustaProporcoesMosaico(); });
        }


        //-[ CONTROLE DE AÇÕES DO MOSAICO ]------------------------------------------------------------------------------------------
        var arrCoresBG = ["bg-laranja", "bg-vermelho", "bg-roxo", "bg-marrom"];
        var _mosaicoStage = _root.find('ul.mosaico-lista');

        // ANIMA IMAGENS DO MOSAICO APOS SEREM CARREGADAS
        _root.find('ul.mosaico-lista .item').each(function () {
            var _this = $(this);
            _this.find('.hover-info').addClass(arrCoresBG[getRandom(0, 3)]);
            _this.find('img').last().one('load', function () {

                $(this).fadeTo('slow', 0.8);

            }).each(function () { if (this.complete) $(this).load(); });
        });

        // EXIBE INFOS DO ITEM
        _root.find('ul.mosaico-lista .item').mouseenter(function () {
            var _this = $(this);
            _this.find('.hover-info').stop();
            _this.find('.hover-info').animate({ top: 0 }, "fast", "easeOutExpo");

            _this.find('img').stop();
            _this.find('img').fadeTo('fast', 0.3);
        });

        // ESCONDE INFOS DO ITEM
        _root.find('ul.mosaico-lista .item').mouseleave(function () {
            var _this = $(this);
            _this.find('.hover-info').stop();
            _this.find('.hover-info').animate({ top: 150 }, "slow", "easeOutBounce");

            _this.find('img').stop();
            _this.find('img').fadeTo('slow', 0.8);
        });

        // ABRE MODAL DO CASE
        $('ul.mosaico-lista .hover-info').click(function () {
            openModal($(this).attr('id'));
        });

        // ATIVA FILTRO ATIVO
        function configuraMenuCategorias(strFiltro) {
            _root.find('ul.categorias-lista li.categoria-ativa span').text(strFiltro);
        }

        //-[ CONTROLE DE PROPORÇÕES DO MOSAICO ]------------------------------------------------------------------------------------------
        function setLarguraMosaicoStage() {
            _root.find('.mosaico-wrapp .stage').width($(window).width() + 196);
        };

        function ajustaProporcoesMosaico() {
            var posicao = _mosaico.position().top + _mosaico.height();
            var limiteHMosaico = 512;

            if (posicao < _root.find('.mosaico-wrapp .stage').height()) {
                _mosaico.css('top', ((_mosaico.height() - _root.find('.mosaico-wrapp .stage').height()) * -1));
            }
            if (_mosaico.height() <= _root.find('.mosaico-wrapp .stage').height()) {
                _mosaico.css('top', 0);
                _root.find('.nav-wrapp').fadeOut('fast');
                _root.find('.mosaico-wrapp .stage').animate({ height: _mosaico.height() }, "fast", "easeInOutExpo", function () { posicionaLogo(); });
            }
            else {
                if (_mosaico.height() > _root.find('.mosaico-wrapp .stage').height()) {
                    if (_mosaico.height() > limiteHMosaico) {
                        _root.find('.mosaico-wrapp .stage').animate({ height: limiteHMosaico }, "fast", "easeInOutExpo", function () { posicionaLogo(); });
                        _root.find('.nav-wrapp').fadeIn('fast');
                    }
                    else {
                        _root.find('.mosaico-wrapp .stage').animate({ height: _mosaico.height() }, "fast", "easeInOutExpo", function () { posicionaLogo(); });
                    }
                }
            }
        }


        //-[ MOSAICO DE DIRETORES ]---------------------------------------------------------------------------------------------------------
        var _diretores = _root.find('.diretores-wrapp ul.lista');
        var _diretorAtivo;

        // MOUSE OVER
        _diretores.find('li').mouseenter(function () {
            var _this = $(this);
            if (_diretorAtivo != undefined)
                _this.fadeTo('fast', 1);
            else
                _this.find('.color').fadeIn('fast');
        });

        // MOUSE OUT
        _diretores.find('li').mouseleave(function () {
            if (!$(this).hasClass('ativo')) {
                if (_diretorAtivo != undefined)
                    $(this).fadeTo('fast', 0.3);

                $(this).find('.color').fadeOut('fast');
            }
        });

        // AÇÃO DE CLIQUE
        _diretores.find('li').click(function () {
            if (_diretorAtivo != undefined) {
                _diretorAtivo.find('.color').fadeOut('fast', function () { });
                _diretorAtivo.removeClass('ativo');
            }
            ativaDiretor($(this));
        });

        // ANIMAÇÕES DE ATIVAÇÃO DO DIRETOR
        function ativaDiretor(diretor) {
            if (!movimentoAtivo) {
                movimentoAtivo = true;

                var _diretoresStage = _root.find('.diretores-wrapp');
                if (_diretorAtivo != undefined) {
                    if (diretor.attr('id') != _diretorAtivo.attr('id')) {
                        diretor.fadeTo('fast', 1);
                        diretor.find('.color').fadeIn('fast');
                        diretor.addClass('ativo');

                        _diretorAtivo.find('.color').fadeOut('fast');
                        _diretorAtivo.removeClass('ativo');
                        _diretoresStage.find('#info-' + _diretorAtivo.attr('id')).fadeOut('fast', function () {
                            _diretoresStage.find('#info-' + diretor.attr('id')).fadeIn('fast', function () {
                                _diretorAtivo = diretor;
                                movimentoAtivo = false;
                            });
                        });
                        filtraMosaico(diretor.attr('id'));
                    }
                    else {
                        movimentoAtivo = false;
                    }
                }
                else {
                    diretor.fadeTo('fast', 1);
                    diretor.find('.color').fadeIn('fast');
                    diretor.addClass('ativo');

                    _diretoresStage.find('#info-' + diretor.attr('id')).fadeIn('fast', function () {
                        _diretorAtivo = diretor;
                        movimentoAtivo = false;
                    });
                    filtraMosaico(diretor.attr('id'));
                }
                _diretoresStage.find('li[class!="ativo"]').fadeTo('fast', 0.4);
            }
        }


        //-[ NAVEGAÇÃO DO MOSAICO ]---------------------------------------------------------------------------------------------------------
        _root.find('.nav-wrapp .bt-arrow').click(function () {
            if (!movimentoAtivo) {
                var _this = $(this);
                var target;

                if (_this.hasClass('left'))
                    if (_mosaico.position().top < 0)
                        target = "+=128";
                    else
                        target = "-=0";
                else
                    if ((_mosaico.position().top + _mosaico.height()) > _root.find('.mosaico-wrapp .stage').height())
                        target = "-=128";
                    else
                        target = "-=0";

                movimentoAtivo = true;
                _mosaico.animate({ top: target }, "fast", "easeInOutExpo", function () {
                    movimentoAtivo = false;
                });
            }
        });

        _root.find('.nav-wrapp .bt-arrow').mouseenter(function () {
            $(this).fadeTo('fast', 1);
        });

        _root.find('.nav-wrapp .bt-arrow').mouseleave(function () {
            $(this).fadeTo('fast', 0.5);
        });



        // MÉTODO DE INICIALIZAÇÃO
        function init() {
            posicionaLogo();
        };
        init();

    };
})(jQuery);
