// PopupMenu 클래스
var PopupMenu = Class.create();

PopupMenu.prototype = {
	
	initialize: function(hotspot, menu) {
		this.hotspotElem = hotspot;
		this.menuElem = menu;
		
		// 메뉴 엘리먼트 초기화
		Position.absolutize(this.menuElem);
		this.hideMenu();
		
		// 핫스팟 엘리컨트에 onclick 이벤트 핸들러 지정
		Event.observe(this.hotspotElem, "click", 
			this.onClickHotSpot.bindAsEventListener(this));
	},
	
	onClickHotSpot: function(event)
	{
		// 메뉴 토글
		if (this.menuShown())
		{
			this.hideMenu();
		}
		else
		{
			this.showMenu();
		}
	},
	
	onMouseMove: function(event)
	{
		if (this.menuShown())
		{
			// 커서가 메뉴리스트 영역을 벗어난 경우
			if (!Position.within(this.menuElem, Event.pointerX(event), Event.pointerY(event)) &&
					!Position.within(this.hotspotElem, Event.pointerX(event), Event.pointerY(event))
			)
			{
				this.hideMenu();
			}
		}
	},
	
	menuShown: function()
	{
		return this.menuElem.visible();
	},
	
	showMenu: function()
	{
		pos = Position.positionedOffset(this.hotspotElem);
		dim = Element.getDimensions(this.hotspotElem);
		pos[1] += dim.height;
		
		Element.setStyle(this.menuElem, 
			{
				"left": pos[0] + "px",
				"top": pos[1] + "px"
			}
		);

		Effect.Appear(this.menuElem, 
			{ 
				duration: 0.1 
			}
		);
		
		// 
		this.onMouseMoveHandler = this.onMouseMove.bindAsEventListener(this);
		Event.observe(document, "mousemove", this.onMouseMoveHandler);
	},
	
	hideMenu: function()
	{
		if (this.onMouseMoveHandler)
		{
			Event.stopObserving(document, "mousemove", this.onMouseMoveHandler);
		}
		
		Effect.Fade(this.menuElem, 
			{ 
				duration: 0.1
			}
		);
	}
};

var currentBoardID = null;
var currentUserNum = null;
var currentName = null;
 
function arrange_popup_menu(board_id, user_num, name)
{
	currentBoardID = board_id;
	currentUserNum = user_num;
	currentName = name;
	
	$("pm_member_info").style.display = user_num > 0 ? 'block' : 'none';
	$("pm_send_memo").style.display = user_num > 0 ? 'block' : 'none';
	$("pm_blog").style.display = user_num > 0 ? 'block' : 'none';
	
	if( user_num > 0 )
		$("popup_menu").style.height = '65px';
	else
		$("popup_menu").style.height = '20px';
}

function memberInfo()
{
	var memberInfoWin = window.open("../user/member_info.php?user_seq="+currentUserNum, null, "width=470,height=500,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes");
	memberInfoWin.focus();
}

function searchArticle()
{
	location = "./list.php?board_id="+currentBoardID+"&sf=name&kw="+currentName;
}

function sendMemo()
{
	var memoWin = window.open("../mypage/mail_send.php?recv_seq="+currentUserNum, null, "width=470,height=400,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes");
	memoWin.focus();
}

function moveToBlog()
{
	location = "../blog/blog_redirect.php?user_seq="+currentUserNum;
}

var oldOnloadFunc = window.onload;
var popupMenuArr = new Array();

window.onload = function()
{
	var spanWriterList = $$("span.span_writer"); 
	
	for(var i=0; i<spanWriterList.length; i++)
	{
		popupMenuArr[i] = new PopupMenu(spanWriterList[i], $("popup_menu"));
	}
	
	if(oldOnloadFunc)
		oldOnloadFunc();
}
