// ==============================================================
// 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 bt_w = 17;
		var bt_h = 26;
		var bt_thumb_w = 15;
		var bt_thumb_h = 15;
		
		
		var buttons = '<div class="up" id="up'+count+'"><a href="javascript:void(0)" onmouseover="theScroll['+count+'].scrollNorth(\''+count+'\')" onmouseout="theScroll['+count+'].endScroll()" onclick="return false;"><img src="/img/scroll/up.gif" width="'+bt_w+'" height="'+bt_h+'"></a></div><div class="thumb" id="'+thumb+'"><img src="/img/scroll/thumb.gif" width="'+bt_thumb_w+'" height="'+bt_thumb_h+'"></div><div class="dn" id="dn'+count+'""><a href="javascript:void(0)" onmouseover="theScroll['+count+'].scrollSouth(\''+count+'\')" onmouseout="theScroll['+count+'].endScroll()" onclick="return false;"><img src="/img/scroll/dn.gif" width="'+bt_w+'" height="'+bt_h+'"></a></div>';
		
		//var buttons = '<div class="up" id="up'+count+'"><a href="#" onclick="return false;"><img src="/img/scroll/up.gif" width="'+bt_w+'" height="'+bt_h+'"></a></div><div class="thumb" id="'+thumb+'"><img src="/img/scroll/thumb.gif" width="'+bt_thumb_w+'" height="'+bt_thumb_h+'"></div><div class="dn" id="dn'+count+'""><a href="#" onclick="return false;"><img src="/img/scroll/dn.gif" width="'+bt_w+'" height="'+bt_h+'"></a></div>';
		
		document.getElementById(root).innerHTML = buttons + document.getElementById(root).innerHTML;
		var iframe = [];
		//iframe[count] = '<iframe src="http://ojosdebrujo.com/gestion/giras/agenda.php?anteriores=0&css=giras-futuras.css" id="iframeAgenda" name="agenda" allowtransparency="true" width="250" height="100%" scrolling="no" framespacing="0" frameborder="0" marginheight="0" marginwidth="0"></iframe>';
		var scrollerContent = document.getElementById("scroll"+count+"Content");
		
		//scrollerContent.innerHTML = iframe[count];
		//var alturaIframe = document.getElementById('iframeAgenda').contentWindow.document.body.scrollHeight;
		//document.getElementById('iframeAgenda').style.height = document.getElementById('iframeAgenda').contentWindow.document.body.offsetHeight+500;
		//var alturaIframe = document.getElementById('iframeAgenda').style.height;
		//alert ("h: "+alturaIframe);
		//document.getElementById('iframeAgenda').style.height = document.body.scrollHeight;
		
		theRoot[count]   = document.getElementById(root);
		theThumb[count]  = document.getElementById(thumb);
		var thisup = document.getElementById("up"+count);
		var thisdn = document.getElementById("dn"+count);
		
		thisup.style.width = parseInt(bt_w) + "px";
		thisup.style.height = parseInt(bt_h) + "px";
		thisup.style.top = parseInt(minY) + "px";
		thisup.style.left = parseInt(minX) + "px";
		thisup.style.position = "relative";
		thisup.style.border = "0px solid #FF0000";
		//thisup.style.backgroundColor = "#FF0000";
		
		thisdn.style.width = parseInt(bt_w) + "px";
		thisdn.style.height = parseInt(bt_h) + "px";
		thisdn.style.top = parseInt(maxY-bt_h) + "px";
		thisdn.style.left = parseInt(minX) + "px";
		thisdn.style.position = "absolute";
		thisdn.style.border = "0px solid #FF0000";
		//thisdn.style.backgroundColor = "#FF0000";
		
		theThumb[count].style.width = parseInt(bt_thumb_w) + "px";
		theThumb[count].style.height = parseInt(bt_thumb_h) + "px";
		theThumb[count].style.top = parseInt(minY) + "px";
		theThumb[count].style.left = parseInt(minX) + "px";
		theThumb[count].style.position = "relative";
		theThumb[count].style.border = "0px solid #FF0000";
		//theThumb[count].style.backgroundColor = "#CC0000";

		theScroll[count].load();

		//Drag.init(theHandle[count], theRoot[count]); //not draggable on screen
		Drag.init(theThumb[count], null, minX, maxX, minY, maxY-bt_thumb_h-bt_h*2);
		
		// 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, 0, theScroll[i].clipH);
		}
	}
}) 