/*
	글 공감, 피플즐겨찾기 관련 함수
*/

// 글 공감, 피플 즐겨찾기 추가시 결과 코드
var BM_RESULT_CODE = { SUCCESS: 1, FAIL: 2, FAIL_DUPLICATION: 3, FAIL_MAX_LIMIT: 4, FAIL_SELF_MENTOR: 5 }

// 글 공감하기
function addBookmark(isPopup, uri) {
	if (null == isPopup) isPopup = false;

	var command = 'addBookmark';
	var articleID = document.getElementById('articleID').value;
	var articleWriter = document.getElementById('articleWriter').value;
	var title = document.getElementById('title').value;

	var parameters = {};
	parameters.score = 1;
	parameters.articleID = articleID;
	parameters.link = uri+'?articleID='+articleID;
	parameters.title = title;
	parameters.articleWriter = articleWriter;

	if (checkLoginAsCombi(isPopup)) {
		new Ajax.Request(unescape(command),
		{
			method:'post',
			encoding: 'UTF-8',
			asynchronous: false,
			parameters: parameters,
			onSuccess: function(request) {				
				var result = eval(request.responseText);
				if ( result == BM_RESULT_CODE.FAIL_DUPLICATION ) {
					alert("이미 공감하셨습니다. \n한 게시물당 한번씩만 공감 가능합니다.");
				} else if ( result == BM_RESULT_CODE.SUCCESS ) {
					alert("공감 감사합니다!");
					location.href = document.location + '&targetTab=scrap';
				} else if ( result == BM_RESULT_CODE.FAIL_MAX_LIMIT) {
					alert("글 공감은 최대 100만개까지 가능합니다.");
				} else if ( result == BM_RESULT_CODE.FAIL ) {
					alert("오류가 발생하였습니다.");
				} 
			},
			onFailure: function(request) {
				ajaxOnFailProc(request, isPopup);	// globallovebeat.js
			}
		}
		);
	}
}

// 특정유저가 공감한 글보기 페이지 이동
function goBookmarkPage(page) {
	if (null == page) page = 1;

	var userId = document.getElementById('userId').value;
	location.href = location.pathname + '?userId=' + userId +'&bookmarkPage=' + page;
}

// 글에 대한 공감 피플리스트 조회
function getBookmarkUserList(page) {
	if (null == page) page = 1;

	var command = 'bookmarkUserList';
	var parameters = {};

	var userListLayer = document.getElementById('scrap');
	if (typeof userListLayer == "undefined" || userListLayer == null) {
		alert("[ERROR getBookmarkUserList] userList display layer not found.");
		return;
	}

	// 게시판의 공감리스트
	var articleID = document.getElementById('articleID');
	if (null != articleID) {
		parameters.articleID = articleID.value;
	}

	// 특정유저가 공감한 글 목록
	var userId = document.getElementById('userId');
	if (null != userId) {
		parameters.userId = userId.value;
	}

	// 노출 탭 선택
	var tagetTab = document.getElementById('targetTab');
	if (null != tagetTab) {
		parameters.targetTab = tagetTab.value;
	}

	// 페이지
	parameters.bookmarkPage = page;
	
	new Ajax.Updater({success: 'scrap'}, command, {
			method:'post',
			encoding: 'UTF-8',
			parameters: parameters,
			evalScripts: true,
			onFailure: function(request) {
				ajaxOnFailProc(request, isPopup);	// globallovebeat.js
			}
		}
	);
}

// 피플 즐겨찾기 추가
function addMentor(mentorUserId, isPopup, id) {
	if (null == mentorUserId) return;
	if (null == isPopup) isPopup = false;

	var command = '/mylb/addMentor';
	var parameters = {};
	parameters.mentorUserId = mentorUserId;

	if (checkLoginAsCombi(isPopup)) {
		new Ajax.Request(unescape(command), {
				method:'post',
				encoding: 'UTF-8',
				parameters: parameters,
				onSuccess: function(request) {
					var result = eval("("+request.responseText+")");
					if ( result[0].result == BM_RESULT_CODE.SUCCESS ) {
						displayMentorLayer('success', id);
						if (isPopup) {
							getMentorList(1, isPopup);
							document.getElementById('pnlBookmarkCount').innerHTML = parseInt(document.getElementById('bookmarkCount').value) + 1;
						}
					} else if ( result[0].result == BM_RESULT_CODE.FAIL_DUPLICATION ) {
						alert("이미 즐겨찾기에 추가 하셨습니다.");
					} else if ( result[0].result == BM_RESULT_CODE.FAIL_SELF_MENTOR ) {
						alert("본인은 즐겨찾기에 추가하실 수 없습니다.");
					} else if ( result[0].result == BM_RESULT_CODE.FAIL_MAX_LIMIT ) {
						alert("피플 즐겨찾기는 최대 50명까지만 가능합니다.");
					} else if ( result[0].result == BM_RESULT_CODE.FAIL ) {
						alert("오류가 발생하였습니다.");
					}
				},
				onFailure: function(request) {
					ajaxOnFailProc(request, isPopup);	// globallovebeat.js
				}
			}
		);
	}
}
// 피플 즐겨찾기 삭제
function deleteMentor(mentorUserId, isPopup) {
	if (null == mentorUserId) return;
	if (null == isPopup) isPopup = false;
	
	if (!confirm("삭제하시겠습니까?")){
	    return;
	}

	var command = '/mylb/deleteMentor';
	var parameters = {};
	parameters.mentorUserId = mentorUserId;

	if (checkLoginAsCombi(isPopup)) {
		new Ajax.Request(unescape(command), {
				method:'post',
				encoding: 'UTF-8',
				parameters: parameters,
				onSuccess: function(request) {
					var result = eval(request.responseText);
					if (true == result) alert("삭제 하였습니다.");
					else alert("삭제 실패하였습니다.");
					getMentorList(1, isPopup);
				},
				onFailure: function(request) {
					ajaxOnFailProc(request, isPopup); 		// globallovebeat.js
				}
			}
		);
	}
}
// 피플 즐겨찾기 조회
function getMentorList(page, isPopup) {
	if (null == page) page = 1;
	if (null == isPopup) isPopup = false;

	var mentorListLayer = document.getElementById('pnlMentorList');
	if (typeof mentorListLayer == "undefined" || mentorListLayer == null) {
		alert("[ERROR getMentorList] mentorList display layer not found.");
		return;
	}

	var command = '/mylb/mentorlist';
	var parameters = {};
	parameters.page = page;

	if (checkLoginAsCombi(isPopup)) {
		new Ajax.Request(unescape(command), {
				method:'post',
				encoding: 'UTF-8',
				parameters: parameters,
				onSuccess: function(request) {
					mentorListLayer.innerHTML = request.responseText;
				},
				onFailure: function(request) {
					ajaxOnFailProc(request, isPopup);		// globallovebeat.js
				}
			}
		);
	}
}
// 피플 즐겨찾기  레이어
function displayMentorLayer(msg, id) {
	var addMentorMsg = 'addMentorMsg';
	var successMentorMsg = 'successMentorMsg';

	if (null != id) {
		addMentorMsg += id;
		successMentorMsg += id;
	}

	var addMentor = document.getElementById(addMentorMsg);
	var successMentor = document.getElementById(successMentorMsg);

	if (('init' == msg) || ('success' !=  msg)) {
		addMentor.style.display = "block";
		successMentor.style.display = "none";
	} else {
		addMentor.style.display = "none";
		successMentor.style.display = "block";
	}
}
// 피플 즐겨찾기 로그인체크 및 레이어 노출 설정
function goMylbBookmarkAdd(isPopup, characterNickname, stausCount){

	if( isLogged() != 'true') {	//layer를 sso로 무조건 가는 것은 무리라고 판단.
		if(isPopup)
			PopupLogin('/mypage/who'+location.search);
		else
			GNBLogin();
		return;
	}

	if(checkGameNick(characterNickname)){		// globallovebeat.js
		if (stausCount == null) {
			toggleLayer('peopleLayer');
			displayMentorLayer('init');
		} else {
			var openLayerNo = document.getElementById('selectedLayerNo').value;
			if ((null != openLayerNo) && ('' != openLayerNo) && (stausCount != openLayerNo)) {
				closeLayer(openLayerNo);
			}
			toggleLayer('peopleLayer' + stausCount);
			displayMentorLayer('init', stausCount);
			document.getElementById('selectedLayerNo').value = stausCount;
		}
	}
}
// 피플 즐겨찾기: 캐릭터검색 페이지의 레이어 닫기
function closeLayer(no) {
	var targetLayer = 'peopleLayer';
	if ((null != no) && ('' != no)) {
		targetLayer += no;
	}

	viewOff(targetLayer);
	displayMentorLayer('close', no);
}
