//*** CLASS MenuAvatar *****************************************************
function MenuAvatar(id, label, soapUrl, parentMenu, width, height, bgColor, fontColor, borderColor, imgUrl, defaultMenuMode) {
	this._id = id;
	
	//interface ProjectionUser
	this._projection = new Projection(id, this, width, height, DEFAULT_FONTSIZE, INIT_VANISH_X, INIT_VANISH_Y, INIT_VANISH_Z, DEFAULT_SPEED);
	this._projection._parent = this; //register this Avatar with its child Projection for callbacks
	this._domId = "Avatar_" + parentMenu._id + "_" + id;
	//end  interface
	
	//interface Avatar
	this._label = label;
	this._bgColor = bgColor;
	this._fontColor = fontColor;
	this._borderColor = borderColor;
	this._imageUrl = imgUrl;
	//end interface
	
	//interface MenuItem
	this._soapUrl = soapUrl;
	this._parentMenu = parentMenu;
	this._isSelfTargeting = 0;
	this._isOpened = 0;
	this._isParked = 0;
	this._isClosing = 0;
	this._isMouseOver = 0;
	this._defaultMenuMode = defaultMenuMode;
	//end interface
	
	this.createDOM();
}

MenuAvatar.prototype._id;
MenuAvatar.prototype._projection;
MenuAvatar.prototype._domId;
MenuAvatar.prototype._label;
MenuAvatar.prototype._bgColor;
MenuAvatar.prototype._fontColor;
MenuAvatar.prototype._borderColor;
MenuAvatar.prototype._imageUrl;
MenuAvatar.prototype._soapUrl;
MenuAvatar.prototype._parentMenu;
MenuAvatar.prototype._isSelfTargeting;
MenuAvatar.prototype._isOpened;
MenuAvatar.prototype._isParked;
MenuAvatar.prototype._isClosing;
MenuAvatar.prototype._isMouseOver;

//interface ProjectionUser
MenuAvatar.prototype.reachedTarget = function() {
	if(this._isOpened == 1) {
		//switch to next menu level (exactly now on menu avatars arrival!)
		ACTIVE_MENU_ID = ACTIVE_MENU_ID + 1;
		
		//retry interval: show avatars of submenu if SOAP request has finished (MenuLevel._isLoaded)
		// (release interval in MenuLevel.finishedLoading() and SOAP error and timeout event handlers)
		OPEN_MENU_INTERVAL = window.setInterval("MENU_STACK[ACTIVE_MENU_ID].finishedLoading()", OPEN_MENU_PERIOD);
		
		//position menu toolbar
		UNIQUE_MENU_TOOLBAR.warpToActiveMenu();
	}//if
	this._projection._speed = DEFAULT_SPEED;
	this._isSelfTargeting = 0;
}

//interface ProjectionUser
MenuAvatar.prototype.createDOM = function() { 
	var ProjectionScreen2D_DOM =  document.getElementById('ProjectionScreen2D');
	var AvatarDOM = document.createElement("div");
	
	var IdAttr = document.createAttribute("id");
	IdAttr.nodeValue = this._domId;
	AvatarDOM.setAttributeNode(IdAttr);
	
	var ClassAttr = document.createAttribute("class");
	ClassAttr.nodeValue = "avatar";
	AvatarDOM.setAttributeNode(ClassAttr);
	
	var LabelDOM = document.createTextNode(this._label);
	AvatarDOM.appendChild(LabelDOM);
	
	ProjectionScreen2D_DOM.appendChild(AvatarDOM);
	if(this._bgColor != "") { $("div#" + this._domId).css("background-color", this._bgColor); }
	$("div#" + this._domId).css("background-color", this._bgColor);
	$("div#" + this._domId).css("border-color", this._borderColor);
	$("div#" + this._domId).css("color", this._fontColor);
	
	$("div#" + this._domId).bind("click", handleClickAvatar);
	$("div#" + this._domId).bind("mouseover", handleMouseOverAvatar);
	$("div#" + this._domId).bind("mouseout", handleMouseOutAvatar);
}

//interface ProjectionUser
MenuAvatar.prototype.updateDOM = function() {
	var AvatarDOM = document.getElementById(this._domId);
	AvatarDOM.style.left = this._projection._projectedX + "px";
	AvatarDOM.style.top = this._projection._projectedY + "px";
	AvatarDOM.style.width = this._projection._projectedWidth + "px";
	AvatarDOM.style.height = this._projection._projectedHeight + "px";
	AvatarDOM.style.fontSize = this._projection._projectedFontsize + "pt";
	AvatarDOM.style.zIndex = (VIRTUAL_SPACE_DEPTH + 100) - this._projection._posZ;
	//uncomment for debugging purposes
	//$("div#" + this._domId).empty().append(this._label + "any debug info");
}

//interface ProjectionUser
MenuAvatar.prototype.removeDOM	= function() {
	$("div#" + this._domId).remove();
}

//interface ProjectionUser
MenuAvatar.prototype.showDOM	= function() {
	$("div#" + this._domId).show();
}

//interface ProjectionUser
MenuAvatar.prototype.hideDOM	= function() {
	$("div#" + this._domId).hide();
}

//interface MenuItem
MenuAvatar.prototype.openItem = function() {
	this._isMouseOver = 0; //free from mouseover effects
	this._isOpened = 1;
	this._isSelfTargeting = 1;
	this._projection._speed = 35;
	var targetX = 50;
	var targetY = (MENU_STACK[ACTIVE_MENU_ID]._groundLevel) - (LEVEL_HEIGHT * 0.8);
	var targetZ = 0;
	this._projection.setTargetPosition(targetX, targetY, targetZ);
	$("div#" + this._domId).css("background-color", "#DE8B8B"); //color: active
	$("div#" + this._domId).css("color", "#333333");
}

//interface MenuItem
MenuAvatar.prototype.closeItem = function() {
	this._isMouseOver = 0; //free from mouse over effects
	
	//unload menu
	MENU_STACK[ACTIVE_MENU_ID]._isLoaded = 0;
	
	//remove related avatars
	for(var i=0; i < MENU_STACK[ACTIVE_MENU_ID]._items.length; i++) {
		MENU_STACK[ACTIVE_MENU_ID]._items[i]._isParked = 1;
		MENU_STACK[ACTIVE_MENU_ID]._items[i]._isSelfTargeting = 0;
		MENU_STACK[ACTIVE_MENU_ID]._items[i].removeDOM();
	}//for
	
	//---------- ACTIVE LEVEL SWITCH ---------------------------------------------------
	//throw menu from stack
	MENU_STACK.pop();
	//decrease menu level count
	ACTIVE_MENU_ID = ACTIVE_MENU_ID - 1;
	//-----------------------------------------------------------------------------------
	
	//move toolbar to decreased level
	if(ACTIVE_MENU_ID > 0) {
		UNIQUE_MENU_TOOLBAR.warpToActiveMenu();
	} else {
		UNIQUE_MENU_TOOLBAR._projection.set3DPosition(20, 400, 15); //init to main menu level
	}
	
	//free the menu avatar that is being closed
	this._projection._speed = DEFAULT_SPEED;
	this._isOpened = 0;
	this._isSelfTargeting = 0;
	$("div#" + this._domId).css("background-color", this._bgColor); //color: inactive
	$("div#" + this._domId).css("color", this._fontColor);
	
	for(var i=0; i < MENU_STACK[ACTIVE_MENU_ID]._items.length; i++) {
		//free avatars of reactivated menu from parking position
		if(i != this._id) {
			//MENU_STACK[ACTIVE_MENU_ID]._items[i]._projection._speed = DEFAULT_SPEED;
			MENU_STACK[ACTIVE_MENU_ID]._items[i]._isParked = 0;
			MENU_STACK[ACTIVE_MENU_ID]._items[i]._isSelfTargeting = 0;
		}
	}//for
	
	//if exists, free parent menu avatar of reactivated menu
	if(ACTIVE_MENU_ID > 0) {
		for(var i=0; i < MENU_STACK[ACTIVE_MENU_ID - 1]._items.length; i++) {
			if(MENU_STACK[ACTIVE_MENU_ID - 1]._items[i]._isOpened == 1) {
				MENU_STACK[ACTIVE_MENU_ID - 1]._items[i]._isParked = 0;
			}
		}//for
	}//if
}//closeItem

//interface MenuItem
MenuAvatar.prototype.handleClick = function() {
	this._isMouseOver = 0; //free from mouseover effects
	
	if(this._isParked == 0) {
		if(this._isOpened == 0) {
			//menu avatar is clicked to be opened. Make a SOAP call to fetch content.
			if(this._soapUrl != "") {
				//park the parent menu avatar of current menu, if exists
				if( ACTIVE_MENU_ID > 0) {
					for(var i=0; i < MENU_STACK[ACTIVE_MENU_ID - 1]._items.length; i++) {
						if(MENU_STACK[ACTIVE_MENU_ID - 1]._items[i]._isOpened) {
							MENU_STACK[ACTIVE_MENU_ID - 1]._items[i]._isParked = 1;
						}
					}
				}
				
				//close all other opened avatars and page viewer of active menu
				if(UNIQUE_PAGE_VIEWER._isOpened) { UNIQUE_PAGE_VIEWER.closeView(); }
				for(var i=0; i < MENU_STACK[ACTIVE_MENU_ID]._items.length; i++) {
					if(i != this._id) {
						if(MENU_STACK[ACTIVE_MENU_ID]._items[i]._isOpened == 1) {
							MENU_STACK[ACTIVE_MENU_ID]._items[i].closeItem();
						}
						//send avatar to parking position
						MENU_STACK[ACTIVE_MENU_ID]._items[i]._isParked = 1;
						MENU_STACK[ACTIVE_MENU_ID]._items[i]._isSelfTargeting = 1;
						MENU_STACK[ACTIVE_MENU_ID]._items[i]._projection._speed = 35;
						var targetX = VIRTUAL_SPACE_WIDTH / 2 + (i * 40);
						var targetY = MENU_STACK[ACTIVE_MENU_ID]._groundLevel + 50;
						var targetZ = VIRTUAL_SPACE_DEPTH - 100 - (i * 30);
						MENU_STACK[ACTIVE_MENU_ID]._items[i]._projection.setTargetPosition(targetX, targetY, targetZ);
					}//if
				}//for
				
				//throw new submenu on stack
				var Submenu = new MenuLevel(ACTIVE_MENU_ID + 1, MENU_STACK[ACTIVE_MENU_ID]._groundLevel - LEVEL_HEIGHT, this);
				MENU_STACK.push(Submenu);
				
				//move to front
				this.openItem();
				
				//send SOAP request to server
				$.ajax({
					type: "GET",
					url: this._soapUrl,
					dataType: "xml",
					success: function(responseXML){
						Submenu.populateXML(responseXML);
					},
					error: function(xmlHttpReq, errStr, exceptionObj){
						alert("SOAP request failed. Can't fetch data. (Message: " + errStr + ")");
						window.clearInterval(OPEN_MENU_INTERVAL);
					}
				//	timeout: function(xmlHttpReq, errStr, exceptionObj){
				// 	window.clearInterval(OPEN_MENU_INTERVAL);
				// 	alert(errStr);
				//	}
				});
				
			} else {
				alert("Submenu has no link set. Can't open");
			}//if _soapUrl
			
		} else {	
			//submenu is currently opened
			
			if(this._isSelfTargeting == 1) {
				//ignore click for now. TODO: accept click to cancel opening
			} else {
				this.closeItem();
			}	
		}//if isOpened
		
	} else {
		//avatar is currently parked
		
		//iterate top-down over every menu level
		//and close all avatars (menus and pages) until the menu level of the clicked avatar is active.
		while(ACTIVE_MENU_ID > this._parentMenu._id) {
			var prevMenuLevel = ACTIVE_MENU_ID - 1;
			for(var i=0; i < MENU_STACK[prevMenuLevel]._items.length; i++) {
				if(MENU_STACK[prevMenuLevel]._items[i]._isOpened == 1) {
					MENU_STACK[prevMenuLevel]._items[i].closeItem();
				}
			}
		}//while
	}//if isParked
	
}//handleClick


//interface MenuItem
MenuAvatar.prototype.handleMouseOver = function() {
	if(this._isOpened == 0) {
		this._isMouseOver = 1;
		$("div#" + this._domId).css("border-color", "#DE8B8B"); //active border
	}
	return false;
}

//interface MenuItem
MenuAvatar.prototype.handleMouseOut = function() {	
	this._isMouseOver = 0;
	$("div#" + this._domId).css("border-color", this._borderColor); //normal border
	return false;
}

//*** END CLASS MenuAvatar ************************************************
