var isMinNS4 = (document.layers) ? 1 : 0;
var isMinIE4 = (document.all) ? 1 : 0;
var isW3C = (document.getElementById) ? 1 : 0;// W3C stands for the W3C standard, implemented in Mozilla (and Netscape 6) and IE5

activeLayerName = '';

function showPopup(evt,name) {
	if (isMinIE4)
		evt = window.event;

	var currentX,		//mouse position on X axis
		currentY,		//mouse position on X axis
		x,				//layer target position on X axis
		y,				//layer target position on Y axis
		docWidth,		//width of current frame
		docHeight,		//height of current frame
		layerWidth,		//width of popup layer
		layerHeight,	//height of popup layer
		ele;			//points to the popup element
	var pageScrollX=0,	//offset of current frame on X axis
		pageScrollY=0,	//offset of current frame on Y axis
		windowWidth,	//width of vision frame
		windowHeight;	//height of vision frame

	spaceDown = 20;		//constant of shifting popup down
	spaceUp = 5;		//constant of shifting popup up
	spaceRight = 10;	//constant of shifting popup right

	// First let's initialize our variables
	if ( isMinNS4 ) {
		ele = document.layers[name];
		if(!ele) return;
		pageScrollX = window.pageXOffset;
		pageScrollY = window.pageYOffset;
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
		currentX = evt.pageX - pageScrollX;
		currentY = evt.pageY - pageScrollY;
		docWidth = document.width;
		docHeight = document.height;
		layerWidth = ele.clip.width;
		layerHeight = ele.clip.height;
		browserType = 'NS4';
	} else  if(isMinIE4) {
		ele = document.all[name];
		if(!ele) return;
		pageScrollX = document.body.scrollLeft;
		pageScrollY = document.body.scrollTop;
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
		currentX = evt.clientX;
		currentY = evt.clientY;
		docWidth = document.body.scrollWidth;
		docHeight = document.body.scrollHeight;
		layerWidth = ele.offsetWidth;//200;
		layerHeight = ele.offsetHeight;
		browserType = 'IE4';
	} else {
		ele = document.getElementById(name);
		if(!ele) return;
		pageScrollX = window.pageXOffset;
		pageScrollY = window.pageYOffset;
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
		currentX = evt.clientX;
		currentY = evt.clientY;
		docWidth = document.width;
		docHeight = document.height;
		layerWidth = ele.offsetWidth;
		layerHeight = ele.offsetHeight;
		browserType = 'W3C';
	}

	verBarWidth = (windowWidth - docWidth);//constant of vertical scroll bar width
	horBarWidth = 16;//constant of horizontal scroll bar width

	// Then we calculate the popup element's new position
	if ( ( currentX + layerWidth + spaceRight + verBarWidth) > windowWidth ) {
		x = ( currentX - layerWidth );
		if( x < 0 )
			x = 0;
	} else
		x = currentX + spaceRight;
	x += pageScrollX;

	if ( ( currentY + layerHeight + spaceDown + horBarWidth ) >= windowHeight ) {
		y = ( currentY - layerHeight - spaceUp );
		if( y < 0 )
			y = 0;
	} else
		y = currentY + spaceDown;
	y += pageScrollY;

	// Finally, we set its position and visibility
	if ( isMinNS4 ) {
		ele.left = parseInt ( x );
		ele.top = parseInt ( y );
		ele.visibility = "show";
	} else {  // IE4 & W3C
		ele.style.left = parseInt ( x );
		ele.style.top = parseInt ( y );
		ele.style.visibility = "visible";
	}
	activeLayerName = name;
}

function hidePopup(name) {
	if(activeLayerName=='') return;

	if (isW3C) {
		document.getElementById(activeLayerName).style.visibility = "hidden";
	} else if (isMinNS4) {
		document.layers[activeLayerName].visibility = "hide";
	} else {
		document.all[activeLayerName].style.visibility = "hidden";
	}
	activeLayerName = '';
}

