function check_upload(obj) { 
	if (obj.value != '') {
		var regex = /[\.jpg|\.JPG|\.jpeg|\.JPEG|\.png|\.PNG]$/;
		result = regex.test(obj.value);
		if (result == 0) {
			alert(POPRAWNY_FORMAT + "\n  -jpg/jpeg\n  -png\n");
			obj.value = "";
			return 0;
		} else {
			return 1;
		}
	} else {
		alert(WYBIERZ_FOTO);
		return 0;
	}
}
var optionsFoto  = { 
    beforeSubmit:  	showRequestLogo, 
    success:       	showResponseLogo, 
    error:			function() { alert("Wystąpił błąd podczas uploadu pliku."); },
    url:      		URL_UPLOAD,       
    type:     		'post'
}
	
function DodajFoto(){
		objInput = document.getElementById('foto_file');
		if (check_upload(objInput) == 1) {
			objForm = document.getElementById('add_file');
			$(objForm).ajaxSubmit(optionsFoto);
		}
}
var onStartUpload = false;
var onEndUpload = false;
function showRequestLogo(formData, jqForm, options)  { 
	if (onStartUpload) {
		onStartUpload();
	}
	$('#zd1').remove(); 
	$('.submit').attr('disabled', 'disabled');
	$('#loading').show();
	
	return true; 
} 


function showResponseLogo(responseText, statusText)  {
	if (onEndUpload) {
		onEndUpload();
	}
	$('#foto_box').append(responseText);
	$('#loading').hide();
	$('#zd1').fadeIn("slow");
	
	setTimeout("loadedImg()", 1000);
}

function usunzdjecie(id) {
	$.ajax({
		type: 'POST',
		data: 'id=' + id,
		url: URL_DEL,
		success: function(s) {
			$('#zd' + id).slideUp(100);
		},
		error: function() {
			alert('blad');
		}
	});
}



function loadedImg() {
	var h = $('#zd1 .head img').height();
	var w = $('#zd1 .head img').width();
	if (h < 1 && w < 1) {
		return setTimeout("loadedImg()", 1000);
	}
	
	$('.submit').attr('disabled', false);
	
	$('#zd1 .head').css({display: 'block', height: ( h + (h - xh )) + 'px', width: (w + (w - xw)) + 'px'});
	$('#zd1 .head').css({ 
		marginTop: '-' + parseInt( (h - xh)) + 'px', 
		marginLeft: '-' + parseInt( (w - xw)) + 'px'
	});
	$('#zd1 img').css( {top: parseInt((h - xh)/2) + 'px', left: parseInt((w - xw) / 2) + 'px'} );
	
	$.ajax({
		url: CORDS_URL,
		type: 'post',
		data: 'x='+ parseInt( ( (w - xw) - parseInt( $('#zd1 img').css('left') ))) +'&y=' + parseInt(((h - xh)) - parseInt($('#zd1 img').css('top')))+'&width=' + xw
	});
	
	$('#zd1 .head img').draggable({containment: 'parent', cursor: 'move', stop: function(event, ui) {
	//	ui.position.top;
		$.ajax({
			url: CORDS_URL,
			type: 'post',
			data: 'x=' + parseInt( (w - xw) - ui.position.left ) + '&y=' + parseInt(((h - xh)) - ui.position.top)+'&width=' + xw
		});
	}});
}
