﻿/*************************************************************
 * StartTestimonialTicker - begins the ticker process
 * * * * *
 * ctrl_body_id - The DIV or SPAN that will hold the body text.
 * ctrl_auth_id - The DIV or SPAN that will hold the author's name.
 * t_body - An array of Testimonial bodies.
 * t_auth - An array of Testimonial authors.
 *************************************************************/

var isPaused = true;
var isFirstLoad = true; // if true, we choose a random first testimonial.
var currentTickerIndex = 0;
var testimonialCount = 0;
var fadeTime = 500;
var timeout = 10000;

var timerMain;
var timerFadeOut;

function StartTicker() {
    testimonialCount = t_body.length;
    isPaused = false;
    
    if (isFirstLoad) {
        //currentTickerIndex = GetRandomTestimonial();
        isFirstLoad = false;
    }
    GoToFirst();
    
    // main loop body
    timerMain = window.setInterval(function(){
        if (!isPaused) {
            GoToNext();
        }
        else
            window.clearInterval(timerMain);
    }, timeout);
}

function PauseTicker () {
    isPaused = true;
    window.clearInterval(timerMain);
    window.clearTimeout(timerFadeOut);
}

function GoToFirst() {
    $('#ticker').fadeOut(fadeTime, function () {
        currentTickerIndex = 0;
        UpdateBodyAndAuthor();
        TickerFadeIn();
    });
}

function GoToNext () {
    $('#ticker').fadeOut(fadeTime, function(){
        AdvanceCounter();
        UpdateBodyAndAuthor();
        TickerFadeIn();
    });    
}

function GoToPrevious () {
    $('#ticker').fadeOut(fadeTime, function(){
        BackCounter();
        UpdateBodyAndAuthor();
        TickerFadeIn();
    });  
}

function AdvanceCounter () {
    if (currentTickerIndex == testimonialCount - 1)
        currentTickerIndex = 0;
    else
        currentTickerIndex++;
}

function BackCounter () {
    if (currentTickerIndex == 0)
        currentTickerIndex = testimonialCount - 1;
    else
        currentTickerIndex--;
}

function UpdateBodyAndAuthor () {
    document.getElementById("t_body").innerHTML = '"' + t_body[currentTickerIndex] + '"';
    document.getElementById("t_auth").innerHTML = '&mdash; ' + t_auth[currentTickerIndex];
}

function TickerFadeIn () {
    $('#ticker').fadeIn(fadeTime);
}

function TickerFadeOut () {
    $('#ticker').fadeOut(fadeTime);
}

function GetRandomTestimonial () {
    return Math.floor(Math.random()*testimonialCount);
}