<?xml version="1.0" encoding="UTF-8"?>
<rmml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="L:\collaborative-nir\program\Cscl3DWS\rmml\RMML.xsd">
	<include src="buttons.xml"/>

	<composition name="CScrollerBase">
		<script>
			this.client = null;
			this.scrollSize = 100;
			this.scrollMax = 300;
			this.scrollStep = 5;
			this.currentScrollPos = 0;
			this.currentSliderPosPy = 0;		// 0..1
			this.sliderHeight = 10;
			this.scrollerSize = 100;
			this.mouseWheelScrollStep = -1;

			this.scrollUp = false;
			this.scrollDown = false;
			this.scrollBack = false;

			this.sliderDarg = false;
			this.sliderDragPointY = 0;

			this.sliderFDrag = null;			// function for slider drag
			this.pyToYCoords = null;
			this.yToPyCoords = null;
			this.horizontal = false;
			this.bHideOnDisabled = false;
		</script>
		<script>
			function setClient(client)
			{
				this.client = client;
			}

			function setScrollSize(size)
			{
				this.scrollSize = size;
			}

			function setScrollMax(max)
			{<![CDATA[
				this.scrollMax = max - this.scrollSize;
				if (this.scrollMax < 0) this.scrollMax = 0;
			}]]>

			function setScrollHeight(h)
			{
				this.setScrollMax(h);
				if (this.reconstructScroller != null)
				this.reconstructScroller();
			}

			function setScrollStep(step)
			{
				this.scrollStep = step;
			}

			function hideOnDisabled(bFlag)
			{
				this.bHideOnDisabled = bFlag;
			}

			function initialize(client,size,max,step)
			{<![CDATA[
				if (client != undefined && client != null)
				{
					this.setClient(client);
				}
				if (size != undefined && size != null)
				{
					this.setScrollSize(size);
				}
				if (max != undefined && max != null)
				{
					this.setScrollMax(max);
				}
				if (step != undefined && step != null)
				{
					this.setScrollStep(step);
				}
				//this.setClientPos(0);
				//this.setSliderPos(0);
				//this.updateSliderPos();
			}]]>

			function setClientPos(pos)
			{<![CDATA[
				if (pos < 0)
				{
					pos = 0;
				}
				else
				{
					if (pos > this.scrollMax)
					{
						pos = this.scrollMax;
					}
				}
				this.currentScrollPos = pos;
				if (this.client.scrollTo != undefined && this.client.scrollTo != null)
				{
					this.client.scrollTo(this.currentScrollPos, this.horizontal);
				}
			}]]>

			function setSliderPos(py)
			{<![CDATA[
				if (py < 0)
				{
					py = 0;
				}
				else
				{
					if (py > 1)
					{
						py = 1;
					}
				}
				this.currentSliderPosPy = py;
//				trace("SliderPos = " +  this.currentSliderPosPy + "   " + this.yToPyCoords(this.b_slider.y));
				if (this.setSlider != null)
					this.setSlider(this.currentSliderPosPy);
			}]]>

			function moveScroller(x,y)
			{<![CDATA[
				if (x != null && x != undefined)
					this.x = x;
				if (y != null && y != undefined)
					this.y = y;
			}]]>

			function onUpButtonPress()
			{
				this.scrollUp = true;
				this.setClientPos(this.currentScrollPos - this.scrollStep);
				this.setSliderPos(this.currentScrollPos/this.scrollMax)
				this.scrollup_seq.rewindAndPlay();
			}

			function onUpButtonOver()
			{
				if (this.scrollUp)
				{
					this.scrollup_seq.play();
				}
			}

			function onUpButtonOut()
			{
				if (this.scrollUp)
				{
					this.scrollup_seq.stop();
				}
			}

			function onUpButtonRelease()
			{
				this.scrollUp = false;
				this.scrollup_seq.stop();
			}

			function onUpButtonReleaseOutside()
			{
				this.scrollUp = false;
				this.scrollup_seq.stop();
			}

			function onDownButtonPress()
			{
				this.scrollDown = true;
				this.setClientPos(this.currentScrollPos + this.scrollStep);
				this.setSliderPos(this.currentScrollPos/this.scrollMax);
				this.scrolldown_seq.rewindAndPlay();
			}

			function onDownButtonOver()
			{
				if (this.scrollDown)
				{
					this.scrolldown_seq.play();
				}
			}

			function onDownButtonOut()
			{
				if (this.scrollDown)
				{
					this.scrolldown_seq.stop();
				}
			}

			function onDownButtonRelease()
			{
				this.scrollDown = false;
				this.scrolldown_seq.stop();
			}

			function onDownButtonReleaseOutside()
			{
				this.scrollDown = false;
				this.scrolldown_seq.stop();
			}

			function onScrollBackPress()
			{<![CDATA[
				this.scrollBack = true;
				if (this.pyToYCoords != null && this.pyToYCoords != undefined)
				{
					var pos = 0;
					if (this.horizontal == true)
					{
						pos = this.xMouse;
					}
					else
					{
						pos = this.yMouse;
					}
					if (pos < this.pyToYCoords(this.currentSliderPosPy))
					{
						this.setClientPos(this.currentScrollPos - this.scrollSize);
						this.setSliderPos(this.currentScrollPos/this.scrollMax);
						this.scrollbackup_seq.rewindAndPlay();
					}
					else
					{
						if (pos > this.pyToYCoords(this.currentSliderPosPy))
						{
							this.setClientPos(this.currentScrollPos + this.scrollSize);
							this.setSliderPos(this.currentScrollPos/this.scrollMax);
							this.scrollbackdown_seq.rewindAndPlay();
						}
					}
				}
			}]]>

			function onScrollBackOver()
			{<![CDATA[
				if (this.scrollBack)
				{
					if (this.pyToYCoords != null && this.pyToYCoords != undefined)
					{
						if (this.yMouse < this.pyToYCoords(this.currentSliderPosPy))
						{
							this.scrollbackup_seq.play();
						}
						else
						{
							if (this.yMouse > this.pyToYCoords(this.currentSliderPosPy))
							{
								this.scrollbackdown_seq.play();
							}
						}
					}
				}
			}]]>

			function onScrollBackOut()
			{
				if (this.scrollBack)
				{
					this.scrollbackdown_seq.stop();
					this.scrollbackup_seq.stop();
				}
			}

			function onScrollBackRelease()
			{
				this.scrollBack = false;
				this.scrollbackdown_seq.stop();
				this.scrollbackup_seq.stop();
			}

			function onScrollBackReleaseOutside()
			{
				this.scrollBack = false;
				this.scrollbackdown_seq.stop();
				this.scrollbackup_seq.stop();
			}

			function onSliderPress()
			{
				this.startDrag();
			}

			function onSliderMouseMove()
			{<![CDATA[
				if(this.sliderDrag)
				{
					if (this.horizontal != undefined && this.horizontal == true)
						this.sliderFDrag(this.xMouse);
					else
						this.sliderFDrag(this.yMouse);
				}
			}]]>

			function onSliderRelease()
			{
				this.endDrag();
			}

			function onSliderReleaseOutside()
			{
				this.endDrag();
			}

			function startDrag()
			{<![CDATA[
				if (this.horizontal != undefined && this.horizontal == true)
					this.sliderDragPointY = this.xMouse;
				else
					this.sliderDragPointY = this.yMouse;
				this.sliderDrag = true;
			}]]>

			function endDrag()
			{
				this.sliderDrag = false;
			}

			function sliderFDrag(ny)
			{<![CDATA[
				shift = ny - this.sliderDragPointY;
				if (this.pyToYCoords != null && this.pyToYCoords != undefined && this.yToPyCoords != null && this.yToPyCoords != undefined)
				{
					this.setSliderPos(this.yToPyCoords(this.pyToYCoords(this.currentSliderPosPy) + shift));
					this.setClientPos(this.currentSliderPosPy*this.scrollMax);
				}
				this.sliderDragPointY = ny;
			}]]>
			function setMouseWheelScrollStep(step)
			{
				this.mouseWheelScrollStep = step;
			}
		</script>

		<sequencer name="scrollup_seq">
			<script>
				500;
			</script>
			<script>
				this.setClientPos(this.currentScrollPos - this.scrollStep);
				this.setSliderPos(this.currentScrollPos/this.scrollMax);
			</script>
			<script>
				this_seq.currentFrame = 1;
			</script>
		</sequencer>

		<sequencer name="scrolldown_seq">
			<script>
				500;
			</script>
			<script>
				this.setClientPos(this.currentScrollPos + this.scrollStep);
				this.setSliderPos(this.currentScrollPos/this.scrollMax);
			</script>
			<script>
				this_seq.currentFrame = 1;
			</script>
		</sequencer>

		<sequencer _name="scrollbackup_seq">
			<script>
				500;
			</script>
			<script>
				this.setClientPos(this.currentScrollPos -  this.scrollSize);
				this.setSliderPos(this.currentScrollPos/this.scrollMax);
				50;
			</script>
			<script>
				this_seq.currentFrame = 1;
			</script>
		</sequencer>

		<sequencer _name="scrollbackdown_seq">
			<script>
				500;
			</script>
			<script>
				this.setClientPos(this.currentScrollPos + this.scrollSize);
				this.setSliderPos(this.currentScrollPos/this.scrollMax);
				50;
			</script>
			<script>
				this_seq.currentFrame = 1;
			</script>
		</sequencer>
		<script event="onMouseWheel"><![CDATA[
			if(horizontal) exit();
			this.bNeedToScrollOnMW = false;
			// если на самом скроллере, то надо скроллировать
			if(Mouse.target != undefined){
				this.scrollOnMWParent = Mouse.target;
				while(this.scrollOnMWParent != null){
					if(this.scrollOnMWParent == this){
						this.bNeedToScrollOnMW = true;
						break;
					}
					this.scrollOnMWParent = this.scrollOnMWParent.parent;
				}
			}
			// если на клиенте скроллера, то тоже надо скроллировать
			if(!this.bNeedToScrollOnMW && client != null){
				if(typeof(client.isMouseOn) == 'function'){
					if(client.isMouseOn())
						this.bNeedToScrollOnMW = true;
				}
			}
			if(this.bNeedToScrollOnMW){
				if(this.mouseWheelScrollStep > 0){
					setClientPos(currentScrollPos - (Mouse.delta/150)*this.mouseWheelScrollStep);
				}else
					setClientPos(currentScrollPos - Mouse.delta/15);
				setSliderPos(currentScrollPos/scrollMax);
			}
		]]></script>
	</composition>

	<composition name="CResizableOffsetSkinedButton" class="CSkinedButtonBase">
		<composition name="compSkin">
			<image name="top"/>
			<image name="middle"/>
			<image name="bottom"/>
		</composition>
		<script><![CDATA[
			this.bHeight = 0;
			this.bWidth = 0;
			function update()
			{
				if (this.horizontal != undefined && this.horizontal == true)
				{
					var cropHeight = this.imSkin.getMediaSize().height / skinCount;
					var cropY = 0;
					for (var i = 0;  i < this.skinState.length;  i++)
						if (this.state == this.skinState[ i] && this.skinCount > i)
							cropY = cropHeight * i;
					compSkin.top.crop(aOffset,cropY, top, cropHeight);
					compSkin.middle.crop(aOffset + top, cropY, aOffsetSize - bottom - top, cropHeight);
					compSkin.middle.x = compSkin.top.width;
					compSkin.middle.width = this.bSize - top - bottom;
					compSkin.bottom.crop(aOffset + aOffsetSize - top, cropY, bottom, cropHeight);
					compSkin.bottom.x = this.bSize - bottom;
				}
				else
				{
					var cropWidth = this.imSkin.getMediaSize().width / skinCount;
					var cropX = 0;
					for (var i = 0;  i < this.skinState.length;  i++)
						if (this.state == this.skinState[ i] && this.skinCount > i)
							cropX = cropWidth * i;
					compSkin.top.crop(cropX, aOffset, cropWidth, top);
					compSkin.middle.crop(cropX, aOffset + top, cropWidth, aOffsetSize - bottom - top);
					compSkin.middle.y = compSkin.top.height;
					compSkin.middle.height = this.bSize - top - bottom;
					compSkin.bottom.crop(cropX, aOffset + aOffsetSize - top, cropWidth, bottom);
					compSkin.bottom.y = this.bSize - bottom;
				}
			}
			function init( aSkinCount, aOffset, aOffsetSize, top, bottom, horizontal)
			{
				if (horizontal != undefined && horizontal == true)
				{
					this.horizontal = true;
				}
				else
				{
					this.horizontal = false;
				}
				if (top != undefined)
				{
					this.top = top;
				}
				if (bottom != undefined)
				{
					this.bottom = bottom;
				}
				this.compSkin.top.src = imSkin.src;
				this.compSkin.middle.src = imSkin.src;
				this.compSkin.bottom.src = imSkin.src;
				if (aOffset != undefined)
				{
					this.aOffset = aOffset;
				}
				if (aOffsetSize != undefined)
				{
					this.aOffsetSize = aOffsetSize;
					this.bSize = this.aOffsetSize;
				}
				this.skinCount = aSkinCount;
				this.update();
			}
			function resize(size)
			{
				this.bSize = size;
				this.update();
			}
			function getSize()
			{
				if (this.horizontal != undefined && this.horizontal == true)
					return (compSkin.top.width + compSkin.middle.width + compSkin.bottom.width);
				else
					return (compSkin.top.height + compSkin.middle.height + compSkin.bottom.height);
			}

		]]></script>
	</composition>

	<composition name="CSkinedScroller" class="CScrollerBase">
		<script>
			this.topLevel = 0;
			this.bottomLevel = 100;
			this.scrollerSize = 100;
		</script>

		<composition _name="b_scrollup" enabled="true" depth="-100" class="COffsetSkinedButton">
			<script event="onPress">
				parent.onUpButtonPress();
			</script>
			<script event="onRollOut">
				parent.onUpButtonOut();
			</script>
			<script event="onRollOver">
				parent.onUpButtonOver();
			</script>
			<script event="onRelease">
				parent.onUpButtonRelease();
			</script>
			<script event="onReleaseOutside">
				parent.onUpButtonReleaseOutside();
			</script>
		</composition>

		<composition name="b_scrolldown" enabled="true" depth="-100" class="COffsetSkinedButton">
			<script event="onPress">
				parent.onDownButtonPress();
			</script>
			<script event="onRollOut">
				parent.onDownButtonOut();
			</script>
			<script event="onRollOver">
				parent.onDownButtonOver();
			</script>
			<script event="onRelease">
				parent.onDownButtonRelease();
			</script>
			<script event="onReleaseOutside">
				parent.onDownButtonReleaseOutside();
			</script>
		</composition>

		<composition name="b_scrollback" _depth="-90" enabled="true" _class="CResizableOffsetSkinedButton">
			<script event="onPress">
				parent.onScrollBackPress();
			</script>
			<script event="onRollOut">
				parent.onScrollBackOut();
			</script>
			<script event="onRollOver">
				parent.onScrollBackOver();
			</script>
			<script event="onRelease">
				parent.onScrollBackRelease();
			</script>
			<script event="onReleaseOutside">
				parent.onScrollBackReleaseOutside();
			</script>
			<script>
				this.showButton = function()
				{<![CDATA[
					if (parent.slider == undefined || parent.slider == 0)
						this.enabled = false;
					else
						this.enabled = true;
				}]]>
			</script>
		</composition>

		<composition _name="b_slider" _depth="-100" enabled="true" _class="CResizableOffsetSkinedButton">
			<script event="onPress">
				parent.onSliderPress();
			</script>
			<script event="onMouseMove">
				parent.onSliderMouseMove()
			</script>
			<script event="onRelease">
				parent.onSliderRelease();
			</script>
			<script event="onReleaseOutside">
				parent.onSliderReleaseOutside();
			</script>
			<script>
				this.showButton = function()
				{<![CDATA[
					if (parent.slider == 0 || parent.slider == undefined)
					{
//						trace("no slider");
						this.hide();
					}
					else
					{
						if (parent.slider == 1)
						{
//							trace("fixed slider");
							if (!this.enabled)
								this.enabled = true;
							if (parent.sheight != undefined)
							{
								this.resize(parent.sheight);
							}
							else
							{
								this.resize(20);
							}
							if (parent.scrollMax > 0)
								parent.setSliderPos(parent.currentScrollPos/parent.scrollMax);
							else
								parent.setSliderPos(0);
						}
						else
						{
							if (parent.slider == 2)
							{
//								trace("auto slider");
								if (!this.enabled)
									this.enabled = true;
								this.resize(parent.scrollSize*(parent.bottomLevel -parent.topLevel)/(parent.scrollSize + parent.scrollMax));
								parent.setSliderPos(parent.currentScrollPos/parent.scrollMax);
							}
						}
						this.show();
					}
				}]]>
			</script>
		</composition>

		<script>
			function reconstructScroller()
			{<![CDATA[
				if (this.scrollMax > 0)
				{
					if (!this.scrollUp && !this.scrollDown && !this.scrollBack)
					{
						this.b_scrolldown.enabled = true;
						this.b_scrollup.enabled = true;
						this.b_scrollback.showButton();
					}
					this.b_slider.showButton();
					if (this.currentScrollPos > this.scrollMax)
					{
						this.setClientPos(this.scrollMax);
						this.setSliderPos(1);
					}
					this.show();
				}
				else
				{
					if (this.bHideOnDisabled)
					{
						this.hide();
					}
					else
					{
						this.show();
					}

					this.b_scrolldown.enabled = false;
					this.b_scrollup.enabled = false;
					this.b_scrollback.enabled = false;

					this.b_slider.hide();
					this.setClientPos(0);
				}

				//this.show();
			}]]>

			function pyToYCoords(py)
			{
				return (py*(this.bottomLevel - this.topLevel - this.b_slider.getSize()) + this.topLevel);
			}

			function yToPyCoords(y)
			{
				return (y - this.topLevel)/(this.bottomLevel - this.topLevel - this.b_slider.getSize());
			}

			function setSlider(spos)
			{<![CDATA[
				if (this.horizontal != undefined && this.horizontal == true)
					this.b_slider.x = this.pyToYCoords(spos);
				else
					this.b_slider.y = this.pyToYCoords(spos);
			}]]>

			function setTopLevel(lvl)
			{
				this.topLevel = lvl;
			}

			function setBottomLevel(lvl)
			{
				this.bottomLevel = lvl;
			}

			function resizeScroller(size)
			{<![CDATA[
				if (this.horizontal != undefined && this.horizontal == true)
				{
					this.scrollerSize = size;
					this.b_scrolldown.x = this.scrollerSize - this.b_scrolldown.imSkin.width;

					this.setTopLevel(this.b_scrollup.x + this.b_scrollup.imSkin.width);		// top level for scrollback and slider
					this.setBottomLevel(this.b_scrolldown.x);													// bottom level for scrollback and slider

					this.b_scrollback.x = this.topLevel;
					this.b_scrollback.resize(this.bottomLevel - this.topLevel);

					this.b_slider.x = this.topLevel;
				}
				else
				{
					this.scrollerSize = size;
					this.b_scrolldown.y = this.scrollerSize - this.b_scrolldown.imSkin.height;

					this.setTopLevel(this.b_scrollup.y + this.b_scrollup.imSkin.height);		// top level for scrollback and slider
					this.setBottomLevel(this.b_scrolldown.y);													// bottom level for scrollback and slider

					this.b_scrollback.y = this.topLevel;
					this.b_scrollback.resize(this.bottomLevel - this.topLevel);

					this.b_slider.y = this.topLevel;
				}
			}]]>

			function attachToObject()
			{<![CDATA[
				this.resizeScroller(this.scrollSize);
				if (this.horizontal != undefined && this.horizontal == true)
					this.moveScroller(this.client.x, this.client.y + this.client.height);
				else
					this.moveScroller(this.client.x + this.client.width, this.client.y);
			}]]>

			function initUpButton( aSkinCount, aOffsetX, aOffsetY, aOffsetWidth, aOffsetHeight, horizontal)
			{
				var im = new Image();
				im.parent = this.b_scrollup;
				im.name = "imSkin";
				im.src = this.imSkin.src;
				b_scrollup.init( aSkinCount, aOffsetX, aOffsetY, aOffsetWidth, aOffsetHeight, horizontal);
			}

			function initDownButton( aSkinCount, aOffsetX, aOffsetY, aOffsetWidth, aOffsetHeight, horizontal)
			{
				var im = new Image();
				im.parent = this.b_scrolldown;
				im.name = "imSkin";
				im.src = this.imSkin.src;
				b_scrolldown.init( aSkinCount, aOffsetX, aOffsetY, aOffsetWidth, aOffsetHeight, horizontal);
			}

			function initSliderButton( aSkinCount, aOffsetY, aOffsetHeight, topH, bottomH, horizontal)
			{
				var im = new Image();
				im.parent = this.b_slider;
				im.name = "imSkin";
				im.src = this.imSkin.src;
				im.hide();
				b_slider.init(aSkinCount, aOffsetY, aOffsetHeight, topH, bottomH, horizontal);
			}

			function initScrollbackButton( aSkinCount, aOffsetY, aOffsetHeight, topH, bottomH, horizontal)
			{
				var im = new Image();
				im.parent = this.b_scrollback;
				im.name = "imSkin";
				im.src = this.imSkin.src;
				im.hide();
				b_scrollback.init(aSkinCount, aOffsetY, aOffsetHeight, topH, bottomH, horizontal);
			}
		</script>
	</composition>

	<composition name="CScrollerVertical" _class="CSkinedScroller">
		<image name="imSkin" src="eval(ENVRPATH+'scroller\\scrollerV.png')" visible="false"/>
		<script>
			this.horizontal = false;
		</script>
		<script>
			initUpButton(4,0,0,13,13);
			initDownButton(4, 0, imSkin.getMediaSize().height-13, 13,13);
			initSliderButton(4, 13, 47-13, 2 , 2);
			initScrollbackButton(4, 47, 65-47, 2 , 2);
			resizeScroller(this.scrollerSize);
		</script>
	</composition>

	<composition name="CScrollerHorizontal" _class="CSkinedScroller">
		<image name="imSkin" src="eval(ENVRPATH+'scroller\\scrollerH.png')" visible="false"/>
		<script>
			this.horizontal = true;
		</script>
		<script>
			initUpButton(4, 0, 0, 13, 13, this.horizontal);
			initDownButton(4, imSkin.getMediaSize().width -13, 0, 13, 13,this.horizontal);
			initSliderButton(4, 13, 47-13, 2 , 2, this.horizontal);
			initScrollbackButton(4, 47, 65-47, 2 , 2, this.horizontal);
			resizeScroller(this.scrollerSize);
		</script>
	</composition>
</rmml>