function browserCheck(){	
	var result;
	
	if( navigator.appName.indexOf("Microsoft") > -1 ){ // IE?
		if( navigator.appVersion.indexOf("MSIE 6") > -1) { // IE6?
			result = 'IE6';
		}
		else{
			result = 'NON-IE6';
		}
	}
	
	return result;
}

function posting_delete(post_id, view_page, p_post_id){
	if(view_page == 'commenting' || view_page == 'commented'){
		comment_del(p_post_id, post_id, view_page);
		return false;
	}
	if( confirm('작성된 글을 삭제하시겠습니까?') ){
		$.ajax({
		  url: '/posting/delete',
		  type: 'POST',
		  data:{
			post_id : post_id
		  },
		  datatype: 'XML',
		  timeout: 10000,
		  success: function(xml)
		  {
			$('#posting_'+post_id).hide('fast');
			showmsg('포스팅이 삭제되었습니다.');
			pageVariable--;
		  }
		});
	}
	else{
		return;
	}
}

function tag_modify(post_id){
	$('#tag_'+post_id+'>span.tag_contents').css('display', 'none');
	$('#tag_'+post_id+'>span.tag_modify').css('display', 'block');
	$('#tag_'+post_id+'>span.tag_modify>form>input.tag_text').focus();
}

function tag_modify_ok(post_id){
	
	var tag = $('#tag_'+post_id+'>span.tag_modify>form>input.tag_text').val();
	$.ajax({
		  url: '/posting/tag_modify',
		  type: 'POST',
		  data:{
			post_id : post_id,
			tag : tag
		  },
		  datatype: 'HTML',
		  timeout: 10000,
		  success: function(html)
		  {
			if(html=="LoginTimeout"){
				showmsg('로그인 세션이 만료되었습니다');
				return false;
			}

			$('#tag_'+post_id+'>.tag_contents>.tag_str').html(html);
			//alert(post_id);
			tag_modify_hidden(post_id);
			showmsg('태그가 수정되었습니다');
		  }
	});
}

function tag_modify_hidden(post_id){
	$('#tag_'+post_id+'>span.tag_modify').css('display', 'none');
	$('#tag_'+post_id+'>span.tag_contents').css('display', 'block');
}

function comment_num(id, html){ //Comment_num 갱신
	num = $(html).find("#comment_num").val();
	if(num == "0")
		num = '+';
	$('#comment_num_'+id).html(num);
}

function commenting(id, post_id, user_id, login_id){
	if(login_id==""){
		openLoginWindow();
		return false;	
	}
	if($('input[name=comment_contents_'+id+']').val()==""){
		showmsg('내용을 입력해 주시기 바랍니다.');
		return false;
	}
	$.ajax({
		  url: '/comment/write',
		  type: 'POST',
		  data:{
			id : id, 
			post_id : post_id,
			commented_user_id : user_id,
			contents : strip_lt($('input[name=comment_contents_'+id+']').val()),
			comment_flag : "1",
			timestamp : new Date()
		  },
		  datatype: 'HTML',
		  timeout: 10000,
		  success: function(html)
		  {
			comment_num(id, html);
			$('#comment_list_'+id).html($(html).find("#comment_list").html());
			showmsg("댓글이 작성되었습니다.");

			$('input[name=comment_contents_'+id+']').focus();
		  }
	});
}
function comment_del(id, comment_id, view_page){
	if( confirm('작성된 댓글을 삭제하시겠습니까?') ){
		$.ajax({
		  url: '/comment/del',
		  type: 'POST',
		  data:{
			  comment_id:comment_id,
			  timestamp : new Date()
		  },
		  datatype: 'HTML',
		  timeout: 10000,
		  success: function(html)
		  {
			    $('div[commentidx='+comment_id+']').slideUp('fast');
			    var commentNum = parseInt($('span[commentnumidx='+id+']').html());
			    $('span[commentnumidx='+id+']').html(commentNum-1);
			    showmsg('댓글이 삭제되었습니다');
		  }
		});
	}
	else{
		return;
	}
}

function comment_list(post_id, user_id, p_post_id){ 
	
	var id;
	if(p_post_id != ""){
		id = post_id;
		post_id = p_post_id;
	}else{
		id = post_id;
	}
	
	if($('#comment_list_'+id).html().length==0){
		$.ajax({
		  url: '/comment/read',
		  type: 'GET',
		  data:{
			  id:id,
			  post_id: post_id,
			  commented_user_id: user_id,
			  timestamp : new Date()
		  },
		  datatype: 'HTML',
		  timeout: 10000,
		  success: function(html)
		  {
  			comment_num(id, html);
			$('#comment_list_'+id).html($(html).find("#comment_list").html());
			$('#comment_list_'+id).slideDown(70, function(){
				if($('input[name=comment_contents_'+id+']').val() == ""){
					$('input[name=comment_contents_'+id+']').focus();
				}
			});
			 //$('#comment_'+post_id).append(html)
		  }
		});
	}else{
		$('#comment_list_'+id).slideUp(70, function(){$('#comment_list_'+id).html('');});
	}

}

function footnote_list(post_id, num){
	if(num==0){
		location.href="/posting/via/"+post_id;
	}else{
		if($('#footnote_list_'+post_id).html()==''){
			$.ajax({
			  url: '/footnote/read',
			  type: 'GET',
			  data:{
				  tagged_post_id: post_id,
				  timestamp : new Date()
			  },
			  datatype: 'HTML',
			  timeout: 10000,
			  success: function(html)
			  {
				$('#footnote_list_'+post_id).html($(html).find('#footnote_list').html());
				 //$('#comment_'+post_id).append(html)
			  }
			});
		}else{
			$('#footnote_list_'+post_id).html('');
		}
	}
}

function scrap(post_id, a){
	
	var img_obj = $(a).find('img');
	var scrap_flag;

	if(img_obj.attr("value") == "off"){ //Bookmark
		imgon(img_obj.get());
		scrap_flag = 1;
		img_obj.attr("value", "on");
	}else{
		scrap_flag = 2;
		img_obj.attr("value", "off");
		imgoff(img_obj.get());
	}

	$.ajax({
	  url: '/posting/scraping',
	  type: 'POST',
	  data:{
		  post_id: post_id,
		  scrap_flag:scrap_flag
	  },
	  datatype: 'XML',
	  timeout: 10000,
	  success: function(html)
	  {
			  if(scrap_flag == 1){
				showmsg("스크랩 되었습니다.");
			  }else{
				showmsg("스크랩 해제되었습니다.");
			  }
	  }	
	});
}

var current_over_post_id = -1;
var tid;

function showObjectDescription(post_id, p_post_id, user_id, show_flag){
	
	var version = browserCheck();
	
	tid = setTimeout(function(){
		if( version == 'IE6'){
			$('#object_description_'+post_id).show();
		}
		else{
			$('#object_description_'+post_id).fadeIn('fast');
			$('#object_description_'+post_id).fadeTo('fast', 1);
		}		
	}, 30);
	
	if( show_flag == 0 ){
		callUserThumbnailOnObject(post_id, p_post_id,user_id);
	}	
	$('#object_description_'+post_id).stop();	
}

function hideObjectDescription(post_id){
	
	var version = browserCheck();
	
	clearTimeout(tid);
	$('#object_description_'+post_id).stop();
	
	if( version == 'IE6'){
		$('#object_description_'+post_id).hide();
	}
	else{
		$('#object_description_'+post_id).fadeOut('fast');
	}
}

function callUserThumbnailOnObject(post_id,p_post_id,user_id){	
	$.ajax({
		url: '/posting/thumbcallback',
		type: 'GET',
		data:{
		  post_id : post_id,
		  p_post_id: p_post_id,
		  user_id: user_id
		},
		datatype: 'HTML',
		timeout: 10000,
		success: function(html){
			$('#object_user_thumb_'+post_id).html(html);
		}
	});	
}