/*

JS files needed:
	- StdLibrary.js
	
Usage:
	- 	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
		<HTML>
			<HEAD>
				<title>ScrollingLayer</title>
				<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
				<meta name="CODE_LANGUAGE" Content="C#">
				<meta name="vs_defaultClientScript" content="JavaScript">
				<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
				<style type="text/css">
					@import "ScrollingLayer.css";
				</style>
				<script type="text/javascript" src="StdLibrary.js"></script>
				<script type="text/javascript" src="ScrollingLayer.js"></script>
				<script>
				function InitScrollTable()
				{
					window.lyrScrollTable = new ScrollLayerTable('lyr');
					window.lyrScrollTable.smoothness = 5;
					window.lyrScrollTable.speed = 10;
					window.lyrScrollTable.interval = 3000;
					window.lyrScrollTable.ciclic = true;
					window.lyrScrollTable.scrollXSize = 180;
					window.lyrScrollTable.scrollYSize = 0;
					window.lyrScrollTable.itemsCount = 9;
					
					window.lyrScrollTable.Start();
				}
			</script>
			</HEAD>
			<body onload="InitScrollTable();">
				<form id="Form1">
					<div id="hold" class="scrollHold">
					  <div id="wnd" class="scrollWnd">
						<div id="lyr" class="scrollLyr">
								<table id="scrollTbl" class="scrollTable" cellpadding="0" cellspacing="0" border="0">
									<tr>
										<td><h5>Headline 1</h5><br /><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras quis tellus nec pede sodales posuere. In id est.</p></td>
										<td><h5>Headline 2</h5><br /><p>Pellentesque rhoncus quam eu dui. Nulla facilisi. Aliquam felis ligula, tristique eget, gravida in, rhoncus vel, dolor. Nulla interdum malesuada felis.</p></td>
										<td><h5>Headline 3</h5><br /><p>Etiam vel nunc ut purus volutpat tincidunt. Nam commodo laoreet sem. Nullam aliquet. Nunc rhoncus. Phasellus vestibulum dui sed est. Morbi tempor tincidunt dolor. Donec erat purus, rhoncus eu, euismod at, molestie vel, nibh.</p></td>
										<td><h5>Headline 4</h5><br /><p>News text 4.</p></td>
										<td><h5>Headline 5</h5><br /><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras quis tellus nec pede sodales posuere. In id est.</p></td>
										<td><h5>Headline 6</h5><br /><p>Pellentesque rhoncus quam eu dui. Nulla facilisi. Aliquam felis ligula, tristique eget, gravida in, rhoncus vel, dolor. Nulla interdum malesuada felis.</p></td>
										<td><h5>Headline 7</h5><br /><p>Etiam vel nunc ut purus volutpat tincidunt. Nam commodo laoreet sem. Nullam aliquet. Nunc rhoncus. Phasellus vestibulum dui sed est. Morbi tempor tincidunt dolor. Donec erat purus, rhoncus eu, euismod at, molestie vel, nibh.</p></td>
										<td><h5>Headline 8</h5><br /><p>News text 4.</p></td>
										<td><h5>Headline 9</h5><br /><p>News text 4.</p></td>
									 </tr>
								</table>
						 </div>  <!-- end lyr div -->
					  </div>  <!-- end wnd div -->
					</div>	<!-- end hold div -->
					
					<br/><br/><br/>
					<input type="button" value="Prev" onclick="lyrScrollTable.ScrollBy(180, 0, false)" />
					<input type="button" value="Next" onclick="lyrScrollTable.ScrollBy(-180, 0, true)" />
				</form>
			</body>
		</HTML>

	- ImageFaderTest.js
		var divFader = new DivFader('DivFader');
		divFader.SetStayVisible(0);
		divFader.Start();
*/


__cfgMODE.Set_CONFIGURATION_RELEASE();


function ScrollLayerTable_OnInit()
{
	if ( false == __CheckReference(window.scrollLayerTableObject) )
	{
		window.scrollLayerTableObject = this;
	}
	
	this.Reset();
	this.layerElem.style.left	= 0;
	this.layerElem.style.top	= 0;
}

function ScrollLayerTable_Start()
{
	if ( true == this.ciclic )
	{
		window.ScrollLayerTable_ScrollAllLoop_timeoutID	= setTimeout("window.ScrollLayerTable_ScrollAllLoop(" + this.forward + ", " + this.scrollXSize + ", " + this.scrollYSize + ")", this.interval);
	}
}

function ScrollLayerTable_Reset()
{
	this.scrolling		= false;
	this.scrolledX		= 0;
	this.scrolledY		= 0;
	if ( false == this.ciclic )
	{
		this.forward	= true;
	}
}

function ScrollLayerTable_CanScroll()
{
	var canScroll =
		false == this.scrolling &&
		( (true == this.forward && this.currentItem >= 0 && this.currentItem < this.itemsCount - this.visibleItems) ||
			  (false == this.forward && this.currentItem > 0 && this.currentItem <= this.itemsCount - this.visibleItems)
		);
		
	return canScroll;
}

// !!! for internal use only !!!
function ScrollLayerTable_ScrollTo(scrollX, scrollY)
{
	this.layerElem.style.left = parseInt(scrollX, 10) + "px";
	this.layerElem.style.top = parseInt(scrollY, 10) + "px";
}

function ScrollLayerTable_ScrollBy(scrollByX, scrollByY, forward)
{
	this.forward = forward;
	
	if ( false == this.CanScroll() )
	{
		return;
	}
	
	if ( true == this.forward )
	{
		++this.currentItem;
	}
	else
	{
		--this.currentItem;
	}
	this.scrolling = true;
	
	if ( false == this.smooth )
	{
		this.layerElem.style.left = (parseInt(this.layerElem.style.left, 10) + scrollByX) + "px";
		this.layerElem.style.top = (parseInt(this.layerElem.style.top, 10) + scrollByY) + "px";
		this.Reset();
		
		return;
	}

	setTimeout("window.ScrollLayerTable_ScrollByLoop(" + scrollByX + "," + scrollByY + ")", window.scrollLayerTableObject.speed);
}

function ScrollLayerTable_ScrollByLoop(scrollByX, scrollByY)
{
	var done = true;
	
	var newX = parseInt(window.scrollLayerTableObject.layerElem.style.left, 10);
	var newY = parseInt(window.scrollLayerTableObject.layerElem.style.top, 10);
	
	var smoothnessX = window.scrollLayerTableObject.smoothness;
	var _scrollByX = scrollByX;
	if ( scrollByX < 0 )
	{
		_scrollByX = -_scrollByX;
		smoothnessX = -smoothnessX;
	}
	
	var smoothnessY = window.scrollLayerTableObject.smoothness;
	var _scrollByY = scrollByY;
	if ( scrollByY < 0 )
	{
		smoothnessY = -smoothnessY;
		_scrollByY = -_scrollByY;
	}
	
	if ( window.scrollLayerTableObject.scrolledX < _scrollByX )
	{
		done = false;
		window.scrollLayerTableObject.scrolledX += window.scrollLayerTableObject.smoothness;
		newX += smoothnessX;
	}
	
	if ( window.scrollLayerTableObject.scrolledY < _scrollByY )
	{
		done = false;
		window.scrollLayerTableObject.scrolledY += window.scrollLayerTableObject.smoothness;
		newY += smoothnessY;
	}
	
	window.scrollLayerTableObject.ScrollTo(newX, newY);
	
	if ( true == done )
	{
		window.scrollLayerTableObject.Reset();
		window.ScrollLayerTable_ScrollAllLoop_timeoutID	= setTimeout("window.ScrollLayerTable_ScrollAllLoop(" + window.scrollLayerTableObject.forward + ", " + window.scrollLayerTableObject.scrollXSize + ", " + window.scrollLayerTableObject.scrollYSize + ")", window.scrollLayerTableObject.interval);
		return;
	}
	
	clearTimeout(window.ScrollLayerTable_ScrollAllLoop_timeoutID);
	setTimeout("window.ScrollLayerTable_ScrollByLoop(" + scrollByX + "," + scrollByY + ")", window.scrollLayerTableObject.speed);
}

function ScrollLayerTable_ScrollAllLoop(forward, scrollXSize, scrollYSize)
{
	if ( false == forward && window.scrollLayerTableObject.currentItem <= 0 )
	{
		forward = true;
	}
	else if ( true == forward && window.scrollLayerTableObject.currentItem >= window.scrollLayerTableObject.itemsCount - window.scrollLayerTableObject.visibleItems )
	{
		forward = false;
	}

	var signX = 1, signY = 1;
	if ( true == forward )
	{
		signX = -1;
		signY = -1;
	}

	window.scrollLayerTableObject.ScrollBy(scrollXSize * signX, scrollYSize * signY, forward);
}

function ScrollLayerTable(layerId)
{
	this.layerElem		= document.getElementById(layerId);
	this.smoothness     = 5;
	this.speed 			= 10;
	this.smooth			= true;
	this.ciclic			= false;
	this.scrollXSize	= 0;
	this.scrollYSize	= 0;
	this.interval 		= 3000;
	this.itemsCount     = 0;
	this.visibleItems   = 1;
	
	this.scrolling		= false;
	this.scrolledX		= 0;
	this.scrolledY		= 0;
	this.currentItem	= 0;
	this.forward		= true;
	
	this.OnInit			= ScrollLayerTable_OnInit;
	this.Start			= ScrollLayerTable_Start;
	this.Reset			= ScrollLayerTable_Reset;
	this.CanScroll		= ScrollLayerTable_CanScroll;
	this.ScrollTo		= ScrollLayerTable_ScrollTo;
	this.ScrollBy		= ScrollLayerTable_ScrollBy;
	
	
	this.OnInit();
}
