﻿var intervalId;
var timeoutId;

//This method should be used on first body load
function StartTestimonials()
{
    intervalId = setInterval(LoadNextTestimonialAuto, 5000);
}; 

function LoadNextTestimonialAuto () 
{
    LoadNext(false);
}

//This method never used in Recsel
function LoadPreviousTestimonialAuto () 
{
    LoadPrevious(false);
}

function LoadNextTestimonial () 
{
    LoadNext(true);
}

function LoadPreviousTestimonial () 
{
    LoadPrevious(true);
}

function LoadPrevious ( isManual ) {
    //assume only used on homepage for now
    LoadTestimonial ( 'Previous', 'Homepage', isManual );
}

function LoadNext ( isManual ) {
    //assume only used on homepage for now
    LoadTestimonial ( 'Next', 'Homepage', isManual );
}

function LoadTestimonial ( direction, featuredTestimonialGroupId, isManual ) {
    
    if ( isManual ) 
    {
        //if it's a manual request, then CLEAR the interval because need to stop auto rotation
        clearInterval(intervalId);
        //also clear the timeout incase it has been set (below) on another call to this method ... 
        clearTimeout(timeoutId);
        //then set a new StartTestimonials for 5 seconds time (so that the testimonials will restart automatically if the user has stopped paging them)
        //this will actually mean the wait is 7 second - 2 to the setTimeout, another 5 to the first setInterval...
        timeoutId = setTimeout(StartTestimonials, 2000);
        //also set the spinner for manual requests so user knows something is happening
        $('PrevNext').set('style', 'background-image: url(/Lib/Img/Spinner.gif);');
    }
    
    var img = $('ImageWrap').getElement('img');
    var imgLink = $('ImageWrap').getElement('a');
    var link = $('Quote').getElement('a');
    $('Quote').set('opacity', 0);
    img.set('opacity', 0);
    
    //This code will send a data object via a GET request and alert the retrieved data.
    var jsonRequest = new Request.JSON({url: "/Handlers/GetTestimonial.ashx", onComplete: function(testimonial){
        
        link.set('text', testimonial.Title);
        link.set('href', '/Testimonials/?tid=' + testimonial.TestimonialId);
        imgLink.set('href', '/Testimonials/?tid=' + testimonial.TestimonialId);
        img.set('src', testimonial.MediaUrl);
        
        //set the id in the hidden field so that next can be request for next time
        setCurrentId(testimonial.TestimonialId);
       
        $('Quote').fade(1);
        img.fade(1);
        
        if ( isManual ) 
        {
            $('PrevNext').set('style', 'background-image: none;');
        }
        
    }}).get({'tid': getCurrentId(), 'direction': direction, 'ftgi': featuredTestimonialGroupId});
}

function getCurrentId () 
{
    var currentId = $("CurrentTestimonialId").getElement('input');
    return currentId.get('value');
}

function setCurrentId (value) 
{
    var currentId = $("CurrentTestimonialId").getElement('input');
    currentId.set('value', value);
}