// ==============================================================
// 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 jump(_name)
{
	var elem = null;
	var elements = document.getElementsByTagName('a');
	for (var i=0; i<elements.length; i++)
		if (elements[i].getAttribute("name")==_name)
		{
			elem = elements[i];
			break;
		}
	if (!elem) return;
	var y_value = (navigator.appName == "Microsoft Internet Explorer"!=-1 && navigator.userAgent.indexOf("MSIE")!=-1 || window.opera)?elem.offsetTop+elem.parentNode.offsetTop:elem.offsetTop-16;
	for (var count=0; count<theScroll.length; count++)
	{
		theScroll[count].jumpTo(null, y_value);
		theThumb[count].style.top = parseInt(((theThumb[count].maxY-theThumb[count].minY)*theScroll[count].scrollTop/theScroll[count].scrollH)+theThumb[count].minY) + "px"; //ok nomes down
	}
}

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, path)
{
		var buttons = '<div class="up" id="up'+count+'"><a href="#" onmouseover="theScroll['+count+'].scrollNorth(\''+count+'\')" onmouseout="theScroll['+count+'].endScroll()" onclick="return false;"><img src="'+path+'/img/scroller_up.gif"></a></div><div class="dn"  id="dn'+count+'""><a href="#" onmouseover="theScroll['+count+'].scrollSouth(\''+count+'\')" onmouseout="theScroll['+count+'].endScroll()" onclick="return false;"><img src="'+path+'/img/scroller_down.gif"></a></div><div class="thumb" id="'+thumb+'" style="left: 135px; top: 15px"><img src="'+path+'/img/scroller_button.gif" width="20" height="62"></div><div id="dn'+count+'" style="width:1px; font-size:0px"></div>';
		
		var scrolContentId = 'scroll'+count+'Content';		
		if(parseInt(document.getElementById(scrolContentId).offsetHeight) < 430)
		{
			document.getElementById('scroll'+count+'Container').className = "scroll0Container1";
			return;
		}
		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) + 20 + "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 + 20, maxY - 57);
		
		// 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]));
		}
		theThumb[count].onDragEnd = function(x, y) {
			var today = new Date();
			var key = location.href+"#"+theThumb[count].id;
			set_cookie(key,Math.round((y - theThumb[count].minY) * ratio[count]), (new Date((today.getFullYear()+1),today.getMonth(),today.getDate())));
		};
		var key = location.href+"#"+theThumb[count].id;
		var y_value = get_cookie(key);
		if (!isNaN(y_value))
		{
			theScroll[count].jumpTo(null, y_value);
			theThumb[count].style.top = parseInt(((theThumb[count].maxY-theThumb[count].minY)*theScroll[count].scrollTop/theScroll[count].scrollH)+theThumb[count].minY) + "px"; //ok nomes down
			//theScroll[count].endScroll(0);
			//theScroll[count].moveTo(theScroll[count].scrollLeft, theScroll[count].scrollTop);
		}	

	var hrf = location.href.indexOf("#")!=-1?location.href.substr(0,location.href.indexOf("#")):location.href;
	var a_elements = document.getElementsByTagName("a");
	for (var i=0; i<a_elements.length; i++)
	{
		if (a_elements[i].href.indexOf("#")!=-1 && a_elements[i].href.substr(0,a_elements[i].href.indexOf("#"))==hrf)
		{
			var cur_name = a_elements[i].href.indexOf("#")!=-1?a_elements[i].href.substr(a_elements[i].href.indexOf("#")+1):"";
			a_elements[i].href = "javascript:void('"+cur_name+"')";
			eval("a_elements[i].onclick = function () { jump('"+cur_name+"'); }");
		}
	}
	if (location.href.indexOf("#")!=-1)
	{
		jump(location.href.substr(location.href.indexOf("#")+1));
	}

}

function set_cookie(name, value)
{
  var argv = set_cookie.arguments;
  var argc = set_cookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

function get_cookie(name)
{
	var temp=document.cookie+";";
    var Pos=temp.indexOf("=",temp.indexOf(name+"="));
    if (temp.indexOf(name+"=")==-1) return "";
    return temp.substring(Pos+1,temp.indexOf(";",Pos));
}