var butWidth=11; //amplada del butó de pujar
var butHeight=11; //alçada del butó de pujar
var sliderWidth=5; //amplada del slider
var sliderHeiht=11; //alçada del slider
var navpos = 0; // Distància dels controls del scroll respecte a la caixa de text.
// ----------------------------------------------



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="javascript:sc();" onmouseover="theScroll['+count+'].scrollNorth(\''+count+'\')" onmouseout="theScroll['+count+'].endScroll()" onclick="return false;"><img src="gfx/up.gif" width="11" height="11" border="0"></a></div><div class="slider-background" id="slider-background'+count+'"">&nbsp;</div><div class="dn"  id="dn'+count+'""><a href="javascript:sc();" onmouseover="theScroll['+count+'].scrollSouth(\''+count+'\')" onmouseout="theScroll['+count+'].endScroll()" onclick="return false;"><img src="gfx/down.gif" width="11" height="11"></a></div><div class="thumb" id="'+thumb+'"><img src="gfx/slider.gif" width="5" height="11"></div>';
		document.getElementById(root).innerHTML = buttons + document.getElementById(root).innerHTML;
		
		//crea els objectes
		theRoot[count]   = document.getElementById(root);
		theThumb[count]  = document.getElementById(thumb);
		var thisup = document.getElementById("up"+count);
		var thisdn = document.getElementById("dn"+count);
		var thisslider = document.getElementById("slider-background"+count);
		
		//Canvia el tamany de la capa Root i l'iguala al tamany de l'escroll
		theRoot[count].style.height= parseInt(minY+maxY+butHeight) + "px";
		theRoot[count].style.width= parseInt(minX+navpos+butWidth+1) + "px";
		
		//Posiciona els butons i el slider
		thisup.style.width = butWidth + "px";
		thisup.style.height = butHeight + "px";
		thisup.style.left = parseInt(minX+navpos) + "px";
		thisup.style.top = 0 + "px";
		
		theThumb[count].style.width = butWidth + "px";
		theThumb[count].style.height = sliderHeiht + "px";
		theThumb[count].style.left = parseInt(minX+navpos) + "px";
		theThumb[count].style.border =0;
		theThumb[count].style.top = parseInt(minY) + "px";
		
		thisdn.style.width = butWidth + "px";
		thisdn.style.height = butHeight + "px";
		thisdn.style.left = parseInt(minX+navpos) + "px";
		thisdn.style.top = parseInt(minY+maxY) + "px";
		
		thisslider.style.width = butWidth + "px";
		thisslider.style.height = parseInt(minY+maxY) + "px";
		thisslider.style.left = parseInt(minX+navpos) + "px";
		

		theScroll[count].load();

		//Drag.init(theHandle[count], theRoot[count]); //not draggable on screen
		Drag.init(theThumb[count], null, minX+navpos, maxX+navpos, 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() {
         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, butHeight+1, theScroll[i].clipH-butWidth-butHeight);
		}
		
		
	}
})

function sc(){

}

