/* ---------------------------------------------------------------------------------------------------------------------------------------- */
/*
	Copyright (c) 2009 Muneer Mohammad (http://www.encodez.com/)
	Permission is hereby granted, free of charge, to any person obtaining
	a copy of this code, to deal in the code without restriction, including
	without limitation the rights to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the code, and to
	permit persons to whom the Software is furnished to do so, subject to
	the following conditions:
	
	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the code.
	
	THE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
	LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
	OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
	WITH THE CODE OR THE USE OR OTHER DEALINGS IN THE CODE.
*/
/* ---------------------------------------------------------------------------------------------------------------------------------------- */
/*
	2009/12/15: massive code corrections by Joachim Legat (http://www.jolegat.com)
*/
/* ---------------------------------------------------------------------------------------------------------------------------------------- */

var gTop = [];
var gIncVal = [];
var gHeight = [];

// var timeOutValue can be used to adjust the speed of scroller.
var timeOutValue = 10;
  
/* ---------------------------------------------------------------------------------------------------------------------------------------- */
  
function InitScrollConstants (node_id, height)
{
	gTop [node_id] = 0;
	gIncVal [node_id] = 0;
	gHeight [node_id] = height - 10;
}  
  
/* ---------------------------------------------------------------------------------------------------------------------------------------- */
  
function scrollMe (arg, node_id)
{    
	var objEncNav = document.getElementById (node_id);
	
	// var scrollAmount define the fast and
	// the amount of scrolling pane
	var scrollAmount = gHeight [node_id] - 50;
	var objEncNavHeight=parseInt(objEncNav.offsetHeight);
	var objEncNavTop=objEncNav.style.top;
	
	// var barHeight defines the heigt of inner layer
	// it must set -10 from max height
	var barHeight = gHeight [node_id];   
	
	if(!objEncNavTop)
		objEncNavTop=0;
	else
		objEncNavTop = parseFloat (objEncNavTop.substring(0,objEncNavTop.length-2));
	
	//.....................................................................................................................................down
	if (arg > 0)
	{
		var max_scroll_diff = objEncNavHeight + objEncNavTop - gHeight [node_id];
		if (scrollAmount > max_scroll_diff) scrollAmount = max_scroll_diff;

		if(objEncNavTop >= 0 || objEncNavTop>(-(objEncNavHeight-(barHeight+scrollAmount))))
		{
			incrementValue=scrollAmount;
		}
		else if(objEncNavTop<=(-(objEncNavHeight-(barHeight+scrollAmount))))
		{
			incrementValue=(objEncNavHeight-barHeight)+parseInt(objEncNavTop);
		}
		else
		{
			incrementValue=0;
		}
		encSmoothScroll(node_id, "minus", parseInt(objEncNavTop), parseInt(incrementValue));
	}
	else
	{
		if(objEncNavTop<0 && ((parseInt(objEncNavTop)+scrollAmount) < 0))
		{
		incrementValue=scrollAmount;
		}
		else
		{
		incrementValue=-objEncNavTop-0;     
		}
		encSmoothScroll(node_id, "plus", parseInt(objEncNavTop), parseInt(incrementValue));
	}
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function encSmoothScroll (node_id, dir, currentVal, incValue)
{
	gTop [node_id]=currentVal;
	gIncVal[node_id] = incValue;
	encScrollBy (node_id, dir, 0)
}

/* ---------------------------------------------------------------------------------------------------------------------------------------- */

function encScrollBy (node_id, dir, val)
{ 
	if (val < gIncVal[node_id])
	{
		var tmpInc;      
		if((gIncVal[node_id] - val) > 1)      
		{
			tmpInc=Math.ceil((gIncVal[node_id] - val)/10);
			if(tmpInc <1)
			tmpInc=1;
		}
		else
		{
			tmpInc=gIncVal[node_id] - val;
		}
		
		val+=tmpInc;
		
		var objEncNav=document.getElementById (node_id);
	
		if(dir=="plus")
		{
			objEncNav.style.top=gTop [node_id] + val + "px";
		}
		else if (dir=="minus")
		{
			objEncNav.style.top=gTop [node_id] - val + "px";
		}
		var t=setTimeout("encScrollBy('"+node_id+"', '"+dir+"', "+val+");", timeOutValue);
	}
	else
	{
		clearTimeout(t);
	}
}
