//*** CLASS PageAvatar *****************************************************
function PageAvatar(id, isExternal, label, soapUrl, parentMenu, width, height, bgColor, fontColor, borderColor, imgUrl) {
	this._id = id;
	this._isExternalPage = isExternal;
	
	//interface Avatar
	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;
	this._label = label;
	this._bgColor = bgColor;
	this._fontColor = fontColor;
	this._borderColor = borderColor;
	this._imageUrl = imgUrl;
	//end interface
	
	//interface MenuItem (see documentation for interface conventions)
	this._soapUrl = soapUrl;
	this._parentMenu = parentMenu;
	this._isSelfTargeting = 0;
	this._isOpened = 0;
	this._isParked = 0;
	this._isClosing = 0;
	this._isMouseOver = 0;
	//end interface
	
	this.createDOM();
}

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

//interface Avatar
PageAvatar.prototype.reachedTarget = function() {		
	if(this._isOpened == 1) {
			//open page viewer will stop animation interval
			UNIQUE_PAGE_VIEWER.openView();
	}
	this._projection._speed = DEFAULT_SPEED;
	this._isSelfTargeting = 0;
}

//interface Avatar
PageAvatar.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);
	
	ProjectionScreen2D_DOM.appendChild(AvatarDOM);
	
	//for now a page avatar contains either an image OR a label
	if(this._imageUrl == "") {
		var LabelDOM = document.createTextNode(this._label);
		AvatarDOM.appendChild(LabelDOM);
	} else {
		$("div#" + this._domId).append("<img src=\"" + this._imageUrl + "\" class=\"avatar_image\" alt=\"\" width=\"" + this._projection._projectedWidth + "\" height=\""+ this._projection._projectedHeight + "\" />");
	}
	
	$("div#" + this._domId).css("color", this._fontColor);
	$("div#" + this._domId).css("background-color", this._bgColor);
	$("div#" + this._domId).css("border-color", this._borderColor);
	
	if(this._isExternalPage == 1) { $("div#" + this._domId).css("font-weight", "bold"); }
	
	//won't work (Mozilla 5.0): parameters are not received in handleClickAvatar(event) { alert(event.data.avatarId }
	//when using:
	//$("div#" + this._domId).bind("mouseover", {avatarId: "FlaschePommes"}, handleClickAvatar);
	//Now using workaround instead. Follow comments in handleClickAvatar().
	$("div#" + this._domId).bind("click", handleClickAvatar);
	$("div#" + this._domId).bind("mouseover", handleMouseOverAvatar);
	$("div#" + this._domId).bind("mouseout", handleMouseOutAvatar);
}

//interface Avatar
PageAvatar.prototype.updateDOM = function() {
	//this function is highly performance critical! Keep it as fast as possible!
	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;
	if(this._imageUrl != "") {
		AvatarDOM.lastChild.width = this._projection._projectedWidth;
		AvatarDOM.lastChild.height = this._projection._projectedHeight;
	}
}

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

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

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

//interface MenuItem
PageAvatar.prototype.openItem = function() {
		this._isMouseOver = 0; //free from mouseover effects
		this._isOpened = 1;
		this._isSelfTargeting = 1;
		this._projection._speed = 60;
		this._projection.setTargetPosition( (VIRTUAL_SPACE_WIDTH / 2), (this._parentMenu._groundLevel- LEVEL_HEIGHT / 2), -50);
		$("div#" + this._domId).css("background-color", "#698198"); //color: active
		$("div#" + this._domId).css("color", "#DDDDDD");			
}

//interface MenuItem
PageAvatar.prototype.closeItem = function() {
	this._isMouseOver = 0;
	this._projection._speed = DEFAULT_SPEED;
	$("div#" + this._domId).css("background-color", this._bgColor); //color: inactive
	$("div#" + this._domId).css("color", "#000000");
	this._isParked = 0; // ???
	this._isOpened = 0;
}

//interface MenuItem
PageAvatar.prototype.handleClick = function() {
	this._isMouseOver = 0; //free from mouseover effects
	
	if(this._isParked == 0) {
		if(this._isExternalPage == 0) {
			if(this._isOpened == 0) {
				//page is clicked to be opened. Make a SOAP call to fetch content.
				if(this._soapUrl != "") {
					//close all other opened avatars and page viewer
					if(UNIQUE_PAGE_VIEWER._isOpened == 1) { UNIQUE_PAGE_VIEWER.closeView(); }
					for(var i=0; i < MENU_STACK[ACTIVE_MENU_ID]._items.length; i++) {
						if(i != this._id && MENU_STACK[ACTIVE_MENU_ID]._items[i]._isOpened == 1) {
							MENU_STACK[ACTIVE_MENU_ID]._items[i].closeItem();
					}
					}//for
					
					//move to top and open page viewer on arrival
					this.openItem();
					
					//preload default busy message into view
					UNIQUE_PAGE_VIEWER.setContent("<div class=\"page_viewer_title\"><h2>Loading... please wait</h2></div><div class=\"page_viewer_content\">Content is being loaded ...<br/><br/>Inhalte werden geladen ...<br/></div></div>");
					
					//send SOAP request to server
					$.ajax({
						type: "GET",
						url: this._soapUrl,
						//dataType: "xml",
						success: function(responseXML) {
								UNIQUE_PAGE_VIEWER.setContent(responseXML);
						},
						error: function(xmlHttpReq, errStr, exceptionObj) {
								alert("SOAP request failed. Can't fetch data. (Message: " + errStr + ")");
						}
						//TODO: timeout
					});
				} else {
					alert("Page has no link set. Can't open.");
				}//if _soapUrl
				
			} else {
				//avatar is currently opened
				if(this._isSelfTargeting == 1)	{
					//close avatar and page viewer
					if(UNIQUE_PAGE_VIEWER._isOpened == 1) { UNIQUE_PAGE_VIEWER.closeView(); }
					this.closeItem();
				}
			}//if _isOpened
			
		} else {
			//avatar is an external link; make a small hop
			this._isSelfTargeting = 1;
			this._projection.set3DPosition(this._projection._posX, this._projection._posY - 40, this._projection._posZ);
			this._projection.setTargetPosition(this._projection._posX, this._projection._posY + 40, this._projection._posZ);
			//open external link in new browser window
			window.open(this._soapUrl, "WindowId", "location=yes, menubar=yes, scrollbars=yes, resizable=yes, toolbar=yes");
			
		}// _isExternalPage
		
	} else {
		//avatar is currently parked
		
		//iterate top-down over every menu level
		//and close all open menuAvatars (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
PageAvatar.prototype.handleMouseOver = function() {
	if(this._isOpened == 0 && this._isClosing == 0) {
		this._isMouseOver = 1;
		$("div#" + this._domId).css("border-color", "#DE8B8B"); //active border
	}
}//handleMouseOver

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