/* 

	Easy Scroll v1.0
	written by Alen Grakalic, provided by Css Globe (cssglobe.com)
	please visit http://cssglobe.com/post/1495/easy-scroll-accessible-content-scroller
	
*/

this.easyscroll = function(){
	
	
		var horizontal  = horizontal;
	
	// id of the container element 
	var id = "contenuti";
	
	// navigation buttons text
	var nav = ["Up", "Down", "Reset"];
	
	//	id for each navigation button (OPTIONAL)
	var navId = ["btnUp", "btnDown", "btnReset"];

	// movement speed
	var speed = 5;
	
	// desired height of the container element (in pixels)
	var height = 385;
	
	//
	// END CONFIG
	// do not edit below this line (unless you want to of course :) )
	//
	
	

	

	var obj = document.getElementById(id);
	
	obj.up = false;
	obj.down = false;
	obj.fast = false;

	var container = document.createElement("div");
	var parent = obj.parentNode;
	
	
	

		
		
		
	
	container.id="scroll";
	parent.insertBefore(container,obj);
	parent.removeChild(obj);
	
	
//	$('scroll').setAttribute("class", 'strumento'); 
		

	container.style.position = "relative";
	container.style.height = height + "px";
	container.style.overflow = "hidden";
	obj.style.position = "absolute";
	obj.style.top = "0";
	obj.style.left = "0";
	container.appendChild(obj);
	
	
	
//	var scrollbar  = 'scorrbar';
	var scrollbar = document.createElement("div");
	scrollbar.id="scorrbar";
	
	var handle  = 'scorrbar';
	//var handle = document.createElement("div").injectInside(scrollbar);
	//handle.id="handle";
	

	
if ( $('scroll').getScrollSize().y - $('scroll').getSize().y > 0){


	var steps = (horizontal?($('scroll').getScrollSize().x - $('scroll').getSize().x):($('scroll').getScrollSize().y - $('scroll').getSize().y));
		
	var slider = new Slider(scrollbar, handle, {
	steps: steps,
	mode: (horizontal?'horizontal':'vertical'),
	onChange: function(step){
	// Scrolls the content element in x or y direction.
	//var x = (horizontal?(((container.getScrollSize().x-container.getSize().x)/100)*step):0);
	//var y = (horizontal?0:(((container.getScrollSize().x-container.getSize().y)/100)*step));
	var x = (horizontal?step:0);
	var y = (horizontal?0:step);

	
	$('scroll').scrollTo(x,y);


	}
	}).set(0);




	
	// Scroll the content element when the mousewheel is used within the
	// content or the scrollbar element.
	$$(container, scrollbar).addEvent('mousewheel', function(e){
	e = new Event(e).stop();
	var step = slider.step - e.wheel * 30;
	slider.set(step);
	
	

	
	
//	$('contenuti').style.top = $('contenuti').offsetTop < 0 ? 0 :  -slider.step + "px";
	
	
	if($('contenuti').offsetTop > 0) 
	$('contenuti').style.top = 0;
	else
	$('contenuti').style.top = -slider.step + "px";


	
	});



	
	
	
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop();   });
	


	
	
	
	var btns = new Array();
	var ul = document.createElement("div");
	ul.id="scorr";
	for (var i=0;i<nav.length;i++){
		var li = document.createElement("li");
		
		li.innerHTML = nav[i];
		li.id = navId[i];
		btns.push(li);
		ul.appendChild(li);
	};
	parent.insertBefore(ul,container);
	
	btns[0].onmouseover = function(){
		obj.up = true;
		this.className = "over";
	};
	btns[0].onmouseout = function(){
		obj.up = false;
		this.className = "";
	};		
	btns[1].onmouseover = function(){
		obj.down = true;
		this.className = "over";		
	};
	btns[1].onmouseout = function(){
		obj.down = false;
		this.className = "";
	};		
	btns[0].onmousedown = btns[1].onmousedown = function(){
		obj.fast = true;
	};	
	btns[0].onmouseup = btns[1].onmouseup = function(){
		obj.fast = false;
	};		
	btns[2].onmouseover = function(){ 		
		this.className = "over";
	};	
	btns[2].onmouseout = function(){ 		
		this.className = "";
	};		
	btns[2].onclick = function(){ 		
		obj.style.top = "0px";
	};	
	
	
	
	
	
	function isdefined(variable)
	{
	    return (typeof(window[variable]) == "undefined")?  false: true;
	}
	
	
	
	
	
	
	
	
		this.start = function(){				
			var newTop;
			var objHeight = obj.offsetHeight;
			var top = obj.offsetTop;
			
		

			
			var fast = (obj.fast) ? 2 : 1;
			
			
			if(obj.down){		 
				newTop = ((objHeight+top) > height) ? top-(speed*fast) : top;	
				obj.style.top = newTop + "px";
			};	
			
			
			if(obj.up){		 
				newTop = (top < 0 ) ? top+(speed*fast) : top;
				obj.style.top = newTop + "px";
			};
			
			
		};	
		obj.interval = setInterval("start()",50);		

	};
	
	
	

	
	
	
	
	

}	




	
		



//
// script initiates on page load. 
//


this.addEvent = function(obj,type,fn){
	if(obj.attachEvent){
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn](window.event );}
		obj.attachEvent('on'+type, obj[type+fn]);
	} else {
		obj.addEventListener(type,fn,false);
	};
};


addEvent(window,"load",easyscroll);
