alerts = function() {
	return {

		init: function() {
			$('a#add').click(function() {return alerts.add();});
			$('input#systemwide').click(function() { alerts.checkSubmit(); });	
			
			$('a.remove').click(function() {
				$(this).parent().parent().fadeOut('fast', function(){
					$(this).remove();
					
					if ($('table.selected tr td').length == 0) {							
						var none = $('<tr class="none"><td colspan="2">None</td></tr>');
						$('table.selected').append(none);						
						alerts.checkSubmit();						
					}					
					$('#input_' + $(this).attr('id')).remove();
				});
				return false;				
			});			
				
		},
		
		add: function() {			
			
			var route = $('#routes option:selected').text();			
			var route_id = route.replace(/ /g, '_'); 
			var exists = $('tr#' + route_id);
			
			if (exists.length > 0) {
				$(exists).fadeOut(100).fadeIn(100);
				return false;
			}
			
			var tr = $('<tr id="'+ route_id +'"><td class="route">'+ route +'</td><td class="remove"><a href="#" class="remove">remove</a></td></tr>').hide();
			var input = $('<input type="hidden" name="route[]" value="'+ route +'" id="input_'+ route_id +'"/>')
			
			tr.find('a.remove').click(function() {
				$(this).parent().parent().fadeOut('fast', function(){
					$(this).remove();
					
					if ($('table.selected tr td').length == 0) {							
						var none = $('<tr class="none"><td colspan="2">None</td></tr>');
						$('table.selected').append(none);						
						alerts.checkSubmit();						
					}					
					$('#input_' + $(this).attr('id')).remove();
					
				});
				return false;
			});
						
			$('tr.none').remove();
						
			tr.prependTo($('table.selected')).fadeIn('fast');
			$('form').prepend(input);
			
			alerts.checkSubmit();
			
			return false;
		},
		
		checkSubmit: function() {
			if ($('input#systemwide').is(':checked') || $('table.selected tr td.route').length > 0) {
				$('input.subscribe').removeAttr('disabled');
			} else {
				$('input.subscribe').attr('disabled', 'disabled');
			}
		}
		

		
		
	};
}();