var loaded, bodyHolder, vScroll;

function showPane(which,series) {
	if (!loaded) { return; }
	vScroll = f_scrollLeft();
	makeHttpRequest('includes/bigimage.php?img=' + which + '&series=' + series, 'showContent', false);
}

function reset() {
	$('bodyContent').innerHTML = bodyHolder;
	document.documentElement.scrollLeft = vScroll;
	document.body.scrollLeft = vScroll;
	init();//set up smooth scroll effect again.
}

function showContent(theHTML) {
	bodyHolder = $('bodyContent').innerHTML;
	$('bodyContent').innerHTML = theHTML;
}

//To determine scroll left position: (from http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html)
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

Event.observe(window, "load", init, false);

function init() {
	loaded = true;
	// Replace all links to named anchors, with a smooth scroll to the div that has id equal to the specified name
	// From http://scripty.walterdavisstudio.com/smooth.html
	$$("a").each(function(el) {
		if (el.href && el.href.indexOf("#") > -1) {
			el.scrollToId = el.href.split("#")[1];
			el.observe("click", handleScrollToId.bind(el));
		}
	});
}

// Smoothly scroll to a div (event handler)
var handleScrollToId = function(e) {
	Event.stop(e);
	this.blur();
	this.onclick = function() { return false; };
	var scrollToEl = $(this.scrollToId);
	if (scrollToEl) {
		// Scroll to the target div
		new Effect.ScrollToVertical(scrollToEl, {offset: -80, duration: 1.0, afterFinish: function() { 
				// Highlight highlightable spans within the scrolled-to div
				scrollToEl.getElementsBySelector(".highlightable").each(function(el) {
					el.addClassName("highlighted");
				});
			}});
	}
}

//extends scriptaculous... seems to have been part of an earlier install, but is no longer included?
Effect.ScrollToVertical = Class.create();
Object.extend(Object.extend(Effect.ScrollToVertical.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    this.start(arguments[1] || {});
  },
  setup: function() {
    Position.prepare();
    var offsets = Position.cumulativeOffset(this.element);
    if(this.options.offset) offsets[1] += this.options.offset;
    var max = window.innerWidth ? 
      window.width - window.innerWidth :
      document.body.scrollWidth - 
        (document.documentElement.clientWidth ? 
          document.documentElement.clientWidth : document.body.clientWidth);
    this.scrollStart = Position.deltaX;
    this.delta = (offsets[1] > max ? max : offsets[1]) - this.scrollStart;
  },
  update: function(position) {
    Position.prepare();
    window.scrollTo(this.scrollStart + (position*this.delta), Position.deltaY);
  }
});


