var openedMsgId = 0;
var $paneTarget = $('.pane');
function StrReplace(p1,p2,s) { 
 if (p1==p2) return s; 
 while (s.indexOf(p1)>=0) s=s.replace(p1,p2); 
 return s; 
}


function gen_mail(ch, uid)
{
	jQuery.post("/ajax.php",{ action: "genMail", channelID: ch, userID: uid}, function(data)
	{
		window.location.reload();
	});
}

function copytoclip(data)
{
	window.clipboardData.setData("Text", data);
	alert()
}

jQuery(document).ready(function()
{
	jQuery(".showLikes").click(function()
	{
		var msgID = jQuery(this).attr('name');
		jQuery(this).attr('class','showLikes voted');
		jQuery("#img"+msgID).hide();
		jQuery.post("/ajax.php",{ action: "doLike", msg_id: msgID, userID: user_id}, function(data)
		{
			if(data=='1')
			{
				var x = parseInt(jQuery("#likesCount"+msgID).text())+parseInt(1);
				jQuery("#likesCount"+msgID).text(x).css('display','inline');
				
			}
		});
	});
	
	jQuery(".showComments").click(function()
	{
			var msgID = jQuery(this).attr('id');
			var visible = jQuery("#commentsBlock"+msgID).css('display');
			
			if(visible=='none')
			{
				if(openedMsgId!=0)
				{
					jQuery("#commentsBlock"+openedMsgId).hide();
				}
				openedMsgId = msgID;
				jQuery.post("/ajax.php",{ action: "getComments", msg_id: msgID, userID: user_id}, function(data)
				{
						if(data!='')
						{
							jQuery("#commentsBlock"+msgID).show();
							jQuery("#comments"+msgID).html(data);
							jQuerypaneTarget.scrollTo('2000px',0);
							jQuery("#commentsBlock"+msgID).hide();
							jQuery("#commentsBlock"+msgID).show();
							
						}
						else
						{
							jQuery("#comments"+msgID).html('<p align=center class="cdescription"><br/><br/>К этому сообщению ответов пока не оставлено<br/><br/></p>');
							jQuery("#commentsBlock"+msgID).show();
						}
				});
				jQuery.post("/ajax.php",{ action: "viewed", msgID: msgID, userID: user_id}, function(data)
				{
					jQuery("#commentsCount"+msgID).attr('class','commentsCount');
				});
			}
			else
			{
				jQuery("#commentsBlock"+msgID).hide();
			}
	});
	
	jQuery(".addComment").click(function(){
		
			var msgID = jQuery(this).attr('name');
			var msg =  jQuery('#commentMsg'+msgID).attr('value');
			
			if(typeof (msg)== 'undefuned')
				return false;
			if(msg.length>140 || msg.length==0)
				return false;
			else
			{
				jQuery('#commentMsg'+msgID).attr('value','');
				jQuery.post("/ajax.php",{ action: "setComment", msg_id: msgID, sessionid:sessionid, message: msg}, function(data)
				{
					if(data=='1')
					{
						jQuery("#counter"+msgID).text(0);
						jQuery.post("/ajax.php",{ action: "getComments", msg_id: msgID, userID: user_id}, function(data)
						{
								jQuery("#comments"+msgID).html(data);
								var x = parseInt(jQuery("#commentsCount"+msgID).text())+parseInt(1);
								jQuery("#commentsCount"+msgID).text(x);
								jQuerypaneTarget.scrollTo('2000px',0);
								jQuery.post("/ajax.php",{ action: "sem", num: x}, function(data)
								{
									jQuery("#"+msgID).text('ответ'+data);
								});
						});
					}
					else
					{
						alert(data);
					}
				});
			}
	});
	
	
	jQuery(".commentReply span").text('0');
	jQuery(".commentReply input").attr('maxlength','140');
	
	jQuery(".commentReply input").bind("keyup",function(event){
		var id = StrReplace('commentMsg','',jQuery(this).attr('id'));
		var c = parseInt(jQuery("#counter"+id).text());
		jQuery("#counter"+id).text(jQuery(this).attr('value').length);
		if(parseInt(c)>140)
		{
			jQuery("#counter"+id).css('color','red');
		}
		else
		{
			jQuery("#counter"+id).css('color','#aaaaaa');
		}
		if((event.ctrlKey==true)&&(event.keyCode==13))
		{
					var msgID = id;
					var msg =  jQuery('#commentMsg'+msgID).attr('value');
					
					if(typeof (msg)== 'undefuned')
						return false;
					if(msg.length>140 || msg.length==0)
						return false;
					else
					{
						jQuery('#commentMsg'+msgID).attr('value','');
						jQuery.post("/ajax.php",{ action: "setComment", msg_id: msgID, sessionid:sessionid, message: msg}, function(data)
						{
							if(data=='1')
							{
								jQuery("#counter"+msgID).text(0);
								jQuery.post("/ajax.php",{ action: "getComments", msg_id: msgID, userID: user_id}, function(data)
								{
										jQuery("#comments"+msgID).html(data);
										jQuery("#commentsCount"+msgID).text(parseInt(jQuery("#commentsCount"+msgID).text())+parseInt(1));
										jQuerypaneTarget.scrollTo('2000px',0);
								});
							}
							else
							{
								alert(data);
							}
						});
					}
		}
	});
});

function subscription(msg_id, user_id)
{
	var action = jQuery("#a"+msg_id).attr('name');
	jQuery.post("/ajax.php",{action: action, msg_id: msg_id, user_id: user_id}, function(data)
	{
			if(action=='subscribe')
			{
				jQuery("#a"+msg_id).text('Не следить за ответами').attr('name','unsubscribe');
			}
			else
			{
				jQuery("#a"+msg_id).text('Cледить за ответами').attr('name','subscribe');
			}
	});
}

