/* [%-# @(#)js_hot_jobs.js	1.9 15:12:29,10/08/18 (yy/mm/dd) -%] */
/**
 * @author JE
 * TO DO: NEED TO UPDATE THIS CODE TO WORK AS A PLUGIN SO THAT THE FUNCTIONALITY CAN BE USED
 * ON OTHER SITES IN THE FUTURE 
 */

$(document).ready(function(){
	$('#jotw').empty();

	var intAgencyID;
	var intMaxResults = 40;
	
	if (window.location.hostname == 'sp.jobsite.co.uk'){
		intAgencyID = 326361;	//DEV AGENCY ID
	} else if (window.location.hostname == 'ref.jobs-north.jobsite.co.uk'){
		intAgencyID = 18462;	//REF AGENCY ID
	} else {
		intAgencyID = '219460,219900,222101,222087';	//LIVE AGENCY ID
	};
		
	var strRequest ='<?xml version="1.0" encoding="ISO-8859-1" ?>';
	strRequest += '<search_request>';
	strRequest += '		<search_fields>';
	strRequest += '			<scc>SP</scc>';
	strRequest += '			<agency_id>' + intAgencyID + '</agency_id>';
	strRequest += '		</search_fields>';
	strRequest += '		<search_options>';
	strRequest += '			<results_from>1</results_from>';
	strRequest += '			<results_max>' + intMaxResults + '</results_max>';
	strRequest += '			<search_order>R</search_order>';
	strRequest += '			<do_display_ads>N</do_display_ads>';
	strRequest += '			<highlight_summary>N</highlight_summary>';
	strRequest += '		</search_options>';
	strRequest += '		<result_fields>';
	strRequest += '			<jobtitle />';
	strRequest += '			<joblocation />';
	strRequest += '			<jobsalary />';
	strRequest += '		</result_fields>';
	strRequest += '</search_request>';
	
	$('<div id="hotJobsLoader"><img src="/images/home/ajax-loader.gif" width="32" height="32" alt="Loading..." /></div>')
	.appendTo('#jotw').ajaxStart(function(){
	   $(this).show();
	 });
	
	$("#hotJobsLoader").ajaxStop(function(){
		$(this).hide();
	});
	
	$.ajax({
		type: 'POST',
		dataType: 'xml',
		url: '/cgi-bin/vacancy_search_server.pl',
		data: strRequest,
		error: function(response)
	        {
	            $.log('Error ' + response)
	            var strMessage = '<div id="msgNoResults">Sorry. There seems to have been a server error...</div>'
	            $('#jotw').append(strMessage)
	        },
		success: function(response){
			var currentContainer = null;
			$('#jotw').append('<ul></ul>');
			
			$(response).find('vacancy').each(function(index){
				var $vacancy = $(this);
				var jobRef = $vacancy.find('jobsite_ref').text();
				var jobTitle = $vacancy.find('job_title').text();
				var location = $vacancy.find('location').text();
				var salary = $vacancy.find('salary').text();
				
				if (index % 3 === 0){              
                    currentContainer = $('<li></li>');
                    $('#jotw ul').append(currentContainer);
                }; 
				currentContainer.append('<div class="hotJob"><p><a href="/cgi-bin/vacdetails.pl?selection=' + jobRef + '" title="' + jobTitle + '">' + jobTitle + '</a><br />' + salary + ' - ' + location + '</p></div>');
				
			});
						
			$('#jotw').jCarouselLite({
		        visible: 1,
		        auto: 4000,
		        speed: 1000,
		        btnNext: '#hjobs .next',
		        btnPrev: '#hjobs .previous'
		    });

		}
	});	
	
});

