function randOrd() { // sorts an array in a random order 
	return (Math.round(Math.random())-0.5);
}
window.addEvent('load', function() {
var imgsrc, timer, fader, pos, imglinks, lastimg, images, descs, loader, descels;
	fader = new Fx.Morph($('bigphoto'), {
		duration: 1000, 
		wait:true, 
		transition: Fx.Transitions.Quart.easeInOut,
		onCancel: function() {
			$('bigphoto').set({
				'src': lastimg,
				'opacity': 1
			});
		},
		onComplete: function() {
			(function() {
				$('bigphoto').set({
					'src': imgsrc,
					'opacity': 1
				});
			}).delay('10'); // thanks ie
		}
	});

	descels = $$('#homethumbs li a img');
	images = new Array();
	descs = new Array();
	descels.each(function(el, i) {
		images.push('images/home/photos/'+(i+1)+'.jpg');
		descs.push(el.get('alt'));
	});
	loader = new Asset.images(images, {
		onComplete: function() {
			timerstart();
		}
	});

	function timerstart() {
		pos = 1;
		timer = (function() {
			fadein(pos);
			pos++;
			if(pos==7) { pos=1; }
		}).periodical(5000);
	}

	function fadein(num){
			num--;
			imgsrc = images[num];
			imgdesc = descs[num];
			$('fadeto').set('html', '<img src="'+imgsrc+'" alt="'+imgdesc+'" />');
			(function() { $('bigphotodesc').set('html', imgdesc); }).delay(500);
			fader.cancel();
			lastimg = imgsrc;
			fader.start({'opacity': 0 });
	}

	imglinks = $('homethumbs').getChildren().sort(randOrd);
	imglinks.each(function(il) {
		il.addEvent('click', function(e) {
			new Event(e).stop();
			//$clear(timer);
			fadein(il.get('id').substring(2));
		});
	});


});
