/*
*
* dm9 DCS (Dynamic content scroller)
* dependencies:
*		./gen.js
*			- getElement()
* 		-	dm9 js browser detection.
*
* tested and working in:
*		firefox 1.5 (win/mac/*nix)
*		internet explorer 6 (win)
*		safari 1.3.2 (mac)
*
* adapted for relationssajten.se by Ragnar Seton of dm9.se
*
*/

/* constants */
var DCS_CONTENT		= 'content';
var DCS_CONTAINER	= 'container';
var DCS_SCROLLER	= 'scroller';
var DCS_INDICATOR	= 'scrollIndicator';
var DCS_SPEED			= 2;
/* indicator w/h in px */
var DCS_IWIDTH		= 8;
var DCS_IHEIGHT		= 8;
/* indicator top padding in px */
var DCS_ITOP			= 12;
var DCS_IBTM			= 12;

/* flags */
var DCS_isRunning=false;

/*
* globals, hey if you dont like it - go to russia!
* 1st row - box and indicator objects
* 2nd row - settings for the current content
* 3rd row - vars used to move the indicator
*/
var DCS_box=new Object, DCS_indicator=new Object,
		DCS_timer=0, DCS_cHeight=0, DCS_sHeight=0,
		DCS_counter=0, DCS_iStepSize=0;

/* should be called each time new content in DIV_CONTENT is loaded */
function DCS_initialize()
{
	/* will write a ns4 version when that day comes */
	if(!ie4 && !ie5)
	{
		if(!ns4) getElement(DCS_CONTAINER).style.overflow='auto';
		else getElement(DCS_CONTAINER).overflow='auto';
		return false;
	}
	
	DCS_cHeight=DCS_getHeight(DCS_CONTENT);
	DCS_sHeight=DCS_getHeight(DCS_CONTAINER);
	getElement(DCS_INDICATOR).style.top=DCS_ITOP;
	
	if(DCS_cHeight<=DCS_sHeight)
	{
		getElement(DCS_SCROLLER).style.visibility='hidden';
		return true;
	}
	else
	{
		DCS_box=getElement(DCS_CONTENT);
		DCS_indicator=getElement(DCS_INDICATOR);
		
		/*
		* num of px to be scrolled in DCS_SCROLLER divided by num of iterations 
		* gives how many px each iteration should move the indicators
		*/
		DCS_counter=DCS_iStepSize=
			(DCS_getHeight(DCS_SCROLLER)-DCS_ITOP-DCS_IBTM-DCS_IHEIGHT+1) /
			Math.ceil((DCS_cHeight-DCS_sHeight)/DCS_SPEED);
			
		getElement(DCS_SCROLLER).style.visibility='visible';
	}
}

function DCS_start(direction)
{
	switch(direction)
	{
		case 'up':
			DCS_timer=setInterval("DCS_up()",10);
		break;
		case 'down':
			DCS_timer=setInterval("DCS_down()",10);
		break;
	}
	DCS_isRunning=(DCS_timer ? true : false);
}

/* stop scroll */
function DCS_stop()
{
	if(DCS_isRunning)
	{
		clearInterval(DCS_timer);
		DCS_isRunning=false;
		DCS_counter=DCS_iStepSize;
	}
}

function DCS_up()
{
	if(parseInt(DCS_box.style.top)<0)
	{
		DCS_box.style.top=Math.min(parseInt(DCS_box.style.top)+DCS_SPEED,0)+'px';
		/* move indicator */
		if(Math.floor(DCS_counter)>0)
		{
			DCS_indicator.style.top=parseInt(DCS_indicator.style.top)-Math.floor(DCS_counter)+'px';
			DCS_counter-=Math.floor(DCS_counter);
		}
		DCS_counter+=DCS_iStepSize;
	}
	else DCS_stop();
}

function DCS_down()
{
	if(parseInt(DCS_box.style.top)>(DCS_sHeight-DCS_cHeight))
	{
		DCS_box.style.top=Math.max(parseInt(DCS_box.style.top)-DCS_SPEED,DCS_sHeight-DCS_cHeight)+'px';
		/* move indicator */
		if(Math.floor(DCS_counter)>0)
		{
			DCS_indicator.style.top=parseInt(DCS_indicator.style.top)+Math.floor(DCS_counter)+'px';
			DCS_counter-=Math.floor(DCS_counter);
		}
		DCS_counter+=DCS_iStepSize;
	}
	else DCS_stop();
}

function DCS_getHeight(strId)
{
	var box=getElement(strId);
	return (box.offsetHeight) ? parseInt(box.offsetHeight) :
			(ns6 && box.style.height) ? parseInt(box.style.height) : box.style.pixelHeight
}