var Carrousel = {
	image_offset : 20,
	element_name: "carrousel",
	element: null,
	list: null,
	list_length: 0,
	interval: null,

	init: function(list) {
        var div, a, img, acc_width, parent_height;

		if (!document.getElementById || ![].push)
			return;
		Carrousel.element = document.getElementById(Carrousel.element_name);
		if (!Carrousel.element)
			return;

		Carrousel.element.innerHTML = '';
        acc_width = 0;
		Carrousel.list = [];
        parent_height = parseInt(Carrousel.element.offsetHeight);
        for (var i=0; i<list.length; i++) {
			div = document.createElement('div');
			with (div.style) {
				position = 'absolute';
				overflow = 'hidden';
				left = acc_width+'px';
				top = Math.floor((parent_height-list[i][2])/2) + 'px';
				width = list[i][1]+'px';
				height = list[i][2]+'px';
			}
			Carrousel.element.appendChild(div);

			var a_mark = false;
			if (list[i][3]!='')
			{
				a = document.createElement('a');
				a.href = list[i][3];
				a.target = '_blank';
				div.appendChild(a);
				a_mark = true;
			}

			img = document.createElement('img');
			img.src = list[i][0];
			
			with (img.style) {
				width = list[i][1]+'px';
				height = list[i][2]+'px';
			}
			
			if (a_mark)
				a.appendChild(img);
			else
				div.appendChild(img);

	        acc_width += list[i][1]+Carrousel.image_offset;

	        Carrousel.list.push(div);
	        Carrousel.list_length = Carrousel.list.length;
        }

		Carrousel.element.onmouseover = function(e) { Carrousel.pause(e); }
		Carrousel.element.onmouseout = function(e) { Carrousel.resume(e); }
		Carrousel.start();
	},

	start: function() {
        Carrousel.interval = setInterval(function() { Carrousel.step(); }, 5);
	},

	pause: function(e) {
        clearInterval(Carrousel.interval);
        Carrousel.interval = null;
	},

	resume: function(e) {
        Carrousel.interval = setInterval(function() { Carrousel.step(); }, 5);
	},

	step: function() {
		Carrousel.move(-1);
	},

	swap_left: function() {
		var el = Carrousel.list.shift();
		el.style.left = (parseInt(Carrousel.list[Carrousel.list_length-2].style.left)+parseInt(Carrousel.list[Carrousel.list_length-2].style.width)+Carrousel.list_length+Carrousel.image_offset/2)+"px";
		Carrousel.list.push(el);
	},

	swap_right: function() {
		var el = Carrousel.list.pop();
		el.style.left = (parseInt(Carrousel.list[0].style.left)-parseInt(Carrousel.list[0].style.width)-Carrousel.list_length-Carrousel.image_offset/2)+"px";
		Carrousel.list.unshift(el);
	},

	move: function(offset) {
		var onedge_width = parseInt(Carrousel.list[offset<0 ? 0 : (Carrousel.list_length-1)].style.width);

		if (Math.abs(parseInt(Carrousel.list[0].style.left)) <= (onedge_width+Carrousel.image_offset)) {
	        for (var i=0; i<Carrousel.list_length; i++)
		        Carrousel.list[i].style.left = (parseInt(Carrousel.list[i].style.left)+offset)+"px";
			return;
		}

		if (offset<0)
			Carrousel.swap_left();
		else
			Carrousel.swap_right();
	}
}

