var community_favorite_max_count = 15;
var community_favorite_check_selected=null;
var community_favorite_check_selected_no=-1;
var community_favorite_check_lists = new Array();

//카테고리 선택
function community_favorite_all_select(type){
	var f = document.community_favorite_admin_form;
	var i=0;

	var o = f["community_favorite_select_" + type];
	var o2 = f["community_favorite_select_" + type + '_all'];
	
	var cnt=0;
	for(i=0; i<o.length; i++){
		o[i].checked = o2.checked;
		if(o[i].onclick()){
			cnt++;
		}else{
			o[i].checked=false;
			break;
		}
	}
	if(cnt==0) o2.checked=false;
}


//체크 박스 선택시 중복 체크
function community_favorite_find_check(name,value){
	var i;
	for(i=0; i<community_favorite_check_lists.length; i++){
		if((community_favorite_check_lists[i][0] == name) && (community_favorite_check_lists[i][1] == value)){
			return i;
		}
	}
	return -1;
}

//선택/해제 할때
function community_favorite_check(name,value,txt,chk){
	if(chk){
		if(community_favorite_check_lists.length >= community_favorite_max_count){
			community_favorite_remove_check(name,value);
			alert("최대 " + community_favorite_max_count + " 개의 게시판만 추가 가능합니다");
			return false;
		}
		if(community_favorite_find_check(name,value)==-1){
			community_favorite_check_lists[community_favorite_check_lists.length] = new Array(name,value,txt);
			community_favorite_refresh_check();
		}
	}else{
		community_favorite_remove_check(name,value);
		community_favorite_refresh_check();
	}
	return true;
}

//선택 해제
function community_favorite_remove_check(name,value){
	var n = community_favorite_find_check(name,value);
	if(n>=0){
		community_favorite_check_lists.splice(n,1);
		community_favorite_refresh_check();
	}
}

//선택에서 삭제
function community_favorite_remove_check2(no){
	var i;
	var o = community_favorite_check_lists[no];
	
	if(!o) return;

	//체크박스 해제
	var o2 = document.community_favorite_admin_form[o[0]];
	if(o2){
		for(i=0; i<o2.length; i++){
			if(o[1] == o2[i].value){
				o2[i].checked = false;
				break;
			}
		}
	}
	
	//체크목록에서 제거
	community_favorite_remove_check(o[0],o[1]);
}



//선택된 목록을 다시 그린다.
function community_favorite_refresh_check(){
	var o2,i;
	var o = document.getElementById('community_favorite_admin_list');
	o.innerHTML = '';

	for(i=0; i<community_favorite_check_lists.length; i++){
		o2 = document.createElement('li');
		o2.id = 'community_favorite_check_lists'+i;
		if(community_favorite_check_selected_no==i){
			o2.className='select';
			community_favorite_check_selected=o2;
		}

		o2.onmousedown = function () {
			if(community_favorite_check_selected){
				community_favorite_check_selected.className='';
				community_favorite_check_selected=null;
				community_favorite_check_selected_no=-1;
			}
			this.className='select';
			community_favorite_check_selected = this;
			community_favorite_check_selected_no = Number(this.id.replace('community_favorite_check_lists',''));
		}

		o2.innerHTML = community_favorite_check_lists[i][2] + " <a href='javascript:void(0)' onclick=\"community_favorite_remove_check2("+i+")\">삭제</a>";
		o.appendChild(o2);
	}
}


function show_community_favorite_admin(){
	document.getElementById('community_favorite_admin_layer').style.display='';
}

function hide_community_favorite_admin(){
	document.getElementById('community_favorite_admin_layer').style.display='none';
}


/*

function show_community_favorite(){
	document.getElementById('community_favorite_layer').style.display='';
}

function hide_community_favorite(){
	document.getElementById('community_favorite_layer').style.display='none';
}
*/

//저장하기
function community_favorite_save(){
	if(confirm('변경한 내용을 저장 하시겠습니까?')){
		var i;
		var tmp = new Array();
		for(i=0; i<community_favorite_check_lists.length; i++){
			tmp[tmp.length] = community_favorite_check_lists[i][0];
			tmp[tmp.length] = community_favorite_check_lists[i][1];
			tmp[tmp.length] = community_favorite_check_lists[i][2];
		}
		document.community_favorite_admin_form.select_lists.value = tmp.join("\t");
		document.community_favorite_admin_form.submit();
	}
}

//선택된 항목 초기화
function community_favorite_clear_check(){
	o = document.community_favorite_admin_form.community_favorite_select_sp;
	for(i=0; i<o.length; i++) o[i].checked = false;
	o = document.community_favorite_admin_form.community_favorite_select_hobby;
	for(i=0; i<o.length; i++) o[i].checked = false;
	o = document.community_favorite_admin_form.community_favorite_select_life;
	for(i=0; i<o.length; i++) o[i].checked = false;
	o = document.community_favorite_admin_form.community_favorite_select_sc;
	for(i=0; i<o.length; i++) o[i].checked = false;
	o = document.community_favorite_admin_form.community_favorite_select_sc2;
	for(i=0; i<o.length; i++) o[i].checked = false;
	o = document.community_favorite_admin_form.community_favorite_select_game;
	for(i=0; i<o.length; i++) o[i].checked = false;
	o = document.community_favorite_admin_form.community_favorite_select_gallery;
	for(i=0; i<o.length; i++) o[i].checked = false;

	o = document.community_favorite_admin_form.community_favorite_select_ucc;
	for(i=0; i<o.length; i++) o[i].checked = false;

	var o = document.getElementById('community_favorite_admin_list');
	o.innerHTML = '';
	community_favorite_check_lists = new Array();
	community_favorite_check_selected=null;
	community_favorite_check_selected_no=-1;
}

//선택된 항목 위치 이동
function community_favorite_move_up(){
	if(community_favorite_check_selected_no>0){
		var old = community_favorite_check_lists[community_favorite_check_selected_no];
		community_favorite_check_lists[community_favorite_check_selected_no] = community_favorite_check_lists[community_favorite_check_selected_no-1];
		community_favorite_check_lists[community_favorite_check_selected_no-1] = old;
		community_favorite_check_selected_no--;
		community_favorite_refresh_check();
	}
}

function community_favorite_move_down(){
	if(community_favorite_check_selected_no<(community_favorite_check_lists.length-1) && community_favorite_check_selected_no>=0){
		var old = community_favorite_check_lists[community_favorite_check_selected_no];
		community_favorite_check_lists[community_favorite_check_selected_no] = community_favorite_check_lists[community_favorite_check_selected_no+1];
		community_favorite_check_lists[community_favorite_check_selected_no+1] = old;
		community_favorite_check_selected_no++;
		community_favorite_refresh_check();
	}
}
