(function(){
	//############################################################
	//CONFIGURAZIONE
	//(PS: la directory delle immagini deve essere impostata sul file .php)

	//classe CSS da applicare a i banner-div per
	//essere individuati dallo script
	//quindi ogni div-banner deve essere del tipo:
	//<div id="banner1" class="rotating-banner" style="width: 200px; height: 200px">
	var classCSS="rotating-banner";

	//timeout da un banner ad un altro (ms)
	var timeout=20000;
	//durata della animazione/transazione (ms)
	var tween_duration=500;

	//---------------------------------------
	//Tipi di animazioni
	//(supportare: 'move-top', 'move-left', 'move-bottom', 'move-right', 'move-topleft','move-topright','move-bottomleft','move-bottomright')
	animations=['move-left','move-top','move-bottom','move-right','move-topleft','move-topright',
				'move-bottomleft','move-bottomright','zoom','wipe-width','wipe-height',
				'wipe-width-centered','wipe-height-centered','wipe-all','wipe-all-centered'];
	//se "animrandom" è "true" e "animations puoi eseguire (in maniera casuale) una delle animazioni scelte
	animrandom=true;
	//(...di conseguenza puoi specificare una sola animazione 'animrandom' è false)

	//############################################################
	//############################################################
	//############################################################
	//############################################################

	//modifica della classe Element
	Element.implement({
		animType: ['opacity'],
		random: false,
		animDir: 'out',

		setAnimType: function(type){
			if(!(type instanceof Array))
				type=new Array('opacity', type);
			else type.push('opacity');
			this.animType=type;
			if(!this.random && this.animType.length > 1){
				alert("E' possibile specificare una sola animazione quando 'random' è false.");
				this.animType=['opacity'];
			}
		},
		
		setRandom: function(random){
			this.random = random;
		},
		
		startAnim: function(dir){
			anims={};
			this.animDir=dir;
			list=this.animType;
			if(dir == 'out' && this.random){
				list.erase('opacity');
				idx=Math.floor(Math.random()*list.length);
				list=new Array(list[idx]);
				list.push('opacity');
				this.lastAnim=list;
			}else if(dir == 'in' && this.random && this.lastAnim){
				list=this.lastAnim;
				this.lastAnim=null;
			}
			for(i=0; i<list.length; i++){
				curanim=list[i];
				if(curanim == 'opacity'){
					if(dir == 'in') anims.opacity=1;
					else anims.opacity=0;
				}
				if(curanim == 'move-left' || curanim == 'move-topleft' || curanim == 'move-bottomleft'){
					if(dir == 'in') anims['margin-left']=this.imgwidth;
					else  anims['margin-left']=0;
				}
				if(curanim == 'move-right' || curanim == 'move-topright' || curanim == 'move-bottomright'){
					if(dir == 'in') anims['margin-left']=this.imgwidth;
					else  anims['margin-left']=this.imgwidth*2;
				}
				if(curanim == 'move-bottom' || curanim == 'move-bottomleft' || curanim == 'move-bottomright'){
					if(dir == 'in') anims['margin-top']=this.imgheight;
					else  anims['margin-top']=this.imgheight*2;
				}
				if(curanim == 'move-top' || curanim == 'move-topleft' || curanim == 'move-topright'){
					if(dir == 'in') anims['margin-top']=this.imgheight;
					else  anims['margin-top']=0;
				}
				if(curanim == 'wipe-width' || curanim == 'wipe-width-centered' || curanim == 'wipe-all' || curanim == 'wipe-all-centered'){
					if(dir == 'in') anims['width']=this.imgwidth;
					else anims['width']=0;
				}
				if(curanim == 'wipe-width-centered' || curanim == 'wipe-all-centered'){
					if(dir == 'in') anims['margin-left']=this.imgwidth;
					else anims['margin-left']=this.imgwidth+(this.imgwidth/2);
				}
				if(curanim == 'wipe-height' || curanim == 'wipe-height-centered' || curanim == 'wipe-all' || curanim == 'wipe-all-centered'){
					if(dir == 'in') anims['height']=this.imgheight;
					else anims['height']=0;
				}
				if(curanim == 'wipe-height-centered' || curanim == 'wipe-all-centered'){
					if(dir == 'in') anims['margin-top']=this.imgheight;
					else anims['margin-top']=this.imgheight+(this.imgheight/2);
				}
				if(curanim == 'zoom'){
					if(dir == 'in') {
						anims['height']=this.imgheight;
						anims['width']=this.imgwidth;
						anims['margin-top']=this.imgheight;
						anims['margin-left']=this.imgwidth;
					}else{
						anims['height']=this.imgheight*2;
						anims['width']=this.imgwidth*2;
						anims['margin-top']=this.imgheight/2;
						anims['margin-left']=this.imgwidth/2;
					}
				}
			}
			return this.get('morph').start(anims);
		}
	});
	
	var RotateBanner = new Class({
		initialize: function(el){
			this.id = el.get('id');
			this.imgwidth=el.getStyle('width').toInt();
			this.imgheight=el.getStyle('height').toInt();
			el.set({
				'styles': {
					'width': this.imgwidth,
					'height': this.imgheight,
					'overflow': 'hidden',
					'cursor': 'hand',
					'cursor': 'pointer'
				}
			});
			internal_el = new Element('div', {
				'styles': {
					'width': this.imgwidth*3,
					'height': this.imgheight*3,
					'top': -this.imgheight,
					'left': -this.imgwidth,
					'overflow':'hidden',
					'position':'relative',
					'text-align':'left',
					'cursor':'pointer',
					'cursor': 'hand',
				}
			});
			internal_el.inject(el);
			img = new Element('img', {
				'class': 'banner_image',
				'width': this.imgwidth,
				'height': this.imgheight,
				'styles':{
					'cursor':'pointer',
					'cursor': 'hand',
					'position':'relative',
					'width': this.imgwidth,
					'height': this.imgheight,
					'margin-left': this.imgwidth,
					'margin-top': this.imgheight
				}
			});
			img.imgwidth=this.imgwidth;
			img.imgheight=this.imgheight;
			img.setRandom(animrandom);
			img.setAnimType(animations);
			img.inject(internal_el);
			this.img = img;
			this.index = 0;
		},
		
		startup: function(){
			new Request.JSON({
				url: 'rotateimgs.php',
				method: 'get',
				async: true,
				onSuccess: function(responseJSON, responseText){
					this.images=responseJSON.images;
					this.changeImage();
				}.bind(this)
			}).send('dir='+this.id);
		},
		
		changeImage: function(){
			if(this.index == this.images.length)
				this.index=0;
			this.cur_image=this.images[this.index];
			this.img.set('morph', {duration: tween_duration,
				onComplete: function(){
					if(this.img.animDir == 'out'){
						this.img.set({
							'src': this.cur_image.src,
							'onclick': "window.open('"+this.cur_image.href+"')"
						});
						if(this.img.animDir == 'out')
							this.img.startAnim('in').chain(function(){
								this.changeImage.delay(timeout, this);
							}.bind(this));
					}
				}.bind(this)
			});
			this.img.startAnim('out');
			this.index++;
		}
	});

	//###################################################################
	//###################################################################
	window.addEvent("domready", function(){
		banners = $$('.'+classCSS);
		for(i=0; i<banners.length; i++){
			b = new RotateBanner(banners[i]);
			b.startup();
		}
	});
})();
