/**
 * FreelanceSwitch Jobs Loader
 * This project reads JS cache files loaded from the RSS feeds from jobs.freelanceswitch.com and displays them in a formatted HTML on the page.
 * 
 * @author Jordie Bodlay <jordie+code@jordie.org> EDIT Derek Herman
 * @package FreelanceSwitchRssReader
 * @subpackage JobsLoader
 */

var FreelanceSwitchJobs = {
	jobsCacheUrl: 'http://freelanceswitch.com/jobswidget/jobscache/', // Must have a trailing slash.
	loadedJobs: {}, // Leave this blank, it is loaded later on
	template: '<li class="fsjobs_job" id="{uniqid}"><a href="{applyurl}">{jobtitle} <span>{location}</span></a> </li>',
	loadJobs: function(category){
		if(typeof(FreelanceSwitchJobs.loadedJobs[category]) == 'undefined') {
			var scriptElement = document.createElement("script");
			scriptElement.type = "text/javascript";
			scriptElement.src= FreelanceSwitchJobs.jobsCacheUrl + "jobs." + category + ".js";
			document.getElementsByTagName("head")[0].appendChild(scriptElement);
		}else{
			FreelanceSwitchJobs.loadJobsFromJSON(FreelanceSwitchJobs.loadedJobs[category]);
		}
	},

	loadJobsFromJSON: function(json){
		var output = '';
		var thisJob = '';
		
		
		// Loop thorugh and use the template to nicely format all the jobs
		for(i=0; i<5; i++) {
			thisJob = FreelanceSwitchJobs.template;
			thisJob = thisJob.replace(/\{date\}/g, json[i].date);
			thisJob = thisJob.replace(/\{jobtitle\}/g, json[i].jobtitle);
			thisJob = thisJob.replace(/\{location\}/g, json[i].location);
			thisJob = thisJob.replace(/\{description\}/g, json[i].description);
			//thisJob = thisJob.replace(/\{company\}/g, json[i].company);
			thisJob = thisJob.replace(/\{applyurl\}/g, json[i].applyurl);
			thisJob = thisJob.replace(/\{uniqid\}/g, "fsjobs_id_" + Math.floor(Math.random()*10001));
			output += thisJob;
		}
		
		// load it into the page
		jQuery('#fsjobs_list').html(output);
		
	}
};


jQuery(document).ready(function($){
	if(typeof(defaultJobCategory) == 'undefined') {
		var defaultJobCategory = 'all';
	}

	$('#fsjobs_select').bind('change', function(){
		FreelanceSwitchJobs.loadJobs($('#fsjobs_select').val());
	});
	
	// Set the default value and trigger the 'change' event
	$('#fsjobs_select').val(defaultJobCategory).change();

});

