var ROArticolo = {
	startGallery: function() {
		GALLERY = setTimeout(function(){
			ROArticolo.nextGallery();
			ROArticolo.startGallery();
		}, 7000);
		I('gallery_play').removeClass('stop');
	},
	stopGallery: function() {
		clearTimeout(GALLERY);
		I('gallery_play').addClass('stop');
	},
	nextGallery: function(back) {
		var finded, active, firstA, firstN, firstD, act, i = 0;
		$('#articolo_thumbs li').each(function(o){
			var a = o.byTag('a')[0], h = a.href.split('#')[1], n = I(h), d = I('desc_' + h);
			if (n) {
				if(!firstA) {
					firstA = a;
					firstN = n;
					firstD = d;
				}
				if(a.hasClass('active')) {
					n.removeClass('active');
					a.removeClass('active');
					d.removeClass('active');
					act = i;
					finded = true;
				} else if(finded) {
					n.addClass('active');
					a.addClass('active');
					d.addClass('active');
					finded = false;
					active = true;
					if(i > 0 && i % 9 == 0) {
						$('#articolo_thumbs ul')[0].morph({
							styles: {
								marginLeft: ['px', ACTUAL, -(i*65)]
							},
							time: 0.5,
							effect: Effects.outCubic
						});
					}
				}
				i++;
			} else {
				o.remove();
			}
		});
		if(!active && firstA) {
			firstN.addClass('active');
			firstA.addClass('active');
			firstD.addClass('active');
			if (act > 8) {
				$('#articolo_thumbs ul')[0].morph({
					styles: {
						marginLeft: ['px', ACTUAL, 0]
					},
					time: 0.5,
					effect: Effects.outCubic
				});
			}
		}
	},
	prevGallery: function(back) {
		var finded, active, firstA, firstN, firstD, act, i = 0,
		all = $('#articolo_thumbs li').reverse(), total = all.length;
		all.each(function(o){
			var a = o.byTag('a')[0], h = a.href.split('#')[1], n = I(h), d = I('desc_' + h);
			if (n) {
				if(!firstA) {
					firstA = a;
					firstN = n;
					firstD = d;
				}
				if(a.hasClass('active')) {
					n.removeClass('active');
					a.removeClass('active');
					d.removeClass('active');
					act = i;
					finded = true;
				} else if(finded) {
					n.addClass('active');
					a.addClass('active');
					d.addClass('active');
					finded = false;
					active = true;
					if((total - i) % 9 == 0) {
						$('#articolo_thumbs ul')[0].morph({
							styles: {
								marginLeft: ['px', ACTUAL, -((total - i - 9) * 65)]
							},
							time: 0.5,
							effect: Effects.outCubic
						});
					}
				}
				i++;
			} else {
				o.remove();
			}
		});
		if(!active && firstA) {
			firstN.addClass('active');
			firstA.addClass('active');
			firstD.addClass('active');
			if (act > 8) {
				$('#articolo_thumbs ul')[0].morph({
					styles: {
						marginLeft: ['px', ACTUAL, -((total - (total % 9)) * 65)]
					},
					time: 0.5,
					effect: Effects.outCubic
				});
			}
		}
	}
}, GALLERY = null;

Event.add(window, 'domloaded', function() {
	if(I('articolo_thumbs')) {
		var firsted = false;
		$('#articolo_thumbs li').each(function(o){
			var a = o.byTag('a')[0], h = a.href.split('#')[1], n = I(h), d = I('desc_' + h);;
			
			if (!firsted) {
				if (n) {
					n.addClass('active');
					a.addClass('active');
					d.addClass('active');
					firsted = true;
				}
			}
				
			a.addEvent('mouseover', function(e){
				ROArticolo.stopGallery();
				if (n) {
					$('#articolo_top .active').each(function(p){
						p.removeClass('active');
					});
					n.addClass('active');
					a.addClass('active');
					d.addClass('active');
				}
				return e.stop();
			}).addEvent('click', function(e){
				return e.stop();
			});
			/*.addEvent('mouseout', function(){
				ROArticolo.startGallery();
			})*/
			/*n.addEvent('mouseover', function(){
				ROArticolo.stopGallery();
			}).addEvent('mouseout', function(){
				ROArticolo.startGallery();
			});*/
			a.append(['span']);
		});
		I('gallery_download').after(['div', {
			id: 'gallery_shadow'
		}],['div', {
			id: 'gallery_play',
			onclick: function() {
				if(this.hasClass('stop')) {
					ROArticolo.startGallery();
				} else {
					ROArticolo.stopGallery();
				}
			},
			title: 'Avvia/Ferma lo scorrimento delle immagini'
		}]);
		I('articolo_top').append(['div', {
			id: 'gallery_prev',
			onclick: function(){
				ROArticolo.stopGallery();
				ROArticolo.prevGallery();
			},
			title: 'Immagine Precedente'
		}], ['div', {
			id: 'gallery_next',
			onclick: function(){
				ROArticolo.stopGallery();
				ROArticolo.nextGallery();
			},
			title: 'Immagine Succesiva'
		}]);
		
		ROArticolo.startGallery();
	
	}
	
	$('#articolo_tools a').each(function(o){
		var t =  o.getText();
		o.title = t;
	});
	
	I('article_normal_font').addEvent('click', function(e){
		I('articolo').removeClass('bigger');
		return e.stop();
	});
	I('article_bigger_font').addEvent('click', function(e){
		I('articolo').addClass('bigger');
		return e.stop();
	});
	I('article_send').addEvent('click', function(e){
		Window.open(this.href, {
			center: true,
			width: 640,
			height: 480
		});
		return e.stop();
	});
	
	if(I('article_reload_page')) {
		var reload = function() {
			var url = (document.location + '').split('?')[0];
			url += '?&reload=' + (new Date()).getTime();
			document.location = url;
		};
		I('article_reload_page').addEvent('click', function(e){
			reload();
			return e.stop();
		});
		setTimeout(reload, 120000);
	}
});
