var ieversion = 10;
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
    var ieversion = new Number(RegExp.$1)
}

//  Open Tell-a-Friend Popup:
function popupTellAFriend(url) {
    var win = window.open(url, '', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=700,height=793');
    return false;
}

// Redirect:
function redir(url) {
    window.location = url;
    return false;
}

// Create External Links:
function add_external_links() {
    var img = ' <img src="/assets/omring/images/newwindow.png" alt="externe website" title="Deze link verwijst naar een externe website" />';
    $('a.external_url').each(function (i) {
        $(this).attr('title', function () {
            return "Deze link verwijst naar een externe website";
        });
        $(this).removeClass('external_url');
        $(this).html($(this).html() + img);
    });
}

// Main Menu Foldout
function popupMainMenuFoldOut(el, action) {
    //  OnMouseOut - Hide popup:
    if (action == 'hide') {
        $(el).removeClass('active');
        $(el).children().each(function () {
            if ($(this, $(el)).is('div')) {
                $(this, $(el)).addClass('offview');
            }
        });
    }
    else {
        //  OnMouseOver - Show popup:
        $(el).addClass('active');
        $(el).children().each(function () {
            if ($(this, $(el)).is('div')) {
                $(this, $(el)).removeClass('offview');
            }
        });
    }
}

// Situations Menu Foldout
function popupSituationsFoldOut(el) {
    if ($(el).parent().parent().attr('style')) {
        $('div.foldout_footer').removeAttr('style');
        $(el).parent().removeAttr('style');
        $(el).parent().parent().removeAttr('style');
        $(el).html('Meer situaties');
        $(el).parent().children().each(function () {
            if ($(this).hasClass('menu_situations_extra')) {
                $(this).addClass('offview')
            }
        });
    }
    else {
        $('div.foldout_footer').attr('style', 'width: 779px;');
        $(el).parent().attr('style', 'width: 764px;');
        $(el).parent().parent().attr('style', 'width: 803px; position: absolute; z-index: 20;');
        $(el).html('Menu sluiten');
        $(el).parent().children().each(function () {
            if ($(this).hasClass('menu_situations_extra')) {
                $(this).removeClass('offview')
            }
        });
    }
    //  
    return false;
}
// Vacancies Menu Foldout
function popupVacanciesFoldOut(el) {
    if ($(el).parent().parent().attr('style')) {
        $('div.foldout_footer').removeAttr('style');
        $(el).parent().removeAttr('style');
        $(el).parent().parent().removeAttr('style');
        $(el).html('Meer vacatures');
        $(el).parent().children().each(function () {
            if ($(this).hasClass('menu_situations_extra')) {
                $(this).addClass('offview')
            }
        });
    }
    else {
        $('div.foldout_footer').attr('style', 'width: 779px;');
        $(el).parent().attr('style', 'width: 764px;');
        $(el).parent().parent().attr('style', 'width: 803px; position: absolute; z-index: 20;');
        $(el).html('Menu sluiten');
        $(el).parent().children().each(function () {
            if ($(this).hasClass('menu_situations_extra')) {
                $(this).removeClass('offview')
            }
        });
    }
    //  
    return false;
}

// Toggle Faq Description:
function toggleFaqDescription(el) {
    $(el).parent().children().each(function () {
        if ($(this).hasClass('foldout')) {
            if ($(this).css('display') == 'none') {
                $(this).show();
                $(this).parent().children('.bullet').hide();
            }
            else {
                $(this).hide();
                $(this).parent().children('.bullet').show();
            }
        }
    });
    return false;
}

//  PhotoBook Prev/Next Buttons:
function swapPhotoBookImage(el, action) {
    var currentId, newId;
    //  Get total number of images:
    var numImages = parseInt($('.hfld_numImages').val());
    //  Get id of current and new image:
    if (action == 'prev') {
        currentId = parseInt($(el)[0].id.replace('btnPrev_', ''));
        newId = (currentId == 1) ? numImages : currentId - 1;

    }
    else {
        currentId = parseInt($(el)[0].id.replace('btnNext_', ''));
        newId = (currentId == numImages) ? 1 : currentId + 1;
    }
    //  Hide current image:
    $('#image' + currentId).hide();
    $('#imageContainer' + currentId).hide();
    //  Fade in requested image:
    $('#imageContainer' + newId).show();
    $('#image' + newId).fadeIn(800);
}

// Resolution check:
function checkResolution() {
    var res, actual_res, w;
    res = GetCookie('resolution');
    //  Get screen width:
    w = screen.width; // -> gaat mis met 2 monitoren en 1tje op high en de andere op low
    //
    if (window.innerWidth) {
        w = window.innerWidth;
    } else {
        if (!(document.documentElement.clientWidth == 0)) { //strict mode
            w = document.documentElement.clientWidth;
        } else { //quirks mode
            w = document.body.clientWidth;
        }
    }
    //  Resize/reset Cookie/reload if necessary:
    actual_res = 1024; //default
    if (w >= 1280) {
        actual_res = 1280;
    }
    if (res == null || res == 'undefined' || actual_res != res) {
        //
        SetCookie('resolution', actual_res);
        if (actual_res != res && navigator.cookieEnabled) {
            location.href = location.href; // dit gaat mis zonder cookies
        }
    }
}

function GetCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}


function SetCookie(name, value) {
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;

    document.cookie = name + "=" + escape(value) +
        ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
        ((path == null) ? "" : ("; path=" + path)) +
        ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
}


function DeleteCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = GetCookie(name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}


function getCookieVal(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

if (window.addEventListener) {
    window.addEventListener("load", checkResolution, false);
} else if (window.attachEvent) {
    window.attachEvent("onload", checkResolution);
}


/*  Kraamzorg Timeline Scripts */
function enableCalendar(tbDateID) {
    //  Get current date:
    var dateObj = new Date();
    var currentDate = dateObj.getDate() + '-' + (dateObj.getMonth() + 1) + '-' + dateObj.getFullYear();
    //  Init. Calendar:
    $('#cal_setDate').datepicker({
        prevText: '&nbsp;',
        nextText: '&nbsp;',
        showWeek: true,
        gotoCurrent: true,
        showOtherMonths: true,
        selectOtherMonths: true,
        defaultDate: currentDate,
        minDate: "-1M", // maxDate: "+21"
        onSelect: function (setDate) {
            $('#' + tbDateID).val(setDate);
            $('#cal_setDate').hide();

            //  Reinit. Timeline Textbox Click:
            $('input.textbox_kztimeline').each(function () {
                $(this).bind('click', function () {
                    enableCalendar(tbDateID);
                });
            });
            //  Reinit. Timeline Calendar Button Click:
            $('input.button_kztimeline').each(function () {
                $(this).bind('click', function () {
                    enableCalendar(tbDateID);
                });
            });
        }
    });
}

function initDraggable(hfldID) {
    var startPos, endPos, movement, steps, nextMonth;
    $("#sliderPointer").draggable({
        axis: "x",
        containment: ".slider",
        grid: [61, 0],
        opacity: 0.85,
        start: function () {
            // Show start dragged position of image.
            startPos = $(this).position().left;
        },
        drag: function () {
        },
        stop: function () {
            endPos = $(this).position().left;
            movement = (startPos - endPos);
            steps = (Math.round((Math.round(movement / 9) / 7)) * -1);
            nextMonth = parseInt($('#' + hfldID).val()) + steps;
            reInitTimeLine(nextMonth, hfldID);
        }
    });
}

var timelinePositions = new Array("0", "-7", "54", "115", "176", "237", "298", "359", "420", "481");
function initTimeLine(nextMonth, hfldID) {
    if (nextMonth > 0 && nextMonth < 10) {
        var prevMonth = $('#' + hfldID).val();
        $('#item' + nextMonth).removeClass('offview');
        //  
        $('#sliderPointer').html(nextMonth);
        $('#sliderPointer').css('left', timelinePositions[nextMonth] + 'px');
        $('#' + hfldID).val(nextMonth);
    }
}
function reInitTimeLine(nextMonth, hfldID) {
    if (nextMonth > 0 && nextMonth < 10) {
        var prevMonth = $('#' + hfldID).val();
        if (nextMonth == prevMonth) {
            //  Same month selected - do nothing ...
        }
        else {
            $('#item' + nextMonth).removeClass('offview');
            if (prevMonth > 0) {
                $('#item' + prevMonth).addClass('offview');
            }
            //  
            $('#sliderPointer').html(nextMonth);
            $('#sliderPointer').css('left', timelinePositions[nextMonth] + 'px');
            $('#' + hfldID).val(nextMonth);
        }
    }
}

function moveTimeLine(direction, hfldID) {
    var nextMonth;
    var prevMonth = $('#' + hfldID).val();
    if (direction == 'prev') {
        nextMonth = parseInt($('#' + hfldID).val()) - 1;
    }
    else {
        nextMonth = parseInt($('#' + hfldID).val()) + 1;
    }
    //  
    if (nextMonth > 0 && nextMonth < 10) {
        //  
        $('#item' + nextMonth).removeClass('offview');
        if (prevMonth > 0) {
            $('#item' + prevMonth).addClass('offview');
        }
        //  
        $('#sliderPointer').html(nextMonth);
        $('#sliderPointer').css('left', timelinePositions[nextMonth] + 'px');
        $('#' + hfldID).val(nextMonth);
    }
}


/*  BE Scripts */
//  Reload:
function reloadPage() {
    window.location.reload();
}
//  Enable InlineEdit:
function enableInlineEditField(el) {
    $(el).addClass('offview');
    $('div.fieldvalue table', $(el).parent()).removeClass('offview');
    $('div.fieldvalue input.tb_inlineEditable', $(el).parent()).first().val($(el).html()).focus();
    $('div.fieldvalue textarea.tb_inlineEditable', $(el).parent()).first().val($(el).html()).focus();
}
//  Disable InlineEdit:
function disableInlineEditField(el) {
    $('div.fieldvalue span.span_inlineEdit', $(el).parent()).removeClass('offview');
    $('div.fieldvalue table', $(el).parent()).addClass('offview');
}
