

function scrollAreaITManager()
{
	var scrollareaITs = new Array();  // associative array
	var listenerSet = false;
	
	
	this.RegisterScrollArea = function(scrollID)
	{
		
		scrollareaITs[scrollID] = new Object();
		scrollareaITs[scrollID].anchors = new Array();
		
		if (!listenerSet)
		{
			YAHOO.util.Event.addListener(window, "load", this.InitScrollareaITs);
			listenerSet = true;
		}
	};
	
	this.RegisterScrollAreaAnchor = function(scrollID, linkID, anchorID)
	{
		if (scrollareaITs[scrollID])
		{
			scrollareaITs[scrollID].anchors[linkID] = anchorID;
		}
	};
	
	this.InitScrollareaITs = function()
	{
		for (var scrollID in scrollareaITs)
		{
			if (document.getElementById(scrollID))
			{
				var saIT = new scrollareaIT(document.getElementById(scrollID));
			
				for (var linkID in scrollareaITs[scrollID].anchors)
				{
					if (document.getElementById(linkID))
					{
						if (document.getElementById(scrollareaITs[scrollID].anchors[linkID]))
						{
							saIT.AttachAnchor(document.getElementById(linkID), document.getElementById(scrollareaITs[scrollID].anchors[linkID]));
						}
					}
				}
			}
		}
	};
	
}
var scrollAreaITManager = new scrollAreaITManager();



function scrollareaIT(content_obj)
{
	var self = this;
	var div_main = "";
	var div_scroll = "";
	
	var div_content_container = "";
	var div_content = "";
	
	var div_scroll_up = "";
	var div_scroll_bg = "";
	var div_scroll_middle = "";
	var div_scroll_middle_above = "";
	var div_scroll_middle_below = "";
	var div_scroll_down = "";
	
	this.viewable_height = 0;
	this.content_height = 0;
	this.content_top_max = 0;
	this.content_top = 0;
	this.scroll_top_max = 0;
	this.up_button_height = 0;
	this.down_button_height = 0;
	
	this.scroll_bg_height = 0;
	this.bar_height = 0;
	
	this.UpClicked = function(mamount){
		this.content_top += mamount;
		if(this.content_top > 0){
			this.content_top = 0;	
		}
		div_content.style.top = this.content_top + "px";
		
		this.MoveBar();
	};
	
	
	this.DownClicked = function(mamount){
		this.content_top -= mamount;
		
		if (this.content_top < this.content_top_max){
			this.content_top = this.content_top_max;
		}
		div_content.style.top = this.content_top + "px";
		
		this.MoveBar();
	};
	
	
	this.MoveBar = function(){
		var temp = Math.round(this.scroll_bg_height * Math.abs(this.content_top)/this.content_height);
		if (temp < 0){
			temp = 0;	
		}
		if (temp > this.max_scroll_top_height){
			temp = this.max_scroll_top_height;
		}
		div_scroll_middle_above.style.height = temp + "px";
	};
	
	
	
	this.AttachAnchor = function(linkObj, anchorObj){
		YAHOO.util.Event.removeListener(linkObj, "click");
		
		if (linkObj.href){
			linkObj.href = "#";
		}
		
		linkObj.onclick = function(){
			self.JumpToAnchor(anchorObj.id);
			return false;
		};
	};
	
	
	this.JumpToAnchor = function(anchorID){
		// calculate the relative top
		var temp = document.getElementById(anchorID);
		this.content_top = this.content_top + YAHOO.util.Dom.getY(div_scroll) - YAHOO.util.Dom.getY(temp);
		if (this.content_top < this.content_top_max){
			this.content_top = this.content_top_max;
		}
		div_content.style.top = this.content_top + "px";
		
		this.MoveBar();
	};
	
	
	
	
	var temp_width = -1;
	//var temp_height = -1;
	if (content_obj.style.width != "")
	{
		var temp = Number(content_obj.style.width.substr(0, content_obj.style.width.length - 2));
		if(temp > 0)
		{
			temp_width = temp;
		}
	}
	
	var content_parent = content_obj.parentNode;
	var content_obj = content_parent.removeChild(content_obj);
	content_obj.className = ""; // remove the classname
	
	div_main = document.createElement("div");
	div_main.className = "scrollareaIT";
	if (temp_width > 0)
	{
		var temp = temp_width + 15;  /* THIS SHOULDN'T BE 15, BUT 'SCROLL AREA WIDTH' + 5 */
		div_main.style.width = temp + "px";
	}
	content_parent.appendChild(div_main);
	
	
	// Content Area
	div_content_container = document.createElement("div");
	div_content_container.className = "contentAreaOverflow";
	if (temp_width > 0)
	{
		div_content_container.style.width = temp_width + "px";
	}
	div_main.appendChild(div_content_container);
	
	div_content = document.createElement("div");
	div_content.className = "contentArea";
	if (temp_width > 0)
	{
		div_content.style.width = temp_width + "px";
	}
	div_content_container.appendChild(div_content);
	
	div_content.appendChild(content_obj);
	
	// Scroll Area
	div_scroll = document.createElement("div");
	div_scroll.className = "scrollArea";
	div_main.appendChild(div_scroll);
	
	// Clear the floated divs
	div_clear = document.createElement("div");
	div_clear.className = "clear";
	div_main.appendChild(div_clear);
	
	
	// must be after content is appended
	this.content_top_max = Number(div_content_container.offsetHeight - div_content.offsetHeight); 
	this.viewable_height = Number(div_content_container.offsetHeight);
	this.content_height = Number(div_content.offsetHeight);
	
	
	if(this.content_height > this.viewable_height)
	{
		div_scroll_up = document.createElement("div");
		div_scroll_up.className = "scrollUpBT";
		div_scroll_up.onclick = function(){
			self.UpClicked(20);
		};
		div_scroll.appendChild(div_scroll_up);
		
		
		div_scroll_bg = document.createElement("div");
		div_scroll_bg.className = "scrollBG";
		div_scroll.appendChild(div_scroll_bg);
		
		
		div_scroll_down = document.createElement("div");
		div_scroll_down.className = "scrollDownBT";
		div_scroll_down.onclick = function(){
			self.DownClicked(20);
		};
		div_scroll.appendChild(div_scroll_down);
		
		
		div_scroll_middle_above = document.createElement("div");		
		div_scroll_middle_above.className = "scrollBarAbove";
		div_scroll_middle_above.onclick = function(){
			self.UpClicked(80);
		};
		div_scroll_bg.appendChild(div_scroll_middle_above);
		
		
		var dragYoffset;
		div_scroll_middle = document.createElement("div");
		div_scroll_middle.className = "scrollBar";
		div_scroll_middle.onmousedown = function(e){
			if (e == null) { 
				e = window.event; 
			}
			
			// enable vertical dragging
			dragYoffset=e.clientY;
			document.onselectstart = function(){return false;};
			document.onmousemove = function(e){
				if (e == null) { 
					e = window.event; 
				}
				
				var temp = e.clientY;
				var change = Math.round((temp - dragYoffset) * (self.content_height/self.viewable_height));
				
				dragYoffset = temp;
				if (change > 0){
					self.DownClicked(change);
				}
				else if (change < 0){
					change = Math.abs(change);
					self.UpClicked(change);
				}
				
			};
			
			document.onmouseup = function(e){
				document.onmousemove = function(e){};
				document.onselectstart = function(){};
			};
			
			return false;
		};
		div_scroll_bg.appendChild(div_scroll_middle);
		
		
		div_scroll_middle_below = document.createElement("div");
		div_scroll_middle_below.className = "scrollBarBelow";
		div_scroll_middle_below.onclick = function(){
			self.DownClicked(80);
		};
		div_scroll_bg.appendChild(div_scroll_middle_below);
		
		
		this.up_button_height = div_scroll_up.offsetHeight;
		this.down_button_height = div_scroll_down.offsetHeight;
		
		// scroll background height
		this.scroll_bg_height = this.viewable_height - this.up_button_height - this.down_button_height;
		div_scroll_bg.style.height = this.scroll_bg_height + "px";
		div_scroll_middle_below.style.height = this.scroll_bg_height + "px";
		
		// bar height
		this.bar_height = Math.round(this.scroll_bg_height*(this.viewable_height/this.content_height));
		div_scroll_middle.style.height = this.bar_height + "px";
		
		this.max_scroll_top_height = this.scroll_bg_height - this.bar_height;
		
		
		
		// add the scroll functionality
		div_main.onmousewheel = function(e) {
			if (e == null) { 
				e = window.event; 
			}
			var delta = 0;
			
			// correct delta for all browsers
			if (e.wheelDelta) /* IE/Opera. */{ 
				delta = e.wheelDelta/120;
				/** In Opera 9, delta differs in sign as compared to IE.*/
				if (window.opera){
					delta = -delta;
				}
			}
			else if (e.detail) { 
				/** Mozilla case. */
				/** In Mozilla, sign of delta is different than in IE.
				 * Also, delta is multiple of 3.
				 */
				delta = -e.detail/3;
			}
			/** If delta is nonzero, handle it.
			 * Basically, delta is now positive if wheel was scrolled up,
			 * and negative, if wheel was scrolled down.
			 */
			if (delta){
				delta *= 24;
				
				if (delta > 0){
					self.UpClicked(delta);
				}
				else if (delta < 0){
					delta = Math.abs(delta);
					self.DownClicked(delta);
				}
			}
			/** Prevent default actions caused by mouse wheel.
			 * That might be ugly, but we handle scrolls somehow
			 * anyway, so don't bother here..
			 */
			if (e.preventDefault){
				e.preventDefault();
			}
			e.returnValue = false;
		};
		
		// DOMMouseScroll is for mozilla.
		if (window.addEventListener){
			div_main.onmouseover = function(){
				window.addEventListener('DOMMouseScroll', div_main.onmousewheel, false);
			};
			div_main.onmouseout = function(){
					window.removeEventListener('DOMMouseScroll', div_main.onmousewheel, false);
			};
		}
	}
	
	
	// Setup Anchors
	var links = YAHOO.util.Dom.getElementsBy(IsLink, "a");
	for (var i = 0; i < links.length; i++){
		
		if (links[i].id.substr(0, 11).toLowerCase() == "scrolllink_"){
			var postStr = links[i].id.substr(11);
			
			if (YAHOO.util.Dom.get("scrollAnchor_" + postStr))
			{
				this.AttachAnchor(links[i], YAHOO.util.Dom.get("scrollAnchor_" + postStr));
			}
		}
	}
	
	
	return this;
}
