// This is defined in the controllers
goodthing = {}


goodthing.imageHandler = function( )
{
	// An arry to hold the errors, so that we don't do
	// them twice
	goodthing.image_errors = new Array();

	goodthing.image_errors
	$("img").error(function()
	{
  		//alert( typeof goodthing.image_errors[$(this).attr( 'src' )] );
  		
  		// First, ensure a zero value for the error record in the array
  		if( typeof goodthing.image_errors[$(this).attr( 'src' )] == 'undefined' )
  		{
  			goodthing.image_errors[$(this).attr( 'src' )] = 0;
  		}
  		
  		if( goodthing.image_errors[$(this).attr( 'src' )] <= 5 )
  		{
  			alert( "attempting a fix for " + $(this).attr( 'src' ) + " by swapping " + goodthing.base_asset_uri_s3 + " with " + goodthing.base_asset_uri );
			str = $(this).attr( 'src' );
			$(this).attr( 'src', str.replace( goodthing.base_asset_uri_s3, goodthing.base_asset_uri ));
			++goodthing.image_errors[$(this).attr( 'src' )];
  		}
	});
}

$(document).ready(function()
{	
	// Fix up broken links to station images
	goodthing.imageHandler();
});	


