mason.Relationship = draw2d.Connection
		.extend({

			NAME : "mason.Relationship",
			kind : "association",
			sourcedecorator : "",
			targetdecorator : "",

			init : function() {
				this._super();
				this.setColor("#555555");
				this.id = "-1";
			},

			setKind : function(value, bool) {
				
				this.kind = value;

				switch (value) {
				case "generalization":
					this.setSourceDecorator(new draw2d.decoration.connection.ArrowDecorator());
					this.setTargetDecorator(null);
					break;
				case "aggregation":
					this.setSourceDecorator(new draw2d.decoration.connection.DiamondDecorator());
					this.setTargetDecorator(null);
					break;
				case "composition":
					var decoration = new draw2d.decoration.connection.DiamondDecorator();
					decoration.setBackgroundColor( new  draw2d.util.Color(0, 0, 0));
					this.setSourceDecorator(decoration);
					this.setTargetDecorator(null);
					break;
				default:
					// association
					this.setSourceDecorator(null);
					this.setTargetDecorator(null);
				}
				if (bool) {
					this.sendUpdate();
				}
			},

			setFocus : function(view, focus) {
				var this_ = this;
				if (focus) {
					jQuery.ajax({
						type : "GET",
						dataType : "json",
						url : '/json/access/relationship/' + this_.id,
						success : function(data, statusText) {
							if (data == 1 ) {
								console.log("Pas maar aan");
							} else {
								view.setCurrentSelection(null);
							}
						},
						error : function(xhr, ajaxOptions, thrownError) {
							console.log('FOUT: Send change text');
						}
					});
				} else {
					jQuery.ajax({
						type : "GET",
						dataType : "json",
						url : '/json/access/checkIn/' + this_.id
					});
				}
			},

			sendUpdate : function() {

				var viewId = $('#viewId').text();
				var this_ = this;

				var data = this_.getPersistentAttributes();

				if (data.generatedKey == "-1") {
					delete data.generatedKey;
				}

				jQuery.ajax({
					type : "POST",
					dataType : "json",
					data : data,
					url : '/json/view/' + viewId + '/relationship',
					success : function(json, statusText) {
						this_.id = json.generatedKey;
					},
					error : function(xhr, ajaxOptions, thrownError) {
						console.log('FOUT: Send size/dimension');
					}
				});
			},

			createCommand : function(request) {
				
				var this_ = this;

				if (request.getPolicy() === draw2d.command.CommandType.DELETE) {
					if (this.isDeleteable()) {
						this_.sendDelete();
					}
				}

				return this._super(request);
			},

			sendDelete : function() {

				var viewId = $('#viewId').text();
				var this_ = this;

				jQuery.ajax({
					type : "DELETE",
					dataType : "json",
					url : '/json/view/' + viewId + '/relationship/' + this_.id,
					success : function(data, statusText) {
						// ok
					},
					error : function(xhr, ajaxOptions, thrownError) {
						console.log('FOUT: Send delete');
					}
				});

			},

			getPersistentAttributes : function() {
				var memento = this._super();

				// add all decorations to the memento
				//
				memento.kind = this.kind;

				memento.sourceIdGenerated = this.getSource().getParent()
						.getId();
				memento.sourceDecorator = this.sourcedecorator;
				memento.sourcePort = this.getSource().getName();

				memento.targetIdGenerated = this.getTarget().getParent()
						.getId();
				memento.targetDecorator = this.targetdecorator;
				memento.targetPort = this.getTarget().getName();

				delete memento.target;
				delete memento.source;

				memento.generatedKey = memento.id;
				delete memento.id;

				return memento;
			},

			setPersistentAttributes : function(memento) {
				this._super(memento);

				// remove all decorations created in the constructor of this
				// element
				//
				this.resetChildren();

				// and restore all children of the JSON document instead.
				//
				
				this.setKind(memento.kind);

				this.setDecoratorSource(memento.sourceDecorator);
				this.setDecoratorTarget(memento.targetDecorator);
				this.id = memento.generatedKey;
			},

			switchTargetSource : function() {
				
				alert("switch");

				var futureTargetId = this.getSource().getParent().getId();
				var futureTargetPort = this.getSource().getName();

				this.getSource().getParent().setId(this.getTarget().getParent().getId());
				this.getSource().setName(this.getTarget().getName());
				
				this.getTarget().getParent().setId(futureTargetId);
				this.getTarget().setName(futureTargetPort);
				
				this.repaint();
				this.sendUpdate();

			},

			setDecoratorSource : function(value) {

			},

			setDecoratorTarget : function(value) {

			}

		});
