var Controllers = Controllers || {}; // Check to see if already defined in current scope
Controllers.TravelPlan = Class.create();

Controllers.TravelPlan.prototype = {
	URL: '',
	tagPrefix: '',
	
	initialize: function(lists) {
		this.URL = '/travel_plans/';
		this.tagPrefix = 'tag-';
	},
	
	tag: function(planId) {
		new Ajax.Request('/travel_plans/tag', {
			parameters: Form.serialize('tag-add-form'),
			onLoading: function(request) {
				$('tag-add-form-log').update(Dictionary.tagging);
			}, 
			onSuccess: function(transport, json) {
				if (json.success) {
					var element = $('TagTag');
					var tag = element.value;
					new Insertion.Bottom('tags', "<span>, <a href='/tags/" + tag + "'>" + tag + "</a></span>");
					element.value = '';
					
					$('tag-add-form-log').update(Dictionary.tag_added);					
				}				
			}, 
			onFailure: function(request) {
				$('tag-add-form-log').update(Dictionary.tag_added_error);
			}
		});
	},
		
	removeTag: function(id, planId) {
		var tag = $(this.tagPrefix + id);
		var value =  tag.innerHTML;
		
		new Ajax.Request(this.URL + 'remove_tag/', {
			parameters : {
				'plan_id' : planId,
				'tag_id' : id
				},
				onLoading : function() {
					tag.update(Dictionary.deleting);
				},
				onSuccess : function() {
					tag.remove();
				},
				onFailure : function() {
					tag.innerHTML = value
				}
			}
		);			
	},
	
	sendToContacts: function() {
		new Ajax.Updater('plan-send-log', this.URL + 'send_to_contacts', {
			parameters: Form.serialize('plan-send-form')
		});		
	}			
};