﻿$(document).ready(function() {
    setCookie('focusTab', 'jobs');
    setCookie('focusSub', 'joblistings');
    header.update();

    getTopEmployer(5);
    getTopCategory();

    if ($question.length > 0) {
        $('#job_keyword').val($question);
        onSearch(1, 1);
    } else {
        onSearch(1, 1);
    }

    $('#btnJobSearch').click(function() {
        gcompany_name = '';
        gjob_title = '';
        gjob_loc = 0;
        gjob_cate = '';
        gjob_type = '';
        onSearch(1, 1);
    });
    if ($('#imgPoll').length > 0) { getPollBox('#imgPoll', 2); }   
    $('#showBasicSearch').click(function() { $('#jlAdvancedSearch').hide(); $('#showBasicSearch').hide(); $('#showAdvancedSearch').show(); });
    $('#showAdvancedSearch').click(function() { $('#jlAdvancedSearch').show(); $('#showAdvancedSearch').hide(); $('#showBasicSearch').show(); });
});

$(this).ajaxStop(function() {
    stopLoading();
});

function printJob(id) {
    var disp_setting = "toolbar=no,location=yes,directories=yes,menubar=no,";
    disp_setting += "scrollbars=no,width=850, height=600, left=100, top=25";
    var content_vlue = document.getElementById(id).innerHTML;

    var docprint = window.open("job" + id + ".html", "", disp_setting);
    docprint.document.open();
    docprint.document.write('<html><head><title>AngkorOne Job Print</title>');
    docprint.document.write('<link type="text/css" href="<%=strServerRoot %>/styles/jquery.tabs.css" rel="Stylesheet" />');
    docprint.document.write('<link type="text/css" href="<%=strServerRoot %>/styles/jobprint.css" rel="Stylesheet" />');
    docprint.document.write('</head><body onload="">');
    docprint.document.write('<div style="width:800px;">');
    docprint.document.write(content_vlue);
    docprint.document.write('<div></body></html>');
    docprint.document.write('<script type="text/javascript">setTimeout("self.print(); self.close();", 100);');
    docprint.document.write('<\/script>');
    docprint.document.close();
    docprint.focus();
}

function onSearch(page_no, paging_status) {
    var job_title = gjob_title != '' ? gjob_title : $('#job_keyword').val();
    var job_loc = gjob_loc > 0 ? gjob_loc : $('#job_location').val();
    var job_com = gcompany_name != '' ? gcompany_name : $('#job_company').val();
    var job_cate = gjob_cate != '' ? gjob_cate : $('#job_occupation').val();
    var job_type = gjob_type != '' ? gjob_type : $('#job_type').val();
    var line_per_page = ($('#cmbLinePerPage').length > 0) ? $('#cmbLinePerPage').val() : 25;
    var com_id = gcom_id;
    var lng_cd = 'EN';
    $('#divLoading').show();
    var url = server_root + "/Processor/GeneralProcess.aspx";
    var respText = $.ajax({
        url: url,
        async: false,
        data: {
            proc_id: 30, //Search Job
            job_title: job_title,
            job_loc: job_loc,
            job_com: job_com,
            job_cate: job_cate,
            job_type: job_type,
            lng_cd: lng_cd,
            page_no: page_no,
            com_id: com_id,
            paging_status: paging_status,
            line_per_page: line_per_page
        },
        success: function() {
            stopLoading();
        }
    }).responseText;

    $('#jobList').html(respText);

    var no_of_record = $('#no_of_record').val();
    if (no_of_record == 1) {
        $(".accordion").accordion({ header: "h3", active: null, autoHeight: false, alwaysOpen: true });
        var job_id = $('#first_job_id').val();
        getJobDetail(job_id);
    } else {
        $('.accordion').accordion({ header: "h3", active: false, autoHeight: false, alwaysOpen: false });
    }

    $('.tabsUI').tabs();

    //Block by Mongkol to block the tool tips of job
    /*$('#jobAccordion a').tooltip({
    track: true,
    delay: 0,
    showURL: false,
    showBody: " - ",
    extraClass: "pretty",
    fixPNG: true,
    opacity: 1,
    left: 0,
    fix_left: 40
    });*/
}

function stopLoading() {
    $('#divLoading').hide();
}


function showEmployerProfile(jobid) {
    $('#jobBodyDetailContentEn' + jobid).hide();
    $('#jobBodyProfileContentEn' + jobid).show();

    $('#jobBodyDetailContentKh' + jobid).hide();
    $('#jobBodyProfileContentKh' + jobid).show();
}

function showJobProfile(jobid) {
    $('#jobBodyDetailContentEn' + jobid).show();
    $('#jobBodyProfileContentEn' + jobid).hide();

    $('#jobBodyDetailContentKh' + jobid).show();
    $('#jobBodyProfileContentKh' + jobid).hide();
}

function onPageLinkClick(page_no, status) {
    onSearch(page_no, status);
}

function onChangeLinePerPageChange() {
    onSearch(1, 1);
}

function onEmployerSearch(com_id) {
    gcompany_name = '';
    gjob_title = '';
    gjob_loc = 0;
    gjob_cate = '';
    gjob_type = '';
    gcom_id = com_id;
    $('#job_keyword').val('');
    onSearch(1, 1);
}

function onOccupationSearch(occupation) {
    gcompany_name = '';
    gjob_title = '';
    gjob_loc = 0;
    gjob_cate = occupation;
    gjob_type = '';
    gcom_id = 0;
    $('#job_keyword').val('');
    onSearch(1, 1);
}

function getTopEmployer(no_top_employer) {
    var url = server_root + "/Processor/TopEmployer.aspx";
    var respText = $.ajax({
        url: url,
        async: false,
        data: {
            no_top_employer: no_top_employer
        }
    }).responseText;
    $('#jlTopEmpList').html(respText);
}

function getTopCategory() {
    var url = server_root + "/Processor/TopOccupation.aspx";
    var respText = $.ajax({
        url: url,
        async: false,
        data: {

    }
}).responseText;
$('#jlTopOccupationList').html(respText);
}

function onAccordionClick(ref_id) {
    if (ref_id != glast_job_click) {
        seed = 1;
        ref_type_cd = 'job';
        var total_view = increaseCounter(ref_type_cd, ref_id, seed);
        $('#total_view' + ref_id).text(total_view);

        getJobDetail(ref_id);
        var com_logo = $('#hd_com_logo' + ref_id).val();
        $('#img_com_logo' + ref_id).attr('src', com_logo);

        glast_job_click = ref_id;
    }
}

function getJobDetail(job_id) {
    var url = server_root + "/Processor/GeneralProcess.aspx";
    var respText = $.ajax({
        url: url,
        async: false,
        data: {
            proc_id: 32, //Get Job Detail by id
            job_id: job_id
        }
    }).responseText;

    $('#jobBodyContent' + job_id).html(respText);
    $('#tabs' + job_id).tabs();

    if (culture == 'km-KH') {
        $('#tabs' + job_id + ' ul li').attr('class', '');
        $('#tabs' + job_id + ' .khflag').parent().parent().attr('class', 'tabs-selected');
        $('#jobEn' + job_id).attr('class', 'tabs-container tabs-hide');
        $('#jobKh' + job_id).attr('class', 'tabs-container');
    }

    $('.jobDescription').decHTML();
    $('.jobResponsibilities').decHTML();
    $('.jobQualification').decHTML();
}