var messages = {  
	'site_unavailable': "We're sorry, there was an error processing this request. Please try again in a few moments."
	}

function untagPhoto( person_id ) { 
	var p = $( "input#photo_id" ).val();
	$.get( "/photos/" + p + "/remove_person/" + person_id, function( data ) { 
		if( data.indexOf( 'OK' ) > -1 ) { 
			$( "#photoTag" + person_id ).remove();
		}
	});
}

$().ready( function() { 
	
	// Associate photo with race
	$( "button#add_photo_race" ).click( function() { 
		var v = $( "select#photo_race" ).val();
		var p = $( "input#photo_id" ).val();
		var name = $('select#photo_race :selected').text();
		if( v.length > 0 ) { 
			$.get( "/photos/" + p + "/to_race/" + v, function( data ) {
				if( data.indexOf( 'OK' ) > -1 ) { 
					$( "#photo_race_placeholder" ).html( name );
				} else { 
					alert( messages[ 'site_unavailable' ] );
				}
			});
		}
		return false;
	});
	
	// Associate photo with person
	$( "button#add_photo_person" ).click( function() { 
		var v = $( "select#photo_person" ).val();
		var p = $( "input#photo_id" ).val();
		var name = $('select#photo_person :selected').text();
		if( v.length > 0 ) { 
			$.get( "/photos/" + p + "/to_person/" + v, function( data ) {
				if( data.indexOf( 'OK' ) > -1 ) { 
					var chtml = $( "#photo_people" ).html();
					chtml += '<li id="photoTag' + v + '">' + name + ' [<a href="javascript:untagPhoto(' + v + ')">Remove</a>]</li>';
					 $( "#photo_people" ).html( chtml );
				} else { 
					alert( messages[ 'site_unavailable' ] );
				}
			});
		}
		return false;
	});
});

