var RovigoOggi = {
	changeApertura: function()
	{
		APERTURA = setTimeout(function()
		{
			var finded = null, active = false, firstA = null, firstN = null;
			
			$('#apertura_thumbs li').each(function(o)
			{
				var a = o.byTag('a')[0], h = a.href.split('#')[1], n = I(h);
				if (!firstA) 
				{
					firstA = a;
					firstN = n;
				}
				
				if (a.hasClass('active')) 
				{
					n.removeClass('active');
					a.removeClass('active');
					finded = true;
				}
				else if (finded) 
				{
					n.addClass('active');
					a.addClass('active');
					finded = false;
					active = true;
				}
			});
			
			if (!active && firstA) 
			{
				firstN.addClass('active');
				firstA.addClass('active');
			}
			
			RovigoOggi.changeApertura();
			
		}, 7000);
	},
	stopApertura: function()
	{
		clearTimeout(APERTURA);
	},
	loading: function(t)
	{
		t = I(t);
		
		var s = t.sizes(), p = t.position(), n = N(['div', {
			style: {
				width: s[0] + 'px',
				height: s[1] + 'px',
				position: 'absolute',
				top: p[1] + 'px',
				left: p[0] + 'px'
			},
			className: 'ajax-loading'
		}, ['div', {
			style: {
				width: s[0] + 'px',
				height: s[1] + 'px',
				position: 'absolute',
				top: '0px',
				left: '0px',
				opacity: 0.7
			},
			className: 'base'
		}], ['div', {
			style: {
				width: s[0] + 'px',
				height: s[1] + 'px',
				position: 'absolute',
				top: '0px',
				left: '0px'
			},
			className: 'loader'
		}]], APPS_LAYER);
		
		return n;
	}
}, APERTURA = null;

Event.add(window, 'domloaded', function()
{
	if (Agent.MSIE && Agent.MSIE < 7) 
	{
	
	BODY.prepend(['div', {
		className: 'ie6-disclaimer',
		innerHTML: '<b>Attenzione:</b> state navigando con una versione di Microsoft Internet Explorer di vecchia generazione.<br/>' +
		'Rovigo Oggi è un sito pensato per browser di seconda generazione che utilizzano linguaggi standard.<br/>' +
		'Potreste incappare in problemi di visualizzazione delle pagine!<br/>' +
		'Per favore <b><a href="http://www.microsoft.com/italy/windows/products/winfamily/ie/default.mspx">aggiornate il vostro browser a Internet Explorer 8</a></b> o superiore<br/> o provate un altro browser gratuito come \n' +
		'<b><a href="http://www.mozilla.com/">Mozilla Firefox</a>, <a href="http://www.google.com/chrome/">Google Chrome</a></b> o <b><a href="http://www.apple.com/it/safari/">Apple Safari</a></b>'
		}
	]);
	/*

		alert('Attenzione: state navigando con una versione di Microsoft Internet Explorer di vecchia generazione.\n' +
		'Rovigo Oggi è un sito pensato per browser di seconda generazione che utilizzano linguaggi standard.\n' +
		'Potreste incappare in problemi di visualizzazione delle pagine!\n\n' +
		'Per favore passate a Internet Explorer 8 o superiore o ad altro browser come \n' +
		'Mozilla Firefox : http://www.mozilla.com/\nOpera : http://www.opera.com/\no Apple Safari : http://www.apple.com/it/safari/');*/
	}
	
	var arcMenuTimeout, edMenuTimeout, ssMenuTimeout;
	
	// Menu
	I('nb_archivio').addEvent('click', function(event)
	{
		I('nb_archivio').booClass('open');
		
		I('nb_edizionilocali').removeClass('open');
		I('nb_spec_sport').removeClass('open');
		
		return event.preventDefault();
		
	}).addEvent('mouseover', function(event)
	{
		I('nb_archivio').addClass('hover');
	}).addEvent('mouseout', function()
	{
		I('nb_archivio').removeClass('hover');
	});
	
	I('nb_archivio_menu').addEvent('click', function(event)
	{
		return event.stopPropagation();
	});
	
	I('nb_edizionilocali').addEvent('click', function(event)
	{
		clearTimeout(edMenuTimeout);
		I('nb_edizionilocali').booClass('open');
		
		I('nb_archivio').removeClass('open');
		I('nb_spec_sport').removeClass('open');
		
	}).addEvent('mouseover', function(event)
	{
		I('nb_edizionilocali').addClass('hover');
	}).addEvent('mouseout', function()
	{
		I('nb_edizionilocali').removeClass('hover');
	});
	
	I('nb_spec_sport').addEvent('click', function(event)
	{
		I('nb_spec_sport').booClass('open');
		
		I('nb_archivio').removeClass('open');
		I('nb_edizionilocali').removeClass('open');
		
	}).addEvent('mouseover', function(event)
	{
		I('nb_spec_sport').addClass('hover');
	}).addEvent('mouseout', function()
	{
		I('nb_spec_sport').removeClass('hover');
	});
	
	// Menu Ricerca
	Require.module('datepicker', function()
	{
		new DatePicker({
			target: 'search_da',
			language: 'it',
			format: 'Y-m-d',
			xOffset: -50
		});
		
		/*
		new DatePicker({
			target: 'search_a',
			language: 'it',
			format: 'Y-m-d',
			xOffset: -50
		});*/
	});
	
	
	// APERTURA
	var firsted = false;
	
	if (I('apertura')) 
	{
		$('#apertura_thumbs li').each(function(o)
		{
			var a = o.byTag('a')[0], h = a.href.split('#')[1], n = I(h);
			
			if (!firsted) 
			{
				if (n) 
				{
					n.addClass('active');
					a.addClass('active');
					firsted = true;
				}
			}
			
			a.addEvent('mouseover', function(e)
			{
				RovigoOggi.stopApertura();
				
				if (n) 
				{
					$('#apertura .active').each(function(p)
					{
						p.removeClass('active');
					});
					
					n.addClass('active');
					a.addClass('active');
				}
				
				return e.stop();
				
			}).addEvent('mouseout', function()
			{
				RovigoOggi.changeApertura();
			}).addEvent('click', function(e)
			{
				document.location = n.byTag('a')[0].href;
				return e.stop();
			});
			
			n.addEvent('mouseover', function()
			{
				RovigoOggi.stopApertura();
			}).addEvent('mouseout', function()
			{
				RovigoOggi.changeApertura();
			});
			
			a.append(['span']);
		});
		
		RovigoOggi.changeApertura();
	}
	
	firsted = false;
	
	if (I('articoli_fotocronache')) 
	{
	
		// ARTICOLI
		$('#articoli_tabs li').each(function(o)
		{
			var a = o.byTag('a')[0], h = a.href.split('#')[1], n = I(h);
			
			if (!firsted) 
			{
				if (n) 
				{
					n.addClass('active');
					a.addClass('active');
					firsted = true;
				}
			}
			
			a.addEvent('mouseover', function(e)
			{
				if (n) 
				{
					$('#articoli .active').each(function(p)
					{
						p.removeClass('active');
					});
					n.addClass('active');
					a.addClass('active');
				}
				return e.stop();
			});
			
			o.append(['span']);
		});
	}
	
	
	Require.JS.once(AIDAJS.baseUrl + '/widgets.js', function()
	{
		// COMMENTI
		if (I('comment_form')) 
		{
			I('comment_form').before(['div', {
				id: 'comment_result'
			}]);
			
			I('comment_body').styles({
				height: '100px'
			});
			
			I('comment_send').addEvent('click', function(e)
			{
				var load = RovigoOggi.loading('comment_form');
				
				Ajax.get({
					url: BASE_URL + '/index.php?mdu=ajax/comments&action=add',
					post: {
						title: V('comment_title'),
						comment: V('comment_body'),
						range: V('comment_range'),
						id: V('comment_content_id'),
						author: V('comment_author_id'),
						reply: V('comment_reply_id')
					},
					onSuccess: function(t)
					{
						load.remove();
						
						var c = 'error', m = 'Errore';
						
						if ((t = JSON.decode(t))) 
						{
							c = t.type;
							m = t.message;
							
							if (t.success) 
							{
								c = 'confirm';
								
								m = 'Grazie per aver commentato questa notizia. Il tuo commento è inserito nella coda di commenti in attesa di approvazione da parte della redazione';
								
								
								I('comment_title').value = '';
								I('comment_body').value = '';
							}
						}
						
						I('comment_result').replaceChilds(m);
						I('comment_result').className = c;
					},
					onFailure: function()
					{
						load.remove();
						
						I('comment_result').replaceChilds('Errore');
						I('comment_result').className = 'error';
					}
				});
				
				return e.stop();
			});
			
			$('#comments .delete').each(function(o)
			{
				var p = o.parentNode, c = p.id.split('_'), r = c[1], i = c[2];
				
				o.addEvent('click', function(e)
				{
					if (confirm('Confermi di voler cancellare questo commento?')) 
					{
						Ajax.get({
							url: BASE_URL + '/index.php?mdu=ajax/comments&action=delete',
							post: {
								range: r,
								id: i
							},
							onSuccess: function(t)
							{
								var c = 'error', m = 'Errore';
								if ((t = JSON.decode(t))) 
								{
									c = t.type;
									m = t.message;
									if (t.success) 
									{
										p.blindUp({
											onfinish: function()
											{
												this.remove();
											}
										});
										return;
									}
								}
								alert(m);
							},
							onFailure: function()
							{
								alert('Errore');
							}
						});
					}
					
					return e.stop();
				});
			});
		}
	});
	
	// Scrolling notizie flash / RSS Google News
	if (I('box_nf_inner')) 
	{
		Require.module('marquee', function()
		{
			new Marquee({
				target: 'box_nf_inner',
				width: 210,
				height: 180,
				mousestop: true
			});
		});
	}
	
	if(I('articoli'))
	{
		var h2 = $('#articoli h2')[0];
		
		if(h2)
		{
			var title = h2.getText();
			
			h2.replaceChilds(['img', {
				src: AIDAJS.baseUrl + '/../index.php?mdu=fontlift&t=' + encodeURIComponent(title) + '&c=000000&m=1&f=TrajanPro-Regular&s=25&w=720&p=10',
				alt: title
			}])
		} 
	}
});

