$(document).ready(function(){
			
	/**************************************************
	 * dom-drag.js
	 * 09.25.2001
	 * www.youngpup.net
	 **************************************************
	 * 10.28.2001 - fixed minor bug where events
	 * sometimes fired off the handle, not the root.
	 **************************************************/
	
	var Drag = {
	
	  obj : null,
	
	  init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	  {
		o.onmousedown  = Drag.start;
	
		o.hmode      = bSwapHorzRef ? false : true ;
		o.vmode      = bSwapVertRef ? false : true ;
	
		o.root = oRoot && oRoot != null ? oRoot : o ;
	
		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";
	
		o.minX  = typeof minX != 'undefined' ? minX : null;
		o.minY  = typeof minY != 'undefined' ? minY : null;
		o.maxX  = typeof maxX != 'undefined' ? maxX : null;
		o.maxY  = typeof maxY != 'undefined' ? maxY : null;
	
		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;
	
		o.root.onDragStart  = new Function();
		o.root.onDragEnd  = new Function();
		o.root.onDrag    = new Function();
	  },
	
	  start : function(e)
	  {
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);
	
		o.lastMouseX  = e.clientX;
		o.lastMouseY  = e.clientY;
	
		if (o.hmode) {
		  if (o.minX != null)  o.minMouseX  = e.clientX - x + o.minX;
		  if (o.maxX != null)  o.maxMouseX  = o.minMouseX + o.maxX - o.minX;
		} else {
		  if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
		  if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}
	
		if (o.vmode) {
		  if (o.minY != null)  o.minMouseY  = e.clientY - y + o.minY;
		  if (o.maxY != null)  o.maxMouseY  = o.minMouseY + o.maxY - o.minY;
		} else {
		  if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
		  if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}
	
		document.onmousemove  = Drag.drag;
		document.onmouseup    = Drag.end;
	
		onMouseDrag(Drag.obj);
	
		return false;
	  },
	
	  drag : function(e)
	  {
		e = Drag.fixE(e);
		var o = Drag.obj;
	
		var ey  = e.clientY;
		var ex  = e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;
	
		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);
	
		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
	
		if (o.xMapper)    nx = o.xMapper(y)
		else if (o.yMapper)  ny = o.yMapper(x)
	
		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX  = ex;
		Drag.obj.lastMouseY  = ey;
	
		Drag.obj.root.onDrag(nx, ny);
		return false;
	  },
	
	  end : function()
	  {
		document.onmousemove = null;
		document.onmouseup   = null;
		
		onMouseRelease();
		
		Drag.obj.root.onDragEnd(  parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
					  parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	  },
	
	  fixE : function(e)
	  {
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	  }
	};
		
	var barHeight;
	var sliderHeight;
	var contentHeight;
	var minimumScrollerHeight = 25;
	var scrollerDistance;
	var answerDistance;
	var contentBox;
	var mouseDragging = false;
	var isLoading = false;

	// Fires when a user clicks a question.  Uses jQuery AJAX to load the answer dynamically.
	$("a.question").click(function(e){
	
		if (!isLoading)
		{
			if (!e) var e = window.event;
			var target = (e.target) ? e.target : e.srcElement;
			
			$(target).parents(".panel_faq").children(".loading_gif").fadeIn(200);
			isLoading = true;
			
			$.ajax({
				url: "questionlookup.php",
				type: "POST",
				data: "question=" + $(this).attr("rel"),
				dataType: ($.browser.msie) ? "xml" : "xmlDoc",
				timeout: 10000,
				//beforeSend:
				error: function(){
					// clear the answer pane
					$(target).parents(".pane_questions").siblings(".pane_answer").children(".scroll_content").html("");
										
					$("<p class=\"question\">Oops!</p>")
						.appendTo($(target).parents(".pane_questions").siblings(".pane_answer").children(".scroll_content"));
					
					$("<p class=\"answer\">There was a problem accessing the answer to this question.  Please use the back button and select another question.</p>")
							.appendTo($(target).parents(".pane_questions").siblings(".pane_answer").children(".scroll_content"));

					$("<a href=\"#\" class=\"back\">Back to Questions</a>")
						.appendTo($(target).parents(".pane_questions").siblings(".pane_answer").children(".scroll_content"));

					// animate the pane wrapper to hide the questions, and show the answer
					$(target).parents(".pane_wrapper").animate({ left: "-318px" }, 500, "swing", function() {
						isLoading = false;
					} );
					
					$(target).parents(".panel_faq").children(".loading_gif").fadeOut(200, function() { 
						$(target).parents(".panel_faq").children(".btn_back").fadeIn(200);
					} );
					
					// set up the scrollbar dimensions when the answer loads
					setAnswerBarHeight($(target).parents(".pane_questions"));
					
					// assign the click event handler to the new back button
					$("a.back").click(function(ev){
					
						if (!ev) var ev = window.event;
						target2 = (ev.target) ? ev.target : ev.srcElement;
						
						$(target2).parents(".pane_wrapper").animate({ left: "0px" }, 500, "swing" );
						$(target2).parents(".panel_faq").children(".btn_back").fadeOut(200);
						
						return false;
					});
				},
				success: function(xmlDoc){

					// clear the answer pane
					$(target).parents(".pane_questions").siblings(".pane_answer").children(".scroll_content").html("");
					
					// construct html elements using the XML we just loaded
					$(xmlDoc).find("question").each(function(){
						var question_text = $(this).text();
						$("<p class=\"question\"></p>")
							.html(question_text)
							.appendTo($(target).parents(".pane_questions").siblings(".pane_answer").children(".scroll_content"));
					});
					
					$(xmlDoc).find("answer").each(function(){
						var answer_text = $(this).text();
						$("<p class=\"answer\"></p>")
							.html(answer_text)
							.appendTo($(target).parents(".pane_questions").siblings(".pane_answer").children(".scroll_content"));
					});
					$(xmlDoc).find("backlink").each(function(){
						var link_text = $(this).text();
				
						$("<a href=\"#\" class=\"back\"></a>")
							.html(link_text)
							.appendTo($(target).parents(".pane_questions").siblings(".pane_answer").children(".scroll_content"));
					});
					
					// animate the pane wrapper to hide the questions, and show the answer
					$(target).parents(".pane_wrapper").animate({ left: "-318px" }, 500, "swing", function() {
						isLoading = false;
					} );
					
					$(target).parents(".panel_faq").children(".loading_gif").fadeOut(200, function() { 
						$(target).parents(".panel_faq").children(".btn_back").fadeIn(200);
					} );
					
					// set up the scrollbar dimensions when the answer loads
					setAnswerBarHeight($(target).parents(".pane_questions"));
					
					// assign the click event handler to the new back button
					$("a.back").click(function(ev){
					
						if (!ev) var ev = window.event;
						target2 = (ev.target) ? ev.target : ev.srcElement;
						
						$(target2).parents(".pane_wrapper").animate({ left: "0px" }, 500, "swing" );
						$(target2).parents(".panel_faq").children(".btn_back").fadeOut(200);
						
						return false;
					});
				}
				//complete:
			});
		}
		return false;
	});

	// Configures an answer pane after the XML content has been loaded into it.			
	function setAnswerBarHeight(thisModule)
	{
		// Dynamically determine content dimensions.
		barHeight = $(thisModule).siblings(".pane_answer").find(".bar").height();
		contentHeight = $(thisModule).siblings(".pane_answer").children(".scroll_content").height();
		contentBox = $(thisModule).parents(".module_faq").height();
		
		// Is the content larger than the content container?
		if (contentHeight > contentBox)
		{
			// Given the content dimenstions, configure the scroller.
			answerDistance = barHeight - (barHeight - (contentHeight - contentBox));

			// Don't allow the scroller to be any less than the minimum height.
			if (answerDistance > (barHeight - minimumScrollerHeight))
			{
				answerDistance = barHeight - minimumScrollerHeight;
			}

			// Set the dimensions of the scrollbar, and make sure it is showing.
			$(thisModule).siblings(".pane_answer").find(".scroller").css("height", (barHeight - answerDistance) + "px");
			$(thisModule).siblings(".pane_answer").find(".scroller").css("top", "0px");
			$(thisModule).siblings(".pane_answer").children(".scrollbar").css("visibility", "visible");
			
			// Make the scrollbar draggable, using dom-drag.js.
			var aThumb = $(thisModule).siblings(".pane_answer").find(".scroller").get(0);
			Drag.init(aThumb, null, 0, 0, 0, answerDistance);
		}
		// The content fits in the box?  No need for a scrollbar...
		else
		{
			$(thisModule).siblings(".pane_answer").children(".scrollbar").css("visibility", "hidden");
		}
		
		// Set the answer content to the top position.
		$(thisModule).siblings(".pane_answer").children(".scroll_content").css("top", "0px");
	}
	
	// Setup all of the FAQ module question pane scrollbars on page load.
	function setBarHeights()
	{
		// Find all the FAQ module question pane scrollbars, and loop through them.
		$(".pane_questions .scrollbar .bar").each(function() {
			
			// Dynamically determine content dimensions.
			barHeight = $(this).height();
			contentHeight = $(this).parent().siblings(".scroll_content").height();
			contentBox = $(this).parents(".module_faq").height();
			
			// Is the content larger than the content container?
			if (contentHeight > contentBox)
			{
				// Given the content dimenstions, configure the scroller.
				scrollerDistance = barHeight - (barHeight - (contentHeight - contentBox));
				
				// Don't allow the scroller to be any less than the minimum height.
				if (scrollerDistance > (barHeight - minimumScrollerHeight))
				{
					scrollerDistance = barHeight - minimumScrollerHeight;
				}
				
				// Set the dimensions of the scrollbar, and make sure it is showing.
				$(this).siblings(".scroller").css("height", (barHeight - scrollerDistance) + "px");
				$(this).parent().css("visibility", "visible");
				
				// Make the scrollbar draggable, using dom-drag.js.
				var aThumb = $(this).siblings(".scroller").get(0);
				Drag.init(aThumb, null, 0, 0, 0, scrollerDistance);
			}
			// The content fits in the box?  No need for a scrollbar...
			else
			{
				$(this).parent().css("visibility", "hidden");					
			}
		});
	}
	
	// Fires event to auto-scroll if the user clicks on the scrollbar (not the scroller)
	$(".scrollbar .bar").click(function(e){
		if (!e) var e = window.event;
	
		barHeight = $(this).height();
		sliderHeight = $(this).siblings(".scroller").height();
		contentHeight = $(this).parent().siblings(".scroll_content").height();
		contentBox = $(this).parents(".module_faq").height();
		
		// 40 is the height of the title image
		var y = e.pageY - this.parentNode.parentNode.parentNode.parentNode.parentNode.offsetTop - 40;

		// fixes the bottom border for the last element so that it doesn't show.				
		if (y > barHeight - 2) { y = barHeight - 2; }
		
		var thisPercentage = Math.round((y/barHeight)*100);
		
		setProgress($(this).siblings(".scroller").attr("id"), thisPercentage);
		
		return false;
	});
	
	$("a.btn_back").click(function(){
		$(this).siblings(".module_faq").children(".pane_wrapper").animate({ left: "0px" }, 500, "swing" );
		$(this).fadeOut(200);
		return false;
	});
	
	// Fires when the user clicks the scrollbar in order to move the scroller.
	function setProgress( progressBarId, progress )
	{
		var newPosition =  Math.round((barHeight - sliderHeight) * (progress / 100));
		
		var progressBar = $( "#" + progressBarId );
		
		var content = progressBar.parent().siblings(".scroll_content");
		
		changeNumbers(progressBar, progress);
		
		mouseDragging = true;
		
		progressBar.stop();
		progressBar.animate({ top: newPosition + "px" }, 500 );
										
		var contentTopPosition = Math.round((contentHeight - contentBox + 0) * (-progress/100)); 
		
		content.stop();
		content.animate({ top: contentTopPosition + "px" }, 500, onMouseRelease );
	}
	
	function changeNumbers( progressBar, progress )
	{
		var percent = Math.round( ((progressBar.position().top + 0) / (barHeight - sliderHeight)) * 100 );
		
		if ( percent != progress)
		{
			setTimeout( function() { changeNumbers(progressBar, progress); progressBar = null; progress = null; }, 15 );
		}
	}
	
	function onMouseDrag(thisObject)
	{
		mouseDragging = true;
		mouseDragLoop(thisObject.id);
	}
	
	function onMouseRelease()
	{
		mouseDragging = false;
	}
	
	// This is the function that loops when the scroller is being dragged, or the mousewheel is being used.
	function mouseDragLoop(thisObjectId)
	{
		var progressBar = $("#" + thisObjectId);
		
		sliderHeight = progressBar.height();
		barHeight = progressBar.siblings(".bar").height();
		contentHeight = progressBar.parent().siblings(".scroll_content").height();
		contentBox = progressBar.parents(".module_faq").height();
		
		if (contentHeight > barHeight)
		{
			var percent = Math.round( ((progressBar.position().top + 0) / (barHeight - sliderHeight)) * 100 );
			
			var content = progressBar.parent().siblings(".scroll_content");
			var contentTopPosition = Math.round((contentHeight - contentBox + 3) * (-percent/100)) + 4;
			var currentPosition = content.position().top - (content.position().top - contentTopPosition) * .3;

			content.css("top", Math.round(currentPosition) + "px");
			
			if (mouseDragging || (Math.abs(content.position().top - contentTopPosition) > 5))
			{
				setTimeout( function() { mouseDragLoop(thisObjectId); thisObjectId = null; }, 15 );
			}
		}
	}

	// Set up mousewheel events for each question and answer pane, using the mousewheel plugin.
	$(".pane_questions, .pane_answer").bind("mousewheel", function(e, delta)
	{
		if (!mouseDragging)
		{
			var dir = delta > 0 ? "Up" : "Down";
			vel = Math.abs(delta);

			barHeight = $(this).children(".scrollbar").height();				
			slider = $(this).find(".scroller");
			sliderHeight = slider.height();
			
			var y = slider.position().top;
			
			if (delta > 0)
			{
				if ( (y-10) >= 0 )
				{
					slider.css("top", y - 10 + "px");
				}
				else
				{
					slider.css("top", "0");
				}
			}
			else
			{
				if ( (y+10) <= (barHeight - sliderHeight) )
				{
					slider.css("top", y + 10 + "px");
				}
				else
				{
					slider.css("top", (barHeight - sliderHeight));
				}
			}
			
			mouseDragLoop(slider.attr("id"));
		}
		return false;
	});
	
	setBarHeights();
	
});
