$(document).ready(function() { 
	$.getScript('/js/maxlength.js', function(){
		$("#commentText").maxlength({maxChars:500});
	});

	
	$('.popComment').bind('click', function() {
		var title = $(this).parent().find('.blue_link').html();
		$('#itemTitle').html(title);
		var art_id = $(this).parent().find('.blue_link').attr('rel');
		$('#article_id').attr('rel', art_id);
		$('.jqmWindow').jqm({target: '.target', overlay: '9', modal: true}).jqmShow(); 
	});
});
jQuery.fn.maxlength = function(options) {
  var settings = jQuery.extend({
    maxChars: 10, 
    leftChars: "" 
  }, options);
  return this.each(function() {
    var me = $(this);
    var color = "33DD33";
    var l = settings.maxChars;
    me.bind('keydown keypress keyup onchange',function(e) {
      if(me.val().length>settings.maxChars) me.val(me.val().substr(0,settings.maxChars));
      //if(me.val().length>settings.maxChars) color = "DD3333";
      //else color = "33DD33";
      l = settings.maxChars - me.val().length;
      me.next('div').html(l + ' ' + settings.leftChars + ' белгиге орун бар').css({color: "#"+color});
      jQuery('#commentSend').attr("disabled", (me.val().length>settings.maxChars));
    });
    me.after('<div class="maxlen" style="color:#'+color+'">' + settings.maxChars + ' ' + settings.leftChars + ' белгиге орун бар</div>');
  });
};
function change_max(id){
	alert(id);
}
function treecomment($id,$cid,$type) {
	//alert($cid);
	timestamp=my_getbyid("treetimestamp_"+$cid);
	root=my_getbyid("treecomment_"+$cid);
	comment=my_getbyid("treecommentText_"+$cid);
	//alert('ixu!');
	//alert($id+"\n"+$cid+"\n"+timestamp.value+"\n"+root.value+"\n"+comment.value+"\n"+$type); 
	if(comment.value) { 
		$.post("/ajaxtree.php",{id:$id,root: root.value,comment: comment.value,type:$type},onAjaxSuccess);
		//alert('Комментарий кошулду!');
	}
	alert(onAjaxSuccess(data));
	}
	function onAjaxSuccess(data) {
		if(data.length>10){
			//window.location.reload();
			document.getElementById("newcom_"+$cid).innerHTML =data;
			document.getElementById("newcom_"+$cid).style.display="block";
			document.getElementById("reply_"+$cid).style.display="none";
		}
		if(data == "restrict_post"){
			alert("Вам запрещено оставлять сообщения на сайте!");
		} else if(data == "not4"){
			alert("Удаа комментарий калтырууга болбойт!");
		} else if(data == "not3"){
			alert("Комментарий жазуу чектелген!");
		} else if(data != "not"){
			$("#commentnew").html(data);setVoted();
			commentText.value="";
		} else alert("У вас нет прав для комментирования!");				
	}


function editComment(id) {
				$.post('/ajax_comment.php', { act: 'edit', id: id },
					  function(data){
						  if (data=='ok')
						  {
						  	$('#mess_'+id).hide();
						  	$('#edit_mess_'+id).show();							  	
						  }
					  });
		}
		
function saveComment(id)
{
	var txt = $('#edit_txt_'+id).val();
	$.post('/ajax_comment.php', { act: 'save', id: id, text: txt},
		  function(data){
			  if (data!='false')
			  {
			  var nowDate = new Date();
			  var strDate = ''+nowDate.getDate()+'.'+nowDate.getMonth()+'.'+nowDate.getFullYear()+' '+nowDate.getHours()+':'+nowDate.getMinutes();

			  
			  txt=txt+'<span style=\"color:#818181;font-size:11px;margin:5px 0;display:block;\">Комментарий т&#1199;з&#1257;тт&#1199; '+strDate+'</span>';
			  //txt=txt+\"".$this->makeModerated("Модератор т&#1199;з&#1257;тт&#1199; ".date("d.m.Y H:i"))."\"	
			    $('#mess_'+id).html(txt);
			  	$('#mess_'+id).show();
			  	$('#edit_mess_'+id).hide();							  	
			  }
		  });
}	

function saveOwnComment(id)
{
	var txt = $('#edit_txt_'+id).val();
	$.post('/ajax_comment.php', { act: 'saveOwn', id: id, text: txt},
		  function(data){
			  if (data!='false')
			  {
			   txt=txt+'<span style=\"color:#818181;font-size:11px;margin:5px 0;display:block;\">т&#1199;з&#1257;тт&#1199;!</span>';
				alert('test');
			    $('#mess_'+id).html(txt);
			  	$('#mess_'+id).show();
			  	$('#edit_mess_'+id).hide();							  	
			  }
		  });
}	

function cancelSaveComment(id)
{
  	$('#mess_'+id).show();
  	$('#edit_mess_'+id).hide();					
}

function blockComment(id) {
			$.get('/ajax_comment.php', { id: id, act: 'block' },
				  function(data){
				  	if (data!='false')
				  	{
				  		if (data=='1')
				  		{
				  			$('#img_block_'+id).attr({ src: '/images/comments/act.gif' });
				  		}
				  			else 
				  		{
				  			$('#img_block_'+id).attr({ src: '/images/comments/block.gif' });
				  		}						  		
				  	}
				  });
	}
	
function killComment(id) {
			if(confirm("Вы действительно хотите удалить этот комментарий?")) { 
				$.get("/ajax.php", { cid: id, killComment: 0 },
					  function(data){
					  	document.getElementById("comment_"+id).innerHTML = "<span style=\'color:#f00;\'>Комментарий &#1257;ч&#1199;р&#1199;лд&#1199;</span>";
					  });
			}
		}
