// THIS CODE IS NOT APPROVED FOR USE IN/ON ANY OTHER UI ELEMENT OR PRODUCT COMPONENT. 
// Copyright (c) 2007 Renderspace. All rights reserved.


/************************************************/
// person object
/************************************************/


function ConnectorObject(parent, element1, element2) {
    this.base = RenderableObject;
    this.base(parent, 0, null, "Connector");
    this.isVisible = this.isEnabled = true;
    
    this.parentCanvas = clientBackgroundCanvas;
    this.element1 = element1;
    this.element2 = element2;
    
    if (element1) element1.onConnected(this);
    if (element2) element2.onConnected(this);
    
    this.line = createVisualLine(this.parentCanvas, CONNECTOR_COLOR, CONNECTOR_SIZE);
    this.updatePosition();
}

ConnectorObject.prototype = new RenderableObject;


ConnectorObject.prototype.onRelease = function() {
    this.line = removeVisualElement(this.parentCanvas, this.line);
    if (this.element1) {
        var elem = this.element1;
        this.element1 = null;
        elem.onDisconnected(this);
    }
    if (this.element2) {
        var elem = this.element2;
        this.element2 = null;
        elem.onDisconnected(this);
    }
}


ConnectorObject.prototype.disconnect = function(element) {
    if (element != this.element1) {
        var elem = this.element1;
        this.element1 = null;
        elem.onDisconnected(this);
    }
    this.element1 = null;
    if (element != this.element2) {
        var elem = this.element2;
        this.element2 = null;
        elem.onDisconnected(this);
    }
    this.element2 = null;
    this.release();
}


ConnectorObject.prototype.bringToFront = function() {
    bringVisualElementToFront(this.parentCanvas, this.line);
}


ConnectorObject.prototype.show = function(isVisible, isEnabled, opacity) {
    this.isVisible = isVisible;
    this.isEnabled = isEnabled;
    this.opacity = opacity;
    setVisualElementPos(this.line, null, null, null, null, isVisible ? opacity : 0);
}


ConnectorObject.prototype.setPosition = function(x1, y1, x2, y2) {
    if ((this.x1 == x1) && (this.y1 == y1) && (this.x2 == x2) && (this.y2 == y2)) return;
    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;
    
    setLineElementPos(this.line, x1, y1, x2, y2, null);
}


ConnectorObject.prototype.updatePosition = function() {
    if ((this.element1 == null) || (this.element2 == null)) return;
    var elem1Center = this.element1.getCenterPos();
    var elem2Center = this.element2.getCenterPos();
    this.setPosition(elem1Center.x, elem1Center.y, elem2Center.x, elem2Center.y);
}
