﻿/* 
Javascript for ProductImages
*/

jQuery(document).ready(function() {
    initProductImage();
    initThumbs();
    initColorboxes();
});

function initProductImage() {
    var thumbImages = getThumbImages();
    if (thumbImages.length > 1) {
        $('#piPrevious').mouseenter(function() {
            showProductImageButton($("#piPreviousImage"));
        });

        $('#piPrevious').mouseleave(function() {
            $("#piPreviousImage").hide();
        });

        $('#piPrevious').click(function() {
            productImageButtonClickHandler('previous');
        });

        $('#piNext').mouseenter(function() {
            showProductImageButton($("#piNextImage"));
        });

        $('#piNext').mouseleave(function() {
            $("#piNextImage").hide();
        });

        $('#piNext').click(function() {
            productImageButtonClickHandler('next');
        });
    }
    else {
        $('#piPrevious').css('cursor', 'none');
        $('#piNext').css('cursor', 'none');
    }
}

function initThumbs() {
    $('#apiThumb1').click(function(event) {
        thumbClickHandler(event, $(this));
    });

    $('#apiThumb2').click(function(event) {
        thumbClickHandler(event, $(this));
    });

    $('#apiThumb3').click(function(event) {
        thumbClickHandler(event, $(this));
    });

    $('#apiThumb4').click(function(event) {
        thumbClickHandler(event, $(this));
    });

    $('#apiThumb5').click(function(event) {
        thumbClickHandler(event, $(this));
    });

    $('#apiThumb6').click(function(event) {
        thumbClickHandler(event, $(this));
    });
}

function initColorboxes() {
    var thumbImages = getThumbImages();
    if ($("a[rel='zoom']")) {
        if (thumbImages.length > 1) {
            $("a[rel='zoom']").colorbox({ innerWidth: 600, innerHeight: 700, iframe: true, scrolling: false });
        }
        else {
            $("a[rel='zoom']").colorbox({ innerWidth: 600, innerHeight: 618, iframe: true, scrolling: false });
        }
    }
    if ($("a[rel='threeSixty']")) {
        $("a[rel='threeSixty']").colorbox({ innerWidth: 600, innerHeight: 600, iframe: true, scrolling: false });
    }
}

function thumbClickHandler(event, newActiveThumbAnchor) {
    var thumbImages = getThumbImages();
    removeActiveThumbClass(thumbImages);
    newActiveThumbAnchor.find('img').addClass('piActiveThumb');

    var currentThumbNumber = getCurrentThumbNumber();
    setSelectedThumbNumberForZoom(currentThumbNumber);
    setNewProductImage(newActiveThumbAnchor);
    
    event.preventDefault();
}

function getThumbImages() {
    var thumbImages = $('#piThumbList li img.piThumb');
    return thumbImages;
}

function setNewProductImage(newActiveThumbAnchor) {
    $('#productImageContainer .productImage img').attr('src', newActiveThumbAnchor.attr('href'));
    
}

function removeActiveThumbClass() {
    $('#piThumbList li img.piActiveThumb').removeClass('piActiveThumb');
}

function showProductImageButton(productImageButton)
{
    var thumbImages = getThumbImages();
    if (thumbImages.length > 1) {
        productImageButton.show();
    }     
}

function productImageButtonClickHandler(action) {
    var currentThumbNumber = getCurrentThumbNumber();
    var thumbImages = getThumbImages();
    var newActiveThumbIndex = getNewActiveThumbIndex(action, currentThumbNumber - 1, thumbImages.length);

    removeActiveThumbClass();
    var newActiveThumbImage = $(thumbImages[newActiveThumbIndex]);
    newActiveThumbImage.addClass('piActiveThumb');

    setSelectedThumbNumberForZoom(newActiveThumbIndex + 1);

    setNewProductImage(newActiveThumbImage.parent());
}

function getCurrentThumbNumber() {
    var currentThumbNumber = 1;
    var thumbImage = $('#piThumbList li img.piActiveThumb');
    if (thumbImage != null) {
        var thumbId = thumbImage.attr('id');
        if (thumbId != null && thumbId.length == 8) {
            var thumbNumber = parseInt(thumbId.charAt(7));
            if (!isNaN(thumbNumber)) {
                currentThumbNumber = thumbNumber;
            }
        }
    }
    return currentThumbNumber;
}

function getThumbImages() {
    var thumbImages = $('#piThumbList li img.piThumb');
    return thumbImages;
}

function getNewActiveThumbIndex(action, currentThumbIndex, numberOfThumbs) {
    var newActiveThumbIndex = 0;
    if (action == 'next') {
        // next
        newActiveThumbIndex = currentThumbIndex + 1;
        if (newActiveThumbIndex == numberOfThumbs) {
            newActiveThumbIndex = 0;
        }
    }
    else {
        // previous
        newActiveThumbIndex = currentThumbIndex - 1;
        if (newActiveThumbIndex == -1) {
            newActiveThumbIndex = numberOfThumbs - 1;
        }
    }

    return newActiveThumbIndex;
}

function removeActiveThumbClass() {
    $('#piThumbList li img.piActiveThumb').removeClass('piActiveThumb');
}

function setSelectedThumbNumberForZoom(currentThumbNumber) {
    var href = $('.zoomControl').attr('href');
    var newHref = href.slice(0, -1) + currentThumbNumber;
    $('.zoomControl').attr('href', newHref);
}