$(document).ready(function() {

    // Call Eduserv apply count script using the click event of the 'apply online' button
    $('#job-apply-button a').click(function() {
        $.ajax({
            url:      "/cgi-bin/applycount.pl",
            data:     {id: reference},
            dataType: "text",
            async:    false,
            cache:    false
        });
    });
    
    // Attach click events to track each outgoing link within the body text
    $('#job-advert-text a').each(function() {
        $(this).click(function() {
            var outgoingUrl = $(this).attr('href');
            return googleTrack(outgoingUrl);
        });
    });

    // Track outgoing link clicks in Google Analytics
    function googleTrack(outgoingUrl)
    { 
        var pageName = '/outgoing/' + reference + '/' + encodeURIComponent(outgoingUrl);
        pageTracker._trackPageview(pageName);
    }

});

