﻿var animInterval = 4000;
var animSpeed = 900;
var delayInterval = 30000;

function computeMaxHeight() {
    var maxHeight = 363;
    $('.gp_content > div').each(function() {
        if ($(this).height() > maxHeight) maxHeight = $(this).height();
    });

    $('.gp_content > div').css('height', maxHeight);

    var liN = $('.gp_slider ul li').length;
    var liHeight = parseInt((maxHeight+26-liN) / liN);
    $('.gp_slider ul li').css('height', liHeight);
    $('.gp_slider ul li:not(:last)').css('border-bottom', 'solid 1px #dedede');
    $('.gp_slider').css('height', maxHeight + 26 + 'px');
    $('.gp_center').css('height', maxHeight + 26 + 'px');
    $('.gp_content').css('height', maxHeight + 26 + 'px');

    $('.gp_slider ul li a').each(function() {
        var top = (liHeight / 2) - ($(this).height() / 2);
        $(this).css('top', top + 'px');
    });
}

function bindClicks() {
    $('.gp_slider ul li a').click(function(e) {
        stopAnimation();
        delayAnimation();
        activateLink(this);
        return false;
    });
}

function activateLink(a) {

    if ($(a)[0].href) {
        var $active = $('.gp_content > div.active');
        if ($active.length == 0) $active = $('.gp_content > div:first');

        var $next = $('#' + $(a)[0].href.split('#')[1]);
        if ($next[0] != $active[0]) {
            $active.addClass('last-active');
            $next
                .css({ opacity: 0.0 })
                .addClass('active')
                .animate({ opacity: 1.0 }, animSpeed, function() {
                    $active.removeClass('active last-active');
                });

            moveIndicator($(a).parent(), $(a)[0].innerHTML);
        }
    }
}

function moveIndicator($li, text, skipAnimation) {
    var $activeLI = $('.gp_slider ul li.active')
    if ($activeLI.length > 0) $activeLI.removeClass('active');
    $li.addClass('active');

    var position = $li.offset().top - $li.parent().offset().top + ($li.height() / 2) - 29;
    var $ind = $('.gp_center .indicator');
    var $next = $ind.children('.nextValue');
    var $old = $ind.children('.oldValue');

    var indTop = $ind.offset().top - $li.parent().offset().top;
    var startTop = position - indTop;
    var endTop = indTop - position;

    $next.html('<span>' + text + '</span>');
    if (!skipAnimation) {
        $ind.animate({ top: position }, animSpeed);
        $next.css({ top: startTop }).animate({ top: 13 }, animSpeed);
        $old.css({ top: 13 }).animate({ top: endTop }, animSpeed, function() {
            $old.html($next.html());
        });
    } else {
        $ind.css({ top: position });
        $old.html($next.html());
    }
}

function startAnimation() {
    $('.gp_slider').addClass('animate');
    window['IndicatorTimeout'] = setInterval(function() { animateIndicator(); }, animInterval);
}

function stopAnimation() {
    if ($('.gp_slider').hasClass('animate')) {
        clearTimeout(eval(IndicatorTimeout));
        $('.gp_slider').removeClass('animate');
    }
}

function delayAnimation() {
    if (delayInterval > 0) {
        if (!$('.gp_slider').hasClass('animate')) {
            if ($('.gp_slider').hasClass('delay')) {
                $('.gp_slider').removeClass('delay');
                clearTimeout(eval(DelayTimeout));
            }
            window['DelayTimeout'] = setInterval(function() {
                startAnimation();
                clearTimeout(eval(DelayTimeout));
            }, delayInterval);
            $('.gp_slider').addClass('delay');
        }
    }
}

function animateIndicator() {
    var $activeLI = $('.gp_slider ul li.active')
    if ($activeLI.length == 0) $activeLI = $('.gp_slider ul li:first');
    var $nextLIs = $activeLI.nextAll();
    var $nextLI;
    if ($nextLIs.length > 0) {
        for (var i = 0; i < $nextLIs.length; i++) {
            if ($($nextLIs[i]).children('a')[0].href) {
                $nextLI = $($nextLIs[i]);
                break;
            }
        }
    }
    if (!$nextLI) $nextLI = getFirstLI();
    activateLink($nextLI.children('a')[0]);
}

function getFirstLI() {
    var $allLI = $('.gp_slider ul li');
    for (var i = 0; i < $allLI.length; i++) {
        if ($($allLI[i]).children('a')[0].href) {
            return $($allLI[i]);
            break;
        }
    }
}

$(document).ready(function() {
    computeMaxHeight();
    bindClicks();
    $('.gp_content > div:first').addClass('active');
    $li = getFirstLI();
    if ($li) {
        $li.addClass('active');
        //$('.gp_center .indicator').children('.oldValue').html('<span>' + $('.gp_slider ul li.active').children('a').html() + '</span>');
        moveIndicator($li, $li.children('a').html(), true);
        startAnimation();
    } else {
        $('.gp_center .indicator').hide();
    }
});