var Index = {};


/**
 * 탑 메뉴
 */
Index.TopMenu = {
	memberJoin: function () {
//		Util.PopUp.open(Constants.CONTEXT_NAME + '/member/join.do', 720, 526);
		Util.PopUp.open(Constants.CONTEXT_NAME + '/member/join.do', 720, 690);
	},
	memberJoinClose: function () {
		Util.PopUp.close();
	},
	memberLogin: function(){
		Util.PopUp.open(Constants.CONTEXT_NAME + '/member/login.do', 500, 531);
	},
	memberLoginClose: function(){
		Util.PopUp.close();
	},
	memberPw: function(){
		Util.PopUp.open(Constants.CONTEXT_NAME + '/member/findPw.do', 500, 531);
	},
	memberPwClose: function(){
		Util.PopUp.close();
	}
};

/**
 * 로그 아웃
 */
Index.logout = function(){
	var myAjax = new Ajax.Request( 
		Constants.CONTEXT_NAME + '/member/logout.do', 
		{
				method: 'post', 
				encoding: "utf-8",
				onComplete: function(xhr){
					var result = xhr.responseText.evalJSON(true);
					
					if(result.result > 0){
						alert("로그아웃 성공.");
						location.href = Constants.CONTEXT_NAME + '/index.do';
					}else{
						alert("로그아웃 실패.");
					}
				},
				onFailure: function (e) {
					return alert("member_join.js error HTTPRequest Err:" + e);
				}
			} 
		);
}

/**
 * MyPage
 */
Index.myPage = function(){
	location.href = Constants.CONTEXT_NAME+'/member/my.do';
}

/**
 * 최신글탭
 */
Index.newestScript = function(e){
	Index.changeTab(Event.element(e).id);
	Index.getPost(1, 20, 1);
}

/**
 * 인기글탭
 */
Index.popularScript = function(e){
	Index.changeTab(Event.element(e).id);
	Index.getPost(0, 20, 1);
}

Index.changeTab = function (id) {
	if (id == 'popularScript') { //인기글 클릭
		$('popularScript').src = '/mbnweb/image/main/tab_02_up.gif';
		$('newestScript').src = '/mbnweb/image/main/tab_01_over.gif';
		
	} else { //최신글 클릭
		$('popularScript').src = '/mbnweb/image/main/tab_02_over.gif';
		$('newestScript').src = '/mbnweb/image/main/tab_01_up.gif';
	}
}


/**
 * 최신글 1/ 인기글 0 가져오기
 */
var localType = "";
var localStart = "";
var localPageCount = "";
Index.getPost = function(type, pageCount, start){
	$('start').value = start;
	$('type').value = type;
	$('pageCount').value = pageCount;
	
	$('searchForm').action = Constants.CONTEXT_NAME + '/index.do';
	$('searchForm').submit();
}

Index.setPost = function(json){	
	Index.setPostJson(json);
	if ($('pageNaviArea')) {
		$('pageNaviArea').show();
	}
}

Index.setPostJson = function(result){
	var manage_role = $('manage_role').innerHTML;
	try {	
		$('pageNaviArea').update(result.pageNavi);
		
		var html = '';

		for (var i = 0; i < result.posts.length; i++) {
			var link = "'";
			link += result.posts[i].link;
			link += "'";
			
			if (result.posts[i].itemimgMid.trim() != '') {
				html += '<div class="list_thum thum2" style="float: left; display: inline;">';
				html += '<img src="' + result.posts[i].itemimgMid + '" />';
				html += '</div>';
				
				html += '<div id="list" ';
				html += 'style="width: 520px; float: left; display: inline; margin-right: 20px;"> ';
				html += '<ul> ';
				html += '<li class="black_title" style="height: 22px; overflow: hidden;"><a href = "#" ';
				html += ' onclick = "javascript:Index.read.post(' + link + ',' + result.posts[i].dataSeq +'); return false;" >' + result.posts[i].title + '</a></li> ';
				html += '<li style="height: 15px; overflow: hidden;"><span ';
				html += 'class="gray11_text">' + result.posts[i].pubDate + '&nbsp;|&nbsp;</span><a  target = "blank" ';
				html += 'href="' + result.posts[i].blogAddr + '"><span class="khaki11_text">' + result.posts[i].blogName + '</span></a></li> ';
				html += '<li class="common_text" ';
				html += 'style="padding-top: 4px; height: 33px; overflow: hidden;"> ';
				html += '' + result.posts[i].rssDesc + '</li> ';
				html += '</ul> ';
				html += '</div> ';
			}else{
				html += '<div class="list_thum thum2" style="float: left; display: none;">';
				html += '<img src="' + result.posts[i].itemimgMid + '" />';
				html += '</div>';
				
				html += '<div id="list" ';
				html += 'style="width: 620px; float: left; display: inline; padding-left: 15px; margin-right: 20px;"> ';
				html += '<ul> ';
				html += '<li class="black_title" style="height: 22px; overflow: hidden;"><a href = "#" ';
				html += ' onclick = "javascript:Index.read.post(' + link + ',' + result.posts[i].dataSeq +'); return false;" >' + result.posts[i].title + '</a></li> ';
				html += '<li style="height: 15px; overflow: hidden;"><span ';
				html += 'class="gray11_text">' + result.posts[i].pubDate + '&nbsp;|&nbsp;</span><a  target = "blank" ';
				html += 'href="' + result.posts[i].blogAddr + '"><span class="khaki11_text">' + result.posts[i].blogName + '</span></a></li> ';
				html += '<li class="common_text" ';
				html += 'style="padding-top: 4px; height: 33px; overflow: hidden;"> ';
				html += '' + result.posts[i].rssDesc + '</li> ';
				html += '</ul> ';
				html += '</div> ';
			}
			
			
			
			if (manage_role != '' && manage_role != 'null' && manage_role != '0') {
				html += '<div id="count_box" style="float: right; display: inline; position: relative; top: -15px;"> ';
				html += '<ul> ';
				html += '<li style="text-align: right;">'
				if($('typeSet').value != 1){
					html += "<span style='font-size:10pt; color:#0C315E; font-family:Malgun Gothic,맑은 고딕,돋움,Dotum,sans-serif;font-weight:bold;line-height:1.6;'>"+result.posts[i].point+"</span>";
				}
				html += '</li>'
			} else {
				html += '<div id="count_box" style="float: right; display: inline;"> ';
				html += '<ul> ';
			}
			
			html += '<li class="cms_up" style="text-align: right; padding-right: 5px;">';
			
			if(manage_role != '' && manage_role != 'null' && manage_role != '0'){
											  
				html += '<img onclick = "javascript:Index.cmsPopUpTag.open(' + result.posts[i].dataSeq + ');" style = "cursor:pointer;"';
				html += 'src="/mbnweb/image/common/icon/icom_cms_minus.gif" ';
				html += 'alt="스팸,추천,동의어,태그입력" title="스팸,추천,동의어,태그입력" />';
			}
			
			html += '</li> ';
			html += '<li id = "upValueLi' + result.posts[i].dataSeq + '" onclick = "javascript:Index.upValue(' + result.posts[i].dataSeq + ', ' + result.posts[i].upValue + ');" style = "cursor:pointer" class="but_bg"> ';
			html += '<div id="but_click' + result.posts[i].dataSeq + '" align="center" style="padding-top: 8px;">';
			
			if(manage_role != '' && manage_role != 'null' && manage_role != '0'){
				html += '<span id = "span_upValue' + result.posts[i].dataSeq + '" class = "input_commend">' + result.posts[i].upValue + '</span></div>';
			}else{
				html += '<span id = "span_upValue' + result.posts[i].dataSeq + '" class = "input_commend">';
				
				if(result.posts[i].upValue < 0){
					html += "0";
				}else{
					html += result.posts[i].upValue;
				}
				
				html += '</span></div>';
			}
			
			html += '</li> ';
			html += '</ul> ';
			html += '</div> ';
			
			html += '<div id="s_tag_bg" style="clear: both;"> ';
			html += '<div id="tag_text" class="blue11_text" ';
			html += 'style="padding: 10px 15px 0 40px; text-align: left;"> ';
			
			for (var j = 0; j < result.posts[i].tags.length; j++) {				
				html += '<a style = "cursor:pointer;" href = "#" onclick = "javascript:Index.searchFormSubmit(\'tag\', \'' + result.posts[i].tags[j] + '\'); return false;"  class="pl05">' + result.posts[i].tags[j] + '</a> ';
			}
			
			html += '</div> ';
			html += '</div> ';
			
		}
		$('mainPost').update(html);
	} catch (e) {
		alert(e);
	}	
}

/**
 * 추천 기능
 * data_seq
 */
Index.upValue = function(data_seq, upValuePrev){
	var infoElement = $("but_click"+data_seq).cumulativeOffset();
	var upValueNow = $('span_upValue'+data_seq).firstChild.nodeValue;
	
	var myAjax = new Ajax.Request( 
		Constants.CONTEXT_NAME + '/index/upValue.do', 
		{
			method: 'post', 
			encoding: "utf-8",
			parameters: {
				data_seq : data_seq,
				upValuePrev : upValuePrev
			}, 
			onLoaded: function () {
				$('but_click'+data_seq).update('<img src = "/mbnweb/image/common/loading.gif" style = "left : '+infoElement[0]+'; right : '+infoElement[1]+'"/>');				
			}, 
			onComplete: function(xhr){
				var json = xhr.responseText.evalJSON(true);
				var result = json.result[0].result;
				var upValue = json.result[0].upValue;
				
				//upValue = 해당 포스트의 추천 점수
				//result = 0 : 추천안됨, 1 : 추천됨, 2 : 이미 추천 하였슴
				if(result == 0){
					alert("추천 되지 않았습니다.");
					var html = '';
					html += '<div id="but_click'+data_seq+'" align="center" style="padding-top: 8px;">';
					html += '<span id = "span_upValue'+data_seq+'" class = "input_commend">'+upValueNow+'</span></div>';
					
					$('upValueLi'+data_seq).update(html);
				}else if(result == 1){
					alert("추천 되었습니다.");
					var html = '';
					html += '<div id="but_click'+data_seq+'" align="center" style="padding-top: 8px;">';
					html += '<span id = "span_upValue'+data_seq+'" class = "input_commend">'+upValue+'</span></div>';
					
					$('upValueLi'+data_seq).update(html);
				}else if(result == 2){
					alert("이미 추천 한 포스트 입니다.");
					var html = '';
					html += '<div id="but_click'+data_seq+'" align="center" style="padding-top: 8px;">';
					html += '<span id = "span_upValue'+data_seq+'" class = "input_commend">'+upValueNow+'</span></div>';
					
					$('upValueLi'+data_seq).update(html);
				}
			},
			onFailure: function (e) {
				return alert("member_join.js error HTTPRequest Err:" + e);
			}
		} 
	);
}


/**
 * hot5
 */
Index.hot5 = function(e, arrow){
	var manage_role = $('manage_role').innerHTML;
	var type = 'HOT5';
	$(arrow).setStyle({
		'display':''
	});
	e.removeClassName('hot5_text'); 
	e.addClassName('hot5_text1');

	var hotTag = e.firstChild.nodeValue;
	var jsonLength = hot5Json.tags.length;
	Index.hot5Empty(jsonLength);
	
	for(var i = 0 ; jsonLength > i; i++){
		if(hotTag == hot5Json.tags[i].name){
			if(hot5Json.tags[i].posts.length > 0){
				//1번
				$('hot5_title_01').update(hot5Json.tags[i].posts[0].title.truncate(25, '...'));
				$('hot5_content_01').update(hot5Json.tags[i].posts[0].rssDesc+"&nbsp;&nbsp;|&nbsp;&nbsp;<a style='color:#929278;' target='_blank' href='"+
											hot5Json.tags[i].posts[0].blogAddr+"'>"+hot5Json.tags[i].posts[0].blogName+"</a>");
				$('hot5_title_01').href = hot5Json.tags[i].posts[0].link;
				
				if (manage_role != '' && manage_role != 'null' && manage_role != '0') {
					var hot5_title_span_tmp = '';
					hot5_title_span_tmp += ' <img onclick = "javascript:Index.cmsPopUpTag.hotFiveOpen('+hot5Json.tags[i].posts[0].dataSeq+');" ';
					hot5_title_span_tmp += ' style = "cursor:pointer;" src="/mbnweb/image/common/icon/icom_cms_minus.gif" ';
					hot5_title_span_tmp += 'alt="스팸,추천,동의어,태그입력" title="스팸,추천,동의어,태그입력" />';
					
					$('hot5_title_01_span').update(hot5_title_span_tmp+"&nbsp;&nbsp;"+"<span style='font-size:10pt;'>"+hot5Json.tags[i].posts[0].point)+"</span>";
				}
				
				
				if(hot5Json.tags[i].posts[0].itemimgMid.length > 0){
					$('hottitle_img').setStyle({
						'display' : '' 
					});
					
					$('hot5_img_01').src = hot5Json.tags[i].posts[0].itemimgMid;
				}else{
					$('hottitle_img').setStyle({
						'display' : 'none' 
					});
//						$('hot5_title_01_span').update('');
				}
				
				
				if (hot5Json.tags[i].posts.length > 1) {
					for (var k = 1; k < 4; k++) {
						if (typeof(hot5Json.tags[i].posts[k]) != 'undefined') {
							if (hot5Json.tags[i].posts[k].title.strip().length > 0) {
								//2 ~ 3 ~ 4 번
								$('hot5_title_0' + (k + 1)).update(hot5Json.tags[i].posts[k].title.truncate(30, '...'));
								$('hot5_blog_0' + (k + 1)).update(hot5Json.tags[i].posts[k].blogName);
								$('hot5_title_0' + (k + 1)).href = hot5Json.tags[i].posts[k].link;
								$('hot5_blog_0' + (k + 1) + '_a').href = hot5Json.tags[i].posts[k].blogAddr;
								
								if (manage_role != '' && manage_role != 'null' && manage_role != '0') {
									var hot5_title_span_tmp_02 = '';
									hot5_title_span_tmp_02 += ' <img onclick = "javascript:Index.cmsPopUpTag.hotFiveOpen(' + hot5Json.tags[i].posts[k].dataSeq + ');" ';
									hot5_title_span_tmp_02 += ' style = "cursor:pointer;" src="/mbnweb/image/common/icon/icom_cms_minus.gif" ';
									hot5_title_span_tmp_02 += 'alt="스팸,추천,동의어,태그입력" title="스팸,추천,동의어,태그입력" />';
									
									$('hot5_title_0' + (k + 1) + '_span').update(hot5_title_span_tmp_02+"&nbsp;&nbsp;"+"<span style='font-size:10pt; color:#0C315E; font-family:Malgun Gothic,맑은 고딕,돋움,Dotum,sans-serif;font-weight:bold;line-height:1.6;'>"+hot5Json.tags[i].posts[k].point+"</span>");
								}
							}
						}else {
							$('hot5_title_0' + (k + 1) + '_span').update('');
						}
					}
				}else{
					for (var k = 1; k < 4; k++) {
						//2 ~ 3 ~ 4 번
						$('hot5_title_0' + (k + 1) + '_span').update('');
					
					}
				}
			}
		}
	}
	
	for(var i = 1 ; i < 4; i++){
		if(!$('hot5_title_0' + (i + 1)).firstChild){
			$('hot5_li_0' + (i + 1)).removeClassName('hottitle_l3_text');
			$('hot5_title_0' + (i + 1) + '_child').update('');
			$('hot5_title_0' + (i + 1) + '_span').update('');
		}else{
			$('hot5_li_0' + (i + 1)).addClassName('hottitle_l3_text');
			$('hot5_title_0' + (i + 1) + '_child').update('|');
		}
	}
}

Index.hot5Empty = function(jsonLength){
	for(var i = 0 ; i < jsonLength ; i++){
		$('hot5_title_01').update('');
		$('hot5_content_01').update('');
		$('hot5_title_01').href = '';
		
		$('hottitle_img').setStyle({
			'display' : 'none' 
		});
		$('hot5_title_01_span').update('');
		for(var k = 1 ; k < hot5Json.tags[i].posts.length ; k++){
			//2 ~ 3 ~ 4 번
			$('hot5_title_0' + (k + 1)).update('');
			$('hot5_blog_0' + (k + 1)).update('');
			$('hot5_title_0' + (k + 1)).href = '';
			$('hot5_blog_0' + (k + 1) + '_a').href = '';
		}
	}
}

Index.hot5_out = function(e, arrow){
//	$(arrow).setStyle({
//		'display':'none'
//	});
//	
//	e.removeClassName('hot5_text1'); 
//	e.addClassName('hot5_text');
	
//	$('hot5_hotarrow01').setStyle({
//		'display':''
//	});
//			
//	$('hot5_tag01').addClassName('hot5_text1');
	
	var arrow = "hot5_hotarrow0";
	var tag = "hot5_tag0";
	for(var i = 1 ; i < 6 ; i++){
		$('hot5_hotarrow0'+i).setStyle({
			'display':'none'
		});
		
		$('hot5_tag0'+i).removeClassName('hot5_text1'); 
		$('hot5_tag0'+i).addClassName('hot5_text');
	}
}

Index.HighlightEffect = function(element){
//	new Effect.Highlight(element, 
//	  {
//	    startcolor: "#ff0000",
//	    endcolor: "#0000ff",
//	    restorecolor: "#F2F8FE",
//	    duration: 0.5
//	  })
}

/**
 * 메인 공지
 * @param {Object} json, type = 0 : back , 1 : position ,2 : forward
 */
var noticeIncre = 0;
var inter;
//Util.PopUp.open(Constants.CONTEXT_NAME + '/cms/popUpHot.do?type=hot5', 500, 400);
Index.mainNotice = function(type){
	var json = mainNotice;
	
		if (json.notices.length == 0) {
			return;
		}
		
		$('noticeAlink').update(json.notices[0].title);
		$('noticeAlink').href = 'javascript:Index.mainNoticePopUp(' + json.notices[0].noticeSeq + ');';
		if (type != 1) {
			if (type == 0) {
				if (noticeIncre == 0) {
					noticeIncre = json.notices.length - 1;
				}
				else {
					noticeIncre--;
				}
				
				$('noticeAlink').update(json.notices[noticeIncre].title);
				$('noticeAlink').href = 'javascript:Index.mainNoticePopUp(' + json.notices[noticeIncre].noticeSeq + '); return false;';
				window.clearInterval(inter);
			}
			else 
				if (type == 2) {
					if (noticeIncre == json.notices.length - 1) {
						noticeIncre = 0;
					}
					else {
						noticeIncre++;
					}
					
					$('noticeAlink').update(json.notices[noticeIncre].title);
					$('noticeAlink').href = 'javascript:Index.mainNoticePopUp(' + json.notices[noticeIncre].noticeSeq + ');';
					window.clearInterval(inter);
				}
		}
		
		inter = self.setInterval(function(){
			noticeIncre++;
			if (noticeIncre + 1 > json.notices.length) {
				noticeIncre = 0;
			}
			$('noticeAlink').update(json.notices[noticeIncre].title);
			$('noticeAlink').href = 'javascript:Index.mainNoticePopUp(' + json.notices[noticeIncre].noticeSeq + ');';
		}, 5000);
		
}

/**
 * 메인 공지사항 팝업
 * @param {Object} json
 * http://localhost:9090/mbnweb/notice/pop.do?noticeSeq=2
 */
Index.mainNoticePopUp = function(noticeSeq){
//	Util.PopUp.open(Constants.CONTEXT_NAME + '/notice/pop.do?noticeSeq='+noticeSeq, 558, 688);
	Util.PopUp.open(Constants.CONTEXT_NAME + '/notice/pop.do?noticeSeq='+noticeSeq, 550, 688);
}


/**
 * 메인 인기베스트
 * @param {Object} json
 */
Index.popularBest = function(json){
	var postAlink = "popularPost0";
	if(json.main.length > 0){
		for(var i = 0 ; i < json.main.length ; i++){
			try {
				var postAlink_tmp = postAlink + i;
				if(i == 0){
					if(json.main[i].image.trim() != ""){
						$('popularPostImage').src = json.main[i].image;
						$('popularPostImageLi').setStyle({
							'display':''
						});
						$('popularPostImageLi02').className = "best_1 blue_text";
					}
				}
				
				$(postAlink_tmp).update(json.main[i].title);
				$(postAlink_tmp).href = json.main[i].url;
			}catch(e){
				alert("popularBest = " + 	e);
			}
		}
	}
}

/**
 * 메인 인기블로그
 * @param {Object} json
 */
Index.popularBlog = function(json){
	var blogAlink = "popularBlog0";
	if(json.main.length > 0){
		for(var i = 0 ; i < json.main.length ; i++){
			try {
				var blogAlink_tmp = blogAlink + i;
				$(blogAlink_tmp).update(json.main[i].title);
				$(blogAlink_tmp).href = json.main[i].url;
			}catch(e){
				alert("popularBlog  = " + 	e);
			}
		}
	}
}

/**
 * 메인 클라우드
 * @param {Object} json
 */
Index.tagClaud = function(json){
	var manage_role = $('manage_role').innerHTML;
	var type = 'HOT5';
	
	if (hot5Json.tags.length == 0) {
		return;
	}
	
	for (var i = 0; i < hot5Json.tags.length; i++) {
		$('hot5_tag0' + (i + 1)).update(hot5Json.tags[i].name);
	}
	
	$('hot5_hotarrow01').setStyle({
		'display': ''
	});
	
	$('hot5_tag01').addClassName('hot5_text1');
	
		
	
	if (hot5Json.tags[0].posts.length > 0) {
		if (hot5Json.tags[0].posts[0].title.length > 0) {
			//1번
			$('hot5_title_01').update(hot5Json.tags[0].posts[0].title.truncate(25, '...'));
			$('hot5_content_01').update(hot5Json.tags[0].posts[0].rssDesc+"&nbsp;&nbsp;|&nbsp;&nbsp;<a style='color:#929278;' target='_blank' href='"+json.tags[0].posts[0].blogAddr+"'>"+json.tags[0].posts[0].blogName+"</a>");
			$('hot5_title_01').href = hot5Json.tags[0].posts[0].link;
			
			if (manage_role != '' && manage_role != 'null' && manage_role != '0') {
				
				var hot5_title_span_tmp = '';
				hot5_title_span_tmp += ' <img onclick = "javascript:Index.cmsPopUpTag.hotFiveOpen('+hot5Json.tags[0].posts[0].dataSeq+');" ';
				hot5_title_span_tmp += ' style = "cursor:pointer;" src="/mbnweb/image/common/icon/icom_cms_minus.gif" ';
				hot5_title_span_tmp += 'alt="스팸,추천,동의어,태그입력" title="스팸,추천,동의어,태그입력" />';
				
				
				$('hot5_title_01_span').update(hot5_title_span_tmp+"&nbsp;&nbsp;"+"<span style='font-size:10pt;'>"+hot5Json.tags[0].posts[0].point+"</span>");
			}
			
			if (hot5Json.tags[0].posts[0].itemimgMid.length > 0) {
				$('hottitle_img').setStyle({
					'display': ''
				});
				
				$('hot5_img_01').src = hot5Json.tags[0].posts[0].itemimgMid;
			}
			else {
				$('hottitle_img').setStyle({
					'display': 'none'
				});
			}
		}
		
		for (var i = 1; i < hot5Json.tags[0].posts.length; i++) {
			if (hot5Json.tags[0].posts[i].title.length > 0) {
				//2번
				$('hot5_title_0' + (i + 1)).update(hot5Json.tags[0].posts[i].title.truncate(30, '...'));
				$('hot5_blog_0' + (i + 1)).update(hot5Json.tags[0].posts[i].blogName);
				$('hot5_title_0' + (i + 1)).href = hot5Json.tags[0].posts[i].link;
				$('hot5_blog_0' + (i + 1) + '_a').href = hot5Json.tags[0].posts[i].blogAddr;
				
				if (hot5Json.tags[0].posts[i].title.strip() != '') {
					if (manage_role != '' && manage_role != 'null' && manage_role != '0') {
						var hot5_title_span_tmp = '';
						hot5_title_span_tmp += ' <img onclick = "javascript:Index.cmsPopUpTag.hotFiveOpen(' + hot5Json.tags[0].posts[i].dataSeq + ');" ';
						hot5_title_span_tmp += ' style = "cursor:pointer;" src="/mbnweb/image/common/icon/icom_cms_minus.gif" ';
						hot5_title_span_tmp += 'alt="스팸,추천,동의어,태그입력" title="스팸,추천,동의어,태그입력" />';
						
						$('hot5_title_0' + (i + 1) + '_span').update(hot5_title_span_tmp+"&nbsp;&nbsp;"+"<span style='font-size:10pt;font-size:10pt; color:#0C315E; font-family:Malgun Gothic,맑은 고딕,돋움,Dotum,sans-serif;font-weight:bold;line-height:1.6;'>"+hot5Json.tags[0].posts[i].point+"</span>");
					}
				}else{
					$('hot5_title_0' + (i + 1) + '_span').update('');
				}
			}
		}
		
		for(var i = 1; i < 4; i++){
			if(!$('hot5_title_0' + (i + 1)).firstChild){
				$('hot5_li_0' + (i + 1)).removeClassName('hottitle_l3_text');
				$('hot5_title_0' + (i + 1) + '_child').update('');
				$('hot5_title_0' + (i + 1) + '_span').update('');
			}else{
				$('hot5_li_0' + (i + 1)).addClassName('hottitle_l3_text');
				$('hot5_title_0' + (i + 1) + '_child').update('|');
			}
		}
	}
}

/**
 * 포스트 클릭시 조회수 증가
 */
Index.read = {
	post : function(url, data_seq){
		window.open(url, "_blank");
		
		var myAjax = new Ajax.Request( 
			Constants.CONTEXT_NAME + '/index/readCnt.do', 
			{
				method: 'post', 
				encoding: "utf-8",
				parameters: {
					data_seq : data_seq
				}, 
				onLoaded: function () {
					
				}, 
				onComplete: function(xhr){
//					location.href = url;
				},
				onFailure: function (e) {
					return alert("member_join.js error HTTPRequest Err:" + e);
				}
			} 
		);
	}
}

/**
 * tag 관련 CMS 팝업 콜
 * @param {Object} data_seq : 글번호
 */
Index.cmsPopUpTag = {
	open : function(data_seq){
		Util.PopUp.open(Constants.CONTEXT_NAME + '/cms/popUpTag.do?dataSeq='+data_seq, 440, 340);
	},
	close : function(){
		Util.PopUp.close();
	},
	hotFiveOpen : function(data_seq){
		Util.PopUp.open(Constants.CONTEXT_NAME + '/cms/popUpTag.do?dataSeq='+data_seq+'&type=hotFive', 440, 340);
	}
}

/**
 * Hot5 관련 CMS 팝업 콜
 */
Index.cmsPopUpHot5 = {
	open: function(value){
		if(value == 'hot5'){
			Util.PopUp.open(Constants.CONTEXT_NAME + '/cms/popUpHot.do?type=hot5', 440, 380);
		}else if(value == 'issue'){
			Util.PopUp.open(Constants.CONTEXT_NAME + '/cms/popUpHot.do?type=issue', 440, 530);
		}else{
			alert('파라미터 전송 실패');
		}
		
	},
	close : function(){
		Util.PopUp.close();
	}
}

/**
 * @param {Object} searchType ['tag','blog']
 * @param {Object} keyword 
 * @param {Object} type [newest: 1, popular : 0]
 */
Index.searchFormSubmit = function (searchType, keyword, type) {
	try {
		if (keyword != null && !keyword.blank()) {
			//TODO  유효성 체크.., 나중에
			$('searchFormkeyword').value = encodeURIComponent(keyword);
			
			if (type) {			
				$('searchFormListType').value = type;
			} else {
				$('searchFormListType').value = '0';
			}
			$('searchFormType').value = searchType; 
			$('searchForm').action = Constants.CONTEXT_NAME +  '/search.do';
			$('searchForm').submit();
			return true;
		} else {
			alert('검색어를 입력하세요!');
			return false;	
		}	
	} catch (e) {
		alert('Index.searchFormSubmit err=' + e);
	}
}

Index.searchFormSubmitImg = function(type){
	if(type == '0'){
		$('popularScript').src = "/mbnweb/image/main/tab_02_up.gif";
		$('newestScript').src = "/mbnweb/image/main/tab_01_over.gif";
	}else if(type == '1'){
		$('popularScript').src = "/mbnweb/image/main/tab_02_over.gif";
		$('newestScript').src = "/mbnweb/image/main/tab_01_up.gif";
	}else{
		$('popularScript').src = "/mbnweb/image/main/tab_02_up.gif";
		$('newestScript').src = "/mbnweb/image/main/tab_01_over.gif";
	}
}

Index.focusByTabKey = function (e) {
	if (Util.getKeyCode(e) == 9) { //TAB
		if ($('user_pw')) {
			$('user_pw').focus();
			return false;
		}
	}
	return true;
}

Index.HotFive = {
	prvRank: [],
	list: {},
	open: function (e) {	
		Util.PopUp.captureOpen(Constants.CONTEXT_NAME + '/admin/hotFive/list.do', 980, 400, Index.HotFive.getJsonList);
	},
	print: function (list) {	
		Index.HotFive.spanPrint(list);
		Index.HotFive.otherSpanPrint(list);
		Event.observe('selectRank', 'change', Index.HotFive.selectRank);	
		Event.observe('submitBt', 'click', Index.HotFive.update);	
		
		Index.HotFive.list = list;		
		Index.HotFive.prvRank = list.rank.toArray();		
	},
	spanPrint: function (list, highlightRank) {
		var spans = $('hotFiveArea').select('span.hot5_text');
		var as = $('hotFiveArea').select('a');
		list.rank.each(function (tag, index) {
			spans[index].innerHTML = tag;
			if (highlightRank == index) {
				new Effect.Highlight(spans[index].parentNode.parentNode);
			}
			as[index].href = Constants.CONTEXT_NAME + 
					'/search.do?start=0&pageCount=20&type=0&type=0&searchType=tag&keyword=' + encodeURIComponent(tag);	
					
		});
	},
	otherSpanPrint: function (list) {
		var otherSpans = $('preHotFiveArea').select('span.hot5_text');
		var otherInputs = $('preHotFiveArea').select('input');
		var otherAs = $('preHotFiveArea').select('a');
		list.tags.each(function (each, index) {
			if (each == '') {
				otherInputs[index].disabled = true;
			} else {
				otherSpans[index].innerHTML = each;
				otherInputs[index].value = each;
				otherAs[index].href = Constants.CONTEXT_NAME + 
					'/search.do?start=0&pageCount=20&type=0&type=0&searchType=tag&keyword=' + encodeURIComponent(each);	
			}
		});
	},
	getJsonList: function () {
		new Ajax.Request( 
			Constants.CONTEXT_NAME + '/admin/hotFive/preHotFives.do', 
			{
				onComplete: function(xhr){					
					Index.HotFive.print(xhr.responseText.evalJSON());
				},
				onException: function (xhr, error) {
					return alert("HOT 5 예비 목록을 구성하는 중에, 에러가 발생했습니다.:\n" + error);
				}
			} 
		);
	},
	selectRank: function (e) {
		var selectRank = $F('selectRank');
		$('preHotFiveArea').select('input').each(function (input) {
			if (input.checked) {
				Index.HotFive.list.rank[selectRank -1] = $F(input);
			}
		});
		Index.HotFive.spanPrint(Index.HotFive.list, selectRank -1);
		$('selectRank').value = 0;
	},
	update: function (e) {		
		Index.HotFive.updateRequest(Index.HotFive.list.rank); 
	},
	updateRequest: function (tagNames, isWrite) {
		var isNotChange = true;
		for (var i = 0; i < tagNames.length; i++) {
			if (tagNames[i] != Index.HotFive.prvRank[i]) {
				isNotChange = false;
				break;
			}
		}		
		if (isNotChange) {
			return alert('변경할  HOT 5 가 없습니다.');
		}
		
		var parameters = {
			tagNames: tagNames
		};
		//관리자  hot5 직접 수정한 경우
		if (isWrite) {
			parameters.isWrite = true;
		}
		
		new Ajax.Request( 
			Constants.CONTEXT_NAME + '/admin/hotFive/update.do', 
			{
				parameters: parameters,
				onComplete: function(xhr){			
					var json = xhr.responseText.evalJSON();		
					if (json.result == 'success') {
						alert("HOT 5 가 업데이트 되었습니다.");
						location.href = Constants.CONTEXT_NAME + "/index.do";
					} else {
						throw 'error';
					}
				},
				onException: function (xhr, error) {
					return alert("HOT 5 예비 목록을 업데이트하는 중에, 에러가 발생했습니다.:\n" + error);
				}
			} 
		);		
	}
}