//Highlighted_job_infobox.js

//Purpose: To fade through highlighted jobs in the top jobs infobox.
//Operates On: bodyBottomInc.php
//Usage: These functions are called automatically when any page it's included in is loaded.
//Dependency: jQuery

//Constants
var legal_refresh_time = 10000; //The duration, in milliseconds, each job is on the users screen.
var legal_fade_duration = 500; //The duration of fade (both in, and out), in miliseconds.
var legal_json_url = "/legal_jobs_json.php";
var job_img_folder = "http://www.eurobrussels.com/ourjobs/";

//Variable
var legal_infbx_obj;
var legal_infbx_obj_count;
var legal_infbx_obj_id = 0;
var legal_infbx_obj_error_count = 0;

//Load legaled Content
//Fades out infoxbox containing div, inserts organisation logo + text and fades back in containing div.
//Once completed the function is called recursivly, and it then calls the next job entry in the JSON object.
function load_legal_content(){

	//Get the ID to be used.
 	var id_to_be_used = legal_infbx_obj_id;

 	//Increase ID by 1, so next call of the function uses the next entry.
 	legal_infbx_obj_id  ++;

 	//If the id to be used is bigger than the largest id we have, start from beginning with 0.
	if((legal_infbx_obj_count - 1) < legal_infbx_obj_id){ //We subtract 1, as the JSON starts from 0.
		legal_infbx_obj_id = 0;
		legal_infbx_obj_error_count = 0;
	}

	var legal_logo_img_obj = new Image();

	//On error with image (probably caused by missing image) reload the function; which moves to next job.
	//If all objects in error, then stop.
	legal_logo_img_obj.onerror=function(){
		legal_infbx_obj_error_count++;
		if( legal_infbx_obj_error_count >= legal_infbx_obj_count ){
			//if all objects are blank just stop to avoid infinite loop
		} else {
			load_legal_content();
		}
	};

	//Once loaded, do this (fade out old image...).
	legal_logo_img_obj.onload=function(){

		//Construct image and job title string.
		var job_title_string = "<a title='" + legal_infbx_obj.legal_jobs[id_to_be_used].long_job_title + "' target= '_blank' href = '" + legal_infbx_obj.legal_jobs[id_to_be_used].display_url + "'>" + legal_infbx_obj.legal_jobs[id_to_be_used].job_title	 + "</a>";
		var job_img_string = "<a title='" + legal_infbx_obj.legal_jobs[id_to_be_used].long_job_title+ "' target= '_blank' href = '" + legal_infbx_obj.legal_jobs[id_to_be_used].display_url + "'>" + "<img src = '" + legal_logo_img_obj.src + "'>" + "</a>";

		//Set Jquery callbacks. Call fadeOut then set image and text, then call fadeIn and start setTimeout to call load_legaled_content() again.
		$(legal_logo_img_obj).ready(function(){
			$("#highlightedLegalJobsInfoBoxContain").show();
			$("#highlightedLegalJobsInfoboxWrapper").fadeOut(legal_fade_duration, function () {
	       		$("#highlightedLegalJobsInfoboxImg").html(job_img_string);
	       		$("#highlightedLegalJobsInfoboxTxt").html(job_title_string);
	        	$("#highlightedLegalJobsInfoboxWrapper").fadeIn(legal_fade_duration, function (){
	        		if(legal_infbx_obj_count !== 1){
						setTimeout("load_legal_content()",legal_refresh_time);
					}
	        	});
		 	});
		});
	};

	//Set the image to be the selected image.
	legal_logo_img_obj.src = job_img_folder + legal_infbx_obj.legal_jobs[id_to_be_used].img_url;
}

//The jquery document ready handler. Any code within will only load after the entire page has loaded.
$(document).ready(function(){

	//Do request to legal infobox json page once page has loaded. This json page retrieves the legal ad data from the database,
	//and returns the json data here.
	$.ajax({
  		type: "POST",
  		url: legal_json_url,
  		dataType: "json",
  		success: function(returned_json_data){

   	 		//Load returned JSON object into a variable.
			legal_infbx_obj = returned_json_data;
		  	//Ascertain how many legal job have been returned.
		  	legal_infbx_obj_count = legal_infbx_obj.legal_jobs.length;
		  	//Load the legal content for the first time. It was load recursively here after.
		  	load_legal_content();
  		},
  		error: function(XMLHttpRequest, textStatus, errorThrown){
			//For the moment, do nothing with errors. Which will result when there are no jobs (and no JSON).
			$("#highlightedLegalJobsInfoBoxContain").fadeOut();
  		}
	});
 });
