
// Piscines / commandes js wiring

TC_CONTENT = "<h2>Conditions de vente & livraison</h2><p>Livraison: dans les 15 jours date de commande<br/>Frais de prise en charge par commande: 9.90 CHF<br/>Conditions de paiement: net à 30 jours<br/>Pas de reprise pour les emballages ouverts</p>";
TC_PROMPT = "Livraison: dans les 15 jours date de commande\nFrais de prise en charge par commande: 9.90 CHF\nConditions de paiement: net à 30 jours\nPas de reprise pour les emballages ouverts";

jQuery(function() {
    $('#tclink').fancybox({content:TC_CONTENT});
});
jQuery(function() {
        var total_span = $("#order_total");
        var inputs = $('.table-info .quantity INPUT');
        var buttons = $('.table-info .quantity .form-item');
        if(!total_span) return; 

        function _update_total() {
            var total = 0;
            inputs.each(function () {
                    var qty = $(this).val();
                    if (qty) {
                        total += parseFloat(qty) * jQuery.data(this, "price");
                    }
                });
            total_span.text(total.toFixed(2).replace(".", ","));
        }
        inputs.each(function () {
                $(this).change(_update_total);
                var price_text = $(".prise", $(this).parents("tr")).text().replace("CHF ", "");
                jQuery.data(this, "price", parseFloat(price_text));
            });
        buttons.each(function () {
                var input = $("INPUT", $(this).parents("tr"));
                $(this).click(function () {
                        var val = parseInt(input.val());
                        if (!val) val = 0;
                        if ($(this).text()=="-") {
                            if (val>0) { 
                                val = val - 1;
                            }
                        } else if ($(this).text()=="+") {
                            val = val + 1;
                        }
                        $(input).val(val || "");
                        _update_total();
                    });
            });
        _update_total();
    
    $("#order-form").submit(function () { return confirm(TC_PROMPT);});
})

jQuery(function () {
    // fix titles
	jQuery('a[rel*="fancybox"]').each(function () {
            var m = $(this).attr("title").match(/<b>(.*)<\/b>\s+(.*)/);
            var stripped_title;
            if (m) {
                stripped_title = m[1];
                if (m[2]) stripped_title += " : " + m[2];
            } else {
                stripped_title = $(this).attr("title");
            }
            $(this).attr("title", stripped_title);
        });
    });



function isScrolledIntoView(scrollable, elem)
{
    var docViewTop = $(scrollable).scrollTop();
    var docViewBottom = docViewTop + $(scrollable).height();

    var elemTop = $(elem).position(scrollable).top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
}

$(document).ready(function () {
    // ensure submenu is always shown
        setTimeout(function () {
                $('#sidebar .box .vscrollable').each(function () {
                        var active_li = $('LI.active', this);
                        var scrollable = $('.scroll-content', this);
                        if (active_li && scrollable && !isScrolledIntoView(scrollable, active_li)) {
                            scrollable.scrollTo(active_li, {duration:500});
                        }
                    });
            }, 500);
    });
