﻿
$(document).ready(function () {
    FixHeight();

    $('.IngredientScroller').jCarouselLite({
        btnNext: ".ButtonNext",
        btnPrev: ".ButtonPrevious",
        speed: 1100,
        scroll: 3,
        visible: 7
    });

    $(".DatePicker").datepicker($.datepicker.regional["nl"]);

});

var maxDLHeight = 20;
function FixHeight() {
    $(".SameHeight").css("min-height", "100px");
    $(".SameHeight").each(function () {
        var h = parseInt($(this).css("height").replace("px", ""));
        //alert(h);
        if (h != NaN && h > maxDLHeight) {
            maxDLHeight = h;
        }
    });

    $(".SameHeight").css("min-height", maxDLHeight + "px");
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}


var shownindex = 0;

function scrollToPhoto(index, controlId, distance, finishFunction) {
    clearInterval(intBanner);
    var di = 0;

    if (shownindex < index) {
        di = (index - shownindex) * distance;
        Scroll(controlId, -di, finishFunction);
    }
    else {
        di = (shownindex - index) * distance;
        Scroll(controlId, di, finishFunction);
    }

    shownindex = index;
}


var m = 0;
function Scroll(controlId, distance, finishFunction) {
    $("#" + controlId).stop(true, true);
    calculateMargin(controlId);
    $("#" + controlId).animate({ marginLeft: (m + distance) + "px" }, 1250, finishFunction);
}

function calculateMargin(controlId) {
    m = Number($("#" + controlId).css("margin-left").replace('px', ''));
    if (isNaN(m)) {
        m = 0;
    }
    return true
}

var intBanner;
setBannerInterval();

function setBannerInterval() {
    intBanner = setInterval("showNextBanner('scrollContainer', 659)", 5000);
}

function showNextBanner(controlId, distance) {
    var max = document.getElementById("hfMax" + controlId);
    if (max != null) {
        if (shownindex < max.value)
            scrollToPhoto(shownindex + 1, controlId, distance, null);
        else
            scrollToPhoto(0, controlId, distance, null);
        setBannerInterval();
    }
}

function showPreviousBanner(controlId, distance) {
    var max = document.getElementById("hfMax" + controlId);
    if (max != null) {
        if (shownindex == 0)
            scrollToPhoto(max.value, controlId, distance, null);
        else
            scrollToPhoto(shownindex - 1, controlId, distance, null);
        setBannerInterval();
    }
}

function urlencode(str) {
    str = escape(str);
    str = str.replace('+', '%2B');
    str = str.replace('%20', '+');
    str = str.replace('*', '%2A');
    str = str.replace('/', '%2F');
    str = str.replace('@', '%40');
    return str;
}



