/* 실 추가 액션 */
var addSeal = function(seal_idx, firsted, mode) {
	seal_idx = parseInt(seal_idx);
	firsted = (firsted) ? true : false;

	if(seal_idx < 1) { alert('올바른 실의 번호가 아닙니다.'); return; }
	$.post(root_path+'/ajax/addSeal.php', {ajax: true, target_path: root_path, seal_idx: seal_idx, mode: mode, firsted: firsted}, addedSeal, 'json');
};
var addedSeal = function(data, status) {
	// "timeout", "error", "notmodified", "success", "parsererror"
	if(status === 'success' || status === 'notmodified') {
		if(data.error) { alert(data.error); return; }
		// 에러 없뜸? 새로고침
		front_seal_count = data.front_seal_count;
		if(findRemoteControllerActiveTab() == 2)
			reloadRemoteController(2);
		else
			changeRemoteControllerTab(2);
	} else {
		// 에러 처리
		alert('에러 발생');
	}
};


var removeSeal = function(seal_idx) {
	seal_idx = parseInt(seal_idx);

	if(seal_idx < 1) { alert('올바른 실의 번호가 아닙니다.'); return; }
	$.post(root_path+'/ajax/removeSeal.php', {ajax: true, target_path: root_path, seal_idx: seal_idx}, addedSeal, 'json');
};
var removedSeal = function(data, status) {
	// "timeout", "error", "notmodified", "success", "parsererror"
	if(status === 'success' || status === 'notmodified') {
		if(data.error) { alert(data.error); return; }
		// 에러 없뜸?
		front_seal_count = data.front_seal_count;
		reloadRemoteController(2);
	} else {
		// 에러 처리
		alert('에러 발생');
	}
};


var selectFirstSeal = function(seal_idx) {
	seal_idx = parseInt(seal_idx);

	if(seal_idx < 1) { alert('올바른 실의 번호가 아닙니다.'); return; }
	$.post(root_path+'/ajax/selectFirstSeal.php', {ajax: true, target_path: root_path, seal_idx: seal_idx}, selectedFirstSeal, 'json');
};
var selectedFirstSeal = function(data, status) {
	// "timeout", "error", "notmodified", "success", "parsererror"
	if(status === 'success' || status === 'notmodified') {
		if(data.error) { alert(data.error); return; }
		// 에러 없뜸? 새로고침
		front_first_seal_idx = data.first_seal_idx;
		reloadRemoteController(3);
	} else {
		// 에러 처리
		alert('에러 발생');
	}
};


var reloadRemoteController = function(remote_tab) {
	$('#rcContainer').load(root_path + '/ajax/reloadRemoteController.php', {ajax: true, target_path: root_path, remote_tab: remote_tab}, reloadedRemoteController);
};
var reloadedRemoteController = function(data, status) {
	if(status === 'success' || status === 'notmodified') {
		// 에러 없뜸?
	} else {
		// 에러 처리
		alert('에러 발생');
	}
};


var changeRemoteControllerTab = function(ancher, remote_tab) {
	if(ancher && ancher.className == 'selected') return; // 이미 선택된 탭은 아예 무시한다.

	if(front_seal_count < 1) {
		if(confirm('본 기능은 원하는 실을 선택하신 후에 사용하실 수 있는 기능입니다.\n원하는 실을 고르러 가시겠습니까?')) {
			document.location.href = root_path + '/seal/list.php';
		}
		return false;
	}

	var list = $('#remoteController ul.menu li');
	for(var i=0; i<list.length; ++i) {
		if(i+1 == remote_tab) $(list[i]).find('a').addClass('selected');
		else $(list[i]).find('a').removeClass('selected');
	}
	document.getElementById('rcTabLine').className = 'tab_line'+remote_tab;

	reloadRemoteController(remote_tab);
};

var findRemoteControllerActiveTab = function() {
	var list = $('#remoteController ul.menu li'), result = 0;
	for(var i=0; i<list.length; ++i) {
		if($(list[i]).find('a').hasClass('selected')) result = i + 1;
	}
	return result;
};


var applyFrontSeal = function() {
	if(front_seal_count < 1) {
		if(confirm('본 기능은 원하는 실을 선택하신 후에 사용하실 수 있는 기능입니다.\n원하는 실을 고르러 가시겠습니까?')) {
			document.location.href = root_path + '/seal/list.php';
		}
		return false;
	}
	if(front_first_seal_idx < 1) {
		alert('첫 화면에 나올 대표실을 골라주세요.\n리모컨의 대표실 탭으로 이동합니다.');
		changeRemoteControllerTab(null, 3);
		return false;
	}
	if(mb_idx < 1) {
		if(confirm('실을 다시려면 회원 가입을 하셔야 합니다.\n회원가입을 하시면 지금 고르신 실을 바로 내 블로그에 달 수 있습니다.\n회원 가입을 하시겠습니까?')) {
			document.location.href = root_path + '/member/join.php';
		}
		return false;
	}
	$.post(root_path+'/ajax/applyFrontSeal.php', {ajax: true, target_path: root_path}, appliedFrontSeal, 'json');
};
var appliedFrontSeal = function(data, status) {
	// "timeout", "error", "notmodified", "success", "parsererror"
	if(status === 'success' || status === 'notmodified') {
		if(data.error) { alert(data.error); return; }
		// 에러 없뜸?
		if(confirm('선택하신 실의 정보가 정상적으로 업데이트되었습니다.\n실을 처음 다시는 분께서는 다음 페이지로 이동하셔서 실통의 코드를 받아가시고,\n이미 실통을 받으신 분께서는 아무 것도 하지 않으셔도 자동으로 내 블로그에 반영됩니다.\n실통 코드를 받으러 가시겠습니까?')) {
			document.location.href = root_path + '/mypage/mySeal.php';
		}
	} else {
		// 에러 처리
		alert('에러 발생');
	}
};

var checkRandomSelectedSeal = function(type) 
{
	//급하게 처리해야 한다는 생각에.
    var img = $('#random_seal');
	switch(type) 
	{
		case 0: 
			alert('랜덤대표실을 선택하지 않으실 경우 다른 실을 클릭하여 대표실을 설정하셔야 합니다.');
			img.attr('src','/images/remote/randomseal_uncheck.png');
			break;
		case 1: 
			img.attr('src','/images/remote/randomseal_check.png');
			break; 
	}
	selectFirstSeal(2);
};