document.writeln('<script type="text/javascript" src="/js/layer_location.js"></script>');

/**
 * 사용자의 Nickname 등을 클릭하여 나타나는 메뉴
 * 
 * ex)
 * onclick="profileLayer.view(this, event, '<c;out value="${lists.memNo}"/>');" 
 * 
 * 와 같이 사용 합니다.
 */
function J_ProfileLayer() {
		
	this.clientXP = 0;
	this.clientYP = 0;
	
	this.element = null;
	this.event = null;
	this.id = null;
	
	//상위 레이어 아이디 
	this.layerId = 'profileLayer';

	this.ballon_layer;
	this.obj_id;
	this.top;
	this.left;
	
	this.view = function(obj, event, id, isFixed) {
		if(isFixed != null) {
			$('profileLayer').style.position = 'fixed';
		}else {
			$('profileLayer').style.position = 'absolute';
		}
		
		this.element = obj;
		this.event = event;
		this.id = id;
		
		this.heightOption = 0;
		
		this.scrolls = {top:0, left:0};
		this.dim = Element.getDimensions(this.element);

		this.clientXP = (Position.cumulativeOffset(this.element)[0]);
		if(browser.check()) {//if IE == true
			if(browser.version() == '7') {
				this.clientYP = Position.cumulativeOffset(this.element.parentNode)[1];
			}else {
				this.clientYP = Position.cumulativeOffset(this.element)[1]
					- Position.realOffset(this.element)[1];
			}
		}else {
			this.clientYP = Position.cumulativeOffset(this.element)[1];
		}

		if(this.vaildate()) {
			this.params = 'targetId=' + this.id;
			new Ajax.Updater(this.layerId, 
				'/user/invite!getUserProfileInfo.do', {
					parameters:this.params,
					evalScripts: true
				}
			);
		}
	}
	this.vaildate = function() {
		if(this.id == null || this.id.isnull())
			return false;
		else return true;
	}
	this.position = function() {
		this.div = $("profileLayer");

		this.div.style.left = ((this.clientXP - parseInt(arguments[0])) - 30) + "px";
		this.div.style.top = (this.clientYP - 150) + "px"

		$('float_balloon').style.display = 'block';
		
		//Layer 가 활성화 되었을 때만, 이벤트 등록
		document.onmousedown = this.close;
		document.body.onmousewheel = function() {
			if (browser.check()) {
				if (browser.version() == '7') {}else {
					$('profileLayer').update('');
					$('profileLayer').style.display = 'block';
					$('float_balloon').style.display = 'block';
				}
			}
		}
	}
	this.close = function(e) {
		var events = e ? e : window.event;
		
		this.srcTag = null;
		if(events.srcElement) {
			this.srcTag = events.srcElement;
		}else this.srcTag = events.target;
		
		if(this.srcTag.id != 'invite' && this.srcTag.id != 'float_balloon') {
			$('profileLayer').update('');
		}

		$('profileLayer').style.display = 'block';
		$('float_balloon').style.display = 'block';
		
		return;
	}
}
var profileLayer = new J_ProfileLayer();

/**
 * 친구 추가
 */
function J_AddToFriends() {
	//상위 레이어 아이디 
	this.layerId = 'profileLayer';
	
	this.view = function() {
		//this.hide().save(arguments[0]);
		$(this.layerId).update('');
		document.onmousedown = null;
		
		this.url = '/user/invite!viewMessageWindow.do?id=' + arguments[0] + '&message.messageType=I';;
		this.popName = 'SendMessage';
		this.options = 'width=460,height=250';
		this.win = window.open(this.url, this.popName, this.options);
		
		/*this.beforeUrl = document.forms[0].action;
		
		document.forms[0].action = this.url;
		document.forms[0].target = this.popName;
		document.forms[0].submit();
		
		document.forms[0].action = this.beforeUrl;
		document.forms[0].target = '_self';*/
		
		return;
	}
	this.hide = function() {
		$('float_balloon').update('');
		return this;
	}
	this.save = function() {
		if(this.validate()) {
			/*$('arguments[0]').value = arguments[0];
			document.forms[0].action = '/user/invite!addToFriends.do?id=' + arguments[0];
			document.forms[0].submit();*/

			this.params = 'targetId=' + arguments[0];
			new Ajax.Updater('profileLayer', 
				'/user/invite!addToFriends.do', {
					parameters:this.params,
					evalScripts: true
				}
			);
		}
	}
	this.validate = function() {
		return true;
	}
}
var friend = new J_AddToFriends();

function J_SendMessage() {
	this.formName = 'messageFrm';
	//상위 레이어 아이디 
	this.layerId = 'profileLayer';
	//메시지 레이어 아이디
	this.layerMemberId = 'messageFrm';
	
	this.view = function() {
		this.hide();
		document.onmousedown = null;

		this.url = '/user/invite!viewMessageWindow.do?id=' + arguments[0];
		this.popName = 'SendMessage';
		this.options = 'width=460,height=310';
		this.win = window.open(this.url, this.popName, this.options);
		
		/*this.beforeUrl = document.forms[0].action;
		
		document.forms[0].action = this.url;
		document.forms[0].target = this.popName;
		document.forms[0].submit();
		
		document.forms[0].action = this.beforeUrl;
		document.forms[0].target = '_self';*/

				
		/*
		this.hide();
		//this.init();
		
		this.params = 'id=' + arguments[0];
		new Ajax.Updater(this.layerId, 
			'/user/invite!viewMessageWindow.do', {
				parameters:this.params,
					evalScripts: true
			}
		);
		*/
		return;
	}
	this.send = function() {
		if(this.validate()) {
			/*new Ajax.Updater(this.layerId, 
				'/user/invite!sendMessage.do', {
					parameters:Form.serialize($('messageFrm')),
					evalScripts: true
				}
			);*/
			document.forms['messageFrm'].action = '/user/invite!sendMessage.do';
			document.forms['messageFrm'].submit();
		}
		return;
	}
	this.validate = function() {
		if($F('message_title') == '') {
			//쪽지 보낼 사용자를 선택하여 주십시오.
			return false;
		}
		if($F('message_content') == '') {
			//쪽지의 내용을 작성하여 주십시오.
			return false;
		}
		return true;
	}
	this.setMember = function() {
		$('message_writeMemNo').value = arguments[0];
		return;
	}
	this.displayCenter = function(){
		this.divWidth = $('profileLayer').offsetWidth; //237
		this.divHeight = $('profileLayer').offsetHeight; //338
		display_center('profileLayer', this.divWidth, this.divHeight);
	}
	this.init = function() {
		$(this.layerId).setStyle({
			'position': 'absolute'
		});
		return this;
	}
	this.cancel = function() {
		$(arguments[0]).style.display = 'none';
		return;
	}
	this.hide = function() {
		$('profileLayer').update('');
		return;
	}
	this.close = function(){
		top.close();
		return;
	}
}
var message = new J_SendMessage();

function J_Browser(){
	this.isIE = false;
	this.check = function() {
		if (navigator.appName == "Microsoft Internet Explorer")
			this.isIE = true;
		else this.isIE = false;

		return this.isIE;
	}
	this.version = function() {
		this.vsion = parseInt(navigator.appVersion), 
		this.agent = navigator.userAgent
		this.realVerstion = parseInt(this.agent.substring(this.agent.indexOf("MSIE ") + 5));
		return this.realVerstion;
	}
}
var browser = new J_Browser();


function J_addPhoneBook()
{
	this.existMsg = null;
	
	this.checkAddPhoneBook = function()
	{
		existMsg = arguments[1];
				
		var url = "/user/phoneBook!checkFriendPhonebookToMe.do?addMemNo=" + arguments[0];
		var myAjax = new Ajax.Request(url, {method: 'get', onComplete: this.createMenuPhonebookComplete}); 
	        
		return;
	}
	
	this.createMenuPhonebookComplete = function()
	{
		var responseText = arguments[0].responseText;
		var jsonObject = eval('(' + responseText + ')');
		if (jsonObject.result == "SUCCESS") {
			this.addMemNo = jsonObject.addMemNo;
			phonebook.view(this.addMemNo);
			return;
		}else if(jsonObject.result == "EXIST"){
			alert(existMsg);
			return;
		}else{
			alert("ERROR");
			return;
		}
	}
	
	this.view = function()
	{
		this.hide();
		document.onmousedown = null;
		this.url = '/user/phoneBook!addPhoneBook.do?id=' + arguments[0];
		this.popName = 'addPhoneBook';
		this.options = 'width=460,height=310';
		this.win = window.open(this.url, this.popName, this.options);

		return;
	}
	
	this.hide = function()
	{
		$('profileLayer').update('');
		return;
	}
}
var phonebook = new J_addPhoneBook();
