/* 
	Todo:
	1. Click on an area of the scrollbar go straight there.
	2. Scrollwheel horizontal
*/
function $h(Elem) {
	var elem;
	if(typeof(Elem)=="object")
		elem = Elem;
	else if(document.getElementById)
		var elem = document.getElementById(Elem);
	else if (document.all)
		var elem = document.all[Elem];
	
	xPos = elem.offsetHeight;
	
	return xPos;
}
function $w(Elem) {
	var elem;
	if(typeof(Elem)=="object")
		elem = Elem;
	else if(document.getElementById)
		var elem = document.getElementById(Elem);
	else if (document.all)
		var elem = document.all[Elem];
	
	if(elem)
		xPos = elem.offsetWidth;
		
	return xPos;
}
var Scrollbar = function()
{
	this.props = { 	containers:new Array(), contents:new Array() };
	this.currentslider = new Array();
	this.sliders = new Array();
	this.mousedown = false;
	this.init=function(container) { this.setup(container); };
	this.re_init=function(){
		// determine if we need a scrollbar
		var scrolling = YAHOO.util.Selector.query('div.scrolling')[0];
		var caption_contents = scrolling.getElementsByTagName('div')[0];
		var caption_slider = scrolling.getElementsByTagName('div')[1];
		
		if($h(caption_contents) > $h(scrolling)){
			if(typeof(caption_slider)=='undefined' || caption_slider == null) {
				var a_scrollbar = new Array();
				a_scrollbar["slider"] = new Array();		
				a_scrollbar["height"] = $h(scrolling);
				a_scrollbar["slider"]["height"] = parseInt(($h(scrolling)/$h(caption_contents)*$h(scrolling)));				
				scrolling.appendChild(this.createSlider(a_scrollbar, this.a_scrollbars.length , this.a_config));
			} else {
				var slider = caption_slider.getElementsByTagName('div')[0];
				var bottom = slider.getElementsByTagName('div')[1];
				var height =  parseInt(($h(scrolling)/$h(caption_contents)*$h(scrolling)));
				
				YAHOO.util.Dom.setStyle(caption_slider, 'top', '0px');
				YAHOO.util.Dom.setStyle(caption_contents, 'top', '0px');
				YAHOO.util.Dom.setStyle(slider, 'top', '0px');
				if(YAHOO.util.Dom.hasClass(caption_slider, 'hidden')) YAHOO.util.Dom.removeClass(caption_slider, 'hidden');
				YAHOO.util.Dom.setStyle(slider, 'height', height+'px');
				YAHOO.util.Dom.setStyle(bottom, 'top', (height-11)+'px');
			}
		} else {
			YAHOO.util.Dom.setStyle(caption_contents, 'top', '0px');
			YAHOO.util.Dom.addClass(caption_slider, 'hidden');
		}
		
	};
	this.setup=function(c)
	{
		var aScrollable = new Array();
		if(typeof(c)=="object")
		{
			
			for(var i=0; i<c.length; i++) {
				if(typeof(c[i].container) == "string")
					aScrollable.push(document.getElementById(c[i].container));
				else
					aScrollable.push(c[i].container);
			}
			
		} else {
			aScrollable.push(document.getElementById(c.container));
		}
		
		this.a_scrollbars = aScrollable;
		this.a_config = (c[0]) ? c[0].config : null;
			
		for(var i=0; i<aScrollable.length; i++)
		{
			if(typeof(aScrollable[i])=='undefined') return true;
			// DUPLICATE THE SCROLLING NODE
			var duplicate = aScrollable[i].cloneNode(true);
			duplicate.className = 'content';
			aScrollable[i].innerHTML = '';			
			// ADD THIS NODE TO THE SCROLLING NODE, REMOVE THE CLASSNAME
			aScrollable[i].appendChild(duplicate);
			
			// GET HEIGHT OF THE INNNER, IF GREATER THAN OUTER THEN WE NEED TO SETUP SCROLLBAR.
			var inner_height = $h(duplicate);
			var outer_height = parseInt(YAHOO.util.Dom.getStyle(aScrollable[i], 'height'));
			var outer_height_raw = $h(aScrollable[i]);
			
			if( inner_height > outer_height_raw ) {
				var contents = duplicate;
				contents.onmousewheel = this.wheel;
				if (contents.addEventListener)
					contents.addEventListener('DOMMouseScroll', this.wheel, false);
				
				aScrollbar = new Array();
				aScrollbar["slider"] = new Array();
				
				var aConfig = (c[i].config)?c[i].config:null;
				
				if(inner_height > outer_height)
				{	
					aScrollbar["height"] = outer_height_raw-20;
					aScrollbar["slider"]["height"] = parseInt((outer_height/(inner_height+20))*outer_height);
				}
				aScrollable[i].appendChild(this.createSlider(aScrollbar, i, aConfig));
				
			} else if($w(contents)>$w(aScrollable[i]) && c[i].config.layout==1){
				contents.onmousewheel = this.wheel;
				if (contents.addEventListener)
					contents.addEventListener('DOMMouseScroll', this.wheel, false);
				var nContentWidth = $w(contents);
				var nContainerWidth = $w(aScrollable[i]);
				
				aScrollbar = new Array();
				aScrollbar["slider"] = new Array();
				
				var aConfig = (c[i].config)?c[i].config:null;
				
				if(nContentWidth>nContainerWidth)
				{	
					aScrollbar["width"] = nContainerWidth;
					aScrollbar["slider"]["width"] = parseInt((nContainerWidth/nContentWidth)*nContainerWidth);
				}
				aScrollable[i].appendChild(this.createSlider(aScrollbar, i, aConfig));
			}
			else {
				YAHOO.util.Dom.setStyle(contents, 'width', ($w(contents))+'px');
			}
		}
	};
	this.createSlider = function(aProperties, index, aConfig){
		var scrollbar = document.createElement("div");
		scrollbar.id = "scrollbar"+index;
		scrollbar.className = "scrollbar";
		scrollbar.style.height = aProperties.height+"px";
		var slider = document.createElement("div");
		slider.id = "slider"+index;
		slider.className = "slider";
		slider.onmousedown = scroller.md;
		slider.onmouseup = scroller.mu;
		slider.onmousemove = scroller.move;
		slider.onmouseout = scroller.blurred;
		
		slider.onmouseover = function(){
			YAHOO.util.Dom.addClass(this, 'active');
		}
		
		if(aConfig==null){
			slider.style.height=aProperties.slider.height+"px";
			slider.style.top = "0px";
		}
		else {
			if(aConfig.layout==1){
				scrollbar.style.width = aProperties.width+"px";		
				slider.style.background = "transparent";
				slider.style.width=aProperties.slider.width+"px";
				slider.style.left = "20px";
				slider.style.height = "19px"
				var tcurve = document.createElement("div");
				tcurve.style.fontSize = "1px";
				tcurve.style.height = "19px";
				tcurve.style.cssFloat = "left";
				tcurve.style.width = "10px";
				tcurve.style.background = "url(slider-left.png) no-repeat";
				slider.appendChild(tcurve);
				var guts = document.createElement("div");
				guts.style.cssFloat = "left";
				guts.style.fontSize = "1px";
				guts.style.height = "19px";
				guts.style.width = (aProperties.slider.width-20)+"px";
				guts.style.background = "url(slider-scaleable.png) repeat-x";
				slider.appendChild(guts);
				var bcurve = document.createElement("div");
				bcurve.style.cssFloat = "left";
				bcurve.style.fontSize = "1px";
				bcurve.style.height = "11px";
				bcurve.style.width = "11px";
				bcurve.style.background = "url(slider-right.png) no-repeat";
				slider.appendChild(bcurve);
			} else { /* Vertical Scrollbar */
			
				slider.style.background = "#cccccc";
				slider.style.height=aProperties.slider.height+"px";
				slider.style.width = "10px";
				slider.style.top = "0px";
				
				var up = document.createElement("span");
				up.src = aConfig.scrollbar_up;
				YAHOO.util.Dom.addClass(up, 'scrollbar-up');
				var down = document.createElement("span");
				down.src = aConfig.scrollbar_down;
				YAHOO.util.Dom.addClass(down, 'scrollbar-down');
					
				scrollbar.appendChild(up);
				scrollbar.appendChild(down);
			}
		}
		scrollbar.appendChild(slider);
		return scrollbar;
	};
	this.md = function(e)
	{
		scroller.mousedown = true;
		scroller.currentcontent = this.parentNode.parentNode.getElementsByTagName("div")[0];
		
		
		var content_height = parseInt(YAHOO.util.Dom.getStyle(scroller.currentcontent, 'height'));
		var content_parent_height = parseInt(YAHOO.util.Dom.getStyle(scroller.currentcontent.parentNode, 'height'));
		
		if(typeof('current_height')==='string') { 
			content_height = $h(scroller.currentcontent);
		}
		
		scroller.currentslider = this;
		if(!scroller.getLayout(this)){
			scroller.currentslider.ratio = (content_height-content_parent_height)/(parseInt(this.parentNode.style.height)-parseInt(this.style.height));
		}else{
			scroller.currentslider.ratio = ($w(scroller.currentcontent)-$w(scroller.currentcontent.parentNode))/(parseInt(this.parentNode.style.width)-parseInt(this.style.width));
		}
		
		scroller.mdpos = scroller.getMouseCoordinates(e);
		scroller.currentslider["top"] = parseInt(this.style.top);
		scroller.currentslider["left"] = parseInt(this.style.left);
		return false;
	};
	this.mu = function()
	{
		scroller.mousedown = false;
		document.onmousemove = null;
		scroller.currentcontent = null;
		scroller.currentslider.ratio = null;
	};
	this.blurred = function()
	{
		if(scroller.mousedown)
		{
			document.onmousemove = scroller.move;
			document.onmouseup = scroller.mu;
		} else {
			YAHOO.util.Dom.removeClass(this, 'active');
		}
	};
	this.scrollmove = function(delta)
	{
		if(delta)
		{
			var st;	
			var bounds = scroller.getBoundaries(scroller.currentslider);
			var horizontal = scroller.getLayout(scroller.currentslider);
			delta *= -15;
			
			if(scroller.withinboundaries(scroller.currentslider))
			{
				if(!horizontal){
					st = parseInt(scroller.currentslider.style.top);
					newposition = (delta+st);
					if(newposition<0) newposition=0;
					else if(newposition>(bounds.y-parseInt(scroller.currentslider.style.height))) newposition = (bounds.y-parseInt(scroller.currentslider.style.height));
					
					scroller.currentslider.style.top = newposition+"px";
					scroller.currentcontent.style.top = -(Math.round(newposition*scroller.currentslider.ratio))+"px";
					
				}else{
					st = parseInt(scroller.currentslider.style.left);
					newposition = (delta+st);
					if(newposition<20) newposition=20;
					else if(newposition>(bounds.y-parseInt(scroller.currentslider.style.width)-20)) newposition = (bounds.y-parseInt(scroller.currentslider.style.width)-20);
					
					scroller.currentslider.style.left = newposition+"px";
					scroller.currentcontent.style.left = -(newposition*scroller.currentslider.ratio)+"px";
				}
			}
			else if(!scroller.withinboundaries(scroller.currentslider))
			{
				st =parseInt(scroller.currentslider.style.top);
				if(st>0) scroller.currentslider.style.top = (bounds.y-scroller.slider.height)+"px";
				else scroller.currentslider.style.top = "0px";
			}
		}
	};
	this.move = function(e)
	{
		if(scroller.mousedown)
		{
			var st;	
			var bounds = scroller.getBoundaries(scroller.currentslider);
			var horizontal = scroller.getLayout(scroller.currentslider);
			
			scroller.mouse = scroller.getMouseCoordinates(e);
			if(scroller.mousedown && scroller.withinboundaries(scroller.currentslider))
			{
				if(!horizontal){
					diff = scroller.mouse.y-scroller.mdpos.y;
					st = parseInt(scroller.currentslider.style.top);
					newposition = (diff+scroller.currentslider.top);
					
					if(newposition<0) newposition=0;
					else if(newposition>(bounds.y-parseInt(scroller.currentslider.style.height))) newposition = (bounds.y-parseInt(scroller.currentslider.style.height));
					scroller.currentslider.style.top = newposition+"px";
					
					scroller.currentcontent.style.top = -(newposition*scroller.currentslider.ratio)+"px";
				}else{
					diff = scroller.mouse.x-scroller.mdpos.x;
					st = parseInt(scroller.currentslider.style.left);
					newposition = (diff+scroller.currentslider.left);
					
					if(newposition<20) newposition=20;
					else if(newposition>(bounds.y-parseInt(scroller.currentslider.style.width)-20)) newposition = (bounds.y-parseInt(scroller.currentslider.style.width)-20);
					
					scroller.currentslider.style.left = newposition+"px";
					scroller.currentcontent.style.left = -(newposition*scroller.currentslider.ratio)+"px";
				}
			}
			else if(!scroller.withinboundaries(scroller.currentslider))
			{
				st =parseInt(scroller.currentslider.style.top);
				if(st>0) scroller.currentslider.style.top = (bounds.y-scroller.slider.height)+"px";
				else scroller.currentslider.style.top = "0px";
			}
		}
	};
	this.withinboundaries=function(slider)
	{
		var bounds = this.getBoundaries(scroller.currentslider);
		var horizontal = scroller.getLayout(scroller.currentslider);
		
		var st = (!horizontal)?parseInt(scroller.currentslider.style.top):parseInt(scroller.currentslider.style.left);
		var sb = (!horizontal)?(parseInt(scroller.currentslider.style.height)+st):(parseInt(scroller.currentslider.style.width)+st);
		
		if(!bounds.y) $d("Scrollbar.withinboundaries() - Error: Couldn't get the y axis limits.");
		return (st>=bounds.x && sb<=bounds.y)?true:false;	
	};
	this.getLayout=function(slider){
		return ( (parseInt(slider.style.width) > parseInt(slider.style.height))?1:0);
	};
	this.getBoundaries = function(slider)
	{
		var bounds = new Array();
		var horizontal = scroller.getLayout(slider);
		bounds.x = 0;
		bounds.y = (slider.parentNode)?((horizontal)?parseInt(slider.parentNode.style.width):parseInt(slider.parentNode.style.height)):false;		
		return bounds;
	};
	this.getMouseCoordinates=function(e)
	{
		var m = new Array();
		m["x"] = 0;
		m["y"] = 0;
		if(!e) var e = window.event;
		if (e.pageX || e.pageY)
		{
			m.x = e.pageX;
			m.y = e.pageY;
		}
		else if (e.clientX || e.clientY)
		{
			m.x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			m.y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		return m;
	};
	this.wheel=function(event){
		var delta = 0;
		scroller.currentcontent = this;

		var scrollbar = YAHOO.util.Dom.getElementsByClassName('scrollbar', 'div', this.parentNode)[0];		
		scroller.currentslider = scrollbar.getElementsByTagName('div')[0];
		
		var content_height = parseInt(YAHOO.util.Dom.getStyle(scroller.currentcontent, 'height'));
		var content_parent_height = parseInt(YAHOO.util.Dom.getStyle(scroller.currentcontent.parentNode, 'height'));
		
		if(typeof('current_height')==='string') { 
			content_height = $h(scroller.currentcontent);
		}
		
		if(!scroller.getLayout(scroller.currentslider)){
			scroller.currentslider.ratio = ((content_height-content_parent_height))/(parseInt(scrollbar.style.height)-parseInt(scroller.currentslider.style.height));
		}else{
			scroller.currentslider.ratio = ($w(scroller.currentcontent)-$w(scroller.currentcontent.parentNode))/(parseInt(scrollbar.style.width)-parseInt(scroller.currentslider.style.width));
		}
		
		
		if (!event) /* For IE. */
        	event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
        	delta = event.wheelDelta/120;
        }
		else if (event.detail) { /** Mozilla case. */
			delta = -event.detail/3;
		}if (delta)
        	scroller.scrollmove(delta); //handle(delta);
        
		if (event.preventDefault)
                event.preventDefault();
		
		event.returnValue = false;
	}
};
var scroller = new Scrollbar();// JavaScript Document