var CmsPopTag = {};

/**
 * 라디오 버튼 선택에 따른 변화
 * @param {Object} radioValue
 */
CmsPopTag.getRadioCheck = function(radioValue){
	if(radioValue == 'spamRadio'){
		$('tagDiv').hide();
		$('recommendDiv').hide();
		
		$('spamDiv').show();
		
		$('spamSpan').className = "o02 pl02";
		$('spamSpan').setStyle({
			'fontWeight' : 'bold'
		});
		
		$('recommendSpan').className = "black_title pl02";
		$('tagSpan').className =  "black_title pl02";
		
		$('recommendSpan').setStyle({
			'fontWeight' : ''
		});
		$('tagSpan').setStyle({
			'fontWeight' : ''
		});
		
		$('spamPostRadio01').checked = true;
	}else if(radioValue == 'recommendRadio'){
		$('spamDiv').hide();
		$('tagDiv').hide();
		
		$('recommendDiv').show();
		
		$('recommendSpan').className = "o02 pl02";
		$('recommendSpan').setStyle({
			'fontWeight' : 'bold'
		});
		
		
		$('spamSpan').className = "black_title pl02";
		$('tagSpan').className =  "black_title pl02";
		
		$('tagSpan').setStyle({
			'fontWeight' : ''
		});
		$('spamSpan').setStyle({
			'fontWeight' : ''
		});
		
		$('recommendRadio01').checked = true;
	}else if(radioValue == 'tagRadio'){
		$('spamDiv').hide();
		$('recommendDiv').hide();
		
		$('tagDiv').show();
		
		$('tagSpan').className =  "o02 pl02";
		$('tagSpan').setStyle({
			'fontWeight' : 'bold'
		});
		
		$('recommendSpan').className = "black_title pl02";
		$('spamSpan').className = "black_title pl02";
		
		$('recommendSpan').setStyle({
			'fontWeight' : ''
		});
		$('spamSpan').setStyle({
			'fontWeight' : ''
		});
	}else{
		alert('라디오 버튼 을 선택해 주세요.');
	}
}

/**
 * 해당 CMS 처리
 */
CmsPopTag.submit = function(){
	var checkValue = CmsPopTag.radioCheckList();
	var dataSeq = $F('dataSeq');
	
	if($('tagRadio').checked){
		CmsPopTag.goSubmit(dataSeq, 'tagRadio');
	}else{
		if(checkValue == 'spamPost'){
			CmsPopTag.goSubmit(dataSeq, checkValue);
		}else if(checkValue == 'spamBlog'){
			CmsPopTag.goSubmit(dataSeq, checkValue);
		}else if(checkValue == 'recommend'){
			CmsPopTag.goSubmit(dataSeq, checkValue);
		}else if(checkValue == 'recommendAdmin'){
			CmsPopTag.goSubmit(dataSeq, checkValue);
		}else if(checkValue == 'minus'){
			CmsPopTag.goSubmit(dataSeq, checkValue);
		}else if(checkValue == 'minusAdmin'){
			CmsPopTag.goSubmit(dataSeq, checkValue);
		}else{
			alert("라디오 버튼을 선택해 주세요.");
		}
	}
}

/**
 * dataSeq 받아서 로직 처리
 */
CmsPopTag.goSubmit = function(dataSeq, choice){
	var tags = '';
	
	if(choice == 'tagRadio'){
		tags = $F('tagInput');
	}
	
	var type = $('hiddenType').value;
	
	var list = [];
	
	$$('span.hot5_text').each(
		function(e, index){
			list[index] = e.firstChild.nodeValue;
		}
	);
	
	
	
	var myAjax = new Ajax.Request(Constants.CONTEXT_NAME + '/cms/popUpTagSubmit.do', {
		method: 'post',
		encoding: "utf-8",
		parameters: {
			dataSeq: encodeURIComponent(dataSeq),
			choice: encodeURIComponent(choice),
			tags: encodeURIComponent(tags),
			type: encodeURIComponent(type),
			tagNames :list
		},
		onLoaded: function(){
		
		},
		onComplete: function(xhr){
			var result = xhr.responseText.evalJSON(true);
			if (result.result == 3) {
				alert("성공적으로 반영되었습니다.");
				location.reload();
			//					location.href = Constants.CONTEXT_NAME+"/index.do";
			}
			else {
				if (result.result > 0) {
					alert("성공적으로 반영되었습니다.");
					location.reload();
					//					location.href = Constants.CONTEXT_NAME+"/index.do";
					//					alert(localType +" / "+ localStart+ " / " + localPageCount);
					//					Index.getPost(localType, localPageCount, 1); 
					Util.PopUp.close();
					
				}
				else {
					alert("반영하는데 실패 하였습니다.");
					//						Index.getPost(localType, localStart, 1); 
					//						location.href = Constants.CONTEXT_NAME+"/index.do";
					location.reload();
					Util.PopUp.close();
				}
			}
			
		},
		onFailure: function(e){
			return alert("cms_pop_tag.js error HTTPRequest Err:" + e);
		}
	});
}


/**
 * 라디오 버튼 체크 리스트 가져오기
 */
CmsPopTag.radioCheckList = function(){
	var checkList = document.getElementsByName("cmsRadio");
	var checkString = "";
	
	for(var i = 0 ; i < checkList.length ; i++){
		if(checkList[i].checked){
			checkString += checkList[i].value;
		}
	}
	
	return checkString;
}
