

// ==============================================================
// 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 = [];
var scrollSave = []; var scrollBarSave = []; var scrollPageName = [];

function instantiateScroller(count, id, left, top, width, height, speed, reset, pagename) {
    if (!reset) { reset = "always"; }
    if (!sessvars.scroll) { sessvars.scroll = scrollSave; }
    if (!sessvars.scrollBar) { sessvars.scrollBar = scrollBarSave; }
    if (!sessvars.scrollPage) { sessvars.scrollPage = scrollPageName; }

    if (reset == "always" || (reset == "pagechange" && sessvars.scrollPage[count]!=pagename)) {
        sessvars.scrollBar[count] = 0;
        sessvars.scroll[count] = 0;
    }
    sessvars.scrollPage[count] = pagename;
    if (document.getElementById) {
	    theScroll[count] = new ypSimpleScroll(id, left, top, width, height, speed, sessvars.scroll[count]);
	}

}

function createDragger(count, minX, maxX, minY, maxY) {

        var thumbHeight = Math.round((maxY + 21) * (maxY - minY) / document.getElementById('scroll' + count + 'Content').offsetHeight);
        if (thumbHeight > (maxY - minY + 12)) {
            thumbHeight = maxY - minY + 12;
        }

        if (!sessvars.scroll) { sessvars.scroll = scrollSave; }
        if (!sessvars.scrollBar) { sessvars.scrollBar = scrollBarSave; }
        if (!sessvars.scrollPage) { sessvars.scrollPage = scrollPageName; }
		var buttons = '<div class="up" id="up'+count+'" style="margin-top:-3px"><a href="#" onmousedown="theScroll['+count+'].scrollNorth(\''+count+'\')" onmouseout="theScroll['+count+'].endScroll()" onclick="return false;"><img src="images/scrollup.jpg" width="12"></a></div><div class="dn"  id="dn'+count+'""><a href="#" onmousedown="theScroll['+count+'].scrollSouth(\''+count+'\')" onmouseout="theScroll['+count+'].endScroll()" onclick="return false;"><img src="images/scrolldw.jpg" width="12"></a></div><div class="scrollbar" id="sbar'+count+'" style="left: 135px; top: 12px;"><img src="images/scrollbar.jpg" width="12" height="'+thumbHeight+'"></div>';
		document.getElementById("smain" + count).innerHTML = buttons + document.getElementById("smain" + count).innerHTML;

		theRoot[count]   = document.getElementById("smain" + count);
		theThumb[count]  = document.getElementById("sbar" + 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;
		var startY;
		if (!sessvars.scrollBar[count] || sessvars.scrollBar[count] == 0) { startY = minY; }
		else { startY = sessvars.scrollBar[count]; }
		theThumb[count].style.top = parseInt(startY) + "px";
		theThumb[count].style.height = 200 + "px";   //Math.round((maxY + 24) * (maxY - minY) / document.getElementById(root).height);
		thisup.style.top = 2 + "px";
		thisdn.style.top = parseInt(minY+maxY+8) + "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 - thumbHeight + 15);
		
		// the number of pixels the thumb can travel vertically (max - min)
		thumbTravel[count] = (theThumb[count].maxY - theThumb[count].minY);
		if (thumbTravel[count] < (theThumb[count].maxY - theThumb[count].min)) {
		    thumbTravel[count] = (theThumb[count].maxY - theThumb[count].min);
		}
        
		// the ratio between scroller movement and thumbMovement
		ratio[count] = theScroll[count].scrollH / thumbTravel[count];

		theThumb[count].onDrag = function(x, y) {
		    var my_y = Math.round((y - theThumb[count].minY) * ratio[count]);
		    theScroll[count].jumpTo(null, my_y);
		    sessvars.scrollBar[count] = y;
		    sessvars.scroll[count] = my_y;
		}
}	

// 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);*/
               createDragger(i, -15, -15, 7, theScroll[i].clipH - 21);
           }
       }
   }) 
