var listurl = '/partneri/loga.txt';

window.addEvent('domready', function(){
	phs = $$('#partneri'); /* Placeholders. */
	phs.each(function(e){
		img = e.getElement('img');
		e.set('text', '');
		i = listurl.lastIndexOf('/');
		baseurl = listurl.substring(0, i+1);
		a = new Element('a').injectInside(e);
		img1 = new Element('img').injectInside(a);
		img2 = new Element('img').injectInside(a);

		e.addEvents({
			'mouseover': function(){ e.store('mouseover', true); },
			'mouseout': function(){ e.store('mouseover', false); }
		});

		new Request({
			method: 'get',
			url: listurl,
			onSuccess: function(list){
				i = 0;
				j = -1;
				n = 0;
				images = [];
				while ((j = list.indexOf('\n', j+1)) != -1) {
					line = list.substring(i, j);
					if (line.trim() == '') continue;
					k = line.indexOf(',');
					l = line.indexOf(',', k+1);
					fname = line.substring(0, k).trim();
					url = line.substring(k+1, l).trim();
					label = line.substring(l+1).trim();
					images[n] = { 'fname': fname, 'url': url,
					    'label': label };
					i = j+1;
					n++;
				}
				e.store('images_pos',
				    Math.floor(Math.random()*images.length));
				func = function(){ rotateImage(e, a,
				    img1, img2, images, baseurl); };
				func.periodical(2000);
				func();
			}
		}).send();
	});
});

function rotateImage(e, a, img1, img2, images, baseurl) {
	if (e.retrieve('mouseover')) return;

	i = e.retrieve('images_pos');
	i++;
	j = i % images.length;
	if (i%2) {
		img2.src = baseurl+images[j].fname;
		img2.alt = images[j].label;
		img2.title = images[j].label;
	} else {
		img1.src = baseurl+images[j].fname;
		img2.alt = images[j].label;
		img2.title = images[j].label;
	}

	fx1 = new Fx.Tween(img2, {duration: 500, fps: 20});
	fx1.start('opacity', i%2);
	fx2 = new Fx.Tween(img1, {duration: 500, fps: 20});
	fx2.start('opacity', (i+1)%2);

	a.href = images[j].url;

	e.store('images_pos', i);
}

