/**

	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;

	var x = popup.style.left;
	var y = popup.style.top;

//	document.getElementById("hdr").innerHTML = "x: " + x + " y: " + y + " tx: " + popup.targetX + " ty: " + popup.targetY;
	
	return ((x==popup.targetX+"px")&&(y==popup.targetY+"px"));
}



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, img, name, headline, location, gender, age, dating, desc){

	//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 infomation

	if(img == ""){

		img = "images/default_pop.gif";

	}

	

	content = 	"<div class=pop_top>&nbsp;</div>"

					+ "<div class=pop_container>"

					+ "<div class=pop_img><img width=100 height=100 src=" + img + "></div>"

					+ "<div class=pop_info>"

					+ "<div class=pop_name>" + name + "</div>"	

					

	if(headline != ""){

		content += "<div class=pop_item>" + headline + "</div>";

	}

	

	if(location != "" || gender != "" || age != ""){

		content += "<div class=pop_seperator></div>"

					+ "<div class=pop_item>A " + " " + age + "/" + gender + " from: " + location + "</div>"

					+ "<div class=pop_seperator></div><div class=pop_item>Description: " + desc + "</div>";

	}

	

	content += "</div>"

				+ "</div>"

				+ "<div class=pop_buttom>&nbsp;</div>";

	

	//get the handle of popup window

	var popup = document.all ? document.all["popup"] : document.getElementById ? document.getElementById("popup") : "";

	popup.innerHTML = content;



	//get the current mouse position

	nowX = MF ? evt.pageX : evt.clientX + getIEBody().scrollLeft;

	nowY = MF ? evt.pageY : evt.clientY + getIEBody().scrollTop;


	//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);	

// If the popup is on top of the mouse, move it to the left
	var xPos = 1*popup.offsetLeft+300;
	var yPos = 1*popup.offsetTop+194;
	if(isInRect(nowX, nowY, popup.offsetLeft, popup.offsetTop, xPos, yPos))
	{
		popup.style.left = "" + (popup.offsetLeft-306) + "px";		
	}

	popup.style.visibility = 'visible';
	movePopup();
}

function isInRect(x, y, x1, y1, x2, y2)
{
	// document.getElementById("hdr").innerHTML = " " + x + " " + y + " " + x1 + " " + y1 + " " + x2 + " " + y2;	
	if(((x>=x1)&&(x<=x2))&&((y>=y1)&&(y<=y2))) return(true);
	return(false);
}

id = 0;
function movePopup(){
	
	if(id) window.clearTimeout(id); //clear timeout first

//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;

	// var nowx = popup.style.left; 
	// var nowy = popup.style.top; 

	if(!isArrived(popup)){
		
	// if not arrived, move
		if(nowx < popup.targetX)
		{
			nowx += step;
			if(nowx > popup.targetX) nowx = popup.targetX;
		}
		else if(nowx > popup.targetX)
		{
			nowx -= step;
			if(nowx < popup.targetX) nowx = popup.targetX;
		}

		if(nowy < popup.targetY)
		{
			nowy += step;
			if(nowy > popup.targetY) nowy = popup.targetY;
		}
		else if(nowy > popup.targetY)
		{
			nowy -= step;
			if(nowy < popup.targetY) nowy = popup.targetY;
		}

		popup.style.left = "" + nowx + "px";
		popup.style.top = "" + nowy + "px";

		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 = 1;

var nowY = 1;



var hPadding = 25;

var vPadding = 25;



//get window size(don't do)

//getWindowSize();
