var mouseY;

function onmousewheel(element, callback) {
		
	// @author	Andrea Giammarchi		[http://www.devpro.it/]
	// @license	MIT 				[http://www.opensource.org/licenses/mit-license.php]
	// @credits	Adomas Paltanavicius 		[http://adomas.org/javascript-mouse-wheel/]
	
	function __onwheel(event) {
		var	delta = 0;
		if(event.wheelDelta) {
			delta = event.wheelDelta / 120;
			// if(window.opera)
				// delta = -delta;
		}
		else if(event.detail)
			delta = -event.detail / 3;
		if(delta)
			callback.call(element, delta);
		if(event.preventDefault)
			event.preventDefault();
		event.returnValue = false;
		return false;
	};
	
	if(element.addEventListener && !window.opera)
		element.addEventListener("DOMMouseScroll", __onwheel, false);
	else
		element.onmousewheel = (function(base){return function(evt){
			if(!evt) evt = window.event;
			if(base) base.call(element, evt);
			return __onwheel(evt);
		}})(element.onmousewheel);
};

function VScroll(divId, scrollbarUpId, scrollbarDownId, scrollbarAreaId, scrollbarScrollerId) {

	var __this = this;
	var pxToScroll = 50;
	var div = document.getElementById(divId);
	var contentDiv;
	var scrollbarUp;
	var scrollbarDown;	
	var scrollbarArea;	
	var scrollbarScroller;
	var ratio;
	var maxScroll;
	var scrollbarTop = 0;
	var scrollDownMouseDown = false;
	var scrollDownTimer = null;
	var scrollUpMouseDown = false;
	var scrollUpTimer = null;
	var scrollbarMouseDown = false;
	var scrollbarTimer = null;
	var prevMouseY = 0;
	
	// Metodo pubblico per il disegno della scrollbar
	this.designScrollbar = function() {
		
		if (contentDiv.scrollHeight > contentDiv.offsetHeight) {
			
			scrollbarScroller.style.visibility = "visible";
			
			// scrollbarScrollerHeight : scrollbarAreaHeight = contentDiv.offsetHeight : contentDiv.scrollHeight
			var scrollbarScrollerHeight = Math.ceil((scrollbarArea.offsetHeight * contentDiv.offsetHeight) / contentDiv.scrollHeight);
			scrollbarScroller.style.fontSize = "1px";
			scrollbarScroller.style.lineHeight = "1px";
			scrollbarScroller.style.height = scrollbarScrollerHeight + "px";
			
			// ratio: rapporto tra i pixel di spostamento della barra e quelli del div
			ratio = (contentDiv.scrollHeight / scrollbarArea.offsetHeight);
			
			// maxScroll - massimo margin-top della barra
			maxScroll = scrollbarArea.offsetHeight - scrollbarScroller.offsetHeight;
			
			// Altezza della barra - contentDiv.scollTop / ratio;
			scrollbarTop = contentDiv.scrollTop / ratio;
			scrollbarScroller.style.marginTop = scrollbarTop + "px";
			
		} else
		
			scrollbarScroller.style.visibility = "hidden";
		
	}
	
	// Metodo privato per far scrollare il div
	function scroll(offset) {
		
		// console.log(scrollbarTop);
		
		if (scrollbarTop == maxScroll)
			contentDiv.scrollTop = contentDiv.scrollHeight - contentDiv.offsetHeight;
		else
			contentDiv.scrollTop = (scrollbarTop + offset) * ratio;
		
		if ( (scrollbarTop + offset) < 0 )
			scrollbarTop = 0;
		else if ( (scrollbarTop + offset) > maxScroll )
			scrollbarTop = maxScroll;
			else
				scrollbarTop = scrollbarTop + offset;
		
		scrollbarScroller.style.top = scrollbarTop + "px";
		
	}
	
	// Metodo privato per lo scroll del contenuto
	function contentScroll(offset) {
		
		contentDiv.scrollTop = contentDiv.scrollTop + offset;
		scrollbarTop = contentDiv.scrollTop / ratio;
		if (scrollbarTop > maxScroll)
			scrollbarTop = maxScroll;
		scrollbarScroller.style.top = scrollbarTop + "px";
		
		// Se il mouse è ancora cliccato sulla freccia sù, richiamo il metodo
		
		if (scrollUpMouseDown && offset < 0)
			scrollUpTimer = setTimeout( function() { contentScroll(offset) }, 100 );
			
		if (scrollDownMouseDown && offset > 0)
			scrollDownTimer = setTimeout( function() { contentScroll(offset) }, 100 );
		
	}
	
	function scrollerMove() {
		
		if (prevMouseY != 0) {
			
			scroll(mouseY - prevMouseY);
			
		}
		
		prevMouseY = mouseY;
		
		if (scrollbarMouseDown)
			scrollbarTimer = setTimeout( function() { scrollerMove(); }, 50 );
		
	}
	
	function startScrollerMoving() {
		
		scrollbarMouseDown = true;
		
		// Blocca la selezione del testo
		div.onselectstart = function() { return false; }
		div.onmousedown = function() { return false; }
		
		// Scrolla la barra
		scrollerMove();
		
	}
	
	function stopScrollerMoving() {
		
		scrollbarMouseDown = false;
		clearTimeout(scrollbarTimer);
		setTimeout( function() { prevMouseY = 0; }, 200);
		
		// Sblocca la sezione del testo
		div.onselectstart = function() { return true; }
		div.onmousedown = function() { return true; }
		
	}
	
	function buildScrollbar() {

		// tolgo scrollbar
		div.style.overflow				=	"hidden";
		
		// position relative per utilizzare il posizionamento assoluto all'interno della cornice
		div.style.position				=	"relative";
		
		// creo il markup
		div.innerHTML					= 	'<div id="' + divId + '_content">' + div.innerHTML + '</div>' +
											'<div id="' + scrollbarUpId + '"></div>' + 
											'<div id="' + scrollbarAreaId + '">' +
											'	<div id="' + scrollbarScrollerId + '"></div>' +
											'</div>' + 
											'<div id="' + scrollbarDownId + '"></div>';
		
		contentDiv						= document.getElementById(divId + "_content");
		scrollbarUp						= document.getElementById(scrollbarUpId);
		scrollbarDown					= document.getElementById(scrollbarDownId);
		scrollbarArea					= document.getElementById(scrollbarAreaId);
		scrollbarScroller				= document.getElementById(scrollbarScrollerId);
		
		// Aggiungo l'evento per la rotellina del mouse
		onmousewheel(div, function(delta){ contentScroll( - delta * pxToScroll ); });
		
		// Associo gli eventi alla pressione e rilascio del mouse sulle frecce
		scrollbarUp.onmousedown			= function() { scrollUpMouseDown = true; contentScroll(-pxToScroll); }
		scrollbarUp.onmouseup			= function() { scrollUpMouseDown = false; clearTimeout(scrollUpTimer); }
		scrollbarUp.onmouseout			= function() { scrollUpMouseDown = false; clearTimeout(scrollUpTimer); }
		scrollbarDown.onmousedown		= function() { scrollDownMouseDown = true; contentScroll(pxToScroll); }
		scrollbarDown.onmouseup			= function() { scrollDownMouseDown = false; clearTimeout(scrollDownTimer); }
		scrollbarDown.onmouseout		= function() { scrollDownMouseDown = false; clearTimeout(scrollDownTimer); }
		
		// Associo gli eventi alla pressione e rilascio del mouse sulla scrollbar
		scrollbarScroller.onmousedown	= startScrollerMoving;
		// document.getElementsByTagName("body")[0].onmouseup	= stopScrollerMoving;
		addSimpleEvent(document.getElementsByTagName("body")[0], "onmouseup", stopScrollerMoving);

		// posiziono pulsante up
		scrollbarUp.style.position		=	"absolute";
		scrollbarUp.style.top			=	"0px";
		scrollbarUp.style.left			=	(div.offsetWidth - scrollbarUp.offsetWidth) + "px";

		// posiziono pulsante down
		scrollbarDown.style.position	=	"absolute";
		scrollbarDown.style.top			=	(div.offsetHeight - scrollbarDown.offsetHeight) + "px";
		scrollbarDown.style.left		=	(div.offsetWidth - scrollbarUp.offsetWidth) + "px";
		
		// posiziono la scrollbararea
		scrollbarArea.style.position	=	"absolute";
		scrollbarArea.style.left		=	(div.offsetWidth - scrollbarUp.offsetWidth) + "px";
		scrollbarArea.style.top			=	scrollbarUp.offsetHeight + "px";
		scrollbarArea.style.height		=	(div.offsetHeight - scrollbarUp.offsetHeight - scrollbarDown.offsetHeight) + "px";
		
		// posiziono il container del testo
		contentDiv.style.overflow		=	"hidden";
		contentDiv.style.height			=	document.getElementById(divId).offsetHeight + "px";
		contentDiv.style.marginRight	=	document.getElementById(scrollbarAreaId).offsetWidth + "px";
		
		// Disegno la scrollbar
		__this.designScrollbar();
		
		// Aggiungo l'evento per la posizione del mouse
		document.onmousemove = mouseMove;
		
	}

	//__construct
	buildScrollbar();
}

function mouseMove(ev) {
	
	ev = ev || window.event;
	mouseY = getMouseY(ev);
	// console.log(mouseY);
	
}

function getMouseY(ev) {
	
	if (ev.pageY) {
		return ev.pageY;
	}
	
	return ev.clientY + document.body.scrollTop  - document.body.clientTop;
	
}

// funzione cross browser per l'aggiunta di eventi
function addSimpleEvent(obj, type, callback) {
    obj[type] = (function(base){           
        return function(evt){           
            if(!evt)evt=window.event;
            if(base)base.call(this,evt);
            callback.call(this,evt);
        }
    })(obj[type])
};