// THIS CODE IS NOT APPROVED FOR USE IN/ON ANY OTHER UI ELEMENT OR PRODUCT COMPONENT. 
// Copyright (c) 2007 Renderspace. All rights reserved.


/************************************************/
// element object
/************************************************/


function ElementObject(parent, id, name, type) {
	this.base = RenderableObject;
	this.base(parent, id, name, type);
	this.isVisible = this.isEnabled = true;
	
	this.parentCanvas = clientForegroundCanvas;
	this.clickTime = DOUBLE_CLICK_TIME;
	this.buttonDisplayTime = BUTTON_DISPLAY_TIME;
	this.isSelected = false;
	this.prevIsSelected = false;
	this.isPressed = false;
	this.isClicked = false;
	this.isDoubleClicked = false;
	this.isFocused = false;
	this.prevIsFocused = true;
	this.isFocusable = true;
	this.dragMouseX = 0;
	this.dragMouseY = 0;
	this.isMoved = false;
	this.connectorList = new Array();
	
	this.closeButton = new ButtonObject(this, 0, null, true, false, false, 0, 0, 0, 0, 1, "close", null, null);
	this.detailsButton = new ButtonObject(this, 0, null, true, false, false, 0, 0, 0, 0, 1, "details", null, null);
	this.expandButton = new ButtonObject(this, 0, null, true, false, false, 0, 0, 0, 0, 1, "expand", null, null);
}

ElementObject.prototype = new RenderableObject;


ElementObject.prototype.disconnect = function() {
    var tempList = this.connectorList;
    this.connectorList = null;
    for (var connIndex in tempList) {
        tempList[connIndex].disconnect(this);
    }
    this.updateVisualState();
    globalIsContentChanged = true;
}


ElementObject.prototype.onDisconnected = function(connector) {
    for (var connIndex in this.connectorList) {
        if (this.connectorList[connIndex] == connector) {
            this.connectorList.splice(connIndex, 1);
            break;
        }
    }
    this.updateVisualState();
    globalIsContentChanged = true;
}


ElementObject.prototype.onConnected = function(connector) {
    this.connectorList.push(connector);
    this.updateVisualState();
    globalIsContentChanged = true;
}


ElementObject.prototype.setFocus = function() {
	if (objectManager != null) {
		objectManager.callFunction(null, "killFocus", null, false);
	}
	this.isFocused = true;
}


ElementObject.prototype.killFocus = function() {
	this.isFocused = false;
}


ElementObject.prototype.getIsPressed = function() {
	return this.isPressed && this.isSelected;
}


ElementObject.prototype.getIsClicked = function() {
	var c = this.isClicked;
	this.isClicked = false;
	return c;
}


ElementObject.prototype.getIsDoubleClicked = function() {
	var c = this.isDoubleClicked;
	this.isDoubleClicked = false;
	return c;
}


ElementObject.prototype.onSetPosition = function(x, y, w, h) {
    for (var connIndex in this.connectorList) {
        this.connectorList[connIndex].updatePosition();
    }
    this.closeButton.setPosition(x + w, y, 8, 8);
    this.detailsButton.setPosition(x + w, y + 9, 8, 8);
    this.expandButton.setPosition(x + w, y + 18, 8, 8);
    globalIsContentChanged = true;
}   
    

ElementObject.prototype.updateScrollArea = function() {
    var maxX = 0, maxY = 0;
	var tempObj = this.parentObject.firstChildObject;
	while (tempObj) {
	    if ((tempObj.type == "Person") || (tempObj.type == "Initiative")) {
    	    if (maxX < tempObj.x + tempObj.w) maxX = tempObj.x + tempObj.w;
    	    if (maxY < tempObj.y + tempObj.h) maxY = tempObj.y + tempObj.h;
    	}
	    tempObj = tempObj.nextObject;
	}
	
	mainHScrollbar.setRange(0, maxX + 32 - screenWidth + SCROLLBAR_SIZE, screenWidth - SCROLLBAR_SIZE);
	mainVScrollbar.setRange(0, maxY + 32 - screenHeight + SCROLLBAR_SIZE, screenHeight - SCROLLBAR_SIZE);
}


ElementObject.prototype.onMouseMove = function(mouseEventArgs, isHandled) {
	if (!this.isEnabled || !this.isVisible) return false;
	
	// get mouse coordinates
	var mousePos = getMousePosition(mouseEventArgs);
	var mouseX = mousePos.x + mainHScrollbar.position;
	var mouseY = mousePos.y + mainVScrollbar.position;
	
	if (!isHandled && this.isPressed) {
		
		var newPosX = mouseX - this.dragMouseX;
		var newPosY = mouseY - this.dragMouseY;
	
		if (newPosX < 0) newPosX = 0;
		if (newPosY < 0) newPosY = 0;
		
		if ((newPosX != this.x) || (newPosY != this.y)) {
			this.setPosition(newPosX, newPosY, this.w, this.h);
			this.isMoved = true;
			globalIsContentChanged = true;
		}
	}
	
	if (isHandled) this.isSelected = false;
	this.isSelected = this.getIsSelected(mouseX, mouseY);
	
	if (!this.isFocusable && (this.prevIsSelected != this.isSelected)) {
	    this.updateVisualState();
	}
	
	if (this.isSelected) setCursor("pointer");
	this.prevIsSelected = this.isSelected;
	
	return this.isSelected || this.isPressed; // handled only when processed
}


ElementObject.prototype.onMouseDown = function(mouseEventArgs, isHandled) {
	if (!this.isEnabled || !this.isVisible) return false;
	this.isPressed = this.isSelected && !isHandled;
	
	if (this.isPressed && (this.clickTime < DOUBLE_CLICK_TIME)) { // double click
		this.isDoubleClicked = true;
		this.clickTime = DOUBLE_CLICK_TIME; // to prevent triple click
		this.isPressed = false;
		showNetworkElementRelations(this);
	}
	
	if (this.isPressed) {
		this.isMoved = false;
		this.clickTime = 0;
		var mousePos = getMousePosition(mouseEventArgs);
		this.dragMouseX = mousePos.x + mainHScrollbar.position - this.x;
		this.dragMouseY = mousePos.y + mainVScrollbar.position - this.y;
		
		if (this.isFocusable) {
			var isSomeIconFocused = false;
			var currObj = this.parentObject.firstChildObject;
			while (currObj != null) {
				if (currObj.isFocused) isSomeIconFocused = true;
				currObj.isFocused = false;
				currObj = currObj.nextObject;
			}
			
			if (isSomeIconFocused) this.isFocused = true;
			else this.setFocus();
				
			this.bringToFront();
			this.setAsFirst();
		}
		
	}
	
	return this.isSelected; // handled only when processed
}


ElementObject.prototype.onMouseUp = function(mouseEventArgs, isHandled) {
	if (!this.isEnabled || !this.isVisible) return false;
	this.isClicked = this.isPressed && this.isSelected && !isHandled;
	this.isPressed = false;
	/*if (this.isSelected && (getMouseButton(mouseEventArgs) == 1)) {
	    return showNetworkElementDetails(this);
	}*/	
	
	if (this.closeButton.getIsClicked()) {
	    this.release();
        globalIsContentChanged = true;
	}
    if (this.detailsButton.getIsClicked()) return showNetworkElementDetails(this);
    if (this.expandButton.getIsClicked()) return showNetworkElementRelations(this);
        
	return this.isSelected; // handled only when processed
}


ElementObject.prototype.onKeyDown = function(keyEventArgs, isHandled) {
	if (!this.isEnabled || !this.isVisible) return false;
	
	var key = getKeyPressed(keyEventArgs);
	if (key == 0) key = getCharPressed(keyEventArgs);
		
	if (!isHandled && this.isFocused) {
		if (key == 46 /* delete */) {
            this.release();
            globalIsContentChanged = true;
            return true;
		}
		if (key == 32 /* space */) {
            return showNetworkElementRelations(this);
		}
		return true;
	}
	
	return false;
}


ElementObject.prototype.onUpdateFrame = function(frameDeltaTime, isHandled) {
    this.clickTime += frameDeltaTime;
    
    var prevDisplayTime = this.buttonDisplayTime;
    if (this.isFocused || this.isSelected) this.buttonDisplayTime = 0;
    else this.buttonDisplayTime += frameDeltaTime;
        
    if ((this.buttonDisplayTime > BUTTON_DISPLAY_TIME) && (prevDisplayTime <= BUTTON_DISPLAY_TIME)) {
        this.closeButton.show(false, false, 1);
        this.detailsButton.show(false, false, 1);
        this.expandButton.show(false, false, 1);
    }
    else if ((this.buttonDisplayTime <= BUTTON_DISPLAY_TIME) && (prevDisplayTime > BUTTON_DISPLAY_TIME)) {
        this.closeButton.bringToFront();
        this.detailsButton.bringToFront();
        this.expandButton.bringToFront();
        this.closeButton.show(true, true, 1);
        this.detailsButton.show(true, true, 1);
        this.expandButton.show(true, true, 1);
    }
    
	if (this.isFocusable && (this.isFocused != this.prevIsFocused)) {
	    this.updateVisualState();
		this.prevIsFocused = this.isFocused;
	}
	return true;
}
