$(document).ready(function () {
    $('input[type=radio]').click(function () {
        var aUnitValues = $(this).val().split(',');
        var iFactor = parseInt(aUnitValues[0]);
        var sUnit = aUnitValues[1].toLowerCase();

        $(this).parents('tr').find('em').add($(this).parents('tr.with-wholesale').next().find('em')).each(function () {
            var aPriceParts = $(this).text().split('/');
            var fPrice = parseFloat(aPriceParts[0].replace(',', '.'));
            var sPriceUnit = aPriceParts[1].toLowerCase();
            if (sPriceUnit !== 'un') {
                fPrice /= iFactor;
            } else {
                fPrice *= iFactor;
            }
            $(this).html('<span>' + fPrice.toFixed(2).replace('.', ',') + '</span>/' + sUnit);
        });
    });

    //    var banner = {
    //        duration: 30, // em segundos
    //        height: 320,
    //        width: 700,
    //        top: 50,
    //        left: 300,
    //        imageUrl: 'images/promo/inauguracao-001.swf',
    //        linkUrl: ''
    //    };

    $.getJSON('/suplementos_alimentares/getfloater.php', {}, function (banner) {
        if (banner.duration) {
            $('#floater-content').width(banner.width).height(banner.height);
            $('#floater').offset(banner).show(function () {
                switch (banner.imageType) {
                    case 'swf':
                        $('#floater-content').flashembed({
                            src: banner.imageUrl,
                            height: banner.height,
                            width: banner.width,
                            wmode: 'opaque',
                            w3c: true
                        });

                        break;

                    default:
                        $('#floater-content').html('<a href="'
                            + banner.linkUrl + '"><img src="'
                            + banner.imageUrl + '" width="' + banner.width
                            + '" height="' + banner.height + '"></a>');
                        break;
                }
            })
            .width(banner.width)
            .height(banner.height + $('#floater-progress').height())
            .everyTime(50, function (i) {
                $('#floater-progress').width(banner.width * (1 - i/(20 * banner.duration)));
                if (i == 20 * banner.duration) {
                    $(this).hide('slow');
                }
            });
            $('#floater-close').click(function () {
                $('#floater').hide('slow');
            });
        }
    });
    var iBannerAtual = 1;
    var iQtdBanners = $('#banners-tv .banner-tv').size();
    if (iQtdBanners > 1) {
        $("#canais").fadeIn('slow').find('span:gt(' + (iQtdBanners-1) + ')').remove();
        $('#banners-tv').everyTime('10s', 'canais', function (i) {
            $(this).find('#banner-'+iBannerAtual).fadeOut('slow');
            $('#canal-' + iBannerAtual).removeClass('current');
            iBannerAtual++;
            if (iBannerAtual > iQtdBanners) {
                iBannerAtual = 1;
            }
            $(this).find('#banner-'+iBannerAtual).fadeIn('slow');
            $('#canal-' + iBannerAtual).addClass('current');
        });
        $('#canais span').click(function () {
            $(this).parent().children().removeClass('current');
            iBannerAtual = parseInt($(this).addClass('current').html());
            $('#banners-tv').find('.banner-tv:visible').fadeOut('slow');
            $('#banner-' + iBannerAtual).fadeIn('slow');
            $('#banners-tv').stopTime('canais');
        });
    } else {
        $('#canais').hide();
    }
});

