
	function addslashes(str) {
		str = str.replace(/\\/g,'\\\\');
		str = str.replace(/\'/g,'\\\'');
		str = str.replace(/\"/g,'\\"');
		str = str.replace(/\0/g,'\\0');
		return str;
	}
	
	function stripslashes(str) {
		str = str.replace(/\\'/g,'\'');
		str = str.replace(/\\"/g,'"');
		str = str.replace(/\\0/g,'\0');
		str = str.replace(/\\\\/g,'\\');
		return str;
	}
	
	var newmsg = 0;
	function checkShowNewmsg() {
		newmsg--;
		if (newmsg == 0) $('#new-msg').hide();
	}
	
	function setPosNeg(type) {
		if (type == 'positive') {
			$("#pos-neg").val(1);
			$("#p-positive").attr("class", "current");
			$("#p-negative").attr("class", "");
		} else if (type == 'negative') {
			$("#pos-neg").val(0);
			$("#p-negative").attr("class", "current");
			$("#p-positive").attr("class", "");
		}
	}
		
	function sendNewMessageForm()
	{
		var author = $("#name").val();
		var content = $("#message").val();
		var subject = $("#subject").val();
		var tags = $("#tags").val();
		var code = $("#message-captcha").val();
		var type = $("#pos-neg").val();
		var categories = '';
		
		for (var idx = 1; idx <= 14; idx++) {
			if ($("#ch"+idx).is(':checked') == true) categories += idx + '-';
		}

		$.ajax({
			   type: "POST",
			   url: pathToRoot+"app/ajax/add_new_msg.php",
			   dataType: "json",
			   data: { author: addslashes(author), categories: addslashes(categories), content: addslashes(content), subject: addslashes(subject), tags: addslashes(tags), code: addslashes(code), type: type },
			   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 = 'Zaznacz czy plumkasz pozytywnie czy negatywnie!';
			else if (json.errno == 8) 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 == 9) html = 'Treść wiadomości powinna zawierać od 10 do 1000 znaków!';
			else if (json.errno == 10) html = 'Można pisać dosadnie bez brzydkich słów. Usuń brzydkie słowa i wyraź to samo inaczej!';
			else if (json.errno == 11) html = 'Wybierz od jednej do trzech kategorii!';
			
			$("#new-msg").html(html);
			
			$("#new-msg").slideDown("slow");
			newmsg++;
			setTimeout("checkShowNewmsg()", 45000);
			
			if (json.errno == 3 || json.errno == 4 || json.errno == 5 || json.errno == 6) {
				$("#captcha").attr("src", pathToRoot+"Captcha?sid=" + Math.random());
				$("#message-captcha").val("");
			}
			
			if (json.errno == 6) {
				$("#p-negative").attr("class", "");
				$("#p-positive").attr("class", "");
				$("#name").val("");
				$("#message").val("");
				$("#subject").val("");
				$("#tags").val("");
				$("#charsleft").html("Pozostało znaków: <em>1000</em>");

				window.location.replace(pathToRoot+'Najnowsze');
			}
			

		},		   
		   beforeSend: function() {
			   $("#msg-ajax").show();
			   $.scrollTo(0);
		   },
		   complete: function() {
			   $("#msg-ajax").hide();
		   }
		 });
	
	}

	
	function sendNewMessageFormByUser()
	{
		var options = {
				url: pathToRoot+"app/ajax/add_new_msg_by_user.php",
				dataType: 'json',
				beforeSubmit: function() {
					$("#msg-ajax").show();
					$.scrollTo(0);
			    },
			    success: function(json, statusText) { 
			    	$("#msg-ajax").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 = 'Zaznacz czy plumkasz pozytywnie czy negatywnie!';
						else if (json.errno == 6) 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 == 7) html = 'Fotka musi być typu JPG, mieć rozmiar conajmniej 200x200 pikseli i ważyć mniej niż 500KB!';
						else if (json.errno == 8) html = 'Wystąpił błąd podczas wysyłania fotki!';
						else if (json.errno == 9) html = 'Treść wiadomości powinna zawierać od 10 do 1000 znaków!';
						else if (json.errno == 10) html = 'Można pisać dosadnie bez brzydkich słów. Usuń brzydkie słowa i wyraź to samo inaczej!';
						else if (json.errno == 11) html = 'Wybierz od jednej do trzech kategorii!';
						
						$("#new-msg").html(html);
						
						$("#new-msg").slideDown("slow");
						newmsg++;
						setTimeout("checkShowNewmsg()", 10000);
								
						if (json.errno == 4) {
							$('#new-message').resetForm();
							$("#p-negative").attr("class", "");
							$("#p-positive").attr("class", "");
							$("#message").val("");
							$("#subject").val("");
							$("#tags").val("");
							$("#charsleft").html("Pozostało znaków: <em>1000</em>");

							window.location.replace(pathToRoot+'Najnowsze');
						}
			    					    		
			    	} else {
						$("#new-msg").html('Błąd!');
						
						$("#new-msg").slideDown("slow");
						newmsg++;
						setTimeout("checkShowNewmsg()", 10000);			    		
			    	}
			    }
			};

		$('#new-message').ajaxSubmit(options);
	
	}
	
	this.imagePreview = function(){
		
		xOffset = 10;
		yOffset = 50;
	
		$("img.preview").hover(function(e){
			
			var addr = this.src.substring(0, this.src.length-7) + '.jpg';
			
			$("body").append("<img id='preview' src='"+ addr +"' alt='' />");

			$("#preview")
				.css("top", (e.pageY - yOffset) + "px" )
				.css("left",(e.pageX + yOffset) + "px")
				.fadeIn();
	    },
		function(){	
			$("#preview").remove();
	    });	
		$("img.preview").mousemove(function(e){
		
			$("#preview")
			.css("top", (e.pageY - yOffset) + "px" )
			.css("left",(e.pageX + yOffset) + "px");
		
		});		
	
	};

	
	
	var sidebar_form_hidden = 1;
	
	function showSidebarForm() {
		
		if (sidebar_form_hidden != 1) return;
		
		var height = $("#sidebar-form-hidden").height();
		
		$("#sidebar-form-hidden").show().css({ height : 0 });	
		
		$("#sidebar-form-hidden").animate({ height : height }, { duration: 1000, complete: function() { 
			$("#sidebar-form-hidden").css({ height : 'auto' });
		}});
		sidebar_form_hidden = 0;
	}

	var categories_form_hidden = 1;
	
	function showCategoriesForm() {
		
		if (categories_form_hidden != 1) return;
		
		var height = $("#categories-form-hidden").height();

		$("#categories-form-hidden").show().css({ height : 0 });	
		
		$("#categories-form-hidden").animate({ height : height }, { duration: 1000 });
		categories_form_hidden = 0;
	}	
	
	function checkCategories(id) {

		var mark = 0;

		for (var idx = 1; idx <= 14; idx++) {

		if ($("#ch"+idx).is(':checked') == true) mark++;
		}
		
		if (mark > 3) $("#ch"+id).attr('checked', false);

	}
	
	$(document).ready(function(){
		
		if (!navigator.cookieEnabled) window.location.replace(pathToRoot+'Ciasteczka');
		
		imagePreview();
	});
