jQuery(document).ready(function() {
    //Initialize the prev/next image links
    if (jQuery("#item_image_next").length > 0)
        change_item_image(0);

    jQuery("#item_image_previous").click(function(event) {
        if (jQuery("#item_image_previous").hasClass("textLinkUnderline"))
            change_item_image(Number(jQuery("#item_image_current").val()) - 1);
    });
    jQuery("#item_image_next").click(function(event) {
        if (jQuery("#item_image_next").hasClass("textLinkUnderline"))
            change_item_image(Number(jQuery("#item_image_current").val()) + 1);
    });
});

//Changes to a specific image displayed for an item, and updates the previous and next links
function change_item_image(index)
{
    //Clean out extra '0' in front that javascript puts there
    index = Number(index);
    
    //Change the source of the image to the newly selected one
    jQuery("#item_image_display").attr("src", "ItemImages/" + jQuery("#item_image_" + index).val());
    jQuery("#item_image_current").val(index);
    
    //Check if there are more 'next' images.. if so, enable link otherwise disable it
    if (jQuery("#item_image_" + (index + 1)).length > 0)
        jQuery("#item_image_next").addClass("textLinkUnderline");
    else
        jQuery("#item_image_next").removeClass("textLinkUnderline");
    //Check if there are more 'previous' images.. if so, enable link otherwise disable it
    if (jQuery("#item_image_" + (index - 1)).length > 0)
        jQuery("#item_image_previous").addClass("textLinkUnderline");
    else
        jQuery("#item_image_previous").removeClass("textLinkUnderline");
}

