// JavaScript Document
function makeScrollable(selector) {
	/*
	Impostare overflow:auto ai div coinvolti
	farà funzionare il tutto anche senza javascript!
	
	Aggiungere i seguenti stili css:
		.sliderCont {
			// Contenitore di slider + frecce
			float: right // posizione dello slider
		}
		.frSu, .frGiu {
			// frecce
		}
		.slider {
			// slider
		}
		.knob {
			// pomello che si muove
		}
	*/
	$$(selector).each(function(el) {
		el.setStyle('overflow','hidden');
		
		var scrollCorpo;
		var inScroll = false;
		var currPos = 0;
		var mySlider;
		
		var div = new Element('div');
		var cont = new Element('div',{
			'class': 'scrollContainer'
		});
		
		var sliderCont = new Element('div',{
			'class': 'sliderCont'
		});
		var frSu = new Element('a',{
			'class': 'frSu',
			'href': '#',
			'events': {
				'click': function(e) {
					e.stop();
					currPos -=10;
					if (currPos < 0)
						currPos = 0;
					mySlider.set(currPos);
				}
			}
		});
		var frGiu = new Element('a',{
			'class': 'frGiu',
			'href': '#',
			'events': {
				'click': function(e) {
					e.stop();
					currPos +=10;
					if (currPos > (altCon - altFin))
						currPos = altCon - altFin;
					mySlider.set(currPos);
				}
			}
		});
		var slider = new Element('div',{
			'class': 'slider'
		});
		var knob = new Element('div',{
			'class': 'knob'
		});
		knob.inject(slider);
		
		frSu.inject(sliderCont);
		slider.inject(sliderCont);
		frGiu.inject(sliderCont);
		
		div.setStyles({
			'width': 'auto',			  
			'height': el.getStyle('height'),
			'overflow': 'hidden'
		});
		div.wraps(el);
		
		var frH = parseInt(frSu.getStyle('height')) + parseInt(frGiu.getStyle('height'));
		frH += parseInt(frSu.getStyle('margin-top')) + parseInt(frGiu.getStyle('margin-top'));
		frH += parseInt(frSu.getStyle('padding-top')) + parseInt(frGiu.getStyle('padding-top'));
		frH += parseInt(frSu.getStyle('border-top-width')) + parseInt(frGiu.getStyle('border-top-width'));
		frH += parseInt(frSu.getStyle('margin-bottom')) + parseInt(frGiu.getStyle('margin-bottom'));
		frH += parseInt(frSu.getStyle('padding-bottom')) + parseInt(frGiu.getStyle('padding-bottom'));
		frH += parseInt(frSu.getStyle('border-bottom-width')) + parseInt(frGiu.getStyle('border-bottom-width'));
		
		slider.setStyle('height', (el.getCoordinates().height - frH)+'px');
		
		el.setStyles({
			'width': 'auto',
			'height': 'auto',
			'overflow': 'visible'
		});
		
		var altFin = div.getCoordinates().height;
		var altCon = el.getCoordinates().height;
		
		if (altCon > altFin) {
			cont.wraps(div);
			sliderCont.inject(cont,'top');
			scrollCorpo = new Fx.Scroll(div, {
									link: 'cancel'
								});
	
			scrollCorpo.addEvent('start', function() { 
									inScroll = true;
								});
	
			scrollCorpo.addEvent('complete', function() { 
									inScroll = false;
									mySlider.set(currPos);
								});
	
			mySlider = new Slider(slider, knob, {
				onChange: function(pos){
						scrollCorpo.set(0,pos);
						currPos = pos;
					},
				mode: 'vertical',
				steps: altCon - altFin
			});
			
			cont.addEvent('mousewheel', function(e) {
				e.stop();
	
				if (e.wheel < 0) {
					currPos +=10;
					if (currPos > (altCon - altFin))
						currPos = altCon - altFin;
					mySlider.set(currPos);
				}
				if (e.wheel > 0) {
					currPos -=10;
					if (currPos < 0)
						currPos = 0;
					mySlider.set(currPos);
				}
			});
		}
	});
}

	var pufApri;
	
	var apriFin = function (lnk) {
		var u, t, l, w, h;
		
		w = Math.round(screen.height*2/3);
		h = Math.round(screen.height*2/3);
		l = Math.round((screen.width - w)/2);
		t = Math.round((screen.height - h)/2);
		u = lnk.href;

		if (u != "")
			pufApri = window.open(u,"Immgagine","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width="+w+",height="+h+",top="+t+",left="+l+"");
		
		return false;
		
	}
	
	function rollover(img, src) {
		$(img).set('src', src);
	}

	function addQSLink(key, value) {
		var qst = location.search.substr(1);
		var dati = new Array();
		if (qst.length > 0) {
			dati=qst.split("&");
		}
		var l = "?";
		for (i=0; i < dati.length; i++) {
			if (dati[i].substr(0,key.length) != key)
				l += dati[i]+"&";
		}
		l += key + "=" + value;
		return l;
	}
	function addQS(key, value) {
		location = addQSLink(key, value);
	}
	
	function simplePreload()
	{ 
	  var args = simplePreload.arguments;
	  document.imageArray = new Array(args.length);
	  for(var i=0; i<args.length; i++)
	  {
		document.imageArray[i] = new Image;
		document.imageArray[i].src = args[i];
	  }
	}


