﻿function JobsRightHand(businessAreaRef, locationRef) {
    //Empty Available Roles
    $(".roles").empty();
    
    //Make call to RESTful service
    $.get("../../Apis/DataServices.svc/feed/mapfeed/" + businessAreaRef + "/" + locationRef, function(data) {
        BuildJobPosting(data);
    });
}

function BuildJobPosting(data) {
    var i = 0; //Used for my loop to indicate an odd field

    $(data).find("position").each(function() {
        var jobTitle = $(this).find("title").text();
        var jobLocation = $(this).find("typedLocation").text();
        var jobClosingDate = $(this).find("close_date").text();
        var jobUrl = $(this).find("url").text();

        var theClass = "role";
        if (i % 2 == 1)
            theClass += " light";

        var formatter = "<div class=\"" + theClass + "\"><h3><a href=\"" + jobUrl + "\">" + jobTitle + "</a></h3>";
            formatter += "<p>Location: " + jobLocation + "<br/>Closing Date: " + jobClosingDate + "</p></div>";
        
        $(".roles").append(formatter);
        i++;
    });

    if ($(data).find("position").length < 1) {
        $('.availableroles .body .roles').append('<p class="info">There are no vacancies available in this location. Please try another Division and/or Business Area.</p>');
    }

}

//Sample output
//<div class="role">
//    <h3><a href="JOB-LINK-HERE">Job title</a></h3>
//    <p>Location: Blah<br />Division: Blah<br />Closing Date: 22/02/2010</p>                    
//</div>
//<div class="role light">
//    <h3><a href="JOB-LINK-HERE">Job title</a></h3>
//    <p>Location: Blah<br />Division: Blah<br />Closing Date: 22/02/2010</p>                    
//</div>
