	var idShowReplyForm = 0;
	var replymsg = 0;
	
	
	function loadReplyForm(id, type) {
		if (idShowReplyForm == id) return;
			
		var html = '<p id="reply-msg" class="ajax-msg"></p>'
					+ '<form action="" method="post" class="reply-body-form" id="replyform'+id+'">'
					+ '<fieldset>'
					+ '<input type="hidden" id="reply-msg-id" name="msg" value="'+id+'" />'
					+ '<textarea id="reply-body" name="content" rows="5" cols="35" title="Porusz Twoją kreatywność ;-)"></textarea>'					
					+ '<script type="text/javascript" src="'+pathToRoot+'view/js/reply_charslimit.js"></script>'																		
					+ '<div class="reply-body-div">'																			
					+ '<label for="reply-name">Podpis</label>'
					+ '<input type="text" id="reply-name" name="" maxlength="12" />'										
					+ '<img id="replycaptcha" src="'+pathToRoot+'view/i/bg-captcha.gif" width="115" height="35" alt="Kod" />'																				
					+ '<label for="reply-captcha">Kod</label>'
					+ '<input type="text" id="reply-captcha" name="" maxlength="4" />'										
					+ '<div><p id="refresh-security-code"><a href="javascript:void(0);" onclick="document.getElementById(\'replycaptcha\').src = \''+pathToRoot+'SmallCaptcha?sid=\' + Math.random(); return false;">Odśwież kod</a></p></div>'										
					+ '<input type="button" onClick="sendReplyForm('+type+')" value="Publikuj" class="submit" title="Przeczytaj Twój wpis zanim klikniesz!" />'																		
					+ '</div>'									
					+ '<p id="reply-charsleft">Pozostało znaków: <em>1000</em></p>'																
					+ '</fieldset>'
					+ '</form>';
		
		if (idShowReplyForm > 0) delReplyForm(idShowReplyForm);
		$("#replyFormArea"+id).html(html);
		$("#replycaptcha").attr("src", pathToRoot+"SmallCaptcha?sid=" + Math.random());
		$("#replyFormArea"+id).slideDown("slow");
		idShowReplyForm = id;
	}

	function loadReplyFormByUser(id, type) {
		if (idShowReplyForm == id) return;
		
		var html = '<p id="reply-msg" class="ajax-msg"></p>'
					+ '<form action="" method="post" class="reply-body-form" id="replyform'+id+'">'
					+ '<fieldset>'
					+ '<input type="hidden" id="reply-msg-id" name="msg" value="'+id+'" />'
					+ '<textarea id="reply-body" name="content" rows="5" cols="35" title="Porusz Twoją kreatywność ;-)"></textarea>'					
					+ '<script type="text/javascript" src="'+pathToRoot+'view/js/reply_charslimit.js"></script>'																		
					+ '<div class="reply-body-div">'
					+ '<label for="reply-photo" style="width: 0px;">Fotka</label>'
					+ '<input type="file" id="reply-photo" name="photo" style="width: 170px;" size="8" />'
					+ '<input type="button" onClick="sendReplyFormByUser('+type+')" value="Publikuj" class="submit" title="Przeczytaj Twój wpis zanim klikniesz!" />'																		
					+ '</div>'									
					+ '<p id="reply-charsleft">Pozostało znaków: <em>1000</em></p>'																
					+ '</fieldset>'
					+ '</form>';
		
		if (idShowReplyForm > 0) delReplyForm(idShowReplyForm);
		$("#replyFormArea"+id).html(html);
		$("#replyFormArea"+id).slideDown("slow");
		idShowReplyForm = id;
	}
	
	function delReplyForm(id) {
		if (idShowReplyForm != id) return;
		$("#replyFormArea"+id).slideUp("slow");
		$("#replyFormArea"+id).html("");
		idShowReplyForm = 0;
	}
	
	function checkShowReplymsg() {
		replymsg--;
		if (replymsg == 0) $('#reply-msg').hide();
	}	
	
	function sendReplyForm(type)
	{
		var msg = $("#reply-msg-id").val();
		var author = $("#reply-name").val();
		var content = $("#reply-body").val();
		var code = $("#reply-captcha").val();

		$.ajax({
			   type: "POST",
			   url: pathToRoot+"app/ajax/add_reply.php",
			   dataType: "json",
			   data: { msg: addslashes(msg), author: addslashes(author), content: addslashes(content), code: addslashes(code) },
			   success: function(json){		
 
			var html = 'Błąd!';
			if (json.errno == 1) html = 'Podpisz wiadomość!';
			else if (json.errno == 2) html = 'Wpisz treść!';
			else if (json.errno == 3) html = 'Podaj poprawny kod!';			
			else if (json.errno == 4) html = 'Błąd!';			
			else if (json.errno == 5) html = 'Błąd zapisu!';
			else if (json.errno == 6) html = 'Wiadomość zapisana!';
			else if (json.errno == 7) html = 'Niedawno zaplumkałeś. <a href="'+pathToRoot+'Rejestracja">Zarejestruj się</a> lub <a href="'+pathToRoot+'Logowanie">zaloguj</a> aby móc plumkać częściej!';
			else if (json.errno == 8) html = 'Treść wiadomości powinna zawierać od 10 do 1000 znaków!';
			else if (json.errno == 9) html = 'Można pisać dosadnie bez brzydkich słów. Usuń brzydkie słowa i wyraź to samo inaczej!';
			
			$("#reply-msg").html(html);
			
			$("#reply-msg").slideDown("slow");
			replymsg++;
			setTimeout("checkShowReplymsg()", 45000);
						
			if (json.errno == 3 || json.errno == 5 || json.errno == 6) {
				$("#replycaptcha").attr("src", pathToRoot+"SmallCaptcha?sid=" + Math.random());
				$("#reply-captcha").val("");
			}
			
			if (json.errno == 6) {
				$("#reply-name").val("");
				$("#reply-body").val("");
				$("#reply-charsleft").html("Pozostało znaków: <em>1000</em>");

				//window.location.replace(pathToRoot+msgLink+'Najnowsze');
				showReplies(msg, true, type, 1, 0);
			}
			
		},		   
		   beforeSend: function() {
			   $("#reply-ajax-"+idShowReplyForm).show();
		   },
		   complete: function() {
			   $("#reply-ajax-"+idShowReplyForm).hide();
		   }
		 });
	
	}
	
	function sendReplyFormByUser(type)
	{		
		var options = {
				url: pathToRoot+"app/ajax/add_reply_by_user.php",
				dataType: 'json',
				beforeSubmit: function() {
					$("#reply-ajax-"+idShowReplyForm).show();
			    },
			    success: function(json, statusText) { 
			    	$("#reply-ajax-"+idShowReplyForm).hide();

			    	if (statusText == 'success') {
						
			    		var html = 'Błąd!';
						if (json.errno == 1) html = 'Wpisz treść!';		
						else if (json.errno == 2) html = 'Błąd!';			
						else if (json.errno == 3) html = 'Błąd zapisu!';
						else if (json.errno == 4) html = 'Wiadomość zapisana!';
						else if (json.errno == 5) html = 'Plumknięto przed chwilą. Jeśli chcesz plumkać częściej zdobądź wyższą pozycję w rankingu. Odczekaj chwilę aby znów plumkać!';
						else if (json.errno == 6) html = 'Fotka musi być typu JPG i mieć rozmiar mniejszy niż 500KB!';
						else if (json.errno == 7) html = 'Wystąpił błąd podczas wysyłania fotki!';
						else if (json.errno == 8) html = 'Treść wiadomości powinna zawierać od 10 do 1000 znaków!';
						else if (json.errno == 9) html = 'Można pisać dosadnie bez brzydkich słów. Usuń brzydkie słowa i wyraź to samo inaczej!';
						
						$("#reply-msg").html(html);
						
						$("#reply-msg").slideDown("slow");
						replymsg++;
						setTimeout("checkShowReplymsg()", 10000);
											
						if (json.errno == 4) {
							$('#replyform'+idShowReplyForm).resetForm();
							$("#reply-body").val("");
							$("#reply-charsleft").html("Pozostało znaków: <em>1000</em>");

							//window.location.replace(pathToRoot+msgLink+'Najnowsze');
							showReplies($("#reply-msg-id").val(), true, type, 1, 1);
						}
			    					    		
			    	} else {
						$("#reply-msg").html('Błąd!');
						
						$("#reply-msg").slideDown("slow");
						replymsg++;
						setTimeout("checkShowReplymsg()", 10000);
			    	}
			    }
			};

		$('#replyform'+idShowReplyForm).ajaxSubmit(options);

	}
	
	function loadFullContent(msg) {
		$.ajax({
			   type: "POST",
			   url: pathToRoot+"app/ajax/get_content.php",
			   dataType: "json",
			   data: { msg: msg },
			   success: function(json) {
				   
				   if (json.errno == 2) {
								   
					   var height1 = $("#msg-content"+msg).height();
					   
					   $('#msg-content'+msg).html(json.content);
						
					   var height2 = $("#msg-content"+msg).height();
						
					   $("#msg-content"+msg).hide().css({ height : height1 });	
					
					   $('#msg-content'+msg).html('');
					   
					   $("#msg-content"+msg).show().animate({ height : height2 }, { duration: 1000, complete: function() {
						   $('#msg-content'+msg).html(json.content);
					   } });
					   
					   
				   }
				   
			   }});

	}
	
	
	function showReplies(msg, last, type, page, user) {
		/*
		if (last == true) page='last';
		else loadFullContent(msg);
		*/
		
		loadFullContent(msg);
		
		/**/
		
		$.ajax({
			   type: "POST",
			   url: pathToRoot+"app/ajax/get_replies.php",
			   dataType: "json",
			   data: { msg: msg, page: page },
			   success: function(json) {

			if (json.errno == 2 && json.replies) {
			
				var last_reply_id = 0;	   
				var str = '';
			
			$.each(json.replies, function(i,item) {
				
				last_reply_id = item.id;
				
				str += '<li';
				if (i % 2 == 1) str += ' class="odd"';
				str += ' id="reply'+item.id+'">';
					
				str += '<cite class="user">'
				+ '<a href="';
				
				if (item.login != '') str += pathToRoot+'Uzytkownik/'+item.login;				
				else str += 'javascript:void(0);';
				
				str += '">'
				+ '	<img src="'+pathToRoot+'view/';
				
				if (item.avatar != '') str += 'avatars/'+item.avatar+'"';
				else {
					str += 'i/avatar-';
					if (type == 1) str += 'positive'; else str += 'negative';
					str += '.gif"';
				}
				
				str += ' title="';
				if (item.login != '') str += item.login; else str += item.author;
				str += '" />'
				
				+ '<em class="userlogin ';
				
				if (item.userlevel == 3) str += 'blue';
				else if (item.userlevel == 2) str += 'green';
				else if (item.userlevel == 1) str += 'graphite';
				else str += 'grey';
				
				str += '">';
				if (item.login != '') str += item.login; else str += item.author;
				str += '</em>'
				
				+ '<span>';
				if (item.login != '') str += 'Punkty: '+item.points; else str += 'Gość';
				str += '</span></a></cite>'

				+ '<div class="body-reply">';
				
				if (item.img == 't') {
					str += '<div class="image-reply2-';
					if (i % 2 == 0) str += 'right'; else str += 'left';
					str += '"><a href="'+pathToRoot+'view/img/replies/'+item.msg+'_'+item.id+'_normal.jpg" target="_blank"><img class="preview" src="'+pathToRoot+'view/img/replies/'+item.msg+'_'+item.id+'_th.jpg" alt="Fotka" /></a></div>';
				}
				
				str += '<blockquote><p>'+item.content+'</p></blockquote>'
					+ '</div>'

					+ '<div class="reply-footer">'
					
					if (useradmin == 1) str += '<a href="javascript:void(0);" onclick="removeReply('+item.msg+','+item.id+');" class="remove"></a>';
					
					str += '<p>';
					
					if (item.vote == '1') {
						str += '<a href="javascript:void(0);" id="rplus'+item.id+'" onClick="voteReply('+item.id+', \'+\');">Plum +</a>'
						+ ' <a href="javascript:void(0);" id="rminus'+item.id+'" onClick="voteReply('+item.id+', \'-\');">Bum -</a>';
					}
					
					str += '<span id="rpoints'+item.id+'"';
					if (item.vote == '1') str += ' style="display: none;"';
					str += '>';
					
					if (item.vote == '1') str += '<img id="reply-vote-loader'+item.id+'" src="'+pathToRoot+'view/i/ajax-loader.gif" />';
					else str += 'Punktów: <em>'+item.rpoints+'</em>';
					
					str += '</span> | Dodano: '+item.dateDiff+'</p>'							
									
					+ '</div></li>';
				
			});
			
			str2 = '';
			
			if (json.pageCount > 1) {
			str2 = '<div class="msg-replies-pagination" id="msg-replies-pagination'+msg+'">';
			
			for (i=1; i<= json.pageCount; i++) {
			
			if (i == 1) {
				str2 += '<a href="javascript:void(0);" onClick="';
				if (json.page == 1) str2 += 'javascript:void(0);'; else str2 += 'javascript:showRepliesPaginate('+msg+',false,'+type+','+(json.page-1)+','+user+');';
				str2 += '">Starsze</a>';
				
				str2 += '<a';
				if (json.page == 1) str2 += ' class="current"';
				str2 += ' href="javascript:void(0);" onclick="javascript:showRepliesPaginate('+msg+',false,'+type+',1,'+user+');">1</a>';  						
				
					
				if (json.page >= 5) str2 += '&hellip;';
				
				if (json.pageCount == 1) str2 += '<a href="javascript:void(0);">Nowsze</a>';	    						
				
				} else {

				if (i == json.pageCount) {							
					if (json.page <= json.pageCount-4) str2 += '&hellip;';
					
					str2 += '<a';
					if (json.page == json.pageCount) str2 += ' class="current"';
					str2 += ' href="javascript:void(0);" onclick="javascript:showRepliesPaginate('+msg+',false,'+type+','+json.pageCount+','+user+');">'+json.pageCount+'</a>'  								
					+ '<a href="javascript:void(0);" onclick="';
					if (json.page == json.pageCount) str2 += 'javascript:void(0);';
					else str2 += 'javascript:showRepliesPaginate('+msg+',false,'+type+','+(json.page+1)+','+user+');';
					str2 += '">Nowsze</a>';
					
					} else {

					if (i >= json.page-2 && i <= json.page+2) {    								
						str2 += '<a href="javascript:void(0);" onclick="javascript:showRepliesPaginate('+msg+',false,'+type+','+i+','+user+');"';
						if (json.page == i) str2 += ' class="current"';
						str2 += '>'+i+'</a>';
					}
				
				}
			
			}
			
			}
			
			str2 += '</div>';
			}
			if (last == true) { 
				if (idShowReplyForm > 0) {
					$("#replyFormArea"+idShowReplyForm).hide(1);
					$("#replyFormArea"+idShowReplyForm).html("");
					idShowReplyForm = 0;
				}
			}				
					var html = '<ol class="replies-ol" id="replies-ol'+msg+'">' + str + '</ol>' 
					+ '<div style="height: 20px;"><a href="javascript:void(0);" onclick="hideReplies('+msg+');" class="replies-hide" id="button-hide-msg'+msg+'">Zwiń</a></div>'
					+ str2;
			
					$("#button-replies-msg"+msg).hide();
					$("#button-readmore-msg"+msg).hide();
					
					$("#messages-replies"+msg).hide().css({ height : 'auto' });
					$("#messages-replies"+msg).html(html);
					
					var height = $("#messages-replies"+msg).height();
					$("#messages-replies"+msg).hide().css({ height : 0 });	
					
					$("#messages-replies"+msg).show().animate({ height : height }, { duration: 1000, complete: function() {
						imagePreview();
						
						if (last == true) {
							if (user == 1) loadReplyFormByUser(msg, type); else loadReplyForm(msg, type);

							if (last_reply_id > 0) $.scrollTo("#reply"+last_reply_id, {duration:500});
							else $.scrollTo("#replyFormArea"+msg, {duration:500});
						} else {
							$.scrollTo("#message"+msg, {duration:500});
						}
						
					}
					});
			
			
			} else if (json.errno == 2 && last == true) {
				if (user == 1) loadReplyFormByUser(msg, type); else loadReplyForm(msg, type);
			}			
			
		
		   },
		   beforeSend: function() {
			   $("#reply-ajax-"+msg).show();		   
		   },
		   complete: function() {
			   $("#reply-ajax-"+msg).hide();
		   }
		   });

	}
	
	
	function hideReplies(msg) {
			
		if (idShowReplyForm == msg) delReplyForm(msg);
		
		$("#button-hide-msg"+msg).hide();
		$("#button-replies-msg"+msg).show();
		$("#button-readmore-msg"+msg).show();
							
		$("#messages-replies"+msg).slideUp(1000, function() {
			$("#messages-replies"+msg).html('');
			$.scrollTo("#message"+msg, {duration:500});
		});
	
	}

	function showRepliesPaginate(msg, last, type, page, user) {

		if (last == true) page='last';
		
		$.ajax({
			   type: "POST",
			   url: pathToRoot+"app/ajax/get_replies.php",
			   dataType: "json",
			   data: { msg: msg, page: page },
			   success: function(json) {

				   
			if (json.errno == 2 && json.replies) {
				   
			var str = '';
			
			$.each(json.replies, function(i,item) {
				
				str += '<li';
				if (i % 2 == 1) str += ' class="odd"';
				str += ' id="reply'+item.id+'">';
					
				str += '<cite class="user">'
				+ '<a href="';
				
				if (item.login != '') str += pathToRoot+'Uzytkownik/'+item.login;				
				else str += 'javascript:void(0);';
				
				str += '">'
				+ '	<img src="'+pathToRoot+'view/';
				
				if (item.avatar != '') str += 'avatars/'+item.avatar+'"';
				else {
					str += 'i/avatar-';
					if (type == 1) str += 'positive'; else str += 'negative';
					str += '.gif"';
				}
				
				str += ' title="';
				if (item.login != '') str += item.login; else str += item.author;
				str += '" />'
				
				+ '<em class="userlogin ';
				
				if (item.userlevel == 3) str += 'blue';
				else if (item.userlevel == 2) str += 'green';
				else if (item.userlevel == 1) str += 'graphite';
				else str += 'grey';
				
				str += '">';
				if (item.login != '') str += item.login; else str += item.author;
				str += '</em>'
				
				+ '<span>';
				if (item.login != '') str += 'Punkty: '+item.points; else str += 'Gość';
				str += '</span></a></cite>'

				+ '<div class="body-reply">';
				
				if (item.img == 't') {
					str += '<div class="image-reply2-';
					if (i % 2 == 0) str += 'right'; else str += 'left';
					str += '"><a href="'+pathToRoot+'view/img/replies/'+item.msg+'_'+item.id+'_normal.jpg" target="_blank"><img class="preview" src="'+pathToRoot+'view/img/replies/'+item.msg+'_'+item.id+'_th.jpg" alt="Fotka" /></a></div>';
				}
				
				str += '<blockquote><p>'+item.content+'</p></blockquote>'
					+ '</div>'

					+ '<div class="reply-footer">'
					
					if (useradmin == 1) str += '<a href="javascript:void(0);" onclick="removeReply('+item.msg+','+item.id+');" class="remove"></a>';
					
					str += '<p>';
					
					if (item.vote == '1') {
						str += '<a href="javascript:void(0);" id="rplus'+item.id+'" onClick="voteReply('+item.id+', \'+\');">Plum +</a>'
						+ ' <a href="javascript:void(0);" id="rminus'+item.id+'" onClick="voteReply('+item.id+', \'-\');">Bum -</a>';
					}
					
					str += '<span id="rpoints'+item.id+'"';
					if (item.vote == '1') str += ' style="display: none;"';
					str += '>';
					
					if (item.vote == '1') str += '<img id="reply-vote-loader'+item.id+'" src="'+pathToRoot+'view/i/ajax-loader.gif" />';
					else str += 'Punktów: <em>'+item.rpoints+'</em>';
					
					str += '</span> | Dodano: '+item.dateDiff+'</p>'							
									
					+ '</div></li>';
				
			});
			
			
			str2 = '';
			
			if (json.pageCount > 1) {
			
			for (i=1; i<= json.pageCount; i++) {
			
			if (i == 1) {
				str2 += '<a href="javascript:void(0);" onClick="';
				if (json.page == 1) str2 += 'javascript:void(0);'; else str2 += 'javascript:showRepliesPaginate('+msg+',false,'+type+','+(json.page-1)+','+user+');';
				str2 += '">Starsze</a>';
				
				str2 += '<a';
				if (json.page == 1) str2 += ' class="current"';
				str2 += ' href="javascript:void(0);" onclick="javascript:showRepliesPaginate('+msg+',false,'+type+',1,'+user+');">1</a>';  						
				
					
				if (json.page >= 5) str2 += '&hellip;';
				
				if (json.pageCount == 1) str2 += '<a href="javascript:void(0);">Nowsze</a>';	    						
				
				} else {

				if (i == json.pageCount) {							
					if (json.page <= json.pageCount-4) str2 += '&hellip;';
					
					str2 += '<a';
					if (json.page == json.pageCount) str2 += ' class="current"';
					str2 += ' href="javascript:void(0);" onclick="javascript:showRepliesPaginate('+msg+',false,'+type+','+json.pageCount+','+user+');">'+json.pageCount+'</a>'  								
					+ '<a href="javascript:void(0);" onclick="';
					if (json.page == json.pageCount) str2 += 'javascript:void(0);';
					else str2 += 'javascript:showRepliesPaginate('+msg+',false,'+type+','+(json.page+1)+','+user+');';
					str2 += '">Nowsze</a>';
					
					} else {

					if (i >= json.page-2 && i <= json.page+2) {    								
						str2 += '<a href="javascript:void(0);" onclick="javascript:showRepliesPaginate('+msg+',false,'+type+','+i+','+user+');"';
						if (json.page == i) str2 += ' class="current"';
						str2 += '>'+i+'</a>';
					}
				
				}
			
			}
			
			}
			
			}
			
			$('#replies-ol'+msg).animate({ width: 0 }, { duration: 500, complete: function() {
				$("#messages-replies"+msg).css({ height : 'auto' });
				$('#replies-ol'+msg).html(str);
				$('#replies-ol'+msg).animate({ width : 548 }, { duration: 500, complete: function() {
					$.scrollTo("#message"+msg, {duration:500});
					$('#msg-replies-pagination'+msg).html(str2);
					imagePreview();
				}});				
			}});	

			
			}			
			
		
		   },
		   beforeSend: function() {
			   $("#reply-ajax-"+msg).show();		   
		   },
		   complete: function() {
			   $("#reply-ajax-"+msg).hide();
		   }
		   });

	}

