window.addEvent('domready',function() {
	/* preloading */
	var imagesDir = 'site_images/homepage/';
	var imagelist = ['home01.jpg','home02.jpg','home03.jpg','home04.jpg','home05.jpg'];
	var images = (imagelist);	
	
	var holder = $('slideshow-holder');
	images.each(function(img,i){ images[i] = imagesDir + '' + img; }); //add dir to images
	var progressTemplate = 'Loading image {x} of ' + images.length;
	var updateProgress = function(num) {
		progress.set('text',progressTemplate.replace('{x}',num));
	};
	var progress = $('progress');
	updateProgress('text','0');
	var loader = new Asset.images(images, {
		onProgress: function(c,index) {
			updateProgress('text',index + 1);
		},
		onComplete: function() {
			var slides = [];
			/* put images into page */
			images.each(function(im) {
				slides.push(new Element('img',{
					src:im,
					width: 383,
					height: 250,
					styles: {
						opacity:0,
						top:0,
						right:-4,
						position:'absolute',
						'z-index': 10
					}
				}).inject(holder));
				/*holder.setStyle('background','url(logo.png) center 80px no-repeat');*/
			});
			var showInterval = 5000;
			var index = 0;
			progress.set('text','Loading Images ...');
			(function() {slides[index].tween('opacity',1); }).delay(1000);
			var start = function() {
				(function() {
					holder.setStyle('background','');
					slides[index].fade(0);
					++index;
					index = (slides[index] ? index : 0);
					slides[index].fade(1);
				}).periodical(showInterval);
			};

			/* start the show */
			start();
		}
	});
});

shuffle = function(o){ 
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};
