

// ==============================================================
// 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);
	}
}

function createDragger(count, handler, root, thumb, minX, maxX, minY, maxY){
		var buttons = '<div class="up" id="up'+count+'"><a href="#" onMouseDown="theScroll['+count+'].scrollNorth(\''+count+'\')" onmouseout="theScroll['+count+'].endScroll()" onMouseUp="theScroll['+count+'].endScroll()" ><img src="up.gif"  width="15" height="15"></a></div><div class="dn"  id="dn'+count+'""><a href="#" onMouseDown="theScroll['+count+'].scrollSouth(\''+count+'\')" onmouseout="theScroll['+count+'].endScroll()" onMouseUp="theScroll['+count+'].endScroll()"><img src="dn.gif" width="15" height="15"></a></div><div class="thumb" id="'+thumb+'" style="left: 135px; top: 15px;"><img src="thumb.gif" width="15" height="15"></div>';
		document.getElementById(root).innerHTML = buttons + document.getElementById(root).innerHTML;

		theRoot[count]   = document.getElementById(root);
		theThumb[count]  = document.getElementById(thumb);
		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();

		//Drag.init(theHandle[count], theRoot[count]); //not draggable on screen
		Drag.init(theThumb[count], null, minX+15, maxX+15, minY, maxY);
		
		// the number of pixels the thumb can travel vertically (max - min)
		thumbTravel[count] = theThumb[count].maxY - theThumb[count].minY;

		// the ratio between scroller movement and thumbMovement
		ratio[count] = theScroll[count].scrollH / thumbTravel[count];

		theThumb[count].onDrag = function(x, y) {
			theScroll[count].jumpTo(null, Math.round((y - theThumb[count].minY) * ratio[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() {
         fn();
         old();
         }
      }
   }
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);
		}
	}
}) 




// ==============================================================
// ==============================================================
// ==============================================================// ==============================================================// ==============================================================
// ==============================================================// ==============================================================// ==============================================================
// ==============================================================
// ==============================================================// ==============================================================// ==============================================================// ==============================================================





/** This is high-level function.
 * It must react to delta being more/less than zero.
 */
var wheelScrolling = false;
var numWheels = 0;
function handle2(delta) {
	for (var i=0; i < theScroll.length; i++) {
		if (delta<0) {
			newY = Math.min(Math.max((theScroll[i].scrollTop + 25), 0), theScroll[i].scrollH)
			//theThumb[i].style.top = newY/ratio[i] + "px";
			theScroll[i].jumpTo(null, newY);
			theThumb[i].style.top = parseInt(((theThumb[i].maxY-theThumb[i].minY)*theScroll[i].scrollTop/theScroll[i].scrollH)+theThumb[i].minY) + "px";
		}
		else {
			newY = Math.min(Math.max((theScroll[i].scrollTop - 25), 0), theScroll[i].scrollH)
			theScroll[i].jumpTo(null, newY);
			theThumb[i].style.top = parseInt(((theThumb[i].maxY-theThumb[i].minY)*theScroll[i].scrollTop/theScroll[i].scrollH)+theThumb[i].minY) + "px";
		}
	}
}
/** Event handler for mouse wheel event.
 */
function wheel(event){
        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta) {
				handle2(delta);
		}
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
	event.returnValue = false;
}

/** Initialization code. 
 * If you use your own event management code, change it as required.
 */
if (window.addEventListener)
        /** DOMMouseScroll is for mozilla. */
        window.addEventListener('DOMMouseScroll', wheel, false);
/** IE/Opera. */
window.onmousewheel = document.onmousewheel = wheel;

