// 

var action_travel_sharedLib = 
{
	/**
	 * Submit form:
	 * - Check valid
	 *  - Send message
	 *   - Create PROSPECTIVE PARTICIPANT edge
	 *    - Create INTEREST edge
	 */
	callSubmit: function(opts)
	{
		var self = this;
		
		self.options = $.extend(self.options, opts);

		if (self._fieldsValid())
		{
			$('.guide-me-wrapper button').attr({disabled: 'disabled'});
			self._sendMessage();
		}
	},
	
	/** 
	 * check if all required fields have valid data 
	 */
	_fieldsValid: function()
	{
		var self = this;
		
		if (!$("textarea[name='mail_body']").val() || !$("input[name='mail_subject']").val() )
		{
			$.any.notification.error('Please add a subject and a message');
			return false;
		}
		
		return true;
	},
	
	/**
	 * Send message to user
	 */
	_sendMessage: function()
	{
		var self = this, message, period, data, callback;     
     
		period = '';
		
		if (!self.options.onlymessage && $("input[name='date_start']").val() && $("input[name='date_end']").val())
		{
			period = "\n\nI will visit from " +
					$("input[name='date_start']").val().split(' ')[0] + " to " +
					$("input[name='date_end']").val().split(' ')[0];
		}
		
		message = 
			$("textarea[name='mail_body']").val() +
			period +
			"\n\nRegards,\n" +
			null +
			"\n\nThis message was sent from this page: " + 
			self.options.sentfrom +
			"\n\n\n";
		
		callback = function(json)
			{
				$.any.notification.success('Your message has been sent to the guide!');
				if (!self.options.onlymessage)
				{
					self._createInterestEdge();
				} else
				{
					setTimeout(function()
					{
						self.cancel();
					}, 2500);
				}
			};
		
		for (var k in self.options.recipients)
		{
		
			data = {
				user_id		: self.options.recipients[k],
				subject		: $("input[name='mail_subject']").val(),
				text		: message	
			};
			
			$.any.rest.post("anymeta.messages.send", data, function(json)
				{
					callback(json);
					callback = function(){};
				});
		}
	},
	
	/**
	 * Create INTEREST edge between user and guide
	 */
	_createInterestEdge: function()
	{
		var self = this, data, callNext;
		
		data = {
			id			: $.any.rest.userId,
			object		: this.options.thg_id,
			predicate	: 'INTEREST'
		};
		
		callNext = function(){ self._createParticipantEdge(); };
		$.any.rest.post('anymeta.edge.add', data, function(){ callNext(); }, function(){ callNext(); });
	},
	
	/**
	 * Create PROSPECTIVE_PARTICIPANT edge between user and guide
	 */
	_createParticipantEdge: function()
	{
		var self = this, data, dateFields, fields = [], values = [];
		
		data = {
			id			: $.any.rest.userId,
			object		: this.options.thg_id,
			predicate	: 'PROSPECTIVE_PARTICIPANT',
			duplicate	: 1
		};
		
		$.any.rest.post('anymeta.edge.add', data, function(json) 
			{
				dateFields	= $('#dates').formToArray();

				for (var k in dateFields)
				{
					fields.push(dateFields[k].name);
					values.push(dateFields[k].value);
				}
		
				data = {
					id		: $.any.rest.userId,
					edge	: json.edg_id,
					field	: fields,
					value	: values
				};
				
				$.any.rest.post('anymeta.edge.attribute.set', data, function()
				{
					setTimeout(function()
						{
							self.cancel();
						}, 3000);
				});
			});
	}
};

$(function() 
{
	$.any.ui.action.js("travel_guideme", action_travel_sharedLib);
	$.any.ui.action.js("travel_guidemessage", action_travel_sharedLib);
});

// 