var testimonialId = 1;
var activeTestimonialId = 0;
var curActiveBtn = 'btn_testimonial8';
var testimonialLength = 8;
var interval;
var intervalLength;
var animationInProgress = false;
var animationSpeed = '400'; //in milliseconds

function rotateTestimonial(intervalInput) {
    
    intervalLength = intervalInput;
    interval=setInterval("rotateNext()", parseInt(intervalInput));
}

function rotateNext(){
    animationInProgress = true;
    var callback = {
        success : function(o) {
            if (isSessionValid(o.responseText)){
                if (o.responseText.indexOf("FALSE") > -1) {
                    //do something for bad
                    return false;
                } else {
                    //do something for good
                    var values = o.responseText.split('|');
                    testimonialId = values[0];                   
                    $("#testimonials").fadeOut(animationSpeed, function() {
                        document.getElementById('testimonial').innerHTML = values[1];
                        document.getElementById('citation').innerHTML = values[2];
                        if(values.length > 2 && values[3].length > 0) {
                            document.getElementById('testimonial-image').style.display = 'block';
                            document.getElementById('testimonial-image').src = '/images/testimonials/'+values[3];
                        } else {
                            document.getElementById('testimonial-image').style.display = 'none';
                        }
                        $("#testimonials").fadeIn(animationSpeed, function(){
                            animationInProgress = false;
                        });
                        
                    } );

                    return true;
                }
            } else {
                self.location.href="/login";
            }
        },
        failure : function(o) {
        //do nothing
        }
    };
    if(activeTestimonialId < testimonialLength){
        activeTestimonialId++;
    }else{
        activeTestimonialId = 1;
    }

    if (curActiveBtn != null){
        document.getElementById(curActiveBtn).className = 'nav_button';
    }
    curActiveBtn = new String('btn_testimonial' + activeTestimonialId);
    document.getElementById(curActiveBtn).className = 'nav_active';
    var conn = YAHOO.util.Connect.asyncRequest("GET", "/get/testimonial/" + activeTestimonialId, callback);
        
}


function getTestimonial(testimonialId){
    animationInProgress = true;
    var callback = {
        success : function(o) {
            if (isSessionValid(o.responseText)){
                if (o.responseText.indexOf("FALSE") > -1) {
                    //do something for bad
                    return false;
                } else {
                    //do something for good
                    var values = o.responseText.split('|');
                    animationInProgress = true;
                    $("#testimonials").fadeOut(animationSpeed, function() {
                        document.getElementById('testimonial').innerHTML = values[1];
                        document.getElementById('citation').innerHTML = values[2];
                        if(values.length > 2 && values[3].length > 0) {
                            document.getElementById('testimonial-image').style.display = 'block';
                            document.getElementById('testimonial-image').src = '/images/testimonials/'+values[3];
                        } else {
                            document.getElementById('testimonial-image').style.display = 'none';
                        }
                        $("#testimonials").fadeIn(animationSpeed, function(){
                            animationInProgress = false;
                        });
                        
                    } );

                    return true;
                }
            } else {
                self.location.href="/login";
            }
        },
        failure : function(o) {
        //do nothing
        }
    };
    clearInterval(interval);
    rotateTestimonial(intervalLength);
    activeTestimonialId = testimonialId;
    if (curActiveBtn != null){
        document.getElementById(curActiveBtn).className = 'nav_button';
    }
    curActiveBtn = new String('btn_testimonial' + activeTestimonialId);
    document.getElementById(curActiveBtn).className = 'nav_active';


    var conn = YAHOO.util.Connect.asyncRequest("GET", "/get/testimonial/" + testimonialId, callback);
        
}


function nextTestimonial(){
    if(!animationInProgress){
        rotateNext();
    }      
    clearInterval(interval);
    rotateTestimonial(intervalLength);
}

function prevTestimonial(){
    if (!animationInProgress){
        animationInProgress = true;
        var callback = {
            success : function(o) {
                if (isSessionValid(o.responseText)){
                    if (o.responseText.indexOf("FALSE") > -1) {
                        //do something for bad
                        return false;
                    } else {
                        //do something for good
                        var values = o.responseText.split('|');
                        testimonialId = values[0];
                        $("#testimonials").fadeOut(animationSpeed, function() {
                            document.getElementById('testimonial').innerHTML = values[1];
                            document.getElementById('citation').innerHTML = values[2];
                            if(values.length > 2 && values[3].length > 0) {
                                document.getElementById('testimonial-image').style.display = 'block';
                                document.getElementById('testimonial-image').src = '/images/testimonials/'+values[3];
                            } else {
                                document.getElementById('testimonial-image').style.display = 'none';
                            }
                            $("#testimonials").fadeIn(animationSpeed, function(){
                                animationInProgress = false;
                            });
                            
                        } );

                        return true;
                    }
                } else {
                    self.location.href="/login";
                }
            },
            failure : function(o) {
            //do nothing
            }
        };
        clearInterval(interval);
        rotateTestimonial(intervalLength);

        if(activeTestimonialId != 1 && activeTestimonialId != 0){
            activeTestimonialId--;
        }
        if (activeTestimonialId == 1) {
            activeTestimonialId = 8;
        }
        if (activeTestimonialId == 0){
            activeTestimonialId = 1
        }


        if (curActiveBtn != null){
            document.getElementById(curActiveBtn).className = 'nav_button';
        }
        curActiveBtn = new String('btn_testimonial' + activeTestimonialId);
        document.getElementById(curActiveBtn).className = 'nav_active';
        var conn = YAHOO.util.Connect.asyncRequest("GET", "/get/testimonial/" + activeTestimonialId, callback);
    }

}

