/**
	showpopup.js
	V 1.0
	3/17/2006
	Copyright Jacky Huang
	flashlib@gmail.com
*/

function hidePopup() {
	// hide popup window (when rollout...)
	var popup = document.getElementById('popup');
	popup.style.visibility='hidden';
	window.clearTimeout(id);
}


function isArrived(popup){
	// check if the popup window is moved to the target place, mouse location
	// return true if it is arrived, otherwise else.
	var x = popup.offsetTop;
	var y = popup.offsetLeft;
	
	return ((x==popup.targetX)&&(y==popup.targetY));
}

function getWindowSize(){
	var w = 0;
	var h = 0;
	
	if(!document.all){
		//not IE
		w = window.innerWidth;
		h = window.innerHeight;
	}else{
		//IE
		w = document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
		h = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	}
	winWidth = w;
	winHeight = h;
}

function getIEBody(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function adjustPosition(evt,popup){
	//ajust popup window's postion if necessary
	
	//check left
	if ((evt.clientX-popup.clientWidth-hPadding)<0){
		// move to the right
		popup.targetX = nowX + hPadding;
	}
	
	//check bottom
	if ((evt.clientY+popup.clientHeight+vPadding)>=winHeight){
		// move to the top
		popup.targetY = nowY - popup.clientHeight - vPadding;
	}
}

function showPopup(evt,content){
	//show popup window with the content
		
	//get the event handle, and fit the IE and MF compatibility
	evt = evt ? evt : (window.event ? window.event : null);

	//get the current mouse position
	nowX = MF ? evt.pageX : evt.clientX + getIEBody().scrollLeft;
	nowY = MF ? evt.pageY : evt.clientY + getIEBody().scrollTop;


	//get the handle of popup window
	var popup = document.all ? document.all["popup"] : document.getElementById ? document.getElementById("popup") : "";
	popup.innerHTML = content;

	//set the target position of the popup window to the mouse position
	popup.targetX = nowX - popup.clientWidth - hPadding;
	popup.targetY = nowY + vPadding;

	//adjust the target position to prevent the edge effect
	getWindowSize();
	
	adjustPosition(evt,popup);	

	//set the popup window visible
	popup.style.visibility = 'visible';
	
	movePopup();

}

function movePopup(){
	//move popup window
	
	//clear timeout first
	window.clearTimeout(id);
	//get popup window handle
	var popup = document.all ? document.all["popup"] : document.getElementById ? document.getElementById("popup") : "";
	
	//get current position
	var nowx = popup.offsetLeft;
	var nowy = popup.offsetTop;
	
	if(!isArrived(popup)){
		// if not arrived, move
		if(nowx < popup.targetX){
			nowx += step;
		}
		if(nowx > popup.targetX){
			nowx -= step;
		}
		if(nowy < popup.targetY){
			nowy += step;
		}
		if(nowy > popup.targetY){
			nowy -= step;
		}
		
		popup.style.left = nowx;
		popup.style.top = nowy;
		
		id = window.setTimeout("movePopup()",1);
	}
}

//init IE and MF browser tags
var IE = document.all;
var MF = document.getElementById && !document.all;

//init window height and width
var winWidth = 0;
var winHeight = 0;

var id;
var step=5;

var nowX = 0;
var nowY = 0;

var hPadding = 25;
var vPadding = 25;

//get window size(don't do)
//getWindowSize();