var indexes = [];
var last_id = 0;

function NewId(a, b) {
	return Math.floor(Math.random() * (b - a + 1)) + a;
}

function NewIndex() {
	return NewId(0, thumbs.length);
}

function NewUniqueIndex() {
	while (true) {
		id = NewIndex();
		if (indexes.indexOf(id) == -1) { return id; }
	}
}

function FadeIn(id) {
	uid = NewUniqueIndex();
	indexes[id] = uid;
	item0 = '#frame'+id+' div';
	item1 = item0+' a';
	thumb = 'url("/data/'+thumbs[uid]['thumb']+'")';
	href = '/portfolio/'+thumbs[uid]['link']+'/';

	$(item1).css({ 'opacity' : 0, 'background-image' : thumb });
	$(item1).attr({ 'href' : href, 'alt' : uid });
	$(item1).animate({ 'opacity' : 1 }, { duration: 1000, queue: true, complete: function() { $(item0).css({ 'background-image' : thumb }); }});
}

function Fader() {
	this_id = NewId(1, slots);
	while (last_id == this_id) { this_id = NewId(1, slots); }
	last_id = this_id;
	FadeIn(this_id);
}


$(document).ready(function() {
	$('a.fancy').fancybox({ 'overlayShow' : true });

	for (id = 1; id <= slots; id++) { setTimeout('FadeIn('+id+')', id * 400); }
	setInterval("Fader()", 4000);
});
