/* --------------------------------------------------------------

	PARSER TO PULL INDEED JOB POSTINGS

-------------------------------------------------------------- */

$(document).ready(function() {

	
	
	function fetchResults(q, l){

		$('.loader').css('display', 'block');
		
		$.getJSON('/api/indeed_feed/?q=' + q + '&l=' + l + '', function(data){
			
			$(data).each(function() {
			
				var out = '<ul>';
				
					out += '<li class="job-info"><h3 class="title"><a href="' + this['url'] + '">' + this['jobtitle'] + '</a></h3>';
					out += '<p class="company">' + this['snippet'] + '</p></li>';
					out += '<li class="location">' + this['company'] + ', ' + this['state'] + '</li>';
					out += '<li class="link"><a href="' + this['url'] + '">&raquo; ' + 'View Posting' + '</a></li>';
					out += '</ul>';
						
				$('#job-search-results').append(out);
				
			});
			
			$('.loader').css('display', 'none');

			if (data == "") {
						
				$('#job-search-results').append('<h5 style="margin: 20px; text-align:center;"> Sorry, there are no job postings for this postion at this time, please check back later</h5>');
					
			}
		
		});
		
	};
	
	job = $('body').find('.find-jobs').text();
	
	if (job == ''){
	
		//alert('no');
		fetchResults(jQuery.url.param('keywords'), jQuery.url.param('location'));
	}
	else {
		//alert('hey');
		
		fetchResults( job, '');
	}
	
	
	
});


/* getJSON Error Handler */

jQuery(document).ajaxError(function(event, request, settings){

	$('#job-search-results').append('<h4 style="margin:0;">No Results Found, Please Check Values and Try Again</h4>');

});
