// list: 		ul/li list con inside text,images src ,alt,title.
// mini_path:	percorso della directory dei thumbnails
// vedi_list:	true/false vedi la lista delle _M
// direction:	true/false lista delle _M in orizzontale/verticale
function Gallery_move_init (d,e){
	var m=OM.mouse,w=OM.world,point=Math.diffPoint(m.Pos(e),m.mouseOffset),_calcola_point=function(p){return Math.constrainTo(p, Math.addPoint(w.origine(),w.scrollPos()), Math.diffPoint(Math.addPoint(w.scrollPos(),w.dimension()),d.Dimension()))};
	if(d.id.find('Image')||d.id.find('Img__')){ 	
		m.mouseOffset=m.Offset(d,e);
		d.scrollpmove(Math.diffPoint(d.Position(),point))
	} else d.pmove(_calcola_point(point))
}
// es : Gallery_onload("nome_lista","$w=100&h=100", "",true, 1 ,true)
function Gallery_onload(lista,dim_foto,dim_minifoto,vedi_minifoto,dir_minifoto,no_html){
	if (!OM.is.browser){
		OM.is.init();
		OM.world.init(Gallery_move_init,null,null,null,no_html)
	}
	Galleria_fotografica($(lista), dim_foto, dim_minifoto, vedi_minifoto, dir_minifoto, no_html)
}
var Galleria_fotografica=function(list, big_path, mini_path,vedi_list,direction,no_html){
	var timer=false,
		alpha__S=70, // trasparenza fondo 0--100
		ztimer=false,
		framerate=30, // velocita di animazione 30 flops
		nframe=15,
		speed=10,
		time=10000,
		foto=0,
	// non cambiare 
		_F=new Array,_M=new Array,
		w_size=w_max=h_size=h_max=0,
		path='themes/gallery/',
		_open='url('+path+'hand_open.cur),default',
		_close='url('+path+'hand_close.cur),default',
		preview=false,
	// page element
		_S=$E('Gallery_Stage'),
		_P=$E('Image_Stage'),pad=$E('pad_slideshow'),open_mini=$E('open_hgallery'),
		_body=(document.documentElement || document.body),
	// function
		_W = OM.world,
		_T = OM.timeline,
		_m = OM.mouse,
		_b = OM.is,
		_n = OM.utility.naming,
		_Pospad={x:50,y:_W.Height()-220};
	// set Stage
	with(_S){
		position(true);
		zIndex(1000);
		visible(false);
		style.backgroundColor='black';
		style.margin='0px';
		imageLoader.display('<span>...sto caricando la Galleria..<br>Clicca su una foto per iniziare...</span>')
	}
	with(_P){
		position(true);
		overflow(false);
		zIndex(1001);
		visible(false);
		cursor(_close);
		style.margin='0px';
		write(
'<table width="100%" height="100%"><tr><td align="center" valign="middle">'+
'<img '+_n('fotografia')+' src="'+path+'space.gif" alt="" />'+
'<div '+_n('errori_gallery')+' style="width:auto; font-size: 18pt;color: white;font-family: Verdana;visibility:hidden;display:none;"></div>'+
'</td></tr></table>'
		)
	}
	// set pad
	var pad_I=[
		'space.gif',
		'open_command.gif',
		'close_command.gif',
		'gallery_previous.gif',
		'gallery_play.gif',
		'gallery_pause.gif',
		'gallery_next.gif',
		'ZoomPiu.gif',
		'ZoomNormale.gif',
		'ZoomMeno.gif',
		'exit.gif',
		'open_'+(direction? '':'V')+'gallery.gif',
		'loading.gif',
		'foto.gif'
	];
	with(pad.imageLoader){
		progressbar=_S.imageLoader.progressbar;
		progressimage=_S.imageLoader.progressimage;
		add(pad_I,path);
		onFinish(
function(){
	pad_I=pad.imageLoader.loaded;
	with(pad){
		visible(false);
		zIndex(1005);
		position(true);
		write(
'<table border="0" cellspacing="0" cellpadding="0">'+
'<tr>'+
'<td><a '+_n('toggle_pad')+' href="javascript:;">'+
'<img '+_n('toggle_img_open')+' src="'+pad_I[1].src+'" title="Apri / Chiudi i pad" alt="Apri / Chiudi i pad" border="0" style="visibility:hidden;display:none;" />'+
'<img '+_n('toggle_img_close')+' src="'+pad_I[2].src+'" title="Apri / Chiudi i pad" alt="Apri / Chiudi i pad" border="0" /></a></td>'+
'<td '+_n('button_pad')+' class="comandi" style="visibility: visible;">'+
'<table height="100%" border="0" cellspacing="0" cellpadding="0"><tr>'+
'<td nowrap="nowrap"><img src="'+pad_I[0].src+'" alt="" height="'+pad_I[1].height+'" width="5" border="0"><td>'+
'<td nowrap="nowrap">'+
'<a '+_n('indietro')+' href="javascript:;"><img '+_n('indietro_img')+' src="'+pad_I[3].src+'" alt="foto precedente" title="foto precedente" border="0" /></a>'+
'<a '+_n('startstop')+' href="javascript:;">'+
'<img '+_n('start_img')+' src="'+pad_I[4].src+'" alt="Inizia presentazione" title="Inizia presentazione" border="0" align="middle" style="visibility:hidden;display:none;" />'+
'<img '+_n('stop_img')+' src="'+pad_I[5].src+'" alt="Ferma presentazione" title="Ferma presentazione" border="0" align="middle" style="visibility:hidden;display:none;" /></a>'+
'<a '+_n('avanti')+' href="javascript:;">'+
'<img '+_n('avanti_img')+' src="'+pad_I[6].src+'" alt="foto successiva" title="foto successiva" border="0" /></a>'+
'<img src="'+pad_I[0].src+'" alt="" height="1" width="5" border="0" /><br>'+
'&nbsp;&nbsp;<img '+_n('foto_icon')+' src="'+pad_I[13].src+'" alt="numero della foto" title="numero della foto" border="0" /><span '+_n('numerofoto')+' class="numerofoto"></span>'+
'</td><td>'+
'<a '+_n('zoom_piu')+' href="javascript:;"><img '+_n('zoom_piu_img')+' src="'+pad_I[7].src+'" alt="Ingrandisci" title="Ingrandisci" border="0" /></a><br>'+
'<a '+_n('resizing')+' href="javascript:;"><img '+_n('resizing_img')+' src="'+pad_I[8].src+'" alt="Riempi lo _S" title="Riempi lo _S" border="0" /></a><br>'+
'<a '+_n('zoom_meno')+' href="javascript:;"><img '+_n('zoom_meno_img')+' src="'+pad_I[9].src+'" alt="Rimpicciolisci" title="Rimpicciolisci" border="0" /></a>'+
'</td><td>'+
'<img src="'+pad_I[0].src+'" alt="" height="1" width="5" border="0" />'+
'<a '+_n('exit_button')+' href="javascript:;"><img src="'+pad_I[10].src+'" title="Ritorna alla pagina" alt="Ritorna alla pagina" border="0" /></a>'+
'<img src="'+pad_I[0].src+'" alt="" height="1" width="5" border="0" />'+
'</td></tr></table></td>'+
'<td '+_n('title_pad')+'>&nbsp;&nbsp;<span '+_n('didascalia')+' class="schermo"></span>&nbsp;&nbsp;</td>'+
'</tr></table>')
	}
	// aree visibili	
	var	didascalia=$('didascalia'),	numeri=$('numerofoto'),
	// buttons	
		indietro=$('indietro'),startstop=$('startstop'),avanti=$('avanti'),zoom_piu=$('zoom_piu'),resizing=$('resizing'),zoom_meno=$('zoom_meno'),uscita=$('exit_button'),toggle=$('toggle_pad'),pulsanti=$('button_pad'),titolo=$('title_pad'),_F=$('fotografia'),errori=$('errori_gallery'),
	// command images
		Img_indietro=$('indietro_img'),Img_start=$('start_img'),Img_stop=$('stop_img'),Img_avanti=$('avanti_img'),Img_open=$('toggle_img_open'),Img_close=$('toggle_img_close'),
		imgCache=new Image;
	//private methods
	var _button=function(img,color,timed){
			img.style.backgroundColor=(color? color:'transparent');
			if(timed)setTimeout(function(){_button(img)},500)
		},
		_buttons=function(colored){
			_button(Img_start,(!colored? 'transparent':'#FF3030'));
			_button(Img_stop,(!colored? 'transparent':'#FF3030'));
			_button(Img_avanti,(!colored? 'transparent':'#50FF50'));
			_button(Img_indietro);
			Img_start.visible(colored? false:'inline');
			Img_stop.visible(!colored? false:'inline')
		},
		_checkpoint=function(p,w,h){return {'x':(isNaN(p.x)? w:p.x),'y':(isNaN(p.y)? h:p.y)}},
		_loadFoto=function(foto){
			with(_F){
				timeline.onframe=function(){_F.alpha(_T.Ease(this.current,0,nframe-1,100,0))},
				timeline.onstart=function(){}
				timeline.onstop=function(){
					_S.scrollpos(0,0);
					_P.scrollpos(0,0);
					errori.visible(false);
					with(_F){
						onload=function(e){
							this.alpha(100);
							numeri.write((foto+1)+' di '+_M.length);
							didascalia.write('<b>'+_F[foto][0]+'</b>');
							imgCache.alt=_F[foto][2];
							imgCache.src=_F[foto][1]
						}
						src=pad_I[12].src;
						width=52;
						alpha(0)
					}
				}
				timeline.start()
			}
		},
		_zoom=function(m){ztimer=setInterval(function(){_zoom_image(m)},speed)},
		_zoom_image=function(m){
			_F.width+=m;
			_F.width=(_F.width<100)?100:_F.width;
			_P.scrollmove(m/2,m/2)
		},
		_stopZoom=function(){clearInterval(ztimer)},
		_stop=function(){clearInterval(timer);_buttons(false)},
		_start=function(){
			if(Img_start.style.display=='none'){_stop();return}
			_buttons(true);
			_button(Img_avanti,'green',true);
			_loadFoto((foto<(_F.length-1)?++foto:0));
			timer=setInterval(_proiect,time)
		},
		_next=function(){
			_stop();
			_button(Img_avanti,'green',true);
			_loadFoto((foto<(_F.length-1)?++foto:0))
		},
		_previous=function(){
			_stop();
			_button(Img_indietro,'green',true);
			_loadFoto((foto>0? --foto:(_F.length-1)))
		},
		_proiect=function(){_loadFoto((foto<(_F.length-1)? ++foto:0))},
		_resizeFoto=function(dim){
			_stop();
			_stopZoom();
			_P.scrollpos(0,0);
			_F.width=100;
			while(_F.height<dim.y&&_F.width<dim.x)_F.width+=2
		},
		_loadList=function(obj){
			var list=(obj.UL? obj.UL.LI:null);
			if (list) $A(list).foreach(function(o){
					if(o.IMG) _F[foto++]=[o.IMG.alt,o.IMG.src.charsub(mini_path,big_path),o.IMG.title,(o.text? o.text:'')];
					_loadList(o)
				})
		},
		_wheel=function(e){
			_m.wheel(e,function(){
				var h=_F.height,w=_F.width,
					m=_m.wheelDelta*Math.round(w/10);
				if (w<100)w=_F.width=100;
				_F.width+=m;
				_P.scrollmove((m*_F.width/w/2),((_F.height-h)*_F.height/h/4))
			})
		},
		_go_open=function(){},
		_go_close=function(){},
		_preview_hide = function (ev){
			var h=_W.scrollY()+_W.Height(),
				p=_m.Pos(ev),
				test = direction? (p.y<(h-h_max+50)) : (p.x<(w-w_max+50));
			if (this.show()&&test) _go_close()
		},
		_go=function(obj){
			var h=_W.scrollY()+_W.Height(),
				h2=h-h_max,
				w=_W.scrollX()+_W.Width(),
				w1=_W.scrollX()+30,h1=_W.scrollY()+30,
				w2=w-w_max,
				g=new AreaAutoScroll(obj,50,false,1000,direction);
			obj.addEvent('mouseout',_preview_hide);
			_go_open=function(){
				with(obj){
					doTimeline(framerate,nframe);
					timeline.onframe=function(){obj.pmove(_checkpoint((direction? {x:w1,y:_T.Ease(this.current,0,nframe-1,h,h2)}:{x:_T.Ease(this.current,0,nframe-1,w,w2),y:h1})))}
					timeline.onstart=function(){obj.pmove((direction? {x:w1,y:h}:{x:w,y:h1}));obj.visible(true);open_mini.visible(false)}
					timeline.onstop=function(){}
					timeline.start()
				}
			};
			_go_close=function(){
				with(obj){
					doTimeline(framerate,nframe);
					timeline.onframe=function(){obj.pmove(_checkpoint((direction? {x:w1,y:_T.Ease(this.current,0,nframe,h2,h)}:{x:_T.Ease(this.current,0,nframe,w2,w),y:h1})))}
					timeline.onstart=function(){}
					timeline.onstop=function(){obj.visible(false);open_mini.visible(true)}
					timeline.start()
				}
			}
		},
		_image_minifoto=function(){
			var txt='';
			_M.foreach(function(o){txt+='<img src="'+o.src+'" title="'+o.title+'" border="0" style="margin: 5px;" align="middle">'+(direction? '':'<br>');});
			return txt
		},
		_displayList=function(){
			if (preview.id)return;
			var h=_W.scrollY()+_W.Height(),w=_W.scrollX()+_W.Width(),
				w1 = (direction? (_W.Width()-2*30):w_max),h1 = (direction? h_max:_W.Height()-2*30);
			// define _M list
		    preview=$E('AreaImg');
			with(preview){
				visible(false);
				zIndex(1002);
				position(true);
				size(w1,h1);
				overflow(false);
				pmove((direction? {x:_W.scrollX()+30,y:h}:{x:w,y:_W.scrollY()+30}));
				write('<div '+_n('AreaImg_scroll')+' class="Scroller" style="width:'+(direction? (w_size*2):w_max)+'px;height: '+(direction? h_max:h_size)+'px;">'+_image_minifoto()+'</div>');
			}
			_go(preview);
			with(open_mini){
				position(true);
				pmove((direction? {x:(w-_W.Width()/2-pad_I[11].width/2), y:(h-13)}:{x:(w-13),y:(h-_W.Height()/2-pad_I[11].height/2)}));// dimensioni immagine
				zIndex(1003);
				cursor('pointer');
				write('<img src="'+pad_I[11].src+'" border="0" alt="vedi _M fotografie" title="vedi _M fotografie">');
				addEvent('click',_go_open)
			}
			// foto scrolling  _M image action
			var imgs=$A($_TagNames('img',preview));
			imgs.foreach(function(o){
				with($(o)){
					addEvent('click', function(){_loadFoto(imgs.indexOf(this)% _M.length)});
					cursor((!(_b.msie&&_b.ver<6)? 'pointer':'hand'))
				}
			})
		},
		_startLoad=function(o,p){with(o){alpha(0);visible(true);pmove(p)}},
		_fadeLoadFoto=function(img,func){
			var h=_W.scrollY()+_W.Height(),w=_W.scrollX()+_W.Width();
			window.scroll (0,0);
			_body.style.overflow="hidden";
			foto=img? _M.indexOf(img):0;
			with(_S){
				timeline.onframe=function(){
					var alfa_s=_T.Ease(this.current,0,nframe-1,0,alpha__S),
						alfa_f=_T.Ease(this.current,0,nframe-1,0,100);
					_S.alpha(alfa_s);
					_P.alpha(alfa_f);
					pad.alpha(alfa_s);
					if(preview){
						open_mini.alpha(alfa_f);
						preview.alpha(alfa_f)
					}
				}
				timeline.onstart=function(){
					_startLoad(_P,_W.scrollX(),_W.scrollY());
					_startLoad(_S,_W.scrollX(),_W.scrollY());
					with(_P){
						size(_W.Width()+(_b.msie? 20:0),_W.Height());
						clip(_W.Width()+(_b.msie? 20:0),_W.Height());
						move(0,0)
					}
					with(_S){
						size(_W.Width()+(_b.msie? 20:0),_W.Height());
						move(0,0)
					}
					// posiziona i pad sullo _S.
					_startLoad(pad,{x:_W.scrollX()+_Pospad.x,y:_W.scrollY()+_Pospad.y});
					if(preview){
						_startLoad(open_mini,(direction? {x:(w-_W.Width()/2-pad_I[11].width/2),y:(h-13)}:{x:(w-13),y:(h-_W.Height()/2-pad_I[11].height/2)}));
						_startLoad(preview,(direction? {x:_W.scrollX()+30,y:h}:{x:w,y:_W.scrollY()+30}))	
					}
				}
				timeline.onstop=function(){
					if (no_html){ with(_S.imageLoader){onFinish(func);load()}} else {
						_loadFoto(foto);
						$process(func)
					}
				}
				timeline.start()
			}
		},
		_fadeOut_S=function(){
			_stop();
			with(_S.timeline){
				onframe=function(){
					var alfa_s=_T.Ease(this.current,0,nframe-1,alpha__S,0),
						alfa_f=_T.Ease(this.current,0,nframe-1,100,0);
					_S.alpha(alfa_s);
					_P.alpha(alfa_f);
					pad.alpha(alfa_s);
					if(preview){
						open_mini.alpha(alfa_f);
						preview.alpha(alfa_f)
					}
				}
				onstart=null;
				onstop=function(){
					if(preview){
						preview.visible(false);
						open_mini.visible(false)
					}
					pad.visible(false);
					_P.visible(false);
					_S.visible(false);
					_body.style.overflow="auto"
				
				}
				start()
			}
		},
		_resizeZoom=function(dim){
			with(_F.timeline){
				onframe=function(){
					_F.alpha(_T.Ease(this.current,0,nframe-1,0,100));
					if(_F.height<(3*dim.y/4))_F.width=_T.Ease(this.current,0,nframe-1,10,Math.round(3*dim.x/4))
				}
				onstart=function(){_F.width=10}
				onstop=null;
				start()
			}
		},
		_setMaxSize=function(){
			w_max=h_max=0;
			_M.foreach(function(o){
				w_size+=o.width? o.width+10:0;
				w_max=w_max<(o.width+10)? o.width+10:w_max;
				h_size+=o.height? o.height+10:0;
				h_max=h_max<(o.height+10)? o.height+10:h_max
			})
		},
		_showpad=function(){
			var stato=pulsanti.show();
			pulsanti.visible(!stato);
			titolo.show(!stato);
			Img_close.visible(stato? false:'inline');
			Img_open.visible(!stato? false:'inline');
			pad.x((stato? 0:_Pospad.x))	
		};
	//
	//
	// search images parsing HTML
	_loadList(OM.Xml.parsing({},list));
	_buttons(false);
	// init timeline 30 flops x 30 frames==1 in un sec. a nero
	_S.doTimeline(framerate,nframe);
	_F.doTimeline(framerate,nframe);
	// preload delle immagini galleria e caricamento galleria
	if (no_html){
		with(_S.imageLoader){
			add(_M);
			onFinish(function(){
				_M=_S.imageLoader.loaded;
				_setMaxSize();
				for(var i=0;i<_M.length;i++)_M[i].title=_M[i].alt=_F[i][0];
				_loadFoto(0)
			})
		}
		_fadeLoadFoto('',(vedi_list? _displayList:null))
	} else {
		// immagini visibili in lista=> evento su foto della lista.
		_M=$A($_TagNames('img',list));
		_setMaxSize();
		_M.foreach(function(o){ if(o.src.find('back.png')) _M.remove(o)});
		_M.foreach(function(o){
			o = $(o);
			o.addEvent('click',function(){_fadeLoadFoto(o,(vedi_list? _displayList:null))});
			o.cursor((!(_b.msie&&_b.ver<6)? 'pointer' : 'hand'));
		})
	}
	// Foto loading event
	var _imageError=function(e){
		with(errori){write('Impossibile caricare immagine<br>['+this.src+']');visible(true)}
		setTimeout(_next,1000)
	}
	_F.onerror=_imageError;
	with(imgCache){
		onerror=_imageError;
		onload=function(e){
			_F.alpha(0);
			_F.onload=null;
			_F.src=this.src;
			_resizeZoom(_S.Dimension())
		}
	}
	// events
	indietro.addEvent('click',_previous);
	startstop.addEvent('click',_start);
	avanti.addEvent('click',_next);	
	uscita.addEvent('click',_fadeOut_S);
	toggle.addEvent('click',_showpad);
	resizing.addEvent('click',function(){_resizeFoto(Math.scalePoint(_S.Dimension(),.8))});
	with(pulsanti){
		addEvent('mousedown',function(){_zoom(20)});
		addEvent('mouseup',_stopZoom)
	}
	with(zoom_meno){
		addEvent('mousedown',function(){_zoom(-20)});
		addEvent('mouseup',_stopZoom)
	}
	with(pulsanti){
		cursor('move');
		addEvent('mousedown',function(e){_m.InitMove(pad,e)});
		addEvent('mouseup',_m.EndMove)
	}
	with(pad){
		addEvent('mouseover',function(){pad.alpha(100)});
		addEvent('mouseout',function(){pad.alpha(30)})
	}
	with(_P){
		cursor(_open);
		addEvent('mousewheel',_wheel);
		addEvent('mousedown',function(e){_P.cursor(_close);_m.InitMove(_P,e)});
		addEvent('mouseup',function(e){_P.cursor(_open);_m.EndMove(e)})
	}
});
		load()
	}
};
