jQuery(document).ready(function() {
	$("a.postphoto").fancybox();
	
	jQuery('.showlogin').click(function(e) {

			var x = e.pageX;
			var y = e.pageY;
						
			$('#login-box').css('left', (x-150) + 'px').css('top', (y+20) + 'px');
			$('input[name="logon_x"]').val(x-150);
			$('input[name="logon_y"]').val(y+20);

		jQuery('#login-box').toggle();

	});
	
	jQuery('#login-close').click(function() {
		
		jQuery('#login-box').hide();

	});
	
	jQuery('#showdoradca').click(function() {
		
		var val = jQuery('#adviser').css('display');
		
		if(val == 'none') {
			jQuery('#adviser').css('display', 'block');
			jQuery(this).html('Jeśli nie chcesz zostać doradcą, kliknij tutaj<input type="hidden" name="doradca" value="1"/>');
			jQuery('#editprofilesubmit').attr('title', 'Wyślij').attr('value', 'Wyślij');
		} else {
			jQuery('#adviser').css('display', 'none');
			jQuery(this).html('Jeśli chcesz zostać doradcą, kliknij tutaj<input type="hidden" name="doradca" value="0"/>');
		}

	});
	
	$('#lokalizacja').autocomplete('/ajax.php?module=posts&autocomplete=lokalizacja', {
			width: 116,
			multiple: false,
			multipleSeparator: ", ",
			mustMatch: false,
			minChars: 1,
			formatItem: function(row) { return row[0]; }
		});
	
	$('#branza').autocomplete('/ajax.php?module=posts&autocomplete=branza', {
			width: 255,
			multiple: false,
			mustMatch: true,
			minChars: 1,
			cacheLength: 15,
			formatItem: function(row) { return row[0]; }
		});
		
	$("#branza").result(function(event, data, formatted) {
		if(data)
		{
			$(this).val("");
			$("#multibox").append("<div style='display: inline;'> <a href=\"#\" onclick=\"return DeleteParent(this)\">x</a> " +data[0]+ "<input type=\"hidden\" name=\"branza[]\" value=\""+data[0]+"\">&nbsp;&nbsp;</div>");
			
		}
	});

	$('#branza2').autocomplete('/ajax.php?module=posts&autocomplete=branza', {
			width: 116,
			multiple: false,
			multipleSeparator: ", ",
			mustMatch: true,
			minChars: 1,
			cacheLength: 15,
			formatItem: function(row) { return row[0]; }
		});
		
	$('#branza3').autocomplete('/ajax.php?module=posts&autocomplete=branza', {
			width: 200,
			multiple: false,
			multipleSeparator: ", ",
			mustMatch: true,
			minChars: 1,
			cacheLength: 15,
			formatItem: function(row) { return row[0]; }
		});
		
	$('#addPost input[type="text"]').keypress(function(event) {
		
		if(event.keyCode==13) {
			return false;
		} else {
			return true;
		}
	
	});
	
	$('#addPost').submit(function() {
		
		var stop = 0;
		
		$('.require').each(function () {
			
			if($(this).val() == '') {
				alert('Proszę wypełnić wszystkie wymagane pola.');
				stop = 1;
				return false;
			}
		
		});
		
		if(stop) {
			return false;
		}
		
		return true;
	});

	$('#newmessage').submit(function() {
		
		var stop = 0;
		
		$('.require').each(function () {
			
			if($(this).val() == '') {
				alert('Proszę wypełnić wszystkie wymagane pola.');
				stop = 1;
				return false;
			}
		
		});
		
		if(stop) {
			return false;
		}
		
		return true;
	});
	
	
	$('#sloc_1').click(function() {
		$('#loc_1').attr("checked", "checked");
		$('#loc_2').removeAttr("checked");
	});
	
	$('#sloc_2').click(function() {
		$('#loc_2').attr("checked", "checked");
		$('#loc_1').removeAttr("checked");
	});
	
	$('#search-word').focus(function() {
		var data = $(this).attr('title');
		if($(this).val() == data) $(this).val('');
	});
	
	$('#search-word').blur(function() {
		var data = $(this).attr('title');
		if($(this).val() == '') $(this).val(data);
	});
	
	$('#search_form').submit(function() {
		
		var inp = $('#search-word').val();
		if(inp.length < 3) {
			alert("Minimum 3 znaki");
			return false;
		}
		return true;
	
	});
	
	$('#advsearch').submit(function() {

		
		if($('input[name="type"]').val() == 'n' && !$('input[name="type2[]"]:checked').length) {
			return false;
		}
		
		return true;
	
	});
	
	jQuery('.searchRadio').click(function() {
		
		if($(this).val() == 'n') {
			$('.branza_s').val('');
			$('.branza_s').hide();
			$('.rodzaj_s').show();
		} else {
			$('.branza_s').show();
			$('.rodzaj_s').hide();

		}

	});
	
	
	/* Feedbacks */
	 $(".contimage").hover(function() {
			$(this).animate({
				opacity:1
			},200);
		}, function() {
			$(this).animate({
				opacity:0.8
			},200);
		});
		$('#submitform').ajaxForm({
			target: '#error',
			success: function() {
			$('#error').fadeIn('slow');
			}
	});
	$('a[rel*=facebox]').facebox();

});

function DeleteParent(variable) {
	$(variable).parent().remove();
	return false;
}
