﻿$(function() {

    /*var $ctx = $('#ProductTable');
    var PicturesCollection = $('#PicturesCollection', $ctx);
    
    $("a[href$='jpg']", PicturesCollection)
        .lightBox({ txtImage: 'Photo', txtOf: 'sur' })
        .mouseover(function() { $("[id$='_imgProduct']", $ctx).attr("src", $("img", this).attr("srcmedium")) });*/
    var $ctx = $('#Product');

    var PicturesPanel = $(".PicturesPanel");
    $("a[href$='jpg']", PicturesPanel).colorbox({ current: "{current} / {total}", rel: 'PicturesCollection', slideshow: false, maxHeight: '80%', maxWidth: '80%', title: function() { var altText = $("img", this).attr('alt'); return altText; } });
    $(".PictureBox", PicturesPanel).hover(function() { $(this).addClass("selected"); }, function() { $(this).removeClass("selected"); });

    if (typeof (products) != 'undefined') {
        var models = $('.rdlModelSelector td');
        models.hover(function() {
            $(this).toggleClass("highlight", true);
            ShowProduct($('.rdlModelSelector td').index(this));
        }, function() {
            $(this).toggleClass("highlight", false);
            ShowSelectedProduct();
        }).click(function() {
            $('input', this).attr("checked", "checked");
            $('.rdlModelSelector td').toggleClass("selected", false);
            $(this).toggleClass("selected", true);
            selectedId = $('.rdlModelSelector td').index(this);
            ShowSelectedProduct();
        });
    }

    var AvailabilityBox = $("[id$='_ProductAvailabilityBox']", $ctx);
    var AvailabilityTitle = $("[id$='_ProductAvailabilityTitle']", AvailabilityBox);
    var AvailabilityDelay = $("[id$='_ProductAvailabilityDelay']", $ctx);
    var PriceBox = $("[id$='_pnlPrice']", $ctx);

    var selectedId = -1;
    function ShowProduct(id) {
        ShowPrice(products[id][1], products[id][2])
        ShowAvailability(products[id][3], products[id][4])
    }

    function ShowAvailability(availability, delay) {
        AvailabilityBox.removeClass();
        switch (availability) {
            case "Available":
                AvailabilityBox.addClass("ProductAvailabilityBox_Available");
                AvailabilityTitle.text("En Stock");
                AvailabilityDelay.empty();
                break;
            case "OnSupply":
                AvailabilityBox.addClass("ProductAvailabilityBox_OnSupply");
                AvailabilityTitle.text("Disponible");
                if (delay != "")
                    AvailabilityDelay.text("Expédiée à partir du " + delay);
                break;
            case "InPreOrder":
                AvailabilityBox.addClass("ProductAvailabilityBox_InPreOrder");
                AvailabilityTitle.text("En Précommande");
                if (delay != "")
                    AvailabilityDelay.text("Expédiée à partir du " + delay);
                break;
            case "OnOrder":
                AvailabilityBox.addClass("ProductAvailabilityBox_OnOrder");
                AvailabilityTitle.text("Sur Commande");
                if (delay != "")
                    AvailabilityDelay.text("Expédiée sous " + delay + " jours ");
                break;
            case "StockOut":
                AvailabilityBox.addClass("ProductAvailabilityBox_StockOut");
                AvailabilityTitle.text("Rupture de Stock");
                AvailabilityDelay.empty();
                break;
        }
    }

    function ShowPrice(price, discount) {
        $("[id$='_lblPriceState']", PriceBox).hide();
        $('.Price', PriceBox).text(discount);

        if (discount != price) {
            var strformat = "au lieu de <span class=\"Discount\">{0}</span>";
            var str = jQuery.format(strformat, price);
            $('.pnlDiscount', PriceBox).html(str);
        }
        else
            $('.pnlDiscount', PriceBox).empty();
    }

    function ShowSelectedProduct() {
        if (selectedId != -1)
            ShowProduct(selectedId)
    }
});
