window.addEvent('load', function() {
	this.sorted = false;
	this.largestImage = false;
	this.overallHeight = 0;
	if($chk($('slideShow')) && typeof images !== 'undefined'){
		var slideshowContainer = new Element('div', {id:'slideshowContainer', style:'float:left; display:inline; position:relative; height:150px; width:225px; margin:5px 5px 5px 14px; background:url('+url+'layout/design/loading.gif) center center no-repeat; overflow:hidden;'}).inject($('slideShow'));
		var loader = new Asset.images(images, {onComplete:function(){
			images.each(function(url, index){
				var top;
				if(index === 0){
					top = 0;
				}else{
					top = parseInt(slideshowContainer.getElements('img')[index-1].getStyle('top')) + parseInt(slideshowContainer.getElements('img')[index-1].getSize().y);
				}
				images[index] = new Element('img',{
					src:url,
					style:'position:absolute; top:'+top+'px; left:0px;'
				}).inject(slideshowContainer);
				
				if(this.largestImage === false){
					this.largestImage = images[index].getSize().y;
				}else if(this.largestImage < images[index].getSize().y){
					this.largestImage = images[index].getSize().y;
				}
				
				this.overallHeight = this.overallHeight + images[index].getSize().y;
				
				images[index].set('opacity',0);
				if(index+1 === images.length){
					if(slideshowContainer.getSize().y > this.overallHeight - this.largestImage){
						slideshowContainer.setStyle('height', parseInt(this.overallHeight) - parseInt(this.largestImage));
					}
					slide(images);
				}
			});
			
		}});
		function slideBase(images){
			if(this.sorted)
				images = this.sorted;
			images.each(function(img, index){
				var top = parseInt(img.getStyle('top'));
				var sizeY = img.getSize().y;
				if(top + sizeY < 0){
					var newTop = parseInt(images[images.length - 1].getStyle('top')) + parseInt(images[images.length - 1].getSize().y) - 2;
					img.setStyle('top', newTop);
					this.sorted = new Array();
					this.sorted = images.map(function(uninterestingButNeeded, key){
						if(key < images.length - 1)
							return images[key + 1];
						else if(key === images.length - 1)
							return images[0];
					});
				}else{
					newTop = top - 2;
					img.setStyle('top', newTop);
				}
				if(img.get('opacity')<1){
					var o = img.get('opacity');
					img.set('opacity', o + 0.05);
				}
			});
		}
		var slide = slideBase.create({
			periodical : 40
		});
	}
});