var Utils = {
	Image: {
		/*In base al thumb selezionato, rende opachi gli altri thumb e visualizza l'immagine grande*/
		showFromThumb: function(thumb, bigImagePath, bigImageElementId){
			$A(document.getElementsByClassName('clear')).each(function(element){
				element.className = 'opaque';
			});
			thumb.className = 'clear';
			$(bigImageElementId).src = bigImagePath;
		},
		
		switchImage: function(toActivate, bigImageElementId, bigImagePath){
			$(bigImageElementId).src = bigImagePath;
			$A(toActivate.parentNode.getElementsByTagName('a')).each(function(element){
				$(element).removeClassName('active');
			});
			toActivate.addClassName('active');
		},
		
		Slider: {
			loop: function(indexStart, indexEnd, prefixId){
				new Effect.Parallel([
					new Effect.Fade(prefixId + indexStart, {duration: 6}),
					new Effect.Appear(prefixId + (indexStart + 1) % (indexEnd + 1), {duration: 6})
				],{
					duration: 6,
					afterFinish: function(){
						Utils.Image.Slider.loop((indexStart + 1) % (indexEnd + 1), indexEnd, prefixId);
					}
				});
			}
		},
		
		ListImageZoomer: {
			
			showZoomed: function(imagesContainer, imgElement, zoomWidthValue){
				$$('#' + imagesContainer + ' img').each(function(element){
					if(element.src != imgElement.src){
						
						if(element.hasClassName('zoomed')){
							Utils.Image.ListImageZoomer.resetZoomed(element, zoomWidthValue);
						} else if(!element.hasClassName('opaque')){
							Utils.Image.ListImageZoomer.reduceNotZoomed(element);
						}
					}
				});
				
				if(!imgElement.hasClassName('zoomed')){
					this.zoomIn(imgElement, zoomWidthValue);
				}
			},
			
			resetAll: function(imagesContainer, zoomWidthValue){
				$$('#' + imagesContainer + ' img').each(function(element){
					
					if(element.hasClassName('zoomed')){
						element.removeClassName('zoomed');
						element.width -= zoomWidthValue;
						element.style.left = '0';
						if(!element.hasClassName('nomargin')){
							element.style.marginRight = zoomWidthValue/2 + 'px';
						}
					} else if(element.hasClassName('opaque')){
						element.removeClassName('opaque');
						element.width += 2;
					}
					
				});
				$('didascalia').innerHTML = '';
			},
			
			zoomIn: function(toZoom, zoomWidthValue){
					if(toZoom.hasClassName('opaque')){
						toZoom.removeClassName('opaque');
						toZoom.width = toZoom.width + 2;
					}
					
					toZoom.addClassName('zoomed');
					toZoom.style.left = - (zoomWidthValue/2) + 'px';
					toZoom.style.marginRight = - (zoomWidthValue/2) + 'px';;
					toZoom.width += zoomWidthValue;
					
					if(toZoom.hasClassName('nomargin')){
						$('didascalia').style.left = Position.positionedOffset(toZoom)[0] - ($('didascalia').getWidth() - toZoom.getWidth()) + "px";
					}
					else{
						$('didascalia').style.left = Position.positionedOffset(toZoom)[0] + "px";
					}
					
					$('didascalia').innerHTML = toZoom.next('div').innerHTML;
					$$('#didascalia img').each(function(element){
						element.removeClassName('opaque');
					});
					
			},
			
			resetZoomed: function(zoomedImage, zoomWidthValue){
				zoomedImage.removeClassName('zoomed');
				zoomedImage.addClassName('opaque');
				zoomedImage.width -= zoomWidthValue + 2;
				zoomedImage.style.left = '0';
				if(!zoomedImage.hasClassName('nomargin')){
					zoomedImage.style.marginRight = zoomWidthValue/2 + 'px';
				}
			},
			
			reduceNotZoomed: function(imgElement){
				imgElement.addClassName('opaque');
				imgElement.width -= 2;
			}
		}
	},
	
	Form: {
		ajaxSubmit: function(formId, idContainerToUpdate){
			new Ajax.Updater(idContainerToUpdate, $(formId).action, {
				method: 'post',
				parameters: $(formId).serialize(),
				evalScripts: true,
				onComplete: function(){
					if(!$(idContainerToUpdate).visible()){
						$(idContainerToUpdate).show();
					}
				}
			});
		}
	},
	
	openPopup: function(windowUrl, windowName, windowWidth, windowHeight, scrollBars){
		var scrollbars = 'no';
		if(scrollBars != null && scrollBars == 'yes'){
			scrollbars = 'yes';
		}
		popupWindow = window.open(windowUrl, windowName, "width=" + windowWidth + ",height=" + windowHeight + ",toolbar=no,location=no,directories=no,menubar=no,scrollbars="+scrollbars+",resizable=yes");
	},
	
	showSimpleWindow: function(simpleWindowId, simpleWindowContentElementClass, windowContentContainer, notDraggable){
		$(simpleWindowId).getElementsByClassName(simpleWindowContentElementClass)[0].innerHTML = windowContentContainer.innerHTML;
		
		var leftMargin = ( (document.viewport.getWidth() - 970) / 2 ) > 0 ? (document.viewport.getWidth() - 970) / 2 : 0;
		$(simpleWindowId).setStyle({
			left: leftMargin + 'px'
		});
		
		if(notDraggable == null){
			$(simpleWindowId).show();
			new Draggable('otpopup');
		} else{
			new Effect.Appear(simpleWindowId);
		}
	},
	
	showAjaxWindow: function(simpleWindowId, simpleWindowContentElementClass, url){
		new Ajax.Updater($(simpleWindowId).getElementsByClassName(simpleWindowContentElementClass)[0], url, {
			onComplete: function(){
				$(simpleWindowId).show();
			}
		});
	}
};

