/* initialize carousels on front page */
function initCarousels() {
    $(".carousel").each(function() {
        var current_carousel = $(this);
        $(this).jcarousel({
            scroll: 1,
            buttonNextHTML: null,
            buttonPrevHTML: null,
            itemFallbackDimension: 300,
            initCallback: function(carousel) {
                $(current_carousel).parent().children('.carousel-controls-home').children('.forward').bind('click', function() {
                    carousel.next();
                    return false;
                });
                $(current_carousel).parent().children('.carousel-controls-home').children('.back').bind('click', function() {
                   carousel.prev();
                   return false;
                });
                
            }
        });
    });
};

/* initialize carousels on category page (/shop/) */
function initCategoryCarousels() {
    $(".cat-carousel").each(function() {
        var current_carousel = $(this);
        $(this).jcarousel({
            scroll: 3,
            buttonNextHTML: null,
            buttonPrevHTML: null,
            initCallback: function(carousel) {
                $(current_carousel).parent().children('.carousel-controls').children('.next').bind('click', function() {
                    carousel.next()
                    return false;
                });
                $(current_carousel).parent().children('.carousel-controls').children('.prev').bind('click', function() {
                   carousel.prev();
                   return false;
                });
                
            }
        });
    });
}

function clearText(field) {
    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;
}

$(document).ready(function () {
    // add collapse/expand buttons for #catFilter list
    $('#catFilter dl dt').prepend('<div />');
    $('#catFilter dl dt div').click(function (){
      $(this).closest('dl').toggleClass('collapsed');
    });

    $(function() {
        var date_format = "dd/mm/yy";
        var minimal = '+1d';
        var currentTime = new Date()
        if (currentTime.getHours() < 4) {
            // If you're early you can still order for today
            minimal = '0';
        };
        $.datepicker.setDefaults($.datepicker.regional['nl']);    
        $("#datepicker").datepicker({
            dateFormat: date_format,
            minDate: minimal,
            defaultDate: +1,
            showWeek: true,
            firstDay: 1,
            onSelect: function(dateText, inst) {
                var date = $.datepicker.parseDate(date_format, dateText);
                var day_of_week = (date.getDay() + 6) % 7
                $('.dagen').hide();
                $('.dagen input:checked').removeAttr('checked');
                $('#dag_'+day_of_week).show();
            }
        });
    });
    $("#datepicker").each(function() {
        // placeholder attribute isn't well supported yet
        if ($(this).val() == "") {
            $(this).val('Klik hier');
        };
    });
    
    // horizontal scrolling in shop category selection
    
    $("input.print-button").bind("click", function() {
        // Chrome won't print if you call window.print() directly
        setTimeout('window.print()', 100);
    });
    
    initCarousels();
    initCategoryCarousels();
});



