// JavaScript Document

var carousel =
{
	slider: Object,
	limit_right: Number,
	limit_left: Number,
	timeout: Number,
	step: Number,

	settings: function(mask_id, slider_id, container_class, timeout)
	{
		var els = getElementsByClass(container_class);
		this.slider = document.getElementById(slider_id);

		var els_width = Number();
		for(i = 0; i < els.length; ++i)
		{
			els_width += els[i].offsetWidth + 2;
		}
		
		this.slider.style.width = els_width + 'px';

		this.timeout = timeout;
		this.step = 30;
		this.limit_right = 0;
		this.limit_left = - els_width + document.getElementById(mask_id).offsetWidth;
	},

	init: function(action)
	{
		if(typeof(move) != 'undefined')
		{
			clearInterval(move);
		}
		if(action == 'stop')
		{
			return;
		}

		move = setInterval(String('carousel.' + action + '()'), this.timeout);
	},
	
	left: function()
	{
		var pos = parseInt(this.slider.style.marginLeft);

		if(pos <= this.limit_left)
		{
			clearInterval(move);
		}
		else
		{
			var new_pos = pos - this.step;
			this.slider.style.marginLeft = new_pos + 'px';
		}
	},

	right: function()
	{
		var pos = parseInt(this.slider.style.marginLeft);

		if(pos >= this.limit_right)
		{
			clearInterval(move);
		}
		else
		{
			var new_pos = pos + this.step;
			this.slider.style.marginLeft = new_pos + 'px';
		}
	},
	
	change_buttons: function(id, value)
	{
		//alert(id + '\n' + value);
		var el = document.getElementById(id);
		var current_pos = el.style.backgroundPosition.split(' ');

		var x = parseInt(current_pos[0]);

		el.style.backgroundPosition = x + 'px ' + value + 'px';
	}
}


function getElementsByClass(theClass)
{
	var elementsArray = [];
	if(document.all)
	{
		elementsArray = document.all;
	}
	else
	{
		elementsArray = document.getElementsByTagName("*");
	}
	
	var newArray = [];
	
	for(i = 0; i < elementsArray.length; i++)
	{
		if(elementsArray[i].className == theClass)
		{
			newArray[newArray.length] = elementsArray[i];
		}
	}
		
	return newArray;
}

