var Aols = {
	initLoginForm: function()
	{
		// Controllo se esiste il pulsante per aprire il pannello
		var loginOpener = I('upper_login_opener');
		if (loginOpener) 
		{
			Require.module('encode', function()
			{
				I('root').before(['div', {
					id: 'upper_login'
				}, ['div', {
					id: 'upper_login_box'
				}, ['p', {
					id: 'upper_login_title'
				}, ['strong', 'Login Panel'], ['br'], 'Effettua il login da questo pannello'], ['div', {
					id: 'upper_login_form'
				}, ['input', {
					type: 'text',
					name: 'login_username',
					id: 'upper_username'
				}], ['input', {
					type: (Agent.engine == 'trident' ? 'text' : 'password'),
					name: 'login_password',
					id: 'upper_password'
				}], ['div', {
					id: 'upper_ok'
				}, 'Ok']], ['div', {
					id: 'upper_login_message'
				}], ['div', {
					id: 'upper_login_loading'
				}], ['div', {
					id: 'upper_login_info'
				}, ['div', {
					className: 'registration'
				}, ['a', {
					href: BASE_URL + '/account/registrazione/'
				}, 'Registrati ora!']], ['div', {
					className: 'activation'
				}, ['a', {
					href: BASE_URL + '/account/attivazione/'
				}, 'Attiva il tuo account!']], ['div', {
					className: 'password'
				}, ['a', {
					href: BASE_URL + '/account/password-persa/'
				}, 'Hai perso la password?']]]]]);
				
				if(Agent.engine != 'trident' && Agent.engine != 'presto')
				{
					I('upper_login').addEvent('clickout', function()
					{
						Aols.closeUpperLogin();
					});
				}
				
				loginOpener.addEvent('click', function(e)
				{
					Aols.openCloseUpperLogin();
					return e.stop();
				});
				
				var upLoading = I('upper_login_loading'), upOk = I('upper_ok'), upMsg = I('upper_login_message');
				
				var lSubmit = function(e)
				{
				
					var uvl = V('upper_username'), pvl = V('upper_password'), ulf = I('upper_login_form'), msg = '', cls = '';
					if (!uvl || !pvl) 
					{
						msg = 'Username o password vuoti!';
						cls = 'error';
						
						upMsg.replaceChilds(msg).className = cls;
						
						upOk.styles({
							opacity: 1
						});
					}
					else 
					{
						Ajax.get({
							url: BASE_URL + '/index.php?mdu=ajax/login',
							post: {
								login_hash: (uvl.toLowerCase() + pvl.md5() + USER_IP).md5()
							},
							onRequest: function()
							{
								upOk.styles({
									opacity: 0.5
								});
								
								upLoading.styles({
									display: 'block'
								});
							},
							onSuccess: function(t)
							{
								var msg = 'Errore!', cls = 'error';
								
								if ((t = JSON.decode(t))) 
								{
									if (t.logged) 
									{
										msg = 'Login eseguito con successo!';
										cls = 'confirm';
										
										setTimeout(function()
										{
											Aols.closeUpperLogin();
										}, 500);
										
										setTimeout(function()
										{
											location.reload();
										}, 1500);
									}
									else 
									{
										msg = 'Username o password errati!';
										cls = 'error';
									}
								}
								
								upMsg.replaceChilds(msg).className = cls;
								
								upOk.styles({
									opacity: 1
								});
								
								upLoading.styles({
									display: 'none'
								});
							}
						});
					}
					return e.stop();
				};
				
				upOk.addEvent('click', lSubmit);
				I('upper_login_form').addEvent('keydown', function(e)
				{
					if (e.getKey().key == 'Enter') 
					{
						lSubmit.call(this, e);
					}
				});
				
			});
		}
	},
	closeUpperLogin: function()
	{
		if (I('upper_login').hasClass('open')) 
		{
			var uLogin = I('upper_login'), s = I('upper_login_box').style;
			
			I('upper_login').removeClass('open').morph({
				styles: {
					height: ['px', 'actual', 0]
				},
				effect: EE.outCubic,
				time: 0.6,
				stop: true
			});
			
			return true;
		}
		
		return false;
	},
	openCloseUpperLogin: function()
	{
		if (!Aols.closeUpperLogin()) 
		{
			window.scrollTo(0, 0);
			
			var uLogin = I('upper_login'), s = I('upper_login_box').style;
			
			I('upper_login').addClass('open').morph({
				styles: {
					height: ['px', 'actual', 150]
				},
				effect: EE.outCubic,
				onfinish: function()
				{
					I('upper_username').focus();
				},
				time: 0.6,
				stop: true
			});
		}
	},
	initHeadMenu: function()
	{
		// RICERCHE
		var searches = ['database', 'news', 'collections', 'forums'],
		searchInputs = [],
		searchBox,
		searchTimeout,
		lastSearch,
		searchFunc = function(event, range)
		{
			var key = event.getKey(), keyKey = key.key,
			value = this.value + '',
			resultsCnt = I('menu_search_results_box'),
			resultsList = I('menu_search_results_list'),
			loading = I('menu_' + range + '_search_loading');
			
			if (keyKey == 'Escape') 
			{
				this.value = value = '';
				lastSearch = null;
			}
			else if(keyKey.has('Cursor'))
			{
				return true;
			}
			
			if(lastSearch === range + value)
			{
				return;
			}
			
			clearTimeout(searchTimeout);
			resultsCnt.hide();
			resultsList.removeChilds();
			loading.hide();
			
			if (value.length > 2) 
			{
				// Ottengo i risultati
				searchTimeout = setTimeout(function()
				{
					Ajax.get({
						url: BASE_URL + '/?mdu=ajax/menu-search&range=' + range + '&q=' + encodeURIComponent(value),
						onRequest: function()
						{
							loading.show();
							resultsCnt.hide();
						},
						onFinish: function(t)
						{
							loading.hide();
							
							if ((t = JSON.decode(t))) 
							{
								if (t.success) 
								{
									var data = t.data;
									
									if (data.length) 
									{
										data.each(function(row)
										{
											resultsList.append(['a', {
												href: row.url,
												className: row.className,
												style: {
													borderRadius: '2px'
												},
												onmouseover: function()
												{
													$('#menu_search_results_list a.hover').each(function(a)
													{
														a.removeClass('hover');
													});
													
													this.addClass('hover');
												},
												onclick: function()
												{
													this.addClass('active');
												}
											}, row.title + ' ', ['span', row.subtitle || ' ']]);
										});
									}
									else
									{
										resultsList.append(['div', {
											className: 'no-one'
										}, 'Nessun Risultato']);
									}
									
									resultsCnt.show();
									
									lastSearch = range + value;
								}
							}
						}
					});
				}, 1000);
			}
		}, cursorFunc = function(event, range)
		{
			var key = event.getKey(), keyKey = key.key,
			activeRow = $('#menu_search_results_list a.hover')[0],
			lastRow, newRow;
			
			if (keyKey == 'Enter') 
			{
				if (activeRow) 
				{
					activeRow.addClass('active');
					document.location = activeRow.href;
				}
			}
			if (keyKey == 'CursorUp') 
			{
				if (activeRow) 
				{
					newRow = activeRow.prev();
				}
				
				lastRow = null;
					
				$('#menu_search_results_list a').each(function(a)
				{
					a.removeClass('hover');
					lastRow = a;
				});
				
				if(!newRow)
				{
					newRow = lastRow;
				}
				
				if (newRow) 
				{
					newRow.addClass('hover');
				}
			}
			else if (keyKey == 'CursorDown') 
			{
				if (activeRow) 
				{
					newRow = activeRow.next();
				}
				
				lastRow = null;
					
				$('#menu_search_results_list a').each(function(a)
				{
					a.removeClass('hover');
					if (!lastRow) 
					{
						lastRow = a;
					}
				});
				
				if(!newRow)
				{
					newRow = lastRow;
				}
				
				if (newRow) 
				{
					newRow.addClass('hover');
				}
				
			}
		};
		
		searches.each(function(range)
		{
			var search = 'menu_' + range + '_search';
			
			searchInputs.push(N(['input', {
				id: search + '_field',
				type: 'text',
				className: 'search',
				onkeyup: function(event)
				{
					searchFunc.call(this, event, range);
				},
				onkeydown: function(event)
				{
					cursorFunc.call(this, event, range);
				},
				autocomplete: 'off'
			}], search));
			
			N(['div', {
				id: search + '_loading',
				className: 'search-loading'
			}], search);
			
		});
		
		searchBox = N(['div', {
			id: 'menu_search_results_box'
		}, ['div', {
			className: 'head'
		}], ['div', {
			className: 'body',
			id: 'menu_search_results_list'
		}], ['div', {
			className: 'foot'
		}]], 'hidden_menu_container');
		
		if(Agent.engine != 'trident' && Agent.engine != 'presto')
		{
			searchBox.addEvent('clickout', function()
			{
				this.hide();
			});
		}
		
		
		// HIDDEN MENUS
		var menuCfg = {
			'menu_database': [],
			'menu_news': [],
			'menu_collections': [],
			'menu_mistery': [],
			'menu_forums': [],
			'menu_aols': []
		}, hiddenMenu = I('hidden_menu'), hideTimeout, showTimeout, hideFunc = function()
		{
			hiddenMenu.morph({
				styles: {
					height: ['px', ACTUAL, 0]
				},
				effect: EE.inCubic,
				time: 0.5,
				stop: true,
				onfinish: function() {
					changeMenu();
				}
			}).removeClass('open');
			
		}, activeMenu, changeMenu = function(id)
		{
			searchBox.hide();
			lastSearch = null;
			
			if (activeMenu) 
			{
				I('hidden_' + activeMenu).morph({
					styles: {
						top: ['px', ACTUAL, 370]
					},
					time: 0.5,
					effect: EE.outCubic,
					stop: true
				});
				
				I(activeMenu).removeClass('open');
			}
			
			if (id) 
			{
				I('hidden_' + id).morph({
					styles: {
						top: ['px', ACTUAL, 0]
					},
					time: 0.5,
					effect: EE.outCubic,
					stop: true
				});
				
				I(id).addClass('open');
			}
			
			activeMenu = id;
		};
		
		for(var i in menuCfg)
		{
			I('hidden_' + i).styles({
				top: '370px'
			});
		}
		
		$('#head_menu ul > li').each(function(menu)
		{
			var id = menu.id, a  = menu.byTag('a').shift(), hiddenMenuSection = I('hidden_' + id);
			
			menu.addEvent('mouseenter', function()
			{
				clearTimeout(hideTimeout);
				
				if (hiddenMenu.hasClass('open')) 
				{
					changeMenu(id);
				}
				else 
				{
					showTimeout = setTimeout(function()
					{
						hiddenMenu.morph({
							styles: {
								height: ['px', ACTUAL, 370]
							},
							effect: EE.outCubic,
							time: 0.5,
							stop: true
						}).addClass('open');
						
						changeMenu(id);
					}, 200);
				}
				
				this.addClass('hover');
				
			}).addEvent('mouseleave', function(e)
			{
				clearTimeout(showTimeout);
				hideTimeout = setTimeout(hideFunc, 500);
				
				this.removeClass('hover');
			});
		});
		
		hiddenMenu.addEvent('mouseenter', function()
		{
			if(!hiddenMenu.hasClass('open'))
			{
				hiddenMenu.morph({
					styles: {
						height: ['px', ACTUAL, 370]
					},
					effect: EE.outCubic,
					time: 0.5,
					stop: true
				}).addClass('open');
			}
			
			clearTimeout(hideTimeout);
			
		}).addEvent('mouseleave', function(e)
		{
			hideFunc();
		});
		
		if (I('menu_database_contrib_cnt')) 
		{
		
			var contribTypes = {
				manga: 'Manga',
				anime: 'Anime',
				toys: 'Toys',
				videogames: 'Videogames',
				jmusic: 'J-Music',
				comics: 'Comics'
			}, contribRanges = {
				collettibili: 'Collettibili',
				editori: 'Editori',
				autori: 'Autori',
				collane: 'Collane'
			}, select = ['select', {
				className: 'types',
				onchange: function()
				{
					var type = this.options[this.selectedIndex].value;
					
					if (!type) 
					{
						return;
					}
					
					$('#menu_database_contrib_cnt select.ranges, #menu_database_contrib_cnt select.actions').each(function(select)
					{
						select.hide();
					});
					
					if (I('menu_db_contrib_' + type)) 
					{
						I('menu_db_contrib_' + type).style.display = '';
					}
					else 
					{
						var subSelect = ['select', {
							id: 'menu_db_contrib_' + type,
							className: 'ranges',
							onchange: function()
							{
								var range = this.options[this.selectedIndex].value;
								
								$('#menu_database_contrib_cnt select.actions').each(function(select)
								{
									select.hide();
								});
								
								if (I('menu_db_contrib_' + type + range)) 
								{
									I('menu_db_contrib_' + type + range).style.display = '';
								}
								else 
								{
									var subSelect = ['select', {
										id: 'menu_db_contrib_' + type + range,
										className: 'actions',
										onchange: function()
										{
											var action = this.options[this.selectedIndex].value;
											document.location = BASE_URL + '/gestione/database/' + range + '-' + type + '/' + action + '/edit/';
										}
									}, ['option', {className: 'label'}, 'Che tipo di risorsa vuoi inserire?']];
									
									subSelect.push(['option', {
										value: 'scheda'
									}, 'Crea una scheda']);
									
									subSelect.push(['option', {
										value: 'recensione'
									}, 'Crea la tua recensione']);
									
									if (range == 'collettibili') 
									{
										subSelect.push(['option', {
											value: 'videorecensione'
										}, 'Aggiungi la tua Video Recensione']);
									}
									
									this.after(subSelect);
								}
								
							}
						}, ['option', {className: 'label'}, 'Seleziona una tipologia...']];
						
						for (var range in contribRanges) 
						{
							subSelect.push(['option', {
								value: range
							}, contribRanges[range]]);
						}
						
						this.after(subSelect);
					}
				}
			}, ['option', {className: 'label'}, 'Seleziona un database...']];
			
			for (var type in contribTypes) 
			{
				select.push(['option', {
					value: type
				}, contribTypes[type]]);
			}
			
			I('menu_database_contrib_cnt').append(select);
		}
	},
	initStatusBar: function()
	{
	
		if (I('logout_anchor')) 
		{
			I('logout_anchor').addEvent('click', function(e)
			{
				Ajax.get({
					url: BASE_URL + '/index.php?mdu=ajax/login&logout=true',
					onSuccess: function(t)
					{
						if ((t = JSON.decode(t))) 
						{
							if (!t.logged) 
							{
								location.reload();
							}
						}
					}
				});
				
				return e.stop();
			});
		}
		
		$('#sb_search input').each(function(o)
		{
			o.styles({
				borderRadius: '3px'
			});
		});
		
		
		var afb = I('add_to_friendlist');
		if (afb) 
		{
			afb.addEvent('click', function(e)
			{
				var usr = this.getAttribute('href').split('#')[1];
				
				Ajax.get({
					url: BASE_URL + '/index.php?mdu=ajax/panel/friendlist&action=add&user=' + usr,
					cache: true,
					onRequest: function()
					{
						ViewPort.open();
					},
					onSuccess: function(t)
					{
						var m = 'Un disguido ha impedito l\'inserimento...', c = 'error';
						if ((t = JSON.decode(t))) 
						{
							switch(t.result) {
								case 'success':
									
									m = 'Utente aggiunto con successo alla tua friendlist.';
									c = 'confirm';
									
								break;
								case 'myself':
									
									m = 'Non puoi aggiungere te stesso!';
								
								break;
								case 'notexists':
								
									m = 'L\'utente selezionato non esiste!';
									
								break;
								case 'unlogged':
								
									m = 'Devi essere registrato per creare la tua friendlist!';
								
								break;
							}
						}
						
						Aols.messageLayer(m, c);
					}
				});
				
				return e.stop();
			});
		}
		
		var atb = I('add_to_bookmarks');
		if (atb) 
		{
		
			atb.addEvent('click', function(e)
			{
				var tit = document.title, url = document.location, typ = 2;
				
				Ajax.get({
					url: BASE_URL + '/index.php?mdu=ajax/panel/bookmarks&action=add',
					post: {
						title: tit,
						url: url,
						type: typ
					},
					onRequest: function()
					{
						ViewPort.open();
					},
					onSuccess: function(t)
					{
						var m = 'Un disguido ha impedito l\'inserimento...', c = 'error';
						if ((t = JSON.decode(t))) 
						{
							switch(t.result) {
								case 'success':
									
									m = 'Bookmark aggiunto con successo!';
									c = 'confirm';
								
								break;
								case 'unlogged':
								
									m = 'Devi essere registrato per creare la tua friendlist!';
								
								break;
							}
						}
						
						Aols.messageLayer(m, c);
					}
				});
				
				return e.stop();
			});
			
		}
		
		
		var pnt = I('print_this');
		if (pnt) 
		{
			pnt.addEvent('click', function(e)
			{
				window.print();
				return e.stop();
			});
		}
		
		var soc = I('add_to_socials');
		if (soc) 
		{
			Require.module('socials', function()
			{
				Socials.init();
				soc.addEvent('click', function(e)
				{
					Socials.show();
					return e.stop();
				});
			});
		}
		
		var sre = I('subscribe_resource');
		if (sre) 
		{
			sre.addEvent('click', function(e)
			{
				var h = this.href.split('#')[1].split('-');
				
				Ajax.get({
					url: BASE_URL + '/index.php?mdu=ajax/panel/alerts&action=add',
					post: {
						range: h[0],
						id: h[1]
					},
					onRequest: function()
					{
						ViewPort.open();
					},
					onSuccess: function(t)
					{
						var m = 'Un disguido ha impedito la sottoscrizione...', c = 'error';
						
						if ((t = JSON.decode(t))) 
						{
							if (t.result == 'success') 
							{
								m = 'Sottoscrizione avvenuta con successo!';
								c = 'confirm';
							}
						}
						
						Aols.messageLayer(m, c);
					}
				});
				
				return e.stop();
			});
		}
		
		$('#sb_tools a, #sb_service a').each(function(a)
		{
			a.append(['strong', a.title]).title = '';
		});
		
		var slv = I('sb_level');
		if(slv)
		{
			var sbMenu = I('sb_menu'), height = parseInt(I('sb_menu_body').styles('height'), 10) + 20, closeSbMenu = function()
			{
				sbMenu.morph({
					styles: {
						height: ['px', ACTUAL, 0]
					},
					time: 0.5,
					effect: EE.outCubic,
					stop: true
				});
			}, openSbMenu = function()
			{
				if (!sbMenu.hasClass('open')) 
				{
					clearTimeout(sbMenuTimeout);
					sbMenu.morph({
						styles: {
							height: ['px', ACTUAL, height]
						},
						time: 0.5,
						effect: EE.outCubic,
						stop: true
					});
				}
			}, sbMenuTimeout;
			
			slv.addEvent('mouseenter', function(e)
			{
				openSbMenu();
			}).addEvent('mouseleave', function(e)
			{
				sbMenuTimeout = setTimeout(closeSbMenu, 100);
			});
			
			sbMenu.addEvent('mouseenter', function()
			{
				openSbMenu();
			}).addEvent('mouseleave', function(e)
			{
				sbMenuTimeout = setTimeout(closeSbMenu, 100);
			});
		}	
		
	},
	initUserbar: function()
	{
		var m = ['userbar_main'];
		
		m.each(function(c)
		{
			var a = I(c), b = I(c + '_menu'), bfc = $('div.menu', b)[0], rh = b.realHeight();
			
			if (Agent.engine == 'trident') 
			{
				b.styles({
					display: 'none'
				});
				a.addEvent('mouseenter', function(e)
				{
					if (!b.hasClass('open')) 
					{
						b.styles({
							display: 'block'
						});
						
						this.addClass('hover');
						
						b.addClass('open');
					}
				}).addEvent('mouseleave', function(e)
				{
					b.styles({
						display: 'none'
					});
					
					this.removeClass('hover');
					
					b.removeClass('open');
				});
			}
			else 
			{
				if (bfc) 
				{
					b.styles({
						overflow: 'hidden',
						display: 'block'
					});
					bfc.styles({
						marginTop: -rh + 'px'
					});
				}
				a.addEvent('mouseenter', function(e)
				{
					if (!b.hasClass('open')) 
					{
						if (bfc) 
						{
							bfc.morph({
								styles: {
									marginTop: ['px', ACTUAL, 0]
								},
								time: 0.5,
								effect: Effects.outCubic,
								stop: true
							});
						}
						
						this.addClass('hover');
						
						b.addClass('open');
					}
				}).addEvent('mouseleave', function(e)
				{
					if (bfc) 
					{
						bfc.morph({
							styles: {
								marginTop: ['px', ACTUAL, -rh]
							},
							time: 0.5,
							effect: Effects.outCubic,
							stop: true
						});
					}
					
					this.removeClass('hover');
					
					b.removeClass('open');
				});
			}
		});
	},
	// Contents Boxes
	box01: {
		closeOthers: function(b, a)
		{
			var d = $('div.body > div', b), i, l = d.length, c, t;
			
			for (i = 0; i < l; i++) 
			{
				c = d[i];
				if (c.style.display != 'none' && a !== c) 
				{
					t = c.id.replace(/^box_/, 'tab_');
					t.removeClass('open').styles('opacity', 0.5);
					Fx.blindUp({
						element: c,
						styles: {
							opacity: ['actual', 0],
							display: ['block', 'none']
						},
						time: 0.5
					});
					
				}
			}
		},
		init: function()
		{
			$('div.box-01').each(function(b)
			{
				var h = $('div.head ul li', b), f = false, p = $('div.body', b)[0];
				h.forEach(function(t)
				{
					var i = t.id.replace(/^tab_/, ''), j = 'box_' + i, c = I(j), s;
					if (!c) 
					{
						return;
					}
					
					s = c.style;
					
					Event.add(t.getElementsByTagName('a')[0], 'click', function(e)
					{
						if (s.display == 'none') 
						{
							Fx.blindDown({
								element: c,
								styles: {
									opacity: ['actual', 1],
									display: ['block']
								},
								time: 0.5
							});
							Aols.box01.closeOthers(b, c);
							t.addClass('open').styles('opacity', 1);
						}
						else 
						{
							Aols.box01.closeOthers(b);
						}
						e.stop();
						return false;
					});
					
					if (!f) 
					{
						I(t).addClass('open');
						f = true;
						return;
					}
					
					t.styles('opacity', 0.5);
					c.styles('opacity', 0).blindInit(true);
					s.display = 'none';
				});
			});
		}
	},
	initRatingButtons: function()
	{
		// Ottengo tutti i star rating
		$('div.star-rating').each(function(b)
		{
			// Ottengo tutti i bottoni per ogni star rating
			I(b).byTag('a').each(function(o)
			{
				I(o).addEvent('click', function(e)
				{
					var h = o.href;
					if (h) 
					{
						var p = h.split('#')[1].split('-'), m = p[0], i = p[1], v = p[2];
						if (m && i && v) 
						{
							Ajax.get({
								url: BASE_URL + '/index.php?mdu=ajax/votes/ratings',
								post: {
									range: m,
									id: i,
									vote: v
								},
								cache: true,
								onRequest: function()
								{
									ViewPort.open();
								},
								onSuccess: function(t)
								{
									var m = 'Un disguido ha impedito la votazione...', c = 'error', q, r, v;
									
									if ((t = JSON.decode(t))) 
									{
										switch(t.result) {
											case 'success':
											
												m = 'Grazie per aver votato!';
												c = 'confirm';
												r = t.rank;
												v = t.votes;
												
												q = I(b).byClass('current')[0];
												
												I(q).byTag('span')[0].replaceChilds((r / v).round(1) + ' su 5');
												
												q.style.width = ((r / v) * 100 / 5).round() + '%';
												
											break;
											case 'alredy':
										
												m = 'Hai già votato questa risorsa!';
											
											break;
											case 'unlogged':
										
												m = 'Devi essere registrato per votare!';
											
											break;
										}
									}
									
									Aols.messageLayer(m, c);
								}
							});
						}
					}
					
					return e.stop();
				});
			});
		});
	},
	initComments: function() {
		
		var f = I('comments_form');
		if (f) 
		{
			// Inizializzo gli stili
			var b = $('#comments_form div.body')[0], z = f.style;
			
			b.styles({
				overflow: 'hidden',
				height: '0px',
				position: 'relative'
			});
			
			f.styles({
				top: '5px'
			});
			
			I('box_comments').addEvent('mouseenter', function(e)
			{
				if (!f.hasClass('open')) 
				{
					f.addClass('open');
					b.blindDown({
						time: 0.6,
						stop: true,
						onchange: function(s)
						{
							if ((s = s.height)) 
							{
								z.marginTop = -(s.toInt() / 2) + 'px';
							}
						}
					});
				}
				return e.stop();
			}).addEvent('mouseleave', function(e)
			{
				f.removeClass('open');
				b.blindUp({
					time: 0.6,
					stop: true,
					onchange: function(s)
					{
						if ((s = s.height)) 
						{
							z.marginTop = -(s.toInt() / 2)  + 'px';
						}
					}
				});
				return e.stop();
			});
			
			// Inizializzo il posting
			var ran = V('comment_range'), cid = V('comment_content_id'), au = V('comment_author'), max = V('comment_max'), pf = function(e)
			{
				var tit = V('comment_title').trim(), com = V('comment_body');
				
				if(tit == 'Titolo (opzionale)')
				{
					tit = '';
				}
				
				if (!com) 
				{
					Aols.messageLayer('Non puoi inserire un commento vuoto!');
				}
				else 
				{
					Ajax.get({
						url: BASE_URL + '/index.php?mdu=ajax/comments&action=post',
						post: {
							range: ran,
							id: cid,
							title: tit,
							comment: com,
							author: au,
							max: max
						},
						onRequest: function()
						{
							ViewPort.open();
						},
						onSuccess: function(t)
						{
							var m = 'Un disguido ha impedito l\'inserimento...', c = 'error', q, s, d;
							if ((t = JSON.decode(t))) 
							{
								switch (t.result)
								{
									case 'success':
										
										I('comments_list').innerHTML = t.data;
										
										ViewPort.close();
										
										Aols.initCommentsList();
										
										var p = I('comment_' + t.id);
										
										if (p) 
										{
											p = p.position();
											
											Env.scroll({
												y: p[1] - 50,
												effect: EE.outCubic,
												time: 0.5
											});
										}
										
										return;
										
									case 'later':
									
										m = 'Devi aspettare un pò per inserire un altro commento!';
										
									break;
									case 'unlogged':
									
										m = 'Devi essere registrato per inserire il tuo commento!';
										
									break;
								}
							}
							Aols.messageLayer(m, c);
						}
					});
				}
				return e.stop();
			};
			I('comment_send').addEvent('click', pf);
			
			I('comment_title').addEvent('focus', function()
			{
				if(this.value.trim() == 'Titolo (opzionale)')
				{
					this.removeClass('optional');
					this.value = '';
				}
			}).addEvent('blur', function()
			{
				if(this.value === '')
				{
					this.addClass('optional');
					this.value = 'Titolo (opzionale)';
				}
			}).addClass('optional').value = 'Titolo (opzionale)';
			
			
			Require.module('wysiwyg', function()
			{
				new Wysiwyg({
					target: 'comment_body',
					height: 200,
					toolbars: [['bold', 'italic', 'foreColor', '|', 'link', 'unlink', 'insertImage']]
				});
			});
			
		}
		
		
		Aols.initCommentsList();
		
	},
	initCommentsList: function()
	{
		var i = 1;
		$('#comments div.comment, #comments div.no-comments').each(function(o)
		{
		
			o.styles({
				borderRadius: '5px'
			});
			
			
			setTimeout(function()
			{
				o.morph({
					styles: {
						borderColor: ['#333366', '#D6D6D6']
					}
				});
			}, (100 * i));
			
			i++;
		});
		
		
		$('#comments .comment.spam').each(function(o)
		{
			o.blindInit();
			
			var m = N(['div', {
				className: 'spam-message'
			}, 'Commento Nascosto: Clicca per aprire']);
			
			o.prepend(m).styles('height', '10px');
			
			m.addEvent('click', function()
			{
				o.morph({
					styles: {
						height: ['px', 10, o.realHeight() - 30]
					},
					effect: Effects.outBounce
				});
				m.blindUp({
					time: 0.3
				});
			});
		});
		
		// Inizializza i bottoni per votare i commenti
		$('#comments .points a').each(function(o)
		{
			I(o).addEvent('click', function(e)
			{
				var h = o.href;
				if (h) 
				{
					var p = h.split('#')[1].split('-'), m = p[0], v = p[1];
					if (m && v) 
					{
						Ajax.get({
							url: BASE_URL + '/index.php?mdu=ajax/votes/points&comment=' + m,
							post: {
								comment: m,
								vote: v
							},
							cache: true,
							onRequest: function()
							{
								ViewPort.open();
							},
							onSuccess: function(t)
							{
								var m = 'Un disguido ha impedito la votazione...', c = 'error', r;
								
								if ((t = JSON.decode(t))) 
								{
									switch (t.result)
									{
										case 'success':
											
											m = 'Grazie per aver votato!';
											c = 'confirm';
											
											I(o.parentNode).byTag('strong')[0].replaceChilds(t.points);
											
										break;
										case 'alredy':
											
											m = 'Hai già votato questo commento!';
											
										break;
										case 'unlogged':
											
											m = 'Devi essere registrato per votare!';
											
										break;
									}
								}
								
								Aols.messageLayer(m, c);
							}
						});
					}
				}
				return e.stop();
			});
		});
		
		$('#comments .comment .delete').each(function(o)
		{
			var p = I(o.parentNode), id = p.id;
			if (id) 
			{
				id = id.split('_')[1];
				o.addEvent('click', function(e)
				{
					if (confirm('Sei sicuro di voler cancellare questo commento?')) 
					{
						Ajax.get({
							url: BASE_URL + '/index.php?mdu=ajax/comments_admin&action=delete',
							post: {
								id: id
							},
							onSuccess: function(t)
							{
								if ((t = JSON.decode(t))) 
								{
									if (t.result == 'success') 
									{
										p.blindUp({
											styles: {
												opacity: [ACTUAL, 0],
												marginTop: ['px', ACTUAL, 0],
												marginBottom: ['px', ACTUAL, 0],
												paddingTop: ['px', ACTUAL, 0],
												paddingBottom: ['px', ACTUAL, 0]
											},
											onfinish: function()
											{
												p.remove();
											},
											time: 0.3
										});
										return;
									}
								}
								Aols.messageLayer('Un errore ha impedito al cancellazione', 'error');
							}
						});
						
					}
				});
			}
		});
		
		$('#comments .comment .ban').each(function(o)
		{
			var p = I(o.parentNode), id = p.id;
			if (id) 
			{
				id = id.split('_')[1];
				o.addEvent('click', function(e)
				{
					var unban = this.hasClass('unban');
					
					if (confirm((unban ? 'Sei sicuro di voler reincludere questo utente?' : 'Sei sicuro di voler escludere questo utente?'))) 
					{
						var m = 'Errore', c = 'error';
						
						Ajax.get({
							url: BASE_URL + '/index.php?mdu=ajax/contents_admin&action=' + (unban ? 'unban' : 'ban'),
							post: {
								id: id
							},
							onSuccess: function(t)
							{
								if ((t = JSON.decode(t))) 
								{
									if (t.result == 'success') 
									{
										
										m = unban ? 'Utente reinserito' : 'Utente escluso';
										
										setTimeout(function(){
											location.reload();
										}, 1500);
									}
								}
								Aols.messageLayer(m, c);
							}
						});
						
					}
				});
			}
		});
		
		$('#comments .comment .edit').each(function(o)
		{
			var p = I(o.parentNode), id = p.id;
			if (id) 
			{
				id = id.split('_')[1];
				o.addEvent('click', function(e)
				{
				
					Ajax.get({
						url: BASE_URL + '/index.php?mdu=ajax/comments&action=edit-get',
						post: {
							id: id
						},
						onSuccess: function(t)
						{
							if ((t = JSON.decode(t))) 
							{
								if (t.result == 'success') 
								{
									var body = $('div.body', p)[0], titleEl = $('h5', p)[0], data = t.data;
									
									if (titleEl) 
									{
										titleEl.hide();
									}
									
									var form = N(['form', ['div', {
										className: 'field-row'
									}, ['div', {
										className: 'field-data'
									}, ['input', {
										type: 'text',
										id: 'comment_edit_title_' + id,
										className: 'text',
										value: data.title
									}]]], ['div', {
										className: 'field-row'
									}, ['div', {
										className: 'field-data'
									}, ['textarea', {
										id: 'comment_edit_body_' + id,
										rows: 10
									}, data.comment]]], ['div', ['button', {
										onclick: function(e)
										{
											var title = V('comment_edit_title_' + id), comment = V('comment_edit_body_' + id);
											
											Ajax.get({
												url: BASE_URL + '/index.php?mdu=ajax/comments&action=edit-save',
												post: {
													id: id,
													title: title,
													comment: comment
												},
												onRequest: function()
												{
													ViewPort.open();
												},
												onSuccess: function(t)
												{
													if ((t = JSON.decode(t))) 
													{
														if (t.result == 'success') 
														{
														
															form.remove();
															if (titleEl) 
															{
																titleEl.replaceChilds(title).show();
															}
															else 
															{
																body.before(['h5', title]);
															}
															body.innerHTML = comment;
															body.show();
															
															o.show();
															
															ViewPort.close();
															
															return;
														}
													}
													Aols.messageLayer('Un disguido ha impedito il salvataggio...', 'error');
												}
											});
											
											return e.stop();
										}
									}, ['strong', ['span', 'Salva']]]]]);
									
									body.before(form).hide();
									
									Require.module('wysiwyg', function()
									{
										new Wysiwyg({
											target: 'comment_edit_body_' + id,
											height: 150,
											toolbars: [['bold', 'italic', 'foreColor', '|', 'link', 'unlink', 'insertImage']]
										});
									});
									
									return;
									
									
								}
							}
							Aols.messageLayer('Errore', 'error');
						}
					});
					
					this.hide();
				});
			}
		});
		
		// navigazione commenti
		$('#comments div.list-navigation a').each(function(a)
		{
			a.addEvent('click', function(e)
			{
				var data = (a.href + '').split('#')[1].split('-');
				Ajax.get({
					url: BASE_URL + '/index.php?mdu=ajax/comments&action=list',
					post: {
						range: data[0],
						id: data[1],
						max: data[2],
						page: data[3]
					},
					onFinish: function(t)
					{
						if ((t = JSON.decode(t))) 
						{
							if (t.result == 'success') 
							{
								I('comments_list').innerHTML = t.data;
								Aols.initCommentsList();
								var p = I('comments').position();
								
								Env.scroll({
									y: p[1] - 50,
									effect: EE.outCubic,
									time: 0.5
								});
							}
						}
					}
				});
				
				return e.stop();
			});
		});
		
	},
	mlTout: null,
	messageLayer: function(m, c){
		// Se necessario inizializzo il layer del messaggio
		if(!I('message_layer')) {
			APPS_LAYER.append(['div', {
				id: 'message_layer',
				style: {
					width: '350px',
					height: '90px',
					paddingTop: '110px',
					position: 'absolute',
					left: '0px',
					top: '0px',
					display: 'none'
				}
			}, ['div', {
				id: 'message_layer_content'
			}], ['div', {
				id: 'message_layer_button'
			}, ['button', {
				className: 'submit',
				onclick: function() {
					ViewPort.close();
				}
			}, ['span', ['em', 'Ok']]]]]);
			
			ViewPort.addCloseEvent(function() {
				clearTimeout(Aols.mlTout);
				var ml = I('message_layer');
				if (ml.style.display == 'block') {
					I('message_layer').morph({
						styles: {
							display: ['block', 'none'],
							top: ['px', ACTUAL, sc[1] - 200]
						},
						effect: EE.inCubic,
						time: 0.3,
						stop: true
					});
				}
			});
		}
		var ml = I('message_layer'), mlc = I('message_layer_content');
		ml.className = c || '';
		mlc.replaceChilds(m);
		var vc = Env.viewCenter([350, 200]), sc = Env.pageScroll();
		ViewPort.open();
		ml.styles({
			left: vc[0] + 'px'
		}).morph({
			styles: {
				display: ['block'],
				top: ['px', sc[1] - 200, vc[1]]
			},
			effect: Effects.outCubic,
			onfinish: function() {
				Aols.mlTout = ViewPort.close.timeout(3000);
			}
		});
	},
	/*messageLayer: function(m, c){
		var tout;
		// Se necessario inizializzo il layer del messaggio
		if(!I('message_layer')) {
			APPS_LAYER.append(['div', {
				id: 'message_layer',
				style: {
					width: '350px',
					height: '90px',
					paddingTop: '110px',
					position: 'absolute',
					left: '0px',
					top: '0px',
					display: 'none',
					opacity: 0
				}
			}, ['div', {
				id: 'message_layer_content'
			}], ['div', {
				id: 'message_layer_button'
			}, ['button', {
				className: 'submit',
				onclick: function() {
					ViewPort.close();
				}
			}, ['span', ['em', 'Ok']]]]]);
			
			ViewPort.addCloseEvent(function() {
				clearTimeout(tout);
				I('message_layer').morph({
					styles: {
						opacity: [ACTUAL, 0],
						display: ['block', 'none']
					},
					time: 0.3,
					stop: true
				});
			});
			
		}
		var ml = I('message_layer'), mlc = I('message_layer_content');
		ml.className = c || '';
		mlc.replaceChilds(m);
		var s = ml.styles({
			opacity: 0,
			display: 'block'
		}).sizes(), vc = Env.viewCenter(s), sc = Env.pageScroll();
		ViewPort.open();
		ml.morph({
			styles: {
				opacity: [ACTUAL, 0.8],
				left: ['px', vc[0], vc[0]],
				top: ['px', sc[1] - s[1], vc[1]]
			},
			effect: Effects.outElastic,
			onfinish: function() {
				tout = ViewPort.close.timeout(2000);
			}
		});
	},*/
	initSectionMenu: function()
	{
		if (I('twocol_rightcol')) 
		{
			var box = $('#twocol_rightcol .box'), last = null;
			
			if (box.length <= 1) 
			{
				return;
			}
			
			box.each(function(o)
			{
				if(o.hasClass('no-blind'))
				{
					return;
				}
				
				var b = I(o).byClass('body')[0].blindInit(true);
				
				o.styles('marginBottom', '10px');
				
				I(o).byTag('h3')[0].addEvent('mouseover', function()
				{
					var now = new Date();
					if (actualMenu != b && (!last || (now - last) > 350)) 
					{
						b.blindDown({
							stop: true,
							time: 0.5
						});
						
						if (actualMenu) 
						{
							actualMenu.blindUp({
								stop: true,
								time: 0.5
							});
						}
						
						actualMenu = b;
						last = now;
					}
				});
				
			});
		}
	},
	fixStyles: function()
	{
		if (Agent.engine != 'trident') 
		{
			$('button').each(function(o)
			{
				var t = parseInt(o.styles('top'), 10);
				if (t > 0) 
				{
					o.styles({
						top: (t - 3) + 'px'
					});
				}
				else 
				{
					o.styles({
						position: 'relative',
						top: '-3px'
					});
				}
			});
		}
		
		$('div.message-body').each(function(div)
		{
			div.styles({
				borderRadius: '5px'
			});
			I(div.parentNode).styles({
				borderRadius: '10px'
			});
		});
		
	},
	initEditions: function()
	{
		if (!I('editions')) 
		{
			return;
		}
		
		Require.module('widgets', function()
		{
			$('#editions a').each(function(o)
			{
				o.tooltip({
					title: o.title,
					content: 'Cliccando su questo banner lascerai anime otakuland per visitare un sito nostro partner.',
					position: 1,
					style: {
						background: 'url(' + TEMPLATE_URL + '/media/img/elements/tooltip.png) no-repeat top left',
						padding: '14px 9px 9px 9px',
						color: '#ffffff',
						fontFamily: 'Tahoma',
						fontSize: '9px',
						width: '212px',
						height: '60px'
					},
					offset: {
						x: 8,
						y: 0
					}
				});
			});
		});
	},
	initBookmarks: function()
	{
		APPS_LAYER.append(['div', {
			id: 'bookmarks_widget'
		}, ['div', {
			id: 'bookmarks_widget_button',
			onclick: function()
			{
				var w = I('bookmarks_widget_wrapper'), b = I('bookmarks_widget_contents');
				if (w.blindState() < 0.5) 
				{
					Ajax.get({
						url: BASE_URL + '/index.php?mdu=ajax/panel/bookmarks&action=list',
						onSuccess: function(t)
						{
							var m = 'Un disguido', c = 'error';
							if ((t = JSON.decode(t))) 
							{
								r = t.result;
								if (r == 'success') 
								{
									b.removeChilds();
									t.data.each(function(o)
									{
										b.append(['div', ['a', {
											href: o.url
										}, o.title]]);
									});
									w.show().blindDown();
								}
								else if (r == 'unlogged') 
								{
									m = 'Devi essere registrato per creare la tua friendlist!';
								}
							}
						}
					});
				}
				else 
				{
					w.blindUp();
				}
			}
		}], ['div', {
			id: 'bookmarks_widget_wrapper'
		}, ['h5', 'I mie pagine di Otakuland preferite'], ['div', {
			id: 'bookmarks_widget_contents'
		}]]]);
		
		I('bookmarks_widget_wrapper').blindInit(true);
	},
	initAdminTools: function()
	{
		if (I('content_admin_tools')) 
		{
			var range = V('at_content_range'), id = V('at_content_id'), tool;
			
			if ((tool = I('at_lock'))) 
			{
				tool.addEvent('click', function(e)
				{
					var closed = this.hasClass('open');
					
					if (confirm((closed ? 'Sicuro di voler riaprire questa discussione?' : 'Sicuro di voler chiudere questa discussione?'))) 
					{
						Ajax.get({
							url: BASE_URL + '/index.php?mdu=ajax/contents_admin&action=' + (closed ? 'open' : 'close'),
							post: {
								range: range,
								id: id
							},
							onRequest: function()
							{
								ViewPort.open();
							},
							onSuccess: function(t)
							{
								var m = 'Errore', c = 'error';
								
								if ((t = JSON.decode(t))) 
								{
									if (t.result == 'success') 
									{
										c = 'confirm';
										m = closed ? 'Discussione riaperta' : 'Discussione chiusa';
										setTimeout(function(){
											location.reload();
										}, 1500);
									}
								}
								
								Aols.messageLayer(m, c);
							}
						});
						
					}
				});
			}
			
			if ((tool = I('at_delete'))) 
			{
				tool.addEvent('click', function(e)
				{
					if (confirm('Sicuro di voler cancellare questa risorsa?')) 
					{
						Ajax.get({
							url: BASE_URL + '/index.php?mdu=ajax/contents_admin&action=delete',
							post: {
								range: range,
								id: id
							},
							onRequest: function()
							{
								ViewPort.open();
							},
							onSuccess: function(t)
							{
								var m = 'Errore', c = 'error';
								
								if ((t = JSON.decode(t))) 
								{
									if (t.result == 'success') 
									{
										c = 'confirm';
										m = 'Risorsa cancellata';
										setTimeout(function(){
											location.reload();
										}, 1500);
									}
								}
								
								Aols.messageLayer(m, c);
							}
						});
					}
				});
			}
			
			if ((tool = I('at_delete_comments'))) 
			{
				tool.addEvent('click', function(e)
				{
					if (confirm('Sicuro di voler cancellare i commenti di questa discussione?')) 
					{
						Ajax.get({
							url: BASE_URL + '/index.php?mdu=ajax/contents_admin&action=delete-comments',
							post: {
								range: range,
								id: id
							},
							onRequest: function()
							{
								ViewPort.open();
							},
							onSuccess: function(t)
							{
								var m = 'Errore', c = 'error';
								
								if ((t = JSON.decode(t))) 
								{
									if (t.result == 'success') 
									{
										c = 'confirm';
										m = 'Commenti cancellati';
										setTimeout(function(){
											location.reload();
										}, 1500);
									}
								}
								
								Aols.messageLayer(m, c);
							}
						});
						
					}
				});
			}
			
		}
		
	}
};
var actualMenu;

Event.onDomLoaded(function()
{
	ViewPort.animation = false;
	Aols.initLoginForm();
	Aols.initStatusBar();
	Aols.initHeadMenu();
	Aols.initSectionMenu();
	Aols.initRatingButtons();
	Aols.initComments();
	Aols.fixStyles();
	Aols.initEditions();
	Aols.initAdminTools();
	
	Require.module('lightshow', function()
	{
		LightShow.init();
	});
	
	if (I('aida_js_version')) 
	{
		I('aida_js_version').innerHTML = AIDAJS.version;
	}
});

Event.add(window, 'load', function()
{
	if (Agent.engine == 'trident' && Agent.version < 7) 
	{
		alert('Anime Otakuland Lifestream non supporta Microsoft Internet Explorer 6 e inferiori.\nTi consigliamo di aggiornare il tuo browser alla versione più recente, o di passare a browser più efficienti come Mozilla Firefox, Apple Safari o Opera.');
	}
});
