

function handleMouseWheel( event )
{
	var delta = 0;
	if ( !event ) 
		event = window.event;
	if ( event.wheelDelta ) 
	{
		delta = event.wheelDelta / 120; 
		if ( window.opera )
		{
			delta = -delta;
		}
	} else if (event.detail) 
	{
		delta = -event.detail/3;
	}
	if (delta)
	{
		var aScroll = document.theScroll[ 0 ];
		var aThumb = document.theThumb[ 0 ];
		aScroll.jumpTo( 0, aScroll.scrollTop - 10 * delta );
		//aScroll.scroll( 45, 5 );
		if( aScroll.scrollH > 0 )
		{
			aThumb.style.top = parseInt( ( ( aThumb.maxY-aThumb.minY) * aScroll.scrollTop / aScroll.scrollH ) + aThumb.minY) + "px"; //ok nomes down
		}
		else
		{
			aThumb.style.top = aThumb.minY + "px"; //ok nomes down
		}
		
		if (event.preventDefault)
		{
        	event.preventDefault();
		}
        event.returnValue = false;

		
	}
}


// ==============================================================
// HANDLES SCROLLER/S
// Modified from Aaron Boodman http://webapp.youngpup.net/?request=/components/ypSimpleScroll.xml
// mixed ypSimpleScroll with dom-drag script and allowed multiple scrolelrs through array instances
// (c)2004 Sergi Meseguer (http://zigotica.com/), 04/2004:
// ==============================================================
var theHandle = []; var theRoot = []; var theThumb = []; var theScroll = []; var thumbTravel = []; var ratio = [];




function instantiateScroller(count, id, left, top, width, height, speed){
	if(document.getElementById) {
		theScroll[count] = new ypSimpleScroll(id, left, top, width, height, speed);
		if( document.theScroll == undefined || document.theScroll == null )
		{
			document.theScroll = new Array();
			document.theThumb = new Array();
		}
		//now add the scroll to the doc too...
		document.theScroll[ count ] = theScroll[ count ];
		
		//and make sure it can wheel!
		/* Initialization code. */
		if (window.addEventListener)
		{
			window.addEventListener('DOMMouseScroll', handleMouseWheel, false);
		}
		window.onmousewheel = document.onmousewheel = handleMouseWheel;

	}
}


function createDragger(count, handler, root, thumb, minX, maxX, minY, maxY){
		var buttons = '<div class="up" id="up'+count+'"><a href="#" onmouseover="theScroll['+count+'].scrollNorth(\''+count+'\')" onmouseout="theScroll['+count+'].endScroll()" onclick="return false;"><img src="up.png" width="15" height="30"></a></div><div class="dn"  id="dn'+count+'""><a href="#" onmouseover="theScroll['+count+'].scrollSouth(\''+count+'\')" onmouseout="theScroll['+count+'].endScroll()" onclick="return false;"><img src="dn.png" width="15" height="30"></a></div><div class="thumb" id="'+thumb+'" style="left: 135px; top: 15px;"><img src="thumb.png" width="15" height="30"></div>';
		document.getElementById(root).innerHTML = buttons + document.getElementById(root).innerHTML;

		theScroll[ count ].initMinX = minX;
		theScroll[ count ].initMaxX = maxX;
		theScroll[ count ].initMinY = minY;
		theScroll[ count ].initMaxY = maxY;

		theRoot[count]   = document.getElementById(root);
		theThumb[count]  = document.getElementById(thumb);
		document.theThumb[ count ] = theThumb[ count ];
		
		var thisup = document.getElementById("up"+count);
		var thisdn = document.getElementById("dn"+count);
		theThumb[count].style.left = parseInt(minX+15) + "px";
		thisup.style.left = parseInt(minX+15) + "px";
		thisdn.style.left = parseInt(minX+15) + "px";
		theThumb[count].style.border =0;
		theThumb[count].style.top = parseInt(minY) + "px";
		thisup.style.top = 0 + "px";
		thisdn.style.top = parseInt(minY+maxY) + "px";
		//thisdn.style.top = 15 + "px";

		theScroll[count].load();
		
		theThumb[ count ].adjustToNewDoc = function( inScrollIndex )
		{
			var aScroll = theScroll[ inScrollIndex ];
			//Drag.init(theHandle[count], theRoot[count]); //not draggable on screen
			Drag.init(theThumb[ inScrollIndex ], null, aScroll.initMinX + 15, aScroll.initMaxX + 15, aScroll.initMinY, aScroll.initMaxY );
			
			// the number of pixels the thumb can travel vertically (max - min)
			thumbTravel[ inScrollIndex ] = theThumb[ inScrollIndex ].maxY - theThumb[ inScrollIndex ].minY;
		
			// the ratio between scroller movement and thumbMovement
			ratio[ inScrollIndex ] = aScroll.scrollH / thumbTravel[ inScrollIndex ];
			
			
			theThumb[ inScrollIndex ].onDrag = function(x, y) {
				theScroll[ inScrollIndex ].jumpTo(null, Math.round((y - theThumb[ inScrollIndex ].minY) * ratio[ inScrollIndex ]));
			}
		}

		
		theThumb[ count ].adjustToNewDoc( count );
		
		
		
}	

// INITIALIZER:
// ==============================================================
// ala Simon Willison http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(fn) 
{
	var old = window.onload;
	if (typeof window.onload != 'function') 
	{
		window.onload = fn;
	}
	else 
	{
		window.onload = function() 
		{
			old();
			fn();
		}
	}
}

addLoadEvent( 
	function()
	{
		if( theScroll.length>0 ) 
		{
			for( var i = 0; i < theScroll.length; i++ )
			{
				createDragger( i, "handle" + i, "root" + i, "thumb" + i, theScroll[ i ].clipW, theScroll[ i ].clipW, 15, theScroll[ i ].clipH - 30 );
			}
		}
	}
) 