var contentWidth = 0;
var lastScroll = new Array();
var slideSpeed = 500;

jQuery(document).ready(function() {

    jQuery("#slider-content").data("currentlyMoving", false);

    jQuery('#nextArrow').hide();
    jQuery('#prevArrow').hide();

    jQuery('.chapter-wrapper li').each(function() {
        contentWidth += jQuery(this).outerWidth(true);
    });

    jQuery('#slider-content').width(contentWidth);

    checkPosition(contentWidth);

    jQuery('.chapter-item').each(function(index) {
        jQuery(this).click(function() {
            goToChapter(index, slideSpeed);
        });
    });

    jQuery('.next-container').hover(
        function () {
            var browserWidth = jQuery(window).width();
            var leftPos = jQuery('#slider-content').position().left;

            if ((browserWidth - leftPos) <= contentWidth) {
                jQuery('#nextArrow').show();
            //moveLeft();
            }
        },
        function () {
            jQuery('#nextArrow').hide();
        });

    jQuery('.prev-container').hover(
        function () {
            var leftPos = jQuery('#slider-content').position().left;
            if (leftPos >= 0) {
            //jQuery('#prevArrow').hide();
            }
            else {
                jQuery('#prevArrow').show();
            }

        },
        function () {
            jQuery('#prevArrow').hide();
        });

    jQuery('#nextArrow').click(function() {
        nextButton();
    });
    jQuery('#prevArrow').click(function() {
        previousButton();
    });

    jQuery('#more-toggle').click(function() {
        toggleMore();
        return false;
    });

    jQuery('#nextArrow').detectFlicks({
        axis: 'x',
        threshold: 60,
        flickEvent:function(d) {
            nextButton();
        }
    });

    jQuery('#prevArrow').detectFlicks({
        axis: 'x',
        threshold: 60,
        flickEvent:function(d) {
            previousButton();
        }
    });

    jQuery(document).keydown(function(e) {
        if (e.keyCode == 37) {
            previousButton();
            return false;
        }
        else if (e.keyCode == 39) {
            nextButton();
            return false;
        }
        else if (e.keyCode == 38 || e.keyCode == 40) {
            toggleMore();
            return false;
        }
        //return false;
    });

});

function toggleMore() {
    var display = jQuery('#more-list').css('display');

    jQuery('#more-list').slideToggle(300);
    jQuery('.plus').toggle();

    if (display == "none") {
        jQuery('#more-wrapper-bkg').animate({
            height: '126px'
        }, 300);
    }
    else {
        jQuery('#more-wrapper-bkg').animate({
            height: '26px'
        }, 300);
    }
}

function checkPosition(contentWidth) {
    var browserWidth = jQuery(window).width();
    var leftPos = jQuery('#slider-content').position().left;

    if (leftPos >= 0) {
        jQuery('#prevArrow').hide();
    }

    if ((browserWidth - leftPos) >= contentWidth) {
        jQuery('#nextArrow').hide();
    }

    highlightChapter();
}

function highlightChapter() {
    var leftPos = Math.abs(jQuery('#slider-content').position().left);

    jQuery('.chapter-wrapper').each(function(index) {
        if (leftPos >= jQuery(this).position().left) {
            jQuery('.chapter-item').removeClass('active');
            jQuery('.chapter-item:eq(' + index + ')').addClass('active');
        }
    });
    Cufon.replace('.chapter-item');

}


function goToChapter(item, speed) {
    var divPos = jQuery('.chapter-wrapper:eq(' + item + ')').position().left;
    var leftPos = Math.abs(jQuery('#slider-content').position().left);
    var tempWidth = 0;
    lastScroll = [];

    jQuery('.chapter-wrapper li').each(function(index) {
        var itemWidth = jQuery(this).outerWidth(true);
        tempWidth += itemWidth;

        if (tempWidth <= divPos) {
            //add width to scroll array
            lastScroll.push(itemWidth);
        }
        else {
            return false;
        }
    });

    jQuery('#slider-content').animate({
        'left': '-=' + (divPos - leftPos) + 'px'
    }, speed, function() {
        // Animation complete.
        checkPosition(contentWidth);
    });

}

function previousButton() {

    if ((jQuery("#slider-content").data("currentlyMoving") == false)) {
            jQuery("#slider-content").data("currentlyMoving", true);

        jQuery('#slider-content').animate({
            'left': '+=' + lastScroll.pop() + 'px'
        }, slideSpeed, function() {
            // Animation complete.
            checkPosition(contentWidth);
            jQuery("#slider-content").data("currentlyMoving", false);
        });
    }
}

function nextButton() {

    if ((jQuery("#slider-content").data("currentlyMoving") == false)) {
        jQuery("#slider-content").data("currentlyMoving", true);

        var leftPos = Math.abs(jQuery('#slider-content').position().left);
        var browserWidth = jQuery(window).width();

        if (browserWidth < contentWidth) {
            var tempWidth = 0;

            jQuery('.chapter-wrapper li').each(function(index) {
                var itemWidth = jQuery(this).outerWidth(true);
                tempWidth += itemWidth;
                //alert(itemWidth);

                if (tempWidth > leftPos) {
                    //begin counted width to scroll
                    if (tempWidth > (browserWidth + leftPos)) {
                        tempWidth -= (itemWidth - parseInt(jQuery(this).css('padding-left'), 10) + leftPos);
                        return false;
                    }
                }
            });

            lastScroll.push(tempWidth);

            jQuery('#slider-content').animate({
                'left': '-=' + tempWidth + 'px'
            }, slideSpeed, function() {
                // Animation complete.
                checkPosition(contentWidth);
                jQuery("#slider-content").data("currentlyMoving", false);
            });

        }
    }
}