﻿$(document).ready(function() {
    Setup_Glossary();
    Setup_Legislation();
    Setup_Simulators();
    Setup_Help();
});

function Setup_Glossary() {
    //Esconder todas as letras no inicio
    $("#dGlossary").children("div").hide();

    //Mostrar a primeira (A)
    $("#dGlossary").children("div:first").show();
    $("#dLetterIndex").children("a:first").addClass("current");

    $("#dLetterIndex").children("a").each(function() {
        var $letter = $(this);

        //Para cada letra clickada
        $letter.click(function() {


            var $letterToShow = $("#dGlossary ." + $letter.attr("id"));
            //Se esta escondida mostra
            if ($letterToShow.css("display") == "none") {
                $("#dLetterIndex").children("a").removeClass("current");

                //Esconder todas as outras
                $("#dGlossary").children("div").fadeOut();
                
                //Mostrar a clickada
                $letterToShow.fadeIn();
                
                //Mudar estilos do index das letras
                $(this).addClass("current");
            }

            return false;

        });
    });
}

function Setup_Legislation() {
    var hash = window.location.hash;
    if (hash != "") {
        hash = hash.substring(1, hash.length);
        var $item = $("a[name='" + hash + "']").parent().siblings(".items");
        $item.slideToggle();
    }
    $("#dLegislation .legislation .lTitle").each(function() {

        $(this).click(function() {
            var $item = $(this).siblings(".items");
            $item.slideToggle();
            return false;
        });

    });

}

function Setup_Simulators() {
    var hash = window.location.hash;
    if (hash != "") {
        hash = hash.substring(1, hash.length);
        var $item = $("a[name='" + hash + "']").parent().siblings(".simulator");
        $item.slideToggle();
    }

    $("#dSimulators .items .lTitle").each(function() {
        $(this).click(function() {
            var $item = $(this).siblings(".simulator");
            $item.slideToggle();
            return false;
        });
    });
}    

function Setup_Help() {
    var hash = window.location.hash;
    if (hash != "") {
        hash = hash.substring(1, hash.length);
        var $item = $("a[name='" + hash + "']").parent().siblings(".items");
        $item.slideToggle();
    }
    $("#dHelp .helpItem").each(function() {

        $(this).children(".lTitle").click(function() {
            var $item = $(this).siblings(".text");
            $item.slideToggle();
            return false;
        });

    });

}


/* @ NEWS CAROUSEL
     ==================================================================== */

function Setup_NewsCarousel() {

    var $control = $('#cyNewsPreview');
    var $stage = $('.stage', $control);
    var $stage_ul = $('ul', $control);
    var $previous_btn, $next_btn;
    var rollingNews;
    var _delaySpeed = 5000;
    
    var _containerWidth = 0;

    var $slideshow_items = $stage_ul.children('li');
    var $slideshow_items_news = $slideshow_items.children('div');

    if (_containerWidth == 0) {
        _containerWidth = $slideshow_items.eq(0).outerWidth();
    }
    
       
    clearInterval(rollingNews);

    var _totalPages = $slideshow_items.length,
    _currentIndex = 1;

    // - clonar items para o inicio e para o fim
    var $firstItem = $slideshow_items.filter(':first'),
    $lastItem = $slideshow_items.filter(':last');
    $firstItem.before($lastItem.clone().addClass('cloned'));
    $lastItem.after($firstItem.clone().addClass('cloned'));
    $slideshow_items = $stage.children('ul').children('li');

    // - configurar largura do container da lista de fotos
    $stage_ul.width(_containerWidth * ($slideshow_items_news.size() + 4));


    // - acrescentar setas de navegação
    if (_totalPages > 1) {
    
        $previous_btn = $('<span></span>').addClass('previous');
        $next_btn = $('<span></span>').addClass('next');
        
        $previous_btn.click(function() {
            GoToPage(_currentIndex - 1);
            return false;
        });
        
        $next_btn.click(function() {
            GoToPage(_currentIndex + 1);
            return false;
        });
        
        $stage.after($previous_btn)
              .after($next_btn);
    }

    // - métodos de navegação
    function GoToPage(index) {

        // - calculo dos deslocamentos
        var direction = index < _currentIndex ? -1 : 1,
            n = Math.abs(_currentIndex - index),
            left = _containerWidth * direction * n;

        // - deslocar 
        $stage.filter(':not(:animated)').animate({
            scrollLeft: '+=' + left
        }, 500, function() {

            if (index == 0) {
                $stage.scrollLeft(_containerWidth * _totalPages);
                index = _totalPages;
            } else if (index > _totalPages) {
                $stage.scrollLeft(_containerWidth);
                index = 1;
            }

            _currentIndex = index;

        });

    };

    // - autoscroll
    if (_totalPages > 1) {
        
        var autoScrolling = true;
        
        rollingNews = setInterval(function() {
            if (autoScrolling) {
                GoToPage(_currentIndex + 1);
            }
        }, _delaySpeed);
        
        $stage.mouseover(function() {
            autoScrolling = false;
        }).mouseout(function() {
            autoScrolling = true;
        });
        
    }    

    // - init
    $stage.css('overflow', 'hidden');
    $slideshow_items.children('div').show();
    $stage.scrollLeft(_containerWidth);
    
};


