var current = 1;
var height = 259;
var pixels = 0;
var running = false;
var time = 7000;
var prec = 20;
var step = 10;
var timer;
var carosuel;


function waiter(c, to) {
	if (!running) {
		obj('js_'+current).className='js_button';
		current = to;
		obj('js_'+current).className+=' js_on';
		if ((images/2)>c) gonow('up',c);
		else gonow('down',images-c);
	}
	else setTimeout("waiter("+c+","+to+")", 10);
}
function gomove() {
	to = this.innerHTML;
	
	if (current>to) dir = true;
	else dir = false;
	
	i=current;
	c=0;
	
	while (i!=to) {
		c++;
		i++;
		if(i>images) i = 1;
	}
	clearTimeout(timer);
	waiter(c, to);
}
function gonow(dir, dist) {
	pixels += ((dir=="up") ? step*dist : (-1)*step*dist);
	
	if (pixels>(images)*height) pixels -= (images)*height;
	else if (pixels<0) pixels += (images)*height;
	
	dff = pixels - (current-1)*height;
	if (dff<0) dff = -dff;
	if (dff<=(step*dist)-1) pixels = (current-1)*height;
	carosuel.style.backgroundPosition = "0px -"+pixels+"px";
	if ((current-1)*height!=pixels) setTimeout("gonow('"+dir+"',"+dist+")", dist);
	else {
		running = false;
		timer = setTimeout("starter()", time);
	}
}
function js_follow_link() {
	window.location.href = js_link[current-1];
}
function index_loader() {
	if (obj('js_carosuel')) {
		carosuel = obj('js_carosuel');
		timer = setTimeout("starter()", time);
		
		for(i=1; i <= images; i++) {
			obj('js_bg').innerHTML += '<div class="js_button" id="js_'+i+'">'+i+'</div>';
		}
		
		buts = getElementsByClass('js_button',obj('js_bg'),'div');
		mlen = buts.length;
		
		for (i = 0; i < mlen; i++) {
			buts[i].onclick = gomove;
		}
		
		obj('js_1').className+=' js_on';
		obj('js_link').onclick = js_follow_link;
	}
	else setTimeout("index_loader()", 100);
}

index_loader();

function starter() {
	obj('js_'+current).className='js_button';
	current++;
	if (current<=images) obj('js_'+current).className+=' js_on';
	else obj('js_1').className+=' js_on';
	running = true;
	move();	
	timer = setTimeout("starter()", time);
}
function move() {
	pixels += step;
	if (pixels>(current-1)*height) pixels = (current-1)*height;
	carosuel.style.backgroundPosition = "0px -"+pixels+"px";
	if ((current-1)*height>pixels) setTimeout("move()", prec);
	else {
		running = false;
		if (current>images) {
			current = 1;
			carosuel.style.backgroundPosition = "0px 0px";
			pixels = 0;
		}
	}
}