﻿/// <reference path="jquery-1.4.1-vsdoc.js" />
/// <reference path="vtex.common.js" />
/// <reference path="vtex.jsevents.js" />
/// <reference path="vtex.skuEvents.js" />

$(document).ready(function() {
    var skuPriceListener = new Vtex.JSEvents.Listener('skuPriceListener', SkuPrice_OnSkuDataReceived);
    skuEventDispatcher.addListener(skuDataReceivedEventName, skuPriceListener);
});

function SkuPrice_OnSkuDataReceived(e) {
    if (e.skuData.id > 0) {
        var pi = e.productIndex;
        if (pi == undefined) { pi = 0; }
        if (e.skuData.availability == false) {
            $('.valor-de[productIndex='+pi+']').attr('style', 'display:none');
            $('.valor-por[productIndex=' + pi + ']').attr('style', 'display:none');
            $('.valor-dividido[productIndex=' + pi + ']').attr('style', 'display:none');
            $('.preco-a-vista[productIndex=' + pi + ']').attr('style', 'display:none');
            $('.economia-de[productIndex=' + pi + ']').attr('style', 'display:none'); 
        }
        else {
            $('.valor-por[productIndex=' + pi + ']').attr('style', 'display:block');
            if (e.skuData.price < e.skuData.listPrice) {
                $('.valor-de[productIndex=' + pi + ']').attr('style', 'display:block');
                $('.skuListPrice[productIndex=' + pi + ']').html('R$ ' + FormatNumber(e.skuData.listPrice));
            }
            else {
                $('.valor-de[productIndex=' + pi + ']').attr('style', 'display:none');
            }


            $('.skuBestPrice[productIndex=' + pi + ']').html('R$ ' + FormatNumber(e.skuData.price));

            if (e.skuData.bestInstallmentNumber > 1) {
                $('.valor-dividido[productIndex=' + pi + ']').attr('style', 'display:block');
                $('.preco-a-vista[productIndex=' + pi + ']').attr('style', 'display:none');
            }
            else {
                $('.valor-dividido[productIndex=' + pi + ']').attr('style', 'display:none');
                $('.preco-a-vista[productIndex=' + pi + ']').attr('style', 'display:block');
            }
            $('.skuBestInstallmentNumber[productIndex=' + pi + ']').html(e.skuData.bestInstallmentNumber);
            $('.skuBestInstallmentValue[productIndex=' + pi + ']').html('R$ ' + FormatNumber(e.skuData.bestInstallmentValue));
        }
    }
}
