
function _HotJobsInit() {
    var candclass = $("#hotjob_iv_cand_class").val();
    var lang = $("#hotjob_lang").val();
    var hierid = $("#hotjob_iv_hier_id").val();
    var company = $("#hotjob_iv_company_id").val();

    $.getJSON('/typo3conf/ext/bilfingerkarriere/proxyhotjobs.php?func=Z_RCF_HOT_JOBS_GET&callback=&iv_hits=10&sap-language=' + lang + '&iv_hier_id=' + hierid + '&iv_cand_class=' + candclass + '&iv_company_id=' + company,
        function (json) {
            if ($('body').hasClass('layout-oc home') && $('ul.career__joblist').length > 0) {
                _HotJobsRenderLinTemplate(json.et_hotjobs);
            } else {

                _HotJobsRender(json.et_hotjobs);
            }
            json = null;
        }).fail(function () {
        $('#hotjob-wrapper').hide();
        $('#hotjob-navi').hide();
        $('#hotjob-error').show();
    });
}

function _HotJobsRenderLinTemplate(data) {

    var url = $("#hotjob_url").val();
    var hierid = $("#hotjob_iv_hier_id").val();
    if (hierid == "") hierid = $("#hotjob_iv_company_id").val();
    var items = [];
    var count = 0;

    maxitems = parseInt($('ul.career__joblist').data('rows'));


    $.each(data, function (key, val) {

        count++;
        var header = val.header;
        var einsatzort = val.einsatzort;
        var company = val.company;
        var lengthcheck = header + einsatzort;
        if (lengthcheck.length >= 100) {
            einsatzort = einsatzort.substring(0, 100 - header.length) + "&nbsp;...";
        }
        items.push('<li><a href="' + url + '?pinst=' + val.pinst_guid + '&scparam=' + encodeURI("ZFRO_REQ_INFO_COMPANY_LB_2=" + hierid) + '">');
        items.push('<span class="">' + header + '</span>');
        items.push('<br>');
        items.push('<span class="hotjob-einsatzort career__company">' + company + '</span>');
        items.push('</a></li>');

        if(count >= maxitems) {
            return false;
        }
    });



    $('ul.career__joblist').html(items.join(''));
}

function _HotJobsRender(data) {
    // Hotjobs
    var url = $("#hotjob_url").val();
    var hierid = $("#hotjob_iv_hier_id").val();
    if (hierid == "") hierid = $("#hotjob_iv_company_id").val();
    var items = [];
    var count = 0;
    $.each(data, function (key, val) {
        if (count % 2 == 0) {
            items.push('<div class="hotjob-page" id="hotjob-page' + count / 2 + '">');
        }
        count++;
        var header = val.header;
        var einsatzort = val.einsatzort;
        var lengthcheck = header + einsatzort;
        if (lengthcheck.length >= 100) {
            einsatzort = einsatzort.substring(0, 100 - header.length) + "&nbsp;...";
        }
        items.push('<a href="' + url + '?pinst=' + val.pinst_guid + '&scparam=' + encodeURI("ZFRO_REQ_INFO_COMPANY_LB_2=" + hierid) + '">');
        items.push('<span class="hotjob-header">' + header + '</span>');
        items.push('&nbsp;');
        items.push('<span class="hotjob-einsatzort">' + einsatzort + '</span>');
        items.push('</a>');
        if (count % 2 == 0) {
            items.push('</div>');
        }
    });
    if (count % 2 == 1) {
        items.push('</div>');
    }
    $('#hotjob-container').html(items.join(''));

    // Navigation
    for (i = 0; i < count; i = i + 2) {
        var li = $("<li/>");
        li.text("0" + (i / 2 + 1));
        li.click(function () {
            var page = $(this).text() - 1;
            $('.hotjob-page').hide();
            $('#hotjob-page' + page).fadeIn();
            $('#hotjob-navi li').removeClass('active');
            $(this).addClass('active');
        });
        li.appendTo('#hotjob-navi');
    }
    $('#hotjob-navi li').filter(":first").addClass('active');

    // Erste Seite anzeigen
    $('#hotjob-container .hotjob-page').filter(":first").fadeIn();
}

(function ($) {
    $(document).ready(function () {
        _HotJobsInit();
    });
})(jQuery);


