<?xml version="1.0" encoding="UTF-8"?>
<rmml>
<!--
$RCSfile: rnmc_stdPlayControl.xml $
$Revision: 957 $

last change: $Date: 2009-02-17 14:00:06 +0300 (Вт, 17 фев 2009) $
             $Author: toha $

	Lacerta Project
	version 1.0.0.1
	Copyright (C) 2007-2009 RNMC
-->
<composition name="rnmc_stdPlayControl" class="rnmc_stdObject">
<script><![CDATA[
	this.id = "playcontrol";
	this.type = "rnmc_stdPlayControl";

	this.sliderObject = null;
	this.timerObject = null;
	this.btnPlay = null;
	this.btnPause = null;
	this.btnStop = null;

	this.currentPercent = 0;
	this.currentPosition = 0;
	this.objectDuration = 0;

	function initFromNode(srcNode){
		var xmlNode = null;
		var xmlAttributeMap = null;
		var xmlAttribute = null;

		xmlNode = srcNode.firstChild;
		while(xmlNode){
			if(xmlNode.nodeName == "param"){
				xmlAttributeMap = xmlNode.attributes;
				if (xmlAttributeMap){
					xmlAttribute =  xmlAttributeMap.getNamedItem("x");
					if (xmlAttribute != null && xmlAttribute.specified)
						this.x = parseInt(xmlAttribute.value);
					xmlAttribute =  xmlAttributeMap.getNamedItem("y");
					if (xmlAttribute != null && xmlAttribute.specified)
						this.y = parseInt(xmlAttribute.value);
				}
			} else if(xmlNode.nodeName == "button"){
				var newButton = new Composition();
				newButton._class = "rnmc_stdButton"
				newButton.parent = this;
				newButton.depth = -2;
				if(newButton.initFromNode(xmlNode)){
					switch(newButton.id){
					case "play":
						this.btnPlay = newButton;
						break;
					case "pause":
						this.btnPause = newButton;
						break;
					case "stop":
						this.btnStop = newButton;
						break;
					default:
						newButton.parent = null;
						newButton = null;
					};
				} else {
					newButton.parent = null;
					newButton = null;
				}
			} else if(xmlNode.nodeName == "element"){
				var newElement = scene.Utils.initStdElementFromNode(xmlNode, this);
				if(newElement){
					switch(newElement.id){
						case "play":
							this.btnPlay = newElement;
							break;
						case "pause":
							this.btnPause = newElement;
							break;
						case "stop":
							this.btnStop = newElement;
							break;
						default:
							newElement.parent = null;
							newElement = null;
					}
				} else {
					newElement.parent = null;
					newElement = null;
				}				
			} else if(xmlNode.nodeName == "timer"){
				var newObject = scene.Utils.initStdTimerFromNode(xmlNode, this);
			// Для совместимости со старым PlayControl
				newObject.timerVisible = true;
				newObject.id = "timer";
				if(newObject)
					this.timerPanel = newObject;
			} else if(xmlNode.nodeName == "slider"){
				var newObject = scene.Utils.initStdSliderFromNode(xmlNode, this);
				if(newObject)
					this.sliderObject = newObject;
			} else if(xmlNode.nodeName == "effect"){
				//this.parseEffectNode(xmlNode);
			};
			xmlNode = xmlNode.nextSibling;
		};
		return true;
	}

	function showObject(){
		if(this.sliderObject){
			this.sliderObject.setCallback(this, this.setPosition);
			this.sliderObject.showObject();
		}
		if(this.timerPanel){
			this.timerPanel.showObject();
		}
		if(this.btnPause)
			this.btnPause.hideObject();
		if(this.btnPlay){
			this.btnPlay.showObject();
		}
		if(this.btnStop)
			this.btnStop.showObject();
		this.synchronizeControl();
		this.visible = true;
	};
	
	function hideObject(){
		if(this.sliderObject){
			this.sliderObject.clearCallback();
			this.sliderObject.hideObject();
		};
		if(this.timerPanel){
			this.timerPanel.hideObject();
		}
		this.visible = false;
	};

	
	function attachToObject(targetObject){
		if (targetObject){
			if("duration" in targetObject && "currentPos" in targetObject){
				this.attachedObject = targetObject;
				this.objectDuration = this.attachedObject.duration;
				this.depth = this.attachedObject.depth - 10;
			} else {
				trace("#FF0000ERROR: Attaching to wrong type of object");
			}
		} else {
			this.attachedObject = targetObject;
			this.objectDuration = this.attachedObject.duration;
			this.depth = this.attachedObject.depth - 10;		
		}
	}
	
	function setDispatcher(targetObject, targetFunction){
		if(targetObject && targetFunction){
			this.dispatchObject = targetObject;
			this.dispatchFunction = targetFunction;
			if(this.btnPlay)
				this.btnPlay.setReleaseCallBack(targetObject, targetFunction);
			if(this.btnPause)
				this.btnPause.setReleaseCallBack(targetObject, targetFunction);
			if(this.btnStop)
				this.btnStop.setReleaseCallBack(targetObject, targetFunction);
		} else {
			this.dispatchObject = null;
			this.dispatchFunction = null;
			if(this.btnPlay)
				this.btnPlay.setReleaseCallBack(targetObject, targetFunction);
			if(this.btnPause)
				this.btnPause.setReleaseCallBack(targetObject, targetFunction);
			if(this.btnStop)
				this.btnStop.setReleaseCallBack(targetObject, targetFunction);		
		}
	}

//	функция необходима для синхронизации контрола извне (из stdVideo например)
	function synchronizeControl(){
		if(this.attachedObject){
			this.updateTimerPanel();
			this.updateSlider();
			if(this.timerPanel){
				this.timerPanel.setCurrentTime(this.attachedObject.currentPos/1000, this.attachedObject.duration/1000);
			}
			if(this.attachedObject.playing == true){
				if(this.btnPlay)
					this.btnPlay.hideObject();
				if(this.btnPause)
					this.btnPause.showObject();
				sliderSequencer.playing = true;
			} else {
				sliderSequencer.playing = false;
				if(this.btnPlay)
					this.btnPlay.showObject();
				if(this.btnPause)
					this.btnPause.hideObject();
			}
		} else {
			trace("ERROR: rnmc_stdPlayControl: no attached object.");
		}
	};
	
//	обновление значения таймера
	function updateTimerPanel(){
		if(this.attachedObject){
			if(Math.abs(this.attachedObject.currentPos - this.currentPosition) >= 1000){
				if(this.timerPanel){
					this.timerPanel.setCurrentTime(this.attachedObject.currentPos/1000, this.attachedObject.duration/1000);
					this.currentPosition = this.attachedObject.currentPos;
				}			
			}
		}
	}
	
//	обновление положения ползунка
	function updateSlider(){
		if(this.sliderObject){
			var newSliderPos = parseFloat(this.attachedObject.currentPos/this.objectDuration);
			this.sliderObject.synchronizeSliderPosition(newSliderPos);
		};
	}

//	 callback от slider'a	
	function setPosition(newPos){
		if(this.attachedObject){
			var objectPlaying = false;
			if(this.attachedObject.playing){
				this.attachedObject.playing = false;
				objectPlaying = true;
			}
			this.attachedObject.currentPos = Math.round(newPos*this.objectDuration);
			if(objectPlaying)
				this.attachedObject.playing = true;
			//	поскольку вызвали функцию из ползунка, его позицию обновлять не надо
			this.updateTimerPanel();
		} else {
trace("ERROR: rnmc_stdPlayControl: no attached object.");
		}
	}


// =============================================
// функции работы с дочерними контролами группы

// получение контрола с заданным именем
	function getChildControl(objectId){
		switch(objectId){
			case "play":
				if(this.btnPlay){
					return this.btnPlay;
				}
				break;
			case "pause":
				if(this.btnPause){
					return this.btnPause;
				}
				break;
			case "stop":
				if(this.btnStop){
					return this.btnStop;
				}
				break;
			case "timer":
				if(this.timerPanel){
					return this.timerPanel;
				}
				break;
			case "slider":
				if(this.sliderObject){
					return this.sliderObject;
				}
				break;
		}
		return null;
	}

	//	показывает дочерний элемент с некоторым id
	function showChildControl(objectId){
		switch(objectId){
		case "play":
			if(this.btnPlay){
				this.btnPlay.showObject();
			}
		case "pause":
			if(this.btnPause){
				this.btnPause.showObject();
			}
		case "stop":
			if(this.btnStop){
				this.btnStop.showObject();
			}
		case "timer":
			if(this.timerPanel){
				this.timerPanel.showObject();
			}
		case "slider":
			if(this.sliderObject){
				this.sliderObject.showObject();
			}
			break;
		}
	}
	
	function hideChildControl(objectId){
		switch(objectId){
		case "play":
			if(this.btnPlay){
				this.btnPlay.hideObject();
			}
		case "pause":
			if(this.btnPause){
				this.btnPause.hideObject();
			}
		case "stop":
			if(this.btnStop){
				this.btnStop.hideObject();
			}
		case "timer":
			if(this.timerPanel){
				this.timerPanel.hideObject();
			}
		case "slider":
			if(this.sliderObject){
				this.sliderObject.hideObject();
			}
			break;
		}
	}
]]></script>
<sequencer name="sliderSequencer" playing="false" loop="true">
	<script>
		updateTimerPanel();
		updateSlider();
	</script>
</sequencer>
</composition>
</rmml>