﻿$(document).ready(function () {
    var $testID = new Array();
    var $testShowing = 1;
    var $testimonial;

    $.get('testimonials.xml', function (data) {

        $(data).find('testimonial').each(function () {
            $testimonial = $(this);

            $testID.push($(this).attr('id'));
            if ($testShowing == $(this).attr('id')) {
                $('#lbl_text').text($testimonial.find('summary').text());
                $('#lbl_author').text($(this).attr('author'));
                $('#lbl_text, #lbl_author').fadeIn('1000');
            }
        });

        var $testLen = $testID.length;

        $('#lbl_page').text($testShowing + "/" + $testLen);

        var timedEvent = setInterval(function () {
            $('#lbl_text, #lbl_author').hide();

            if ($testShowing < $testLen) {

                $testShowing++;
                $('#lbl_page').text($testShowing + "/" + $testLen);

                $(data).find('testimonial').each(function () {
                    $testimonial = $(this);
                    if ($testShowing == $(this).attr('id')) {

                        $('#lbl_text').text($testimonial.find('summary').text());
                        $('#lbl_author').text($(this).attr('author'));
                    }
                });
            }
            else {

                $testShowing = 1;
                $('#lbl_page').text($testShowing + "/" + $testLen);

                $(data).find('testimonial').each(function () {
                    $testimonial = $(this);
                    if ($testShowing == $(this).attr('id')) {
                        $('#lbl_text').text($testimonial.find('summary').text());
                        $('#lbl_author').text($(this).attr('author'));
                    }
                });
            }

            $('#lbl_text, #lbl_author').fadeIn('1000');
        }, 8000);

        $('#link_prev, #link_next').click(function () {

            var $ifTest;
            var $show;
            var $advanceBy;

            $('#lbl_text, #lbl_author').stop();

            clearInterval(timedEvent);

            $('#lbl_text, #lbl_author').hide();

            if ($(this).attr('id') == "link_prev") {
                $ifTest = 1;
                $show = $testLen;
            }
            else {
                $ifTest = $testLen;
                $show = 1;
            }

            if ($testShowing == $ifTest) {

                $testShowing = $show;
                $('#lbl_page').text($testShowing + "/" + $testLen);

                $(data).find('testimonial').each(function () {
                    $testimonial = $(this);
                    if ($testShowing == $(this).attr('id')) {

                        $('#lbl_text').text($testimonial.find('summary').text());
                        $('#lbl_author').text($(this).attr('author'));
                    }
                });
            }
            else {
                if ($(this).attr('id') == "link_prev") {
                    $advanceBy = $testShowing--;
                }
                else {
                    $advanceBy = $testShowing++;
                }
                $('#lbl_page').text($testShowing + "/" + $testLen);

                $(data).find('testimonial').each(function () {
                    $testimonial = $(this);
                    if ($testShowing == $(this).attr('id')) {
                        $('#lbl_text').text($testimonial.find('summary').text());
                        $('#lbl_author').text($(this).attr('author'));
                    }
                });
            }

            $('#lbl_text, #lbl_author').fadeIn('1000');

        });

        timedEvent;
    });
});
