/*
 * slider.js copyright 2008 Biobit d.o.o. http://www.biobit.hr/
 * 
 * Kopiranje ovog dokumenta i njegovih dijelova, kao i njegova upotreba
 * u originalnom ili izmijenjenom obliku, kako u komercijalne, tako i u
 * druge svrhe dozvoljena je pod uvjetom da svaka kopija, kao i izmijenjena
 * verzija dokumenta i njegovog dijela sadrži ovaj tekst u neizmijenjenom
 * obliku, uključujući referencu na Biobit d.o.o. i URL http://www.biobit.hr/.
 * 
 */

function calculateStoppingDistance (speed, accel)
{
	s = Math.abs(speed);
	a = Math.abs(accel);
	d = 0;
	while (s > 0)
	{
		d += s;
		s -= a;
	}
	return d;
}
function calculateAcceleratedSpeed (distance, accel)
{
	d = Math.abs(distance);
	a = Math.abs(accel);
	s = 0;
	while (d > 0)
	{
		d -= s;
		s += a;
	}
	return s;
}

function sliderSetWidth ()
{
	var bb = document.getElementById(this.objectId);
	var W = this.width+"px";
	
	if (bb)
	{
		if ("style" in bb)
		{
			if ("setProperty" in bb.style)
			{
				bb.style.setProperty("width", W, null);
			}
			else if ("width" in bb.style)
			{
				bb.style.width = W;
			}
		}
	}
}
function sliderShow()
{
	if (this.action == 0)
		setTimeout(mainLoop, this.timeout);
	this.action = 1;
}

function sliderHide()
{
	if (this.action == 0)
		setTimeout(mainLoop, this.timeout);
	this.action = 2;
}

function sliderMain()
{
	switch(this.action)
	{
		case 1:
			this.speed += this.acceleration;
			if (this.speed > this.max_speed)
			{
				this.speed = this.max_speed;
			}
			ms = calculateAcceleratedSpeed(this.MAX-this.width, this.acceleration);
			if (this.speed > ms)
			{
				this.speed = ms;
			}
			this.width += this.speed;
			if (this.width >= this.MAX)
			{
				this.width = this.MAX;
				this.speed = 0;
				this.action = 0;
			}
			this.setWidth();
			break;
		case 2:
			this.speed -= this.acceleration;
			ms = calculateAcceleratedSpeed(this.width-this.MIN, this.acceleration);
			if (this.speed < -ms)
			{
				this.speed = -ms;
			}
			this.width += this.speed;
			if (this.width <= this.MIN)
			{
				this.width = this.MIN;
				this.speed = 0;
				this.action = 0;
			}
			this.setWidth();
			break;
	}
	if (this.action != 0)
	{
		setTimeout(mainLoop,this.timeout);
	}
}

function Slider (objectId)
{
	this.action = 0;
	this.MIN = 33;
	this.MAX = 130;
	this.width = this.MIN;
	this.timeout = 50;
	this.acceleration = 1;
	this.max_speed = 7;
	this.speed = 0;
	this.objectId = objectId;

	this.setWidth = sliderSetWidth;
	this.show = sliderShow;
	this.hide = sliderHide;
	this.main = sliderMain;
}

var allSliders = new Array();

function registerSlider(s)
{
	allSliders.push(s);
}

function mainLoop ()
{
	for (var i=0; i<allSliders.length; i++)
	{
		allSliders[i].main();
	}
}

