﻿    //Funções
    
    var site = {
    
        anhambi :
        {
            
            //Limitador de texto
            txtLimitator : function($el, v) {
            
                $el.each(function(){
                    txt = '';
                    txt = jQuery.trim($(this).text());
                    if (txt.length > v) {
                        $(this).text(txt.substr(0,v) + '...')
                    }
                });
                    
            },
            
            //Slider topo
            sliderTopo : function($obj, controls) {
            
                var go = true;
                var $c = $('#control-slider-topo');
                var tempoAuto = 8000;
                var intervalo;
                var prefixName = 'stli';
            
                $(window).load(function() {
                    $obj.find('li').each(function(i){
                        $(this).attr('id', prefixName + i);                    
                    });
                });
            
                animateLoad($obj.parent().find('.loading'));

                $(window).load(function(){
                
                    $obj.fadeIn();
                    $obj.find('li:first').fadeIn().parents().find('.loading').remove();
                    $('#menu').slideDown();
                    
                    if (controls) {
                        $c.fadeIn();
                    }
                                        
                    $obj.find('li').first().fadeIn().parents().find('.controls .control').first().removeClass('inativo').addClass('ativo');
            
                    $c.children('.control').click(function() {
                        if(!$obj.find('li').is(':animated') && $obj.find('li').length > 1) {
                            clearInterval(intervalo);
                            var c = $(this);
                            c.parent().find('.control').removeClass('ativo').addClass('inativo');
                            c.removeClass('inativo').addClass('ativo');
                            $obj.find('li').each(function() {
                                if ($(this).css('display') != 'none') {
                                    $(this).fadeOut(function() {
                                        $('#' + prefixName + c.index()).fadeIn(function(){
                                            new sliderAuto().rodar();
                                        });
                                    });
                                    return false;
                                }
                            });
                        }
                    }); 
                    
                    //Auto
                    var sliderAuto = function () {
                        var _eu = this;
                        this.rodar = function () {
                            if ($c.children('.control').filter('.ativo').index() == ($c.children('.control').length - 1)) {
                                $c.children('.control').eq(0).trigger('click');
                            } else {
                                $c.children('.control').filter('.ativo').next().trigger('click');
                            }                      
                        }
                        intervalo = window.setInterval(function () { _eu.rodar() }, tempoAuto); 
                    }
                    $obj.delay(tempoAuto).queue(function() {
                        new sliderAuto().rodar();
                        $(this).dequeue;
                    }); 
                    
                });              
            },
            
            //Firula página inicial
            welcome : function($obj) {
                if (!$obj.find('li').is(':animated')) {
                    $obj.find('li').hover(function(){
                        $(this).stop().animate({
                            marginTop: -5
                        }, 250);                
                    }, function() {
                        $(this).stop().animate({
                            marginTop: 0
                        }); 
                    }); 
                }               
            }, 
            
            //Menu Produtos
            menuProdutos : function($obj) {
            
                $('<img src="../img/bottom_menu_produtos.png" alt="Produtos Anhambi" />').appendTo($obj.find('li').not($obj.find('li ul li')));  
                
                $obj.find('li p').click(function() {                    
                    $(this).parent().find('ul').slideToggle();
                });            
            }        
        }
        
    }
    
    function animateLoad($l) {
        $(function al() {
            if ($l.length > 0) {
                $l.animate({top:"120px"}, 400).animate({top:"100px"}, 370, function(){al();});
            }
        });					 
    }
    
    $(document).ready(function(){
       
        //Adiciona html com cantos arredondados
        var $obj = $('#conteudo-interna, #conteudo-interna2');
        $obj.wrapInner('<div class="container-i"></div>');
        $('<div class="top-conteudo"></div>').prependTo($obj);
        $('<div class="bottom-conteudo"></div>').appendTo($obj);

        //Abre login
        var $rep = $('#area-restrita');
        var hRepresentantes = $rep.outerHeight(true);
        
        $('#aba-login').click(function(){
            if ($rep.css('display') == 'none') {
                $('body').animate({backgroundPosition: '(50% ' + hRepresentantes +  'px)'});
            } else {
                $('body').animate({backgroundPosition: '(50% 4px)'});    
            }
            $rep.slideToggle(); 
        });
        
        //Marca o menu & carrega os Li's para "Slider Topo"  
        var menu = $('#menu');
        var url = window.location.href;
        if (url.indexOf('empresa') > 0) {
            menu.find('li').eq(0).addClass('ativo');
            $('#slider-topo ul').load('../modulos/slider-topo/institucional.html');
        } else if (url.indexOf('frangos') > 0) {
            menu.find('li').eq(1).addClass('ativo');
            $('#slider-topo ul').load('../modulos/slider-topo/frangos.html');
        } else if (url.indexOf('linha-pet') > 0) {
            menu.find('li').eq(2).addClass('ativo');
            $('#slider-topo ul').load('../modulos/slider-topo/linha_pet.html');
        } else if (url.indexOf('racoes') > 0) {
            menu.find('li').eq(3).addClass('ativo');
            $('#slider-topo ul').load('../modulos/slider-topo/racoes.html');
        } else if (url.indexOf('energia') > 0) {
            menu.find('li').eq(4).addClass('ativo');
            $('#slider-topo ul').load('../modulos/slider-topo/energia.html');
        } else if (url.indexOf('imprensa') > 0) {
            menu.find('li').eq(5).addClass('ativo');
            $('#slider-topo ul').load('../modulos/slider-topo/institucional.html');
        } else if (url.indexOf('receitas') > 0) {
            $('#slider-topo ul').load('../modulos/slider-topo/frangos.html');
        } else if (url.indexOf('contatos') > 0) {
            menu.find('li').eq(6).addClass('ativo');
            $('#slider-topo ul').load('../modulos/slider-topo/institucional.html');
        } else if (url.indexOf('representantes') > 0) {
            //menu.find('li').eq(6).addClass('ativo');
            $('#slider-topo ul').load('../modulos/slider-topo/institucional.html');
        }
        
        //Fotos institucional
        $('.institucional').find('.foto, .foto2').each(function() {
            ahref = $(this).find('a');
            $('<a href="' + ahref.attr('href') +  '" class="ampliar lightbox" title="' + ahref.attr('title') + '"></a>').prependTo($(this));
        });
                
    });
    
    $(window).load(function() {
        
        //Menu (abas)
        var menuAbas = $('#menu-abas');
        menuAbas.find('li').each(function() {
            $('<div class="l"></div>').prependTo($(this));
            $('<div class="r"></div>').appendTo($(this));
        });

        menuAbas.find('li').first().addClass('ativo');

        var cAbas = $('#content-abas');
        cAbas.height(cAbas.find('li').first().outerHeight());
        
        menuAbas.find('li').click(function() {
            menuAbas.find('li').removeClass('ativo');
            $(this).addClass('ativo');
            id = $(this).find('a').attr('href');
            $('#target').stop().animate({height: $(id).outerHeight()}, 'slow').animate({scrollTop: $('#target').scrollTop() + $(id).position().top }, 'slow');
            return false;
        }); 
    
    });
