/* ---------------------------------------------------- */
/* JQuery												*/
/* ---------------------------------------------------- */
$jQuery(document).ready(function() 
{
	
	/*############################################################################
	Start the logo override system display
	############################################################################ */
	
	/* run the logo image changing function */
	var time_delay = 600;

	/* Run the AJAX Request */
	$jQuery.ajax({
		url: 'index.php?action=ajax.countJobAlerts', 
		type: 'POST', 
		dataType: 'xml', 
		error: function(){
			ShowNotification( "Unable to count job alerts", 0 );
		},
		success: function( xml ){
			
			/* Stop processing if we have found an error */
			if (!xml) { ShowNotification( "Unable to count job alerts", 0 ); return false; } 
			
			
			/* Show an error, if we have not found the page */
			$jQuery("/root/error", xml ).each(function(){ ShowNotification( "Unable to retrieve the selected job alert", 0 ); });
			
			/* Loop through the return result and populate the form values */
			$jQuery("/root/jobalerts", xml ).each(function(){
				
				var num_job_alerts = $jQuery("jacounter", this).text();
			
				if ( num_job_alerts > 0 )
				{
					for ( i=0; i < num_job_alerts; i++ ) 
					{
						/* call the image changing function */
						change_jobalert_image(i, time_delay, 1, 1, 4);
					}
				}				
				
			});
		}
	});
	

	
	/* ---------------------------------------------------- */
	/* A recursive function which cycles through images     */
	/* ---------------------------------------------------- */
	function change_jobalert_image(img_id, time_delay, first, img_num, last) {
		
		window.setTimeout(function() {
			$jQuery("#job_alert_image"+img_id).fadeOut('slow', function() {
				$jQuery("#job_alert_image"+img_id).attr( "src", "images/coloured_logos/teamwork-logo-" + img_num + ".gif" );
				$jQuery("#job_alert_image"+img_id).fadeIn('slow');
			
				/* if the current image processed is not the final image, then add one to it and continue, else reset it to the 1st vehicle number (ie start again) */
				if (img_num != last) { img_num = img_num + 1; } else { img_num = first;}
				/* run the function recursively with the next image */
				change_jobalert_image(img_id, time_delay, first, img_num, last);
				return;		
			});
		}, time_delay);
		
	}	
	 
});
