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

last change: $Date: 2008-09-15 14:44:27 +0400 (Пн, 15 сен 2008) $
             $Author: toha $

	Lacerta Project
	version 1.0.0.1
	Copyright (C) 2008 RNMC
-->
<composition name="rnmc_puzzle_dragobject">
<script><![CDATA[
// константы скорости, рабочей зоны и эффекта появления
    this.flowdelta = 12;
	this.sensitivity = 50;

	this.nWorkZoneX = 0;
	this.nWorkZoneY = 0;
	this.nWorkZoneWidth = Player.module.scenes.width;
	this.nWorkZoneHeight = Player.module.scenes.height;

	this.captured = false;
	this.placed = false;
	this.originalDepth = 0;
	this.xOffset = 0;
	this.yOffset = 0;
	this.xTarget = 0;
	this.yTarget = 0;
	this.xDestination = 0;
	this.yDestination = 0;
	this.xSource = 0;
	this.ySource = 0;

	function initObject(objId, imgSrc, cropX, cropY,
						cropWidth, cropHeight, scaleWidth, scaleHeight){
		this.visible = false;
		this.id = objId;
		this.imageObj = new Image();
		this.imageObj.visible = true;
		this.imageObj.src = imgSrc;
		this.imageObj.x = 0;
		this.imageObj.y = 0;
		this.imageObj.parent = this;
		this.imageObj.crop(cropX, cropY, cropWidth, cropHeight);
		this.imageObj.width = scaleWidth;
		this.imageObj.height = scaleHeight;
	};

	function setTarget(targetX, targetY){
		this.xTarget = targetX;
		this.yTarget = targetY;
		this.xDestination = targetX;
		this.yDestination = targetY;
	};
	
	function setSource(srcX, srcY){
		this.xSource = srcX;
		this.ySource = srcY;
	// пока сразу на месте
		this.x = srcX;
		this.y = srcY;
	};

	function showObject(){
		this.visible = true;
		this.onRelease = releaseHandler;
		this.onReleaseOutside = releaseHandler;
		this.onPress = pressHandler;
	};

	function hideObject(){
		this.visible = false;
		this.onRelease = null;
		this.onReleaseOutside = null;
		this.onPress = null;
	};

	function update(){
		var xTargetOffset = 0;
		var yTargetOffset = 0;
		if(this.placed){
		// если деталь встала на место, то
		// плавно подтащить ее к нужным координатам (xDestination, yDestination)
			xTargetOffset = this.xDestination - this.x;
			yTargetOffset = this.yDestination - this.y;
		} else {			//if(bInStart){
		// если промахнулись, то
		// плавно вернуть деталь на место (xSource, ySource)
			xTargetOffset = this.xSource - this.x;
			yTargetOffset = this.ySource - this.y;
		};

		if(xTargetOffset != 0){
			if (xTargetOffset > this.flowdelta)
				this.x += this.flowdelta;
			else if (xTargetOffset < - this.flowdelta)
				this.x -= this.flowdelta;
			else
				this.x += xTargetOffset;
		};

		if(yTargetOffset != 0){
			if (yTargetOffset > this.flowdelta)
				this.y += this.flowdelta;
			else if (yTargetOffset < - this.flowdelta)
				this.y -= this.flowdelta;
			else
				this.y += yTargetOffset;
		};	

		if (Math.round(xTargetOffset - 0.5) == 0 && Math.round(yTargetOffset - 0.5) == 0){
			objectLoop.playing = false;
			this.depth = this.originalDepth;
			if(this.placed){
				this.dragOnPlaced();
			};
			return;
		};
	};

	function testIsInPlace(){
		if((Math.abs(this.x - this.xTarget) < this.sensitivity)
			&& (Math.abs(this.y - this.yTarget) < this.sensitivity))
			return true;
		else
			return false;
	};

// обработчики событий
	function pressHandler(){
		objectLoop.playing = false;
		this.originalDepth = this.depth;
		this.depth = -10;
		this.onMouseMove = mouseMoveHandler;
		this.xOffset = xMouse;
		this.yOffset = yMouse;
		this.captured = true;
	};

	function dragOnPlaced(){
		this.enabled = false;
		parent.notyfyPlaced();
	};

	function releaseHandler(){
		if(captured){
			this.onMouseMove = null;
			this.captured = false;
			if(this.testIsInPlace()){
				this.placed = true;
				this.useHandCursor = false;
				this.onRelease = null;
				this.onReleaseOutside = null;
				this.onPress = null;
			};
			objectLoop.playing = true;
		};
	};

	function mouseMoveHandler(){
		if(captured){
			var newX = this.x + (xMouse - this.xOffset);
			var newY = this.y + (yMouse - this.yOffset);

			if(newX  < nWorkZoneX)
				newX = nWorkZoneX;
			else if(newX  > nWorkZoneWidth - width)
				newX = nWorkZoneWidth - width;

			if(newY  < nWorkZoneY)
				newY = nWorkZoneY;
			else if(newY  > nWorkZoneHeight - height)
				newY = nWorkZoneHeight - height;

			this.x = newX;
			this.y = newY;	
		}
	};
]]>
</script>
<sequencer name="objectLoop" playing="false" loop="true">
    <script>
		update();
	</script>
</sequencer>
</composition>

<composition name="rnmc_labwork_puzzle" class="rnmc_labwork_base">
<script><![CDATA[
	this.type = "RNMC_LABWORK_PUZZLE";
	this.id = null;

// настройки
	this.startAllX = 514;
	this.startAllY = 50;
	this.startOneX = 514;
	this.startOneY = 50;
	this.destinationX = 74;
	this.destinationY = 50;
	this.destinationWidth = 322;
	this.destinationHeight = 322;

// подпись текущей показанной картинки
	this.currentImageTitleX = 0;
	this.currentImageTitleY = 0;
	this.currentImageTitleWidth = 0;
	this.currentImageTitleStyle = null;

//	селектор картинок (сама картинка маленькая)
	this.selectorX = 800;
	this.selectorY = 400;
	this.selectorWidth = 150;
	this.selectorHeight = 150;

// 
	this.borderBgColor = "#FFFFFF";
	this.borderColor = "#000000";

	this.sliderX = 0;
	this.sliderY = 0;
	this.sliderWidth = 0;
	this.sliderBg = null;
	this.sliderBall = null;

// кнопки 
	this.btnNextTask = null;
	this.btnBreakTask = null;
	this.btn8pieces = null;
	this.btn16pieces = null;
	this.btn24pieces = null;
	this.btnOne = null;
	this.btnAll = null;
	this.btnPreviousPiece = null;
	this.btnNextPiece = null;
	this.btnSelectImage = null;
	this.btnPreviousImage = null;
	this.btnNextImage = null;
	this.sldrImageSelect = null;
	this.btnZoomImage = null;

// подпись текущей картинки
	this.currentImageTitle = null;
// ============================================================================= 
	this.screenObjects = null;
	this.puzzleImages = null;

	this.currentImage = 0;
	this.selectorImage = 0;

	this.pieceTotal = 16;
// количество кусков мозаики по горизонтаои
	this.pieceHorizontal = 4;
// количество кусков мозаики по вертикале
	this.pieceVertical = 4;
// режим отображения кусков мозаики
	this.currentMode = PUZZLE_MODE_ALL;

	this.startOffsetX = 0;
	this.startOffsetY = 0;
	this.targetOffsetX = 0;
	this.targetOffsetY = 0;

	function loadStep(srcNode){
//trace("labwork_puzzle.initStep");
		var xmlNode = null;
		var xmlAttributeMap = null;
		var xmlAttribute = null;

		this.childObjects = new Array();
		this.loadChildsObject(srcNode);

		this.puzzleImages = new Array();
		this.imageBuffer = new Image();
		this.imageBuffer.parent = this;
		this.imageBuffer.visible = false;

		xmlNode = srcNode.firstChild;
		while(xmlNode){
			if(xmlNode.nodeName == "conditions"){
				var conditionNode = xmlNode.firstChild;
				while(conditionNode){
					if(conditionNode.nodeName == "condition"){
						this.parseConditionNode(conditionNode);
					}
					conditionNode = conditionNode.nextSibling;
				}
				this.parseConditionNode(xmlNode);
			} else if(xmlNode.nodeName == "settings"){
				this.parseSettingsNode(xmlNode);
				var settingNode = xmlNode.firstChild;
				while(settingNode){
					if(settingNode.nodeName == "controls"){
						var ctrlNode = settingNode.firstChild;
						while(ctrlNode){
							if(ctrlNode.nodeName == "button"){
								var newCtrl = scene.Utils.initStdButtonFromNode(ctrlNode, this);
								if(newCtrl)
									newCtrl.setReleaseCallBack(this, this.buttonDispatcher);
								switch(newCtrl.id){
								case "8pieces":
									this.btn8pieces = newCtrl;
									break;
								case "16pieces":
									this.btn16pieces = newCtrl;
									break;
								case "24pieces":
									this.btn24pieces = newCtrl;
									break;
								case "one":
									this.btnOne = newCtrl;
									break;
								case "all":
									this.btnAll = newCtrl;
									break;
								case "previouspiece":
									this.btnPreviousPiece = newCtrl;
									break;
								case "nextpiece":
									this.btnNextPiece = newCtrl;
									break;
								case "selectimage":
									this.btnSelectImage = newCtrl;
									break;
								case "previousimage":
									this.btnPreviousImage = newCtrl;
									break;
								case "nextimage":
									this.btnNextImage = newCtrl;
									break;
								case "zoomimage":
									this.btnZoomImage = newCtrl;
									this.btnZoomImage.setRollOverCallBack(
										this, zoomInCurrentImage);
									this.btnZoomImage.setRollOutCallBack(
										this, zoomOutCurrentImage);
									break;
								default:
									newCtrl.parent = null;
									newCtrl = null;
									break;
								};
							} else if(ctrlNode.nodeName == "text"){
								xmlAttributeMap = ctrlNode.attributes;
								if (xmlAttributeMap){
									xmlAttribute = xmlAttributeMap.getNamedItem("id");
									if (xmlAttribute != null && xmlAttribute.specified){
										if(xmlAttribute.value == "counter"){
											this.selectorCounter =
												scene.Utils.initStdTextFromNode(ctrlNode, this);
										} else if(xmlAttribute.value == "title"){
											this.imageTitle =
												scene.Utils.initStdTextFromNode(ctrlNode, this);
										}
									}
								};
							} else if(ctrlNode.nodeName == "slider"){
								this.sldrImageSelect = 
									scene.Utils.initStdSliderFromNode(ctrlNode, this);
//								this.sldrImageSelect = new Composition();
//								this.sldrImageSelect._class = "rnmc_stdSlider";
//								this.sldrImageSelect.parent = this;
//								this.sldrImageSelect.backgroundSrc = this.sliderBg;
//								this.sldrImageSelect.ballNormalSrc = this.sliderBall;
//								this.sldrImageSelect.ballPressedSrc = this.sliderBall;
//								this.sldrImageSelect.useTicks = true;
//								this.sldrImageSelect.visible = false;
//								this.sldrImageSelect.x = this.sliderX;
//								this.sldrImageSelect.y = this.sliderY;
//								this.sldrImageSelect.width = this.sliderWidth;
								this.sldrImageSelect.callOnMouseMove = true;
								this.sldrImageSelect.callOnRelease = true;
								this.sldrImageSelect.setCallback(this, this.showSelectorImage);
							};
							ctrlNode = ctrlNode.nextSibling;
						};
					} else if(settingNode.nodeName == "startplace"){
						xmlAttributeMap = settingNode.attributes;
						if (xmlAttributeMap){
							xmlAttribute =  xmlAttributeMap.getNamedItem("all_x");
							if (xmlAttribute != null && xmlAttribute.specified){
								this.startAllX = parseInt(xmlAttribute.value);
							};
							xmlAttribute =  xmlAttributeMap.getNamedItem("all_y");
							if (xmlAttribute != null && xmlAttribute.specified){
								this.startAllY = parseInt(xmlAttribute.value);
							};
							xmlAttribute =  xmlAttributeMap.getNamedItem("one_x");
							if (xmlAttribute != null && xmlAttribute.specified){
								this.startOneX = parseInt(xmlAttribute.value);
							};
							xmlAttribute =  xmlAttributeMap.getNamedItem("one_y");
							if (xmlAttribute != null && xmlAttribute.specified){
								this.startOneY = parseInt(xmlAttribute.value);
							};
						};
					} else if(settingNode.nodeName == "selector"){
						xmlAttributeMap = settingNode.attributes;
						if (xmlAttributeMap){
							xmlAttribute =  xmlAttributeMap.getNamedItem("x");
							if (xmlAttribute != null && xmlAttribute.specified){
								this.selectorX = parseInt(xmlAttribute.value);
							};
							xmlAttribute =  xmlAttributeMap.getNamedItem("y");
							if (xmlAttribute != null && xmlAttribute.specified){
								this.selectorY = parseInt(xmlAttribute.value);
							};
							xmlAttribute =  xmlAttributeMap.getNamedItem("width");
							if (xmlAttribute != null && xmlAttribute.specified){
								this.selectorWidth = parseInt(xmlAttribute.value);
							};
							xmlAttribute =  xmlAttributeMap.getNamedItem("height");
							if (xmlAttribute != null && xmlAttribute.specified){
								this.selectorHeight  = parseInt(xmlAttribute.value);
							};
						};
					} else if(settingNode.nodeName == "target"){
						xmlAttributeMap = settingNode.attributes;
						if (xmlAttributeMap){
							xmlAttribute =  xmlAttributeMap.getNamedItem("x");
							if (xmlAttribute != null && xmlAttribute.specified){
								this.destinationX = parseInt(xmlAttribute.value);
							};
							xmlAttribute =  xmlAttributeMap.getNamedItem("y");
							if (xmlAttribute != null && xmlAttribute.specified){
								this.destinationY = parseInt(xmlAttribute.value);
							};
							xmlAttribute =  xmlAttributeMap.getNamedItem("width");
							if (xmlAttribute != null && xmlAttribute.specified){
								this.destinationWidth = parseInt(xmlAttribute.value);
							};
							xmlAttribute =  xmlAttributeMap.getNamedItem("height");
							if (xmlAttribute != null && xmlAttribute.specified){
								this.destinationHeight  = parseInt(xmlAttribute.value);
							};
							xmlAttribute =  xmlAttributeMap.getNamedItem("bordercolor");
							if (xmlAttribute != null && xmlAttribute.specified){
								this.borderColor = xmlAttribute.value;
							};
							xmlAttribute =  xmlAttributeMap.getNamedItem("bgcolor");
							if (xmlAttribute != null && xmlAttribute.specified){
								this.borderBgColor = xmlAttribute.value;
							};
						};
					};
					settingNode = settingNode.nextSibling;
				};
			} else if(xmlNode.nodeName == "puzzleimages"){
				var puzzleImgNode = xmlNode.firstChild;
				while(puzzleImgNode){
					if(puzzleImgNode.nodeName == "puzzleimage"){
						var newImg = new Array();
						xmlAttributeMap = puzzleImgNode.attributes;
						if (xmlAttributeMap){
							xmlAttribute =  xmlAttributeMap.getNamedItem("src");
							if (xmlAttribute != null && xmlAttribute.specified)
								newImg.src = xmlAttribute.value;
							xmlAttribute =  xmlAttributeMap.getNamedItem("title");
							if (xmlAttribute != null && xmlAttribute.specified)
								newImg.title = xmlAttribute.value;
							xmlAttribute =  xmlAttributeMap.getNamedItem("preview");
							if (xmlAttribute != null && xmlAttribute.specified)
								newImg.preview = xmlAttribute.value;
						}
						this.puzzleImages.push(newImg);
					};
					puzzleImgNode = puzzleImgNode.nextSibling;
				};
			}  else if(xmlNode.nodeName == "actions"){
				this.parseActionsNode(xmlNode);
				this.stepEnterActions = scene.Utils.parseActionsNode(xmlNode, "startStep");
				this.stepFinishActions = scene.Utils.parseActionsNode(xmlNode, "finishStep");
				
				this.onAllObjectsPlacedActions =
					scene.Utils.parseActionsNode(xmlNode, "onAllObjectsPlaced");
				this.onPlacedRightActions =
					scene.Utils.parseActionsNode(xmlNode, "onPlacedRight");
				this.onPlacedWrongActions =
					scene.Utils.parseActionsNode(xmlNode, "onPlacedWrong");				
			} else if(xmlNode.nodeName == "image"){
/*
				var newObject = new Composition();
				newObject._class = "rnmc_stdImage";
				newObject.parent = this;
				newObject.visible = false;
				if(newObject.initFromNode(xmlNode)){
					var objectEnabled = true;
					xmlAttributeMap = xmlNode.attributes;
					if (xmlAttributeMap){
						xmlAttribute = xmlAttributeMap.getNamedItem("enabled");
						if (xmlAttribute != null && xmlAttribute.specified
							&& xmlAttribute.value == "false")
							objectEnabled = false;
					}
					if(objectEnabled){
						this.childObjects.push(newObject);
					} else {
						this.nonActiveContent.push(newObject);
					}
				} else {
					newObject.parent = null;
					newObject = null;
				};
*/
			} else if(xmlNode.nodeName == "text"){
/*
				var newObject = new Composition();
				newObject._class = "rnmc_stdText";
				newObject.parent = this;
				newObject.visible = false;
				if(newObject.initFromNode(xmlNode)){
					this.childObjects.push(newObject);
				} else {
					newObject.parent = null;
					newObject = null;
				};
*/
			}
			xmlNode = xmlNode.nextSibling;
		};
//trace("succesfully init");
		return true;
	};

	function startStep(){
		rnmc_labwork_base_startStep();

		if (childObjects)
		{
			for(var i = 0; i < this.childObjects.length; i++){
				if(this.childObjects[i].visibility == OBJECT_VISIBILITY_TRUE)
					this.childObjects[i].showObject();
				else
					this.childObjects[i].hideObject();
			}
		}

		if(this.btn8pieces){
			this.btn8pieces.showObject();
		}
		if(this.btn16pieces){
			//btn16pieces.pressButton();
			this.btn16pieces.showObject();
		}
		if(this.btn24pieces){
			//btn24pieces.pressButton();
			this.btn24pieces.showObject();
		}
		if(this.btnOne)
			this.btnOne.showObject();
		if(this.btnAll){
			//btnAll.pressButton();
			this.btnAll.showObject();
		}
		if(this.btnSelectImage)
			this.btnSelectImage.showObject();
		if(this.btnPreviousImage)
			this.btnPreviousImage.showObject();
		if(this.btnNextImage)
			this.btnNextImage.showObject();
		if(this.btnNextTask)
			this.btnNextTask.showObject();
		if(this.btnBreakTask)
			this.btnBreakTask.showObject();
		if(this.btnZoomImage)
			this.btnZoomImage.showObject();
		if(this.sldrImageSelect){
			this.sldrImageSelect.setItems(this.puzzleImages.length);
			this.sldrImageSelect.showObject();
		}
		if(this.selectorCounter)
			this.selectorCounter.showObject();
		this.showSelectorImage();
		this.refreshPuzzle();
	};

	function exitStep(){
		this.cleanUp();
	};

	function cleanUp(){
		if(this.dragObjects){
			while(this.dragObjects.length > 0)
				this.dragObjects.pop().parent = null;
			this.dragObjects = null;
		}
	};

	function refreshPuzzle(){
		if(this.pieceSequence)
			this.pieceSequence = null;
		if(this.cropCoords)
			this.cropCoords = null;
		if(this.scaleCoords)
			this.scaleCoords = null;
		if(this.startCoords)
			this.startCoords = null;
		if(this.dragObjects)
			this.dragObjects = null;

		if(this.dragObjects){
			while(this.dragObjects.length > 0)
				this.dragObjects.pop().parent = null;
			this.dragObjects = null;
		}
		this.dragObjects = new Array();

		if(!this.imageBuffer){
			this.imageBuffer = new Image();
			this.imageBuffer.parent = this;
			this.imageBuffer.visible = false;
		}

		if(this.currentMode == PUZZLE_MODE_ONE){		// режим по одному
			if(btnPreviousPiece)
				btnPreviousPiece.showObject();
			if(btnNextPiece)
				btnNextPiece.showObject();
		} else {
			if(btnPreviousPiece)
				btnPreviousPiece.hideObject();
			if(btnNextPiece)
				btnNextPiece.hideObject();
		}

		var STARTPLACE_SPACE_H = 5;
		var STARTPLACE_SPACE_V = 5;

		if(this.imageTitle){
			this.imageTitle.showObject();
			this.imageTitle.setTextValue(this.puzzleImages[this.currentImage].title);
		}

		this.imageBuffer.src = null;
		this.imageBuffer.width = 0;
		this.imageBuffer.height = 0;
		this.imageBuffer.src = this.puzzleImages[this.currentImage].src;
		this.imageSrc = this.puzzleImages[this.currentImage].src;

		var imageProportion = this.imageBuffer.width/this.imageBuffer.height;
		if(imageProportion > 1)
			this.imageOrientation = PUZZLE_IMAGE_HORIZONTAL;
		else if(imageProportion < 1)
			this.imageOrientation = PUZZLE_IMAGE_VERTICAL;
		else
			this.imageOrientation = PUZZLE_IMAGE_SQUARE;

	// колво кусков по горизонтали и вертикали для разных режимов		
		if(this.imageOrientation == PUZZLE_IMAGE_HORIZONTAL){
			switch(this.pieceTotal){
			case 8:
				this.pieceHorizontal = 4;
				this.pieceVertical = 2;
				break;
			case 16:
				this.pieceHorizontal = 4;
				this.pieceVertical = 4;
				break;
			case 24:
				this.pieceHorizontal = 6;
				this.pieceVertical = 4;
				break;
			};
		} else if(this.imageOrientation == PUZZLE_IMAGE_VERTICAL){
			switch(this.pieceTotal){
			case 8:
				this.pieceHorizontal = 2;
				this.pieceVertical = 4;
				break;
			case 16:
				this.pieceHorizontal = 4;
				this.pieceVertical = 4;
				break;
			case 24:
				this.pieceHorizontal = 4;
				this.pieceVertical = 6;
				break;
			};
		} else {
			switch(this.pieceTotal){
			case 8:
				this.pieceHorizontal = 4;
				this.pieceVertical = 2;
				break;
			case 16:
				this.pieceHorizontal = 4;
				this.pieceVertical = 4;
				break;
			case 24:
				this.pieceHorizontal = 6;
				this.pieceVertical = 4;
				break;
			};
		}

		var scaledDimensions = getDimensions(this.imageBuffer.width, this.imageBuffer.height,
			this.destinationWidth, this.destinationHeight);
	// округлим размерность до кратных значений			
		this.scaledWidth =
			Math.round(scaledDimensions[0]/this.pieceHorizontal - 0.5)*this.pieceHorizontal;
		this.scaledHeight =
			Math.round(scaledDimensions[1]/this.pieceVertical - 0.5)*this.pieceVertical;

	// высчитываем destinationX & destinationY с учетом вписывания в рамку
		this.startOffsetX =
			Math.round(this.startAllX + (this.destinationWidth - this.scaledWidth)/2);
		this.startOffsetY =
			Math.round(this.startAllY + (this.destinationHeight - this.scaledHeight)/2);
		this.targetOffsetX =
			Math.round((this.destinationX + (this.destinationWidth - this.scaledWidth)/2));
		this.targetOffsetY =
			Math.round((this.destinationY + (this.destinationHeight - this.scaledHeight)/2));

		this.cropCoords =
			this.getCoords(this.imageBuffer.width, this.imageBuffer.height);
		this.scaleCoords =
			this.getCoords(this.scaledWidth, this.scaledHeight);
		this.startCoords =
			this.getCoords(this.scaledWidth + this.pieceHorizontal*STARTPLACE_SPACE_H,
				this.scaledHeight + this.pieceVertical*STARTPLACE_SPACE_V);

	// создаём порядок на старте
		this.pieceSequence = new Array();
		var tempSequence = new Array();

		for(var i = 0; i < this.pieceTotal; i++)
			tempSequence.push(i);
		this.pieceSequence = scene.Utils.permutateArray(tempSequence);
		tempSequence = null;
	//

		var borderCoords = this.getCoords(this.scaledWidth, this.scaledHeight);
		this.drawBorder(borderCoords);
		this.piecesRemained = this.pieceTotal;

		if(this.currentMode == PUZZLE_MODE_ONE){		// режим по одному
			for(var i = 0; i < this.pieceTotal; i++){
				var newPuzzlePiece = new Composition();
				newPuzzlePiece._class = "rnmc_puzzle_dragobject";
				newPuzzlePiece.parent = this;
				newPuzzlePiece.visible = true;
				newPuzzlePiece.initObject(i, this.imageSrc, this.cropCoords[i][0],
					this.cropCoords[i][1], this.cropCoords[i][2], this.cropCoords[i][3],
					this.scaleCoords[i][2], this.scaleCoords[i][3]);
				newPuzzlePiece.setSource(this.startOneX, this.startOneY);
				newPuzzlePiece.setTarget(targetOffsetX + this.scaleCoords[i][0],
					targetOffsetY + this.scaleCoords[i][1] - 1);
				this.dragObjects.push(newPuzzlePiece);
			};
			this.currentPiece = Math.round(Math.random()*(this.pieceTotal - 1));
			this.dragObjects[this.pieceSequence[this.currentPiece]].showObject();
		} else {										// режим все сразу
			for(var i = 0; i < this.pieceTotal; i++){
				var piecePosition = this.pieceSequence[i];
				var newPuzzlePiece = new Composition();
				newPuzzlePiece._class = "rnmc_puzzle_dragobject";
				newPuzzlePiece.parent = this;
				newPuzzlePiece.visible = true;
				newPuzzlePiece.initObject(i, this.imageSrc, this.cropCoords[i][0],
					this.cropCoords[i][1], this.cropCoords[i][2], this.cropCoords[i][3],
					this.scaleCoords[i][2], this.scaleCoords[i][3]);
				newPuzzlePiece.setSource(startOffsetX + this.startCoords[piecePosition][0],
					startOffsetY + this.startCoords[piecePosition][1]);
				newPuzzlePiece.setTarget(targetOffsetX + this.scaleCoords[i][0],
					targetOffsetY + this.scaleCoords[i][1] - 1);
				newPuzzlePiece.showObject();
				this.dragObjects.push(newPuzzlePiece);
			};
		};
	};

// функции высчитыавния координат кусков и вписывания в область
	function getCoords(imageWidth, imageHeight){
		var cropAreas = new Array();
		var cropX = 0;
		var cropY = 0;

		var cropWidth = Math.round(imageWidth/this.pieceHorizontal - 0.5);
		var cropHeight = Math.round(imageHeight / this.pieceVertical - 0.5);
		for(var i = 0; i < this.pieceVertical; i++){
			for(var j = 0; j < this.pieceHorizontal; j++){
				var cropArea = new Array();
				cropArea.push(cropX);
				cropArea.push(cropY);
				if(j != (this.pieceHorizontal - 1)){
					cropArea.push(cropWidth);
				} else {
					cropArea.push(cropWidth); //imageWidth - cropX);
				};
				if(i != (this.pieceVertical - 1)){
					cropArea.push(cropHeight);
				} else {
					cropArea.push(cropHeight); //imageHeight - cropY);
				};
				cropAreas.push(cropArea);
				cropArea = null;
				cropX += cropWidth;
			};
			cropX = 0;
			cropY += cropHeight;
		}
		return cropAreas;
	};

	function getDimensions(imageWidth, imageHeight, destinationRectWidth, destinationRectHeight){
		var newDimensions = new Array();
		var originalProp = imageWidth/imageHeight;
		var destProp = destinationRectWidth/destinationRectHeight;

		if(originalProp >= 1){	// horizontal or square
			if(originalProp < destProp){
				var newHeight = destinationRectHeight;
				var newWidth = (imageWidth/imageHeight) * destinationRectHeight;
				newDimensions.push(Math.round(newWidth));
				newDimensions.push(Math.round(newHeight));
			} else if(originalProp > destProp){
				var newWidth = destinationRectWidth;
				var newHeight = (imageHeight/imageWidth) * destinationRectWidth;
				newDimensions.push(Math.round(newWidth));
				newDimensions.push(Math.round(newHeight));
			} else{
				newDimensions.push(destinationRectWidth);
				newDimensions.push(destinationRectHeight);
			}
		} else { 				// vertical
			if(originalProp < destProp){
				var newHeight = destinationRectHeight;
				var newWidth = (imageWidth/imageHeight) * destinationRectHeight;
				newDimensions.push(Math.round(newWidth));
				newDimensions.push(Math.round(newHeight));
			} else if(originalProp > destProp){
				var newWidth = destinationRectWidth;
				var newHeight = (imageHeight/imageWidth) * destinationRectWidth;
				newDimensions.push(Math.round(newWidth));
				newDimensions.push(Math.round(newHeight));
			} else{
				newDimensions.push(destinationRectWidth);
				newDimensions.push(destinationRectHeight);
			}
		}

		return newDimensions;
	};

	function drawBorder(BorderCoords){
		if(this.puzzleBorder){
			while(this.puzzleBorder.length > 0){
				this.puzzleBorder.pop().parent = null;
			};
			this.puzzleBorder = null;
		};
		this.puzzleBorder = new Array();

		var newBorder = newBorder = new Text();
		newBorder.parent = this;
		newBorder.bkgColor = this.borderColor;
		newBorder.adjustHeight = false;
		if (Player.version)
			newBorder.x = this.targetOffsetX - 1;
		else
			newBorder.x = this.targetOffsetX;
		if (Player.version)
			newBorder.y = this.targetOffsetY - 1;
		else
			newBorder.y = this.targetOffsetY;
		newBorder.width = this.scaledWidth;
		newBorder.width = this.scaledWidth + 1;
		newBorder.height = this.scaledHeight + 1;
		newBorder.depth = 200;
		newBorder.visible = true;
		this.puzzleBorder.push(newBorder);

		for(var i = 0; i < BorderCoords.length; i++){
			newBorder = new Text();
			newBorder.parent = this;
			newBorder.visible = false;
			newBorder.bkgColor = this.borderBgColor;
			newBorder.adjustHeight = false;
			if (Player.version)
				newBorder.x = this.targetOffsetX + BorderCoords[i][0];
			else
				newBorder.x = this.targetOffsetX + BorderCoords[i][0] + 1;
			if (Player.version)
				newBorder.y = this.targetOffsetY + BorderCoords[i][1];
			else
				newBorder.y = this.targetOffsetY + BorderCoords[i][1] + 1;
			newBorder.width = BorderCoords[i][2] - 1;
			newBorder.height = BorderCoords[i][3] - 1;
			newBorder.depth = 198;
			newBorder.visible = true;
			this.puzzleBorder.push(newBorder);
		};
	}

	function notyfyPlaced(placedObject){
		this.piecesRemained--;
		if(this.currentMode == PUZZLE_MODE_ONE){
			if(this.piecesRemained > 0){
				if(this.onPlacedRightActions){
trace("this.onPlacedRightActions this.onPlacedRightActions this.onPlacedRightActions");
					for(var i = 0; i < this.onPlacedRightActions.length; i++)
						this.dispatchAction(this.onPlacedRightActions[i]);
				}
//				sndPuzzlePlaced.rewind();
//				sndPuzzlePlaced.playing = true;
				if(this.piecesRemained == 1){
					btnPreviousPiece.visible = false;
					btnNextPiece.visible = false;
				};
				showNextDragObject();
			} else {
				if(this.onAllObjectsPlacedActions){
					for(var i = 0; i < this.onAllObjectsPlacedActions.length; i++){
						this.dispatchAction(this.onAllObjectsPlacedActions[i]);
					}
				}			
//				sndPuzzleFinished.rewind();
//				sndPuzzleFinished.playing = true;
			}
		} else if(this.currentMode == PUZZLE_MODE_ALL){
			if(this.piecesRemained > 0){
				if(this.onPlacedRightActions){
trace("2. this.onPlacedRightActions this.onPlacedRightActions");				
					for(var i = 0; i < this.onPlacedRightActions.length; i++)
						this.dispatchAction(this.onPlacedRightActions[i]);
				}			
//				sndPuzzlePlaced.rewind();
//				sndPuzzlePlaced.playing = true;
				//this.piecesRemained--;
			} else {
				if(this.onAllObjectsPlacedActions){
					for(var i = 0; i < this.onAllObjectsPlacedActions.length; i++){
						this.dispatchAction(this.onAllObjectsPlacedActions[i]);
					}
				}			
//				sndPuzzleFinished.rewind();
//				sndPuzzleFinished.playing = true;
			}
		};
	};

	function showPreviousDragObject(){
		var bFound = false;
		var i = this.currentPiece;

		while(!bFound){
			if(i == 0){
				i = this.dragObjects.length - 1;
			} else {
				i--;
			};
			if(this.dragObjects[this.pieceSequence[i]].placed == false){
				bFound = true;
				if(!this.dragObjects[this.pieceSequence[this.currentPiece]].placed)
					this.dragObjects[this.pieceSequence[this.currentPiece]].hideObject();
				this.currentPiece = i;
				this.dragObjects[this.pieceSequence[this.currentPiece]].showObject();
				break;
			};
			if(i == this.currentPiece)
				break;
		};
	};

	function showNextDragObject(){
		var bFound = false;
		var i = this.currentPiece;
		while(!bFound){
			if(i >= (this.dragObjects.length - 1)){
				i = 0;
			} else {
				i++;
			};
			if(this.dragObjects[this.pieceSequence[i]].placed == false){
				bFound = true;
				if(!this.dragObjects[this.pieceSequence[this.currentPiece]].placed)
					this.dragObjects[this.pieceSequence[this.currentPiece]].hideObject();
				this.currentPiece = i;
				this.dragObjects[this.pieceSequence[this.currentPiece]].showObject();
				break;
			};
			if(i == this.currentPiece){
				break;
			}
		};
	};

	function showPreviousImage(){
		if(this.selectorImage == 0){
				this.selectorImage = this.puzzleImages.length - 1;
		} else {
			this.selectorImage--;
		};
		if(this.sldrImageSelect)
			this.sldrImageSelect.setSliderItem(this.selectorImage);
		showSelectorImage();
	};

	function showNextImage(){
		if(this.selectorImage == (this.puzzleImages.length - 1)){
			this.selectorImage = 0;
		} else {
			this.selectorImage++;
		};
		if(this.sldrImageSelect)
			this.sldrImageSelect.setSliderItem(this.selectorImage);
		showSelectorImage();
	};

	function showSelectorImage(newSelectorImg){
		if(newSelectorImg != undefined)
			this.selectorImage = newSelectorImg;

		if (this.selectorImage >= this.puzzleImages.length)
			this.selectorImage = this.puzzleImages.length - 1;

		if(this.selectorCounter)
			this.selectorCounter.setTextValue(
				(this.selectorImage + 1) + " из " + this.puzzleImages.length);

		imageSelector.visible = false;
		imageSelector.src = null;
		imageSelector.height = 0;
		imageSelector.width = 0;
		imageSelector.src = this.puzzleImages[this.selectorImage].preview;
		var selectorDimensions = getDimensions(imageSelector.width, imageSelector.height,
			this.selectorWidth, this.selectorHeight);
		imageSelector.width = selectorDimensions[0];
		imageSelector.height = selectorDimensions[1];
		imageSelector.x = this.selectorX + (this.selectorWidth - imageSelector.width)/2;
		imageSelector.y = this.selectorY + (this.selectorHeight - imageSelector.height)/2;

		imageSelector.visible = true;
	};

	function stepFinished(){
		if(SHOW_BTNNEXT == "COMPLETE"){
			btnBreakTask.visible = false;
			btnNextTask.visible = true;
		};

		if(AUTO_CONTINUE)
			waitAndCall(AUTO_CONTINUE_TIMEOUT, nextTask);
	};

	function waitAndCall(msWait, fnctCallBack){
		this.waitTimeout = msWait;
		this.waitCallback = fnctCallBack;
		this.waitStart = Player._time;
		wait_timer.playing = true;
	};

	function wait_loop(){
		if(Player._time - this.waitStart >= this.waitTimeout){
			wait_timer.playing = false;
			this.waitCallback();
		};
	};

	function zoomInCurrentImage(){
	// Максимальные размеры увеличенной картинки
		var maxWidth  = 800;
		var maxHeight = 600;
		if(this.zoomImage){
			this.zoomImage.parent = null;
			this.zoomImage = null;
		}		
		var zoomDimensions = getDimensions(this.imageBuffer.width, this.imageBuffer.height,
			maxWidth, maxHeight);
		var zoomX = 0 + (maxWidth - zoomDimensions[0])/2;
		var zoomY = 0 + (maxHeight - zoomDimensions[1])/2;
		this.zoomImage = new Image();
		this.zoomImage.parent = this;
		this.zoomImage.src = this.puzzleImages[this.currentImage].src;
		this.zoomImage.x = zoomX;
		this.zoomImage.y = zoomY;
		this.zoomImage.width = zoomDimensions[0];
		this.zoomImage.height = zoomDimensions[1];
		this.zoomImage.depth = -1000;
		this.zoomImage.visible = true;
	}

	function zoomOutCurrentImage(){
		if(this.zoomImage){
			this.zoomImage.parent = null;
			this.zoomImage = null;
		}
	};

	function buttonDispatcher(btnId){
		switch(btnId){
		case "8pieces":
			if(this.pieceTotal != 8){
				//this.btn8pieces.pressButton();
				//this.btn16pieces.releaseButton();
				//this.btn24pieces.releaseButton();
				this.cleanUp();
				this.pieceTotal = 8;
				this.refreshPuzzle();
			};
			break;
		case "16pieces":
			if(this.pieceTotal != 16){
				//this.btn8pieces.releaseButton();
				//this.btn16pieces.pressButton();
				//this.btn24pieces.releaseButton();
				this.cleanUp();
				this.pieceTotal = 16;
				this.refreshPuzzle();
			};
			break;
		case "24pieces":
			if(this.pieceTotal != 24){
				//this.btn8pieces.releaseButton();
				//this.btn16pieces.releaseButton();
				//this.btn24pieces.pressButton();
				this.cleanUp();
				this.pieceTotal = 24;
				this.refreshPuzzle();
			};
			break;
		case "one":
			if(this.currentMode != PUZZLE_MODE_ONE){
				//this.btnOne.pressButton();
				//this.btnAll.releaseButton();
				this.cleanUp();
				this.currentMode = PUZZLE_MODE_ONE;
				this.refreshPuzzle();
			}
			break;
		case "all":
			if(this.currentMode != PUZZLE_MODE_ALL){
				//this.btnOne.releaseButton();
				//this.btnAll.pressButton();
				this.cleanUp();
				this.currentMode = PUZZLE_MODE_ALL;
				this.refreshPuzzle();
			}
			break;
		case "previouspiece":
			showPreviousDragObject();
			break;
		case "nextpiece":
			showNextDragObject();
			break;
		case "selectimage":
			this.cleanUp();
			this.currentImage = this.selectorImage;
			this.refreshPuzzle();
			break;
		case "previousimage":
			showPreviousImage();
			break;
		case "nextimage":
			showNextImage();
			break;
		default:
			trace("rnmc_labwork_puzzle: UNKNOWN BUTTON ID: " + btnId);
			break;
		};
	};
]]></script>

<sequencer name="wait_timer" loop="true" playing="false">
<script><![CDATA[
	wait_loop();
]]></script>
</sequencer>
<image name="imageSelector" visible="false"/>
<!--text name="imageCounter" visible="false" width="80"/-->
<!--text name="puzzleTitle" visible="false" width="80" adjustHeight="true"/-->
<!--audio name="sndPuzzlePlaced" src="\Interface\Labwork_Puzzle\puzzle_placed.mp3" playing="false" loop="false"/-->
<!--audio name="sndPuzzleFinished" src="\Interface\Labwork_Puzzle\puzzle_finished.mp3" playing="false" loop="false"/-->
</composition>
</rmml>