var pageLoaded = false;
/*----- SlideShow Code -----*/
$(document).ready(function() {

    //---------- sliding window ----------//
    var slidingElement;
    var boxWidth;
    var elementWidth;
    var currentPosition;
    var currentImage = 0;
    var goingNext = true;

    elementsLoaded = function() {
        pageLoaded = true;

        // Element to be slid inside of overflow box
        slidingElement = $('div.slideInner table');

        // Width of the container (with css overflow applied)
        boxWidth = $('div.slideInner').width();

        // width of element inside of 
        elementWidth = $(slidingElement).width() - boxWidth;

        // get the position of the sliding element
        currentPosition = $(slidingElement).css('left');

        currentPosition = currentPosition.split('px');
        currentPosition = currentPosition[0];
    }


    function previousSlide() {
        if (pageLoaded == false) {
            elementsLoaded();
        }
        // create a positive number for comparison
        var absPositionValue = Math.abs(currentPosition);

        if (absPositionValue > 0) {
            // subtract the width of the visible area from the current position
            currentPosition = (eval(currentPosition) + boxWidth);

            if (currentPosition == 0) {
                currentImage = 0;
                goingNext = true;
                // if slider is back to starting position disable previous button
                $('div.leadingEdgeSlideShow a.previousBtn').addClass('disabled');
            } else {
                currentImage--;
            }

            var cssPosition = currentPosition + "px";

            $(slidingElement).each(function() {
                $(this).animate({ left: cssPosition }, { duration: 500, easing: 'easeOutQuad' });
            });

            // if previous button clicked remove disabled class from the next button
            $('div.leadingEdgeSlideShow a.nextBtn').removeClass('disabled');
        }
        return false;
    }

    function nextSlide() {
        if (pageLoaded == false) {
            elementsLoaded();
        }
        // create a positive number for comparison
        var absPositionValue = Math.abs(currentPosition);

        // if the current position is less the the width of the sliding element
        if (absPositionValue <= (elementWidth - boxWidth)) {
            // subtract the width of the visible area from the current position
            currentPosition = (currentPosition - boxWidth);

            if (Math.abs(currentPosition) >= (elementWidth - boxWidth)) {
                // if slider is back to starting position disable previous button
                currentImage++;
                goingNext = false;
                $('div.leadingEdgeSlideShow a.nextBtn').addClass('disabled');
            } else {
                currentImage++;
            }

            var cssPosition = currentPosition + "px";

            $(slidingElement).each(function() {
                $(this).animate({ left: cssPosition }, { duration: 500, easing: 'easeOutQuad' });
            });
            // if next button clicked remove disabled class from the previous button
            $('div.leadingEdgeSlideShow a.previousBtn').removeClass('disabled');
        }
        return false;
    }

    // on click of the previous button
    $('div.leadingEdgeSlideShow a.previousBtn').click(function() {
        clearInterval(playSlidesInterval);
        previousSlide();
    });

    // on click of the next button
    $('div.leadingEdgeSlideShow a.nextBtn').click(function() {
        clearInterval(playSlidesInterval);
        nextSlide();
    });

    function playSlides() {
        if (goingNext) {
            nextSlide();
        } else {
            previousSlide();
        }
    }
    slideShowExists = $('div.leadingEdgeSlideShow');
    var playSlidesInterval;
    if (slideShowExists.length > 0) {
        playSlidesInterval = setInterval(function() { playSlides() }, 4000);
    }

    var allTitles = $('table.healthServices h3');
    var allSubNav = $('table.healthServices tr td > ul');
    $(allSubNav).hide();
    $(allTitles).click(function() {
        var currentSection = $(allTitles).index($(this));
        $(allTitles[currentSection]).toggleClass('expanded');
        $(allSubNav[currentSection]).toggle();
    });
});
