// THIS CODE IS NOT APPROVED FOR USE IN/ON ANY OTHER UI ELEMENT OR PRODUCT COMPONENT. 
// Copyright (c) 2007 Renderspace. All rights reserved.


/************************************************/
// initiative object
/************************************************/


function InitiativeObject(parent, id, name) {
	this.base = ElementObject;
	this.base(parent, id, name, "Initiative");
	this.isVisible = this.isEnabled = true;
	
	this.icon = createVisualImage(this.parentCanvas, null, "/" + IMAGE_DIR + "initiative1.png", 0, 0, 0, 0, 0);
	this.title = createTextElement(this.parentCanvas, FONT_SIZE, FONT_COLOR, name);
	setVisualElementPos(this.title, 0, 0, 0, 0, 0);
	this.titleWidth = name ? getTextWidth(name, FONT_SIZE) : 0;
}

InitiativeObject.prototype = new ElementObject;


InitiativeObject.prototype.onRelease = function() {
	this.icon = removeVisualElement(this.parentCanvas, this.icon);
	this.title = removeVisualElement(this.parentCanvas, this.title);
	this.disconnect();
}


InitiativeObject.prototype.updateVisualState = function() {
    var name = "initiative";
    if (this.isFocused) name += "sel";
    name += (this.connectorList && this.connectorList.length > 0) ? "2" : "1";
	setVisualElementSource(this.icon, "/" + IMAGE_DIR + name + ".png");
}


InitiativeObject.prototype.getIsSelected = function(mouseX, mouseY) {
    var isSelected = (mouseX >= this.x) && (mouseY >= this.y) && (mouseX < this.x + this.w) && (mouseY < this.y + this.h);
	if (this.name != null) {
		var titleX1 = this.x + this.w/2 - this.titleWidth/2 - 2;
		var titleY1 = this.y + this.h + 2;
		var titleX2 = titleX1 + this.titleWidth + 4;
		var titleY2 = titleY1 + FONT_SIZE + 4;
		isSelected = isSelected || ((mouseX >= titleX1) && (mouseY >= titleY1) && (mouseX < titleX2) && (mouseY < titleY2));
	}
	return isSelected;
}
	
	
InitiativeObject.prototype.getCenterPos = function() {
	return {x: this.x + this.w / 2, y: this.y + this.h / 2};
}


InitiativeObject.prototype.bringToFront = function() {
	bringVisualElementToFront(this.parentCanvas, this.icon);
	bringVisualElementToFront(this.parentCanvas, this.title);
}


InitiativeObject.prototype.show = function(isVisible, isEnabled, opacity) {
	this.isVisible = isVisible;
	this.isEnabled = isEnabled;
	this.opacity = opacity;
	setVisualElementPos(this.icon, null, null, null, null, isVisible ? opacity : 0);
	setVisualElementPos(this.title, null, null, null, null, isVisible ? opacity : 0);
}


InitiativeObject.prototype.setPosition = function(x, y, w, h) {
	this.x = x;
	this.y = y;
	this.w = ELEMENT_SIZE;
	this.h = ELEMENT_SIZE + FONT_SIZE;
	this.onSetPosition(this.x, this.y, this.w, this.h);
	
	setVisualElementPos(this.icon, x, y, ELEMENT_SIZE, ELEMENT_SIZE, null);
	setVisualElementPos(this.title, x + (ELEMENT_SIZE - this.titleWidth) / 2, y + ELEMENT_SIZE, this.titleWidth + 16, null, null);
	
	this.updateScrollArea();
}
