<?xml version="1.0" encoding="UTF-8"?>
<rmml>
<!--
$RCSfile: rnmc_rollovermenu.xml,v $
$Revision: 845 $

last change: $Date: 2008-11-19 16:46:47 +0300 (Ср, 19 ноя 2008) $
             $Author: toha $

	Lacerta Project
	version 1.0.0.1
	Copyright (C) 2008 RNMC
-->

<composition name="rnmc_RollOverMenu" class="rnmc_stdObject">
<xml name="settingsXML"/>
<script><![CDATA[
	this.targetObject = null;

	this.menuAttachPoint = ROLLOVERMENU_POINT_RIGHTBOTTOM;
	this.menuOffsetX = 0;
	this.menuOffsetY = 0;

	this.buttonCoords = null;
//	
	this.layouts = null;
	this.childObjects = null;

	this.targetObjectX = 0;
	this.targetObjectY = 0;
	this.targetObjectWidth = 0;
	this.targetObjectHeight = 0;
	this.targetObjectDepth = 0;

	this.optionsEnabled = null;
	this.optionCount = 0;	
	this.menuName = null;

	function initObject(){
		var xmlNode = null;
		var xmlAttributeMap = null;
		var xmlAttribute = null;

		if(!this.childObjects){
			this.childObjects = new Array();
		}
		if(!this.layouts){
			this.layouts = new Array();
		}

		if(!resourceExists(scene.rollover_menu_xml)){
trace("ОШИБКА: Файл настроек rollovermenu не найден.");
			return false;
		}
		settingsXML.src = scene.rollover_menu_xml;
		if (!settingsXML.XMLDocument){
			// Если загружаемый файл не правильный - пишем в трэйсер и выходим
			trace("#FF0000Ошибка: файл " + settingsSrc + " не является правильным XML файлом.");
			return false;
		} else {
			settingsXML.XMLDocument.source = settingsXML.src;
			xmlNode = settingsXML.XMLDocument.firstChild.firstChild;
			var buttonCoords_old = null
			while(xmlNode){	
				if(xmlNode.nodeName == "param"){
					xmlAttributeMap = xmlNode.attributes;
					if(xmlAttributeMap){
						xmlAttribute =  xmlAttributeMap.getNamedItem("attachPoint");
						if (xmlAttribute != null && xmlAttribute.specified){
							switch(xmlAttribute.value){
							case "TOP_LEFT":
								this.menuAttachPoint = ROLLOVERMENU_POINT_LEFTTOP;
								break;
							case "BOTTOM_LEFT":
								this.menuAttachPoint = ROLLOVERMENU_POINT_LEFTBOTTOM;
								break;
							case "TOP_RIGHT":
								this.menuAttachPoint = ROLLOVERMENU_POINT_RIGHTTOP;
								break;
							case "BOTTOM_RIGHT":
								this.menuAttachPoint = ROLLOVERMENU_POINT_RIGHTBOTTOM;
								break;
							}
						}
						xmlAttribute =  xmlAttributeMap.getNamedItem("x");
						if (xmlAttribute != null && xmlAttribute.specified)
							this.menuOffsetX = parseInt(xmlAttribute.value);
						xmlAttribute =  xmlAttributeMap.getNamedItem("y");
						if (xmlAttribute != null && xmlAttribute.specified)
							this.menuOffsetY = parseInt(xmlAttribute.value);
				
						xmlAttribute = xmlAttributeMap.getNamedItem("buttonCoords");
						if(xmlAttribute != null && xmlAttribute.specified){
							var coords = scene.Utils.parsePolygonToArray(xmlAttribute.value);
							if(!coords || coords.length == 0){
								return false;
							} else {
								this.buttonCoords = coords;
							}
						}
					}
				} else if(xmlNode.nodeName == "button"){
					var newObject = scene.Utils.initStdButtonFromNode(xmlNode, this);
					if(newObject){
						newObject.setReleaseCallBack(this, this.buttonDispatcher);
						this.childObjects.push(newObject);
					}
					newObject = null;
				} else if(xmlNode.nodeName == "element"){
					var newObject = scene.Utils.initStdElementFromNode(xmlNode, this);
					if(newObject){
						if("setReleaseCallBack" in newObject)
							newObject.setReleaseCallBack(this, this.buttonDispatcher);
						this.childObjects.push(newObject);
					}
					newObject = null;
				} else if(xmlNode.nodeName == "layouts"){
					var layoutNode = xmlNode.firstChild;
					while(layoutNode){
						if(layoutNode.nodeName == "layout"){
							var newLayout = new Array();
							newLayout.optionCount = 0;
							newLayout.buttonCoords = null;
							xmlAttributeMap = layoutNode.attributes;
							if (xmlAttributeMap){
								xmlAttribute =  xmlAttributeMap.getNamedItem("optionCount");
								if (xmlAttribute != null && xmlAttribute.specified)
									newLayout.optionCount = parseInt(xmlAttribute.value);
								xmlAttribute =  xmlAttributeMap.getNamedItem("buttonCoords");
								if (xmlAttribute != null && xmlAttribute.specified)
									newLayout.buttonCoords =
										scene.Utils.parsePolygonToArray(xmlAttribute.value);
								xmlAttribute =  xmlAttributeMap.getNamedItem("name");
								if (xmlAttribute != null && xmlAttribute.specified)
									newLayout.name = xmlAttribute.value;
							}
							var objectNode = layoutNode.firstChild;
							while(objectNode){
								if(objectNode.nodeName == "image"){
									var newObject =
										scene.Utils.initStdImageFromNode(objectNode, this);
									if(newObject)
										newLayout.push(newObject);
								} else if(objectNode.nodeName == "text"){
									var newObject =
										scene.Utils.initStdTextFromNode(objectNode, this);
									if(newObject)
										newLayout.push(newObject);
								}
								objectNode = objectNode.nextSibling;
							}
							this.layouts.push(newLayout);
						}
						layoutNode = layoutNode.nextSibling;
					}
				} else if (xmlNode.nodeName == "background"){
				//  Для совместимости со старой версией
					var newLayout = new Array();
					newLayout.optionCount = 0;
					newLayout.buttonCoords = this.buttonCoords;
					
					xmlAttributeMap = xmlNode.attributes;
					if(xmlAttributeMap){
						xmlAttribute = xmlAttributeMap.getNamedItem("options");
						if(xmlAttribute != null && xmlAttribute.specified){
							newLayout.optionCount =  xmlAttribute.value;
							xmlAttribute = xmlAttributeMap.getNamedItem("src");
							if(xmlAttribute != null && xmlAttribute.specified){
								var newObject = new Composition();
								newObject._class = "rnmc_stdImage";
								newObject.visible = false;
								newObject.parent = this;
								newObject.objectSrc = xmlAttribute.value;
								newLayout.push(newObject);
							}
						}
					}
					this.layouts.push(newLayout);
				} else {
					var newObject = scene.Utils.initStdObjectFromNode(xmlNode, this);
					if(newObject){
						if("setReleaseCallBack" in newObject)
							newObject.setReleaseCallBack(this, this.buttonDispatcher);
						this.childObjects.push(newObject);
					}
					newObject = null;
				}
				xmlNode = xmlNode.nextSibling;
			}
		}
		return true;
	}

	function attachToObject(targetObject, targetPoint, targetX, targetY){
		if(targetPoint != undefined)
			this.menuAttachPoint = targetPoint;
		if(targetX != undefined)
			this.menuOffsetX = targetX;
		if(targetY != undefined)
			this.menuOffsetY = targetY;
		this.reattachToObject(targetObject);
	}

	function absoluteObjectDepth(object){
		var tmp = 0;
		while (object){
			tmp += object.depth;
			object = object.parent;
		}
		return tmp;
	}

	function reattachToObject(trgbject){
		this.targetObject = trgbject;
		if(trgbject){
			this.targetObjectX = targetObject.x;
			this.targetObjectY = targetObject.y;
			this.targetObjectWidth = targetObject.width;
			this.targetObjectHeight = targetObject.height;
			this.targetObjectDepth = this.absoluteObjectDepth(trgbject); 
			var tmp = targetObject.parent.localToGlobal(this.targetObjectX, this.targetObjectY);
			this.targetObjectX = tmp.x - scene.x;
			this.targetObjectY = tmp.y - scene.y;
//			this.parent = scene;
		}
	}

	function enableMenuOptions(menuOptions, menuName){
		var optionCounter = 0;

		if(menuOptions){
			this.optionsEnabled = menuOptions.slice(0);
			this.optionCount = this.optionsEnabled.length;
		} else {
			this.optionsEnabled = null;
			this.optionCount = 0;
		}

		this.menuName = (menuName != undefined) ? menuName : null;
	}

	function showObject(){
	//	показываем сначала то что относится к лайот
	//	для того чтобы знать размеры 
		if (this.menuName) {
			for(var i = 0; i < this.layouts.length; i++){
				if(this.menuName == this.layouts[i].name
					&& this.layouts[i].optionCount == this.optionCount){
					this.currentLayout = i;
					for(var j = 0; j < this.layouts[i].length; j++){
						this.layouts[i][j].showObject();
					}
				}
			}		
		} else {
			for(var i = 0; i < this.layouts.length; i++){
				if(this.layouts[i].optionCount == this.optionCount){
					this.currentLayout = i;
					for(var j = 0; j < this.layouts[i].length; j++){
						this.layouts[i][j].showObject();
					}
				}
			}
		}

		for(var i = 0; this.optionsEnabled && i < this.optionsEnabled.length; i++){
			for(var j = 0 ; this.childObjects && j < this.childObjects.length; j++){
				if((this.childObjects[j].id).toLowerCase()
					== (this.optionsEnabled[i]).toLowerCase()){
					this.childObjects[j].x = this.layouts[this.currentLayout].buttonCoords[i].x;
					this.childObjects[j].y = this.layouts[this.currentLayout].buttonCoords[i].y;
					if (this.childObjects[j].type == "rnmc_stdPlayControl"){
						this.childObjects[j].attachToObject(this.targetObject);
						this.childObjects[j].setDispatcher(
							this.targetObject.parent,
							this.targetObject.parent.buttonDispatcher);
					}
					this.childObjects[j].showObject();
				}
			}
		}

		this.visible = true;

		switch(this.menuAttachPoint){
		case ROLLOVERMENU_POINT_LEFTTOP:
			this.x = this.targetObjectX + this.menuOffsetX;
			this.y = this.targetObjectY  + this.menuOffsetY;
			break;
		case ROLLOVERMENU_POINT_RIGHTTOP:
			this.x = ((this.targetObjectX + this.targetObjectWidth) - this.width)
				+ this.menuOffsetX;
			this.y = this.targetObjectY  + this.menuOffsetY;
			break;
		case ROLLOVERMENU_POINT_RIGHTBOTTOM:
			this.x = ((this.targetObjectX + this.targetObjectWidth) - this.width)
				+ this.menuOffsetX;
			this.y = ((this.targetObjectY  + this.targetObjectHeight) - this.height)
				+ this.menuOffsetY;
			break;
		case ROLLOVERMENU_POINT_LEFTBOTTOM:
			this.x = this.targetObjectX + this.menuOffsetX;
			this.y = ((this.targetObjectY  + this.targetObjectHeight) - this.height)
				+ this.menuOffsetY;
			break;
		}
		this.depth = this.targetObjectDepth - 20;
	}

	function hideObject(){
		this.targetObject = null;
		for(var i = 0; this.childObjects && i < this.childObjects.length; i++){
			this.childObjects[i].hideObject();
		}
		for(var i = 0; this.layouts && i < this.layouts.length; i++){
			if(this.layouts[i].optionCount == this.optionCount){
				for(var j = 0; j < this.layouts[i].length; j++){
					if (this.childObjects[j].type == "rnmc_stdPlayControl"){
						this.childObjects[j].attachToObject(null);
						this.childObjects[j].setDispatcher(null, null);
					}
					this.layouts[i][j].hideObject();
				}
			}
		}
		this.visible = false;
	}

	function setCallBack(callbackObj, callBackFunct){
		this.callbackObject = callbackObj;
		this.callbackFunction = callBackFunct;
	}

	function buttonDispatcher(btnId){
		if(this.callbackObject && this.callbackFunction)
			this.callbackFunction.call(this.callbackObject, btnId);
	}
	
	function synchronizeControl(){
		for(var i = 0 ; this.childObjects && i < this.childObjects.length; i++){
			if (this.childObjects[i].visible
				&& this.childObjects[i].type == "rnmc_stdPlayControl")
				this.childObjects[i].synchronizeControl();
		}
	}
]]></script>
</composition>
</rmml>