﻿var guidRegex = /[\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12}/;

$(document).ready(function() {
    $("a.lightbox").fancybox({
        overlayShow: true
    }).hover(function() {
        if ($("#defaultPhoto", $(this)).length < 1) {
            var newImg = $(this).attr("href").match(guidRegex);
            var productId = $(this).attr("rel").replace("p", "");

            if (selectedProductId() != productId) {
                $("#selectedProduct").val(productId);
                $("#selectedProduct").change();
            }

            changeSelectedProductImage(newImg);
        }
    });

    $("a[rel=fancyvideo]").fancybox({
        overlayShow: true,
        frameWidth: 640,
        frameHeight: 360
    });

    $("#selectedProduct").change(function() {
        var newImg = images["p" + $(this).val()];
        changeSelectedProductImage(newImg);

        hideProducts();
        showProduct($(this).val());

        var currentUrl = document.location.toString();
        if (currentUrl.match('#')) {
            document.location.replace(currentUrl.split('#')[0] + "#" + $(this).val());
        } else {
            document.location.replace(currentUrl + "#" + $(this).val());
        }

        checkProductStockStatus();
    });

    $(".inventoryOption").change(checkProductStockStatus);

    $("#frmAddToCart").validate({
        submitHandler: function(form) {
            if (isCurrentProductOutOfStock) {
                alert("The specified product is currently out of stock.");
                return false;
            } else {
                form.submit();
            }
        }
    });

    hideProducts();
    selectProductFromUrl();
    checkProductStockStatus();
});

function containsId(id) {
    var pid = "p" + id;
    var found = false;

    $.each(images, function(index, imageKey) {
        if (pid == index) {
            found = true;
        }
    });

    return found;
}

function hideProducts() {
    $(".pricing").hide();
    $(".options").hide();
    $(".options input, .options select").attr("disabled", "disabled");
}

function showProduct(id) {
    if (id == "first") {
        $(".pricing:first").show();
        $(".options:first").show();
        $(".options:first input, .options:first select").removeAttr("disabled");
    } else {
        $("#productPricing" + id).show();
        $("#productOptions" + id).show();
        $("#productOptions" + id + " input, #productOptions" + id + " select").removeAttr("disabled");
    }
}

function selectedProductId() {
    return $("#frmAddToCart :input[name=id]").val();
}

function showOutOfStockMessage() {
    if ($("#outOfStockMessage").length < 1) {
        $("input[type=image]").parent().before("<p id='outOfStockMessage'>The specified product is currently out of stock.</p>");
    }
}

function removeOutOfStockMessage() {
    $("#outOfStockMessage").remove();
}

var isCurrentProductOutOfStock;

function checkProductStockStatus() {
    $("#frmAddToCart").ajaxSubmit({
        url: "/catalog/isinstock",
        dataType: "json",
        success: function(isInStock) {
            isCurrentProductOutOfStock = !isInStock;
            if (isCurrentProductOutOfStock) {
                showOutOfStockMessage();
            } else {
                removeOutOfStockMessage();
            }
        }
    });
}

function selectProductFromUrl() {
    var currentUrl = document.location.toString();
    if (currentUrl.match('#')) {
        var id = currentUrl.split('#')[1];
        if (containsId(id)) {
            $("#selectedProduct").val(id);
            $("#selectedProduct").change();
        } else {
            showProduct("first");
        }
    } else {
        showProduct("first");
    }
}

function changeSelectedProductImage(newImg) {
    $("#defaultPhoto").attr("src", $("#defaultPhoto").attr("src").replace(guidRegex, newImg));
    var lnk = $("#defaultPhoto").parent();
    lnk.attr("href", lnk.attr("href").replace(guidRegex, newImg));
}
