// set image SIZE
// ________________________________________________

function resizeImage (imageobj) 
{
	var contentsize = $("#boardbody").width();
	var imgmaxsize = $('#imgmaxsize').val();
	imageobj.style.display = 'inline';
	
	if (imgmaxsize <= 100) {
		imgboxsize = contentsize - document.viewinfo.vbw.value * 2; // 전체크기에서 보더의 2배만큼 감소
		imgmaxsize = Math.round(imgboxsize * imgmaxsize / 100);
	}

	if (imageobj.width > imgmaxsize) {
		imageobj.width = imgmaxsize;
	}
}

// go previous or next pages
function goprev () 
{
	var previdx = $('#previdx').val();
	var urlfornav = $("#urlfornav").val();

	if (previdx) {
		window.location.href = urlfornav + '&idx=' + previdx;
	}
}

function gonext () 
{
	var nextidx = $('#nextidx').val();
	var urlfornav = $("#urlfornav").val();
	
	if (nextidx) {
		window.location.href = urlfornav + '&idx=' + nextidx;
	}
}

// event handler
$(function()
{
	var g = document.getElementById('addgood');
	var b = document.getElementById('addbad');
	var m = document.getElementById('domodify');
	var d = document.getElementById('dodelete');
	var r = document.recoform;
	var o = document.opform;
	
	if (g) {
		g.onclick = function () {
			if (confirm('추천 하시겠습니까?')) {
				r.mode.value = "reco";
				r.submit();
				return true;
			} 
			
			return false;
		}
	}
	
	if (b) {
		b.onclick = function () {
			if (confirm('신고 하시겠습니까?')) {
				r.mode.value = "repo";
				r.submit();
				return true;
			} 
			
			return false;
		}
	}
	
	if (m) {
		m.onclick = function () {
			if (confirm('수정 하시겠습니까?')) {
				o.mode.value = "trymodify"
				o.submit();
				return true;
			}
			
			return false;
		}
	}
	
	if (d) {
		d.onclick = function () {
			if (confirm('삭제 하시겠습니까?')) {
				o.mode.value = "trydelete"
				o.submit();
				return true;
			}
			
			return false;
		}
	}
	
	// key navigation
	var enablekeynav = function () {
		shortcut.add("home",function() {
			var urlfornav = $("#urlfornav").val().replace('&code=view', '');
			window.location.href = urlfornav;
		},{'keycode':36});
		
		shortcut.add("page_up",goprev,{'keycode':33});
		shortcut.add("page_down",gonext,{'keycode':34});
		/*shortcut.add('s', function() {
			Shadowbox.open($(".slideview")[0], null);
		},{'keycode':(83)});*/
	}
		
	Shadowbox.init();
	if ($('#viewnav').val()=='1' || $('#viewnav').val()=='3') {
		enablekeynav();
	}
		
	// img event Handlers
	// __________________________________________
	

	$('a[href*=#]').click(function() {
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
		&& location.hostname == this.hostname) {
			var $target = $(this.hash);
			
			$target = $target.length && $target
			|| $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top - 10;
				
				$('html,body')
				.animate({scrollTop: targetOffset}, 0, 'easeOutCirc');
				return false;
			}
		}
	});
	
	
	/**
	 * CONTROL COMMENT
	 */
	
	// COMMON FUNCTINO COMMENT
	// ______________________________________
	
		function checkDocomment ()
		{
			var cn = document.getElementById('coName');
			var cp = document.getElementById('coPasswd');
			
			if (cn) {
				if (cn.value.trim() == '') {
					alert('이름을 입력하세요.');
					cn.focus();
					return false;
				}
			}
			if (cp) {
				if (cp.value == '') {
					alert('비밀번호를 입력하세요.');
					cp.focus();
					return false;
				}
			}
			if ($('textarea[name=coContent]').val().trim() == '') {
				alert('내용을 입력하세요.');
				$('textarea[name=coContent]').focus().val('');
				return false;
			}
			
			return true;
		}
	
	
	// DO COMMENT
	// ______________________________________
	
		$('#btn_docomment').click(function () 
		{
			return checkDocomment();
		});
		
	// DO COMMENT AND VOTE
	// ______________________________________
	
		$('#btn_docommentandvote1').click(function ()
		{
			if (!checkDocomment()) {
				return false;
			}
			
			document.commentForm.mode.value = 'docommentandvote';
		});
	
	// MODIFY COMMENT
	// ______________________________________
	
		$('.btn_modifyopen').click(function ()
		{
			$('.modifybox').remove();
			$('.deletebox').remove();
			
			$(this).parent().append(mbox);
			
			// IS MEMBER'S COMMENT?
			if ($(this).parent().parent().find('.ismember').val()) {
				// REMOVE PASSWORD INPUTBOX
				$('.passwordbox').remove();
			}
			$('.modifybox > textarea').val($(this).parent().parent().find('input[name=coHiddenContent]').val());

			var form = $(this).parent().parent()
			var modeinput = $(this).parent().parent().find('input[name=mode]');
			modeinput.val('modifycomment');


			// SUBMIT BUTTON EVENT HANDLER
			
				$('#btn_modify_submit').click(function ()
				{
					return (
						$(this).parent().parent().parent().find('input[name=mode]').val() == 'modifycomment'
					);
				});
		});
		
	
	// DELETE COMMENT
	// _______________________________________
	
		$('.btn_deleteopen').click(function ()
		{
			$('.modifybox').remove();
			$('.deletebox').remove();
			
			var form = $(this).parent().parent()
			var modeinput = $(this).parent().parent().find('input[name=mode]');
			var ismember = $(this).parent().parent().find('.ismember');
			
			modeinput.val('deletecomment');
			
			// IS MEMBER'S COMMENT?
			if (ismember.val()) {
				// REMOVE PASSWORD INPUTBOX
				if (confirm('삭제하시겠습니까?')) {
					if (modeinput.val() == 'deletecomment') {
						form.submit();
					}
				}
			}
			else {
				$(this).parent().append(dbox);
				
				// SUBMIT BUTTON EVENT HANDLER
			
					$('#btn_delete_submit').click(function ()
					{
						if (confirm('삭제하시겠습니까?')) {
							return (
								$(this).parent().parent().parent().find('input[name=mode]').val() == 'deletecomment'
							)
						}
						else {
							return false;
						}
					});
			}

			return false;
		});
		
	// REPLY COMMENT
	// _______________________________________
	
		$('.btn_replyopen').click(function ()
		{
			$('.modifybox').remove();
			$('.deletebox').remove();
			$('.replybox').remove();
			
			$(this).parent().append(rbox);
			
			// IS MEMBER'S COMMENT?
			if ($('#ismember').val()==1) {
				// REMOVE PASSWORD INPUTBOX
				$('.passwordbox').remove();
			}
			
			var form = $(this).parent().parent()
			var modeinput = $(this).parent().parent().find('input[name=mode]');
			modeinput.val('replycomment');
		});
});