// Browser detection and scroll code gratefully based on ScrollSpy Version 0.9.2
// By Willem Broekema, <willem@pastelhorn.com>
//
// More info and latest release:
// <http://pastelhorn.com/project/js/scrollspy>
//
// Licensed under the GNU LGPL
// <http://www.gnu.org/copyleft/lesser.html>

/// Part of Opera browser detection script copied from 
/// http://developer.netscape.com/docs/examples/javascript/browser_type.html

var agt = navigator.userAgent.toLowerCase();
var isOpera = ( (agt.indexOf("opera") != -1) && document.getElementById   ) ? 1 : 0;
var isNav4 = (document.layers && !isOpera) ? 1 : 0;
var isIE4  = (document.all && !isOpera) ? 1 : 0;
var isMoz  = (document.getElementById && !(isNav4 || isIE4 | isOpera) ) ? 1 : 0;
var isNone = (isOpera+isNav4+isIE4+isMoz) ? 0 : 1;

// ********* function to swap "new" and "read" onMouseOver *********

function newPlacePreview(picName, gif) {
	var imgObj=eval("document." + picName);
	imgObj.src="Images/read.gif";
	placePreview(gif);
	return;
}

// ********* initialise global variables ***************

var MinVP, yScroll, viewPortHeight, markerHeight, criticalPoint, relocatePreview = 0;
var statusString="";
var markerTop=76;	// need to add height of banner to value returned by document.marker.y

// ************ finds the specified <div> for further processing *********

function getDiv(divname) {

// given name 'divname', return div object
// if div is within otherdiv, use 'otherdivname.divname' format as parameter

	divs = divname.split('.'); 

	if (isNav4) {				// left in for completeness - not used
		s = ''
		for (i = 0; i < divs.length; i++) {
			s += 'document.layers.' + divs[i];
			if (i < divs.length - 1) 
				s += '.';
		}		
		return  eval(s);
	}

	if (isIE4)
		return eval( 'document.all.' + divs[ divs.length-1 ] );

	if (isMoz || isOpera)
		return document.getElementById(divs[ divs.length-1 ] );

}

// ************* function sets top position of chosen <div> ***********

function setTop(layer, t) {
  if (isNav4)
    layer.top = t;				// left in for completeness - not used
  else
    if (isIE4)
      layer.style.pixelTop = t;
    else
	  if (isMoz) {
	    var p = '' + t + 'px';
		layer.style.top = p;
	  }
	  else
	    if (isOpera)          
		  layer.style.top = t;
}

// ************* find out where the image goes... ************

function getMarkerData() {
	if (isNav4 || isNone){return}			// preview placing not supported
	else if (isMoz) {
		markerTop += document.marker.y;
		markerHeight  = document.marker.height;
	}
	else if (isIE4 || isOpera) {
		markerTop += document.marker.offsetTop;
		markerHeight  = document.marker.height;
	}
}

// ******** ... and put it there ***************

function placePreview(previewGif) {

	if (isNav4 || isNone){return}			// preview placing not supported
	var placeTop=markerTop;				// default
	var thediv = getDiv('picmove');

	if (isMoz || isOpera) {
		yScroll = parseInt(window.pageYOffset);
		viewPortHeight = parseInt(window.innerHeight);
	}
	else if (isIE4) {
		yScroll = parseInt(document.documentElement.scrollTop);
		viewPortHeight = parseInt(document.documentElement.clientHeight);
	}

	placeTop = Math.max((yScroll + (viewPortHeight - markerHeight)/2), markerTop);
	setTop(thediv, placeTop);
	document.scrollPic.src = "Images/previews/" + previewGif;

	return;
}

// ****************** write code to place the preview image over the (blank) marker) ******

function placeOverlay() {
	var overlayString = "";
	overlayString += "<div id='picmove' style='position: absolute; top: ";
	if (isNav4 || isNone) {return}
		else if (isMoz) {overlayString += document.marker.y + 76}
		else if (isIE4 || isOpera) {overlayString += document.marker.offsetTop + 76}
	overlayString += "px; right: 5px; width: 118px;'>";
	overlayString += "<img height=200 width=118 src='Images/previews/overlay.gif' 	name='scrollPic' alt=''></div>";
	document.open();
	document.writeln(overlayString);
	document.close();
}
