pop_offset_x = 12;
pop_offset_y = 1;

//--

var pop = null;
var initialized = false;
var pop_width = 100;
var mouse_x=0, mouse_y=0;

// Decide browser version
var ie4 = document.all && !document.getElementById;
var ie5 = document.all && document.getElementById;
var ns6 = !document.all && document.getElementById;
var ns4 = document.layers;


function init(){
	// popup
	if(ie4 || ie5 || ns4 || ns6) {
		// Make a popup object shortcut
		if (ns4) pop = document.popupDiv;
		if (ie4 || ie5) pop = popupDiv;
		if (ns6) pop = document.getElementById("popupDiv");

		// Capture mouse movement
		if (ns4) document.captureEvents(Event.MOUSEMOVE);
		document.onmousemove = mouseMoved;
	} else {
		popup = do_nothing;
		popdn = do_nothing;
	}

	initialized = true;
}


// Show a popup
function popup(graphic) {
	graphic = "<img src=" + graphic + ">";

	if (!initialized) return false;

	// Write layer
	layerWrite(graphic);

	// Position layer
	setPopupPos(mouse_x, mouse_y);


	// Show layer
	show(pop);


//		document.all.popupDiv.style.width = 700;
//		document.all.popupDiv.style.height = 100;


	return true;
}


// Clears popups if appropriate
function popdn() {
	if ( initialized && (ie4 || ie5 || ns4 || ns6) && (pop != null) ) {
		hide(pop);
	}

	return true;
}


// Store mouse coordinates and move popup layer accordingly
function mouseMoved(e) {
	if ( ns4 || ns6 ) {
		mouse_x=e.pageX;
		mouse_y=e.pageY;
	}

	if (ie4) {
		mouse_x=event.x;
		mouse_y=event.y;
	}

	if (ie5) {
		mouse_x=event.x+self.document.body.scrollLeft;
		mouse_y=event.y+self.document.body.scrollTop;
	}

	setPopupPos(mouse_x, mouse_y);
}


// Determine popup position (relative to cursor)
function setPopupPos(mouse_x, mouse_y) {
	if (!initialized) return false;

	// Get window size
	if (ie4 || ie5) { var win_width = self.document.body.clientWidth;  var win_height = self.document.body.clientHeight; }
	if (ns4) { var win_width = self.innerWidth;  var win_height = self.innerHeight; }
	if (ns6) { var win_width = self.outerWidth;  var win_height = self.outerHeight; }

	// Get window scroll position
	var win_scroll_x = (ie4 || ie5) ? self.document.body.scrollLeft : self.pageXOffset;
	var win_scroll_y = (ie4 || ie5) ? self.document.body.scrollTop : self.pageYOffset;

	// Get popup dimensions
	var pop_width_current = (ie4 || ie5) ? pop.clientWidth:pop.offsetWidth;
	var pop_height_current = (ie4 || ie5) ? pop.clientHeight:pop.offsetHeight;

	var scrollbar_size = (ie4 || ie5)? 0:16;

	// Set horizontal pos
	var pos_x = mouse_x + win_scroll_x + pop_offset_x;
	if (pos_x + pop_width_current > win_width + win_scroll_x - scrollbar_size)
		pos_x = win_width + win_scroll_x - pop_width_current - scrollbar_size;
	if (pos_x < win_scroll_x)
		pos_x = win_scroll_x;

	// Set vertical pos
	var pos_y = mouse_y + pop_offset_y;

	
	// Move the layer
	moveLayerTo(pop, pos_x, pos_y);
}


function moveLayerTo(obj,x,y) {
	if (ns4) {
		obj.left = x;
		obj.top = y;
	} else if (ie4 || ie5) {
		obj.style.left = x;
		obj.style.top = y;
	} else if (ns6) {
		obj.style.left = x + "px";
		obj.style.top = y + "px";
	}
}


// Make an object visible
function show(obj) {
	if (ns4) {
		obj.visibility = "show";
	} else if (ie4 || ie5) {
		obj.style.visibility = "visible";
	} else if (ns6) {
		obj.style.visibility = "visible";
	}
}


// Hides an object
function hide(obj) {
	if (ns4) {
		obj.visibility = "hide";
	} else if (ie4 || ie5) {
		obj.style.visibility = "hidden";     // something else might need to be reset here
	} else if (ns6) {
		obj.style.visibility = "hidden";
	}
}


// Writes to a layer
function layerWrite(txt) {
	if (ns4) {
		var lyr = self.document.popupDiv.document;
		lyr.write(txt);
		lyr.close();
	} else if (ie4 || ie5) {
		document.all.popupDiv.innerHTML=txt;
	} else if (ns6) {
		var range = self.document.createRange();
		range.setStartBefore(pop);
		domfrag = range.createContextualFragment(txt);
		while (pop.hasChildNodes()) {
			pop.removeChild(pop.lastChild);
		}
		pop.appendChild(domfrag);
	}
}


function do_nothing() {
	return 1;
}