﻿
function setupProductCarousel(containerId) {
    var numContainers = $(containerId + " .featured_items .container").length;
    var itemsWidth = $(containerId + " .featured_items .items").width();
    var currentPage = getCurrentPage(containerId, itemsWidth);

    // setup
    setFeaturedArrows(currentPage, numContainers, containerId);
    setDots(currentPage, containerId);

    // right arrow click
    $(containerId + " .arrblu.right").live("click", function () {
        var current = getCurrentLeftPosition("#latest");
        var maxLeft = 0 - (itemsWidth * (numContainers - 1));
        if (current > maxLeft) {
            var newLeft = current - itemsWidth;
            $(containerId + " .featured_items .items_inner").css("left", newLeft + 'px');
        }

        var currentPage = getCurrentPage(containerId, itemsWidth);
        setFeaturedArrows(currentPage, numContainers, containerId);
        setDots(currentPage, containerId);
    });

    // left arrow click
    $(containerId + " .arrblu.left").live("click", function () {
        var current = getCurrentLeftPosition(containerId);
        if (current !== 0) {
            var newLeft = current + itemsWidth;
            $(containerId + " .featured_items .items_inner").css("left", newLeft + 'px');
        }

        var currentPage = getCurrentPage(containerId, itemsWidth);
        setFeaturedArrows(getCurrentPage(containerId, itemsWidth), numContainers, containerId);
        setDots(currentPage, containerId);
    });

    centerImagesVertically();
}

function getCurrentPage(parentSelector, width) {
    var asInt = getCurrentLeftPosition(parentSelector);
    return Math.abs((asInt - width) / width)
}
function getCurrentLeftPosition(parentSelector) {
    var current = $(parentSelector + " .featured_items .items_inner").css("left");
    current = (current === 'auto') ? '0px' : current;

    return parseInt(current.substring(0, current.length - 2)); // remove 'px'   
}
function setDots(currentPage, parentSelector) {
    $(parentSelector + " .pagedots div").removeClass("activedot");
    $(parentSelector + " .pagedots div:nth-child(" + currentPage + ")").addClass("activedot");
}
function setFeaturedArrows(currentPage, totalContainers, parentSelector) {
    if (totalContainers === 1) { // one container - hide both
        hideLeftArrow(parentSelector);
        hideRightArrow(parentSelector);
    }
    else if (currentPage === totalContainers) { // last container - only show back
        hideRightArrow(parentSelector);
        showLeftArrow(parentSelector);
    }
    else if (currentPage === 1) { // first container - only show forward
        showRightArrow(parentSelector);
        hideLeftArrow(parentSelector);
    }
    else { // show all
        showLeftArrow(parentSelector);
        showRightArrow(parentSelector);
    }
}
function hideLeftArrow(parentSelector) {
    $(parentSelector + " .featured_items .arrblu.left img").hide();
}
function hideRightArrow(parentSelector) {
    $(parentSelector + " .featured_items .arrblu.right img").hide();
}
function showLeftArrow(parentSelector) {
    $(parentSelector + " .featured_items .arrblu.left img").show();
}
function showRightArrow(parentSelector) {
    $(parentSelector + " .featured_items .arrblu.right img").show();
}
