(function($) {
	$.widget('ui.DpSendKiss', {
		recipientId: 0,
		options: {
			config: {}
		},
		_init: function() {
			this.config = this.options.config;
			this.recipientId = this.element.attr("id").replace("kiss_","");
			var that = this;
			this.element.click(function(){
				that.renderLightbox();
				that.send();
			});
		},
		send: function() {
			$.ajax({
				url: '/ajax/own/sendKiss/',
				data: {
					id: this.recipientId
				},
				type: 'POST',
				dataType: 'json',
				success: function(data) {
					var result;
					if(typeof data.error == 'undefined') {
						result = data.result;
					} else {
						$('.ui-DpDialogBody .buttons .closeButton').css('display', 'none');
						$('.ui-DpDialogBody .buttons .paymentButton').css('display', 'block');
						result = data.error;
					}
					$('.ui-DpDialogBody .sendKiss .content').html(result);
				}
			});
		},
		renderLightbox: function() {
			var that = this;
			// build dialog content element
			var contentEl = $(	'<div class="sendKiss">' +
								'<div class="content"><div class="ajaxLoading"></div></div>' +
								'<div class="buttons"></div>' +
								'<div class="clr"></div>' +
								'</div>'
								);
			//// create dialog buttons
			var buttons = contentEl.find('.buttons').first();
			
			////// close
			var closeButton = $('<div class="closeButton">'+that.config.okayButtonText+'</div>');
			closeButton.click(function(){
				// close dialog
				$.DpDialog.close();
				// don't execute the elements native click behaviour by returning false
				return false;
			});
			buttons.append(closeButton);
			
			////// close
			var paymentButton = $('<div class="paymentButton">'+that.config.okayButtonText+'</div>');
			paymentButton.click(function() {
				//forward to payment
				window.location = that.config.paymentUrl;
				// don't execute the elements native click behaviour by returning false
				return false;
			});
			buttons.append(paymentButton);
			
			$.DpDialog.open(that.config.kissBoxTitle, contentEl);
		}
	});

	$(function() {
		//legacy
		$('a[name=gallerySendKiss]').DpSendKiss({
			config: gDpsendKissConf
		});
	});
})(jQuery);
