/* 
	Todo:
	1. Click on an area of the scrollbar go straight there.
	2. Scrollwheel horizontal
*/
function $d(msg)
{
	if(typeof(console)!="undefined") console.log(msg);
	else alert(msg);
}
function $(Elem)
{
	if(document.getElementById)
		return document.getElementById(Elem);
	else if(document.all)
		return document.all[Elem];
}
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];
	
	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('#image-caption .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++)
		{
			var contents = aScrollable[i].getElementsByTagName("div")[0];
			if($h(contents) > $h(aScrollable[i])+15){
				contents.onmousewheel = this.wheel;
				if (contents.addEventListener)
					contents.addEventListener('DOMMouseScroll', this.wheel, false);
				var nContentHeight = $h(contents);
				var nContainerHeight = $h(aScrollable[i]);
				
				aScrollbar = new Array();
				aScrollbar["slider"] = new Array();
				
				var aConfig = (c[i].config)?c[i].config:null;
				
				if(nContentHeight>nContainerHeight)
				{	
					aScrollbar["height"] = nContainerHeight;
					aScrollbar["slider"]["height"] = parseInt((nContainerHeight/nContentHeight)*nContainerHeight);
				}
				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)-10)+'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 = "#896e56 repeat-y";
				slider.style.height=aProperties.slider.height+"px";
				slider.style.width = "11px";
				slider.style.top = "0px";
				var tcurve = document.createElement("div");
				tcurve.style.position = "absolute";
				tcurve.style.top = "0";
				tcurve.style.left = "0";
				tcurve.style.fontSize = "1px";
				tcurve.style.height = "11px";
				tcurve.style.width = "11px";
				tcurve.style.backgroundImage = 'url(/m/imgs/sb/top.gif)';
				var bcurve = document.createElement("div");
				bcurve.style.position = "absolute";
				bcurve.style.top = (aProperties.slider.height-11)+"px";
				bcurve.style.left = "0";
				bcurve.style.fontSize = "1px";
				bcurve.style.height = "11px";
				bcurve.style.width = "11px";
				bcurve.style.backgroundImage = "url(/m/imgs/sb/bottom.gif)";
				slider.appendChild(tcurve);
				slider.appendChild(bcurve);
			}
		}
		scrollbar.appendChild(slider);
		return scrollbar;
	};
	this.md = function(e)
	{
		scroller.mousedown = true;
		scroller.currentcontent = this.parentNode.parentNode.getElementsByTagName("div")[0];
		scroller.currentslider = this;
		if(!scroller.getLayout(this)){
			scroller.currentslider.ratio = ($h(scroller.currentcontent)-$h(scroller.currentcontent.parentNode))/(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 = -(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.childNodes[0];
		
		if(!scroller.getLayout(scroller.currentslider)){
			scroller.currentslider.ratio = ($h(scroller.currentcontent)-$h(scroller.currentcontent.parentNode))/(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;
        	if (window.opera)
        		delta = -delta;
        }
		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();

window.onload = function(){
	var to_apply = new Array();
	if(document.getElementById("page-content")!=null) {
		to_apply.push({
			container:"page-content", config:{top:"fancy-top.gif", bottom:"fancy-bottom.gif", scalable:"fancy-scalable.gif"}
		});
	}
	scroller.init(to_apply);
};