// ==============================================================
// 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 theThumbSrc = []; var theThumbHeight = [];

// color: "g" for green, "r" for red, "b" for blue and "t" for teal.
// add "l" for a long scrollbar (i.e. "gl", "rl", "bl", "tl")
function instantiateScroller(count, id, left, top, width, height, speed, thumbcolor, thumbheight){
	theThumbSrc[count] = "thumb_"+thumbcolor+".gif";
	theThumbHeight[count] = thumbheight;

	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="#" onmouseover="theScroll['+count+'].scrollNorth(\''+count+'\')" onmouseout="theScroll['+count+'].endScroll()" onclick="return false;"><img src="up.gif" width="13" height="14"></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.gif" width="13" height="14"></a></div><div class="thumb" id="'+thumb+'" style="left: 135px; top: 15px;"><img src="'+theThumbSrc[count]+'" width="13" height="'+theThumbHeight[count]+'"></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+1) + "px";
		thisup.style.left = parseInt(minX+1) + "px";
		thisdn.style.left = parseInt(minX+1) + "px";
		theThumb[count].style.border =0;
		theThumb[count].style.top = parseInt(minY) + "px";
		thisup.style.top = 0 + "px";
		thisdn.style.top = parseInt(minY+maxY+(theThumbHeight[count]-14)) + "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+1, maxX+1, 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];
		
		if(ratio[count] > 0){
			theThumb[count].style.visibility = "visible";
			thisup.style.visibility = "visible";
			thisdn.style.visibility = "visible";
		}
		else {
			theThumb[count].style.visibility = "hidden";
			thisup.style.visibility = "hidden";
			thisdn.style.visibility = "hidden";
		}
		
		theThumb[count].onDrag = function(x, y) {
			theScroll[count].jumpTo(null, Math.round((y - theThumb[count].minY) * ratio[count]));
		}
}	

function recreateDragger(count, handler, root, thumb, minX, maxX, minY, maxY, RwH){

		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+1) + "px";
		thisup.style.left = parseInt(minX+1) + "px";
		thisdn.style.left = parseInt(minX+1) + "px";
		theThumb[count].style.border =0;
		theThumb[count].style.top = parseInt(minY) + "px";
		thisup.style.top = 0 + "px";
		thisdn.style.top = parseInt(minY+maxY+(theThumbHeight[count]-14)) + "px";
		//thisdn.style.top = 15 + "px";

		theScroll[count].clipH = RwH;
		theScroll[count].load();

		//Drag.init(theHandle[count], theRoot[count]); //not draggable on screen
		Drag.init(theThumb[count], null, minX+1, maxX+1, 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];

		if(ratio[count] > 0){
			theThumb[count].style.visibility = "visible";
			thisup.style.visibility = "visible";
			thisdn.style.visibility = "visible";
		}
		else {
			theThumb[count].style.visibility = "hidden";
			thisup.style.visibility = "hidden";
			thisdn.style.visibility = "hidden";
		}

		theThumb[count].onDrag = function(x, y) {
			theScroll[count].jumpTo(null, Math.round((y - theThumb[count].minY) * ratio[count]));
		}
}	

// INITIALIZER:
// ==============================================================


$(document).ready(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, 14, theScroll[i].clipH-(theThumbHeight[i]+14));
		}
	}
	$(window).resize(function(){
		redrawScrollbars();
	});
});


var maxSmall = 300;

function redrawScrollbars() {
	if(theScroll.length>0) {
		for(var i=0;i<theScroll.length;i++){
			if(theScroll[i].clipH != 200){
				var SwH = $(window).height() - 280;
				if(SwH < 160)SwH = maxSmall;
				recreateDragger(i, "handle"+i, "root"+i, "thumb"+i, theScroll[i].clipW, theScroll[i].clipW, 14, SwH-(theThumbHeight[i]+14), SwH);
			}
		}
	}
}

function redrawScrollbars2() {
	if(theScroll.length>0) {
		for(var i=0;i<theScroll.length;i++){
			if(theScroll[i].clipH != 200){
				var SwH = $(window).height() - 280;
				if(SwH < 160)SwH = maxSmall;
				recreateDragger(i, "handle"+i, "root"+i, "thumb"+i, theScroll[i].clipW, theScroll[i].clipW, 14, SwH-(theThumbHeight[i]+14), SwH);
				theScroll[i].scrollSouth(i);

			}
		}
	}
}

function ScollUp() {
if(theScroll.length>0) {
for(var i=0;i<theScroll.length;i++){
if(theScroll[i].clipH != 200){
theScroll[i].scrollNorth(i);
}
}
}
}

function ScollDown() {
if(theScroll.length>0) {
for(var i=0;i<theScroll.length;i++){
if(theScroll[i].clipH != 200){
theScroll[i].scrollSouth(i);
}
}
}
} 

function stopScroll() {
 theScroll[1].endScroll() ;
 theScroll[2].endScroll() ;
 theScroll[3].endScroll() ;
 theScroll[4].endScroll() ;
 theScroll[5].endScroll() ;
 theScroll[6].endScroll() ;
 theScroll[7].endScroll() ;
 theScroll[8].endScroll() ;
 theScroll[9].endScroll() ;
 theScroll[10].endScroll() ;
 theScroll[11].endScroll() ;
}