var globCropLBox = null;
var globImgSrc = null;

function createUploadLightbox(url, options, parameters) {
	var lbIdent, lbAction, create;

	if (url == undefined) {
		create = false;
	} else {
		create = true;
	}

	if (typeof url == 'object') {
		url = url.href;
	}

	if (create) {
		globCropLBox = new lightbox(url, options, parameters);
	}

	lbIdent = 'lightboxClose';
	lbAction = 'delete 	globCropperObj; globCropperObj = null;';

	if (!Dosia.CheckAction(lbIdent, lbAction)) {
		Dosia.AddAction2Run(lbIdent, lbAction);
	}
}

function displayUpload(url, options) {
	createUploadLightbox(url, options);
}

function cropperLoading(display, alt) {
	var uploadForm, loading;

	try {
		uploadForm = document.forms['photoCropperPhotoUpload'];

		if (display == true) {

			uploadForm.style.display = 'none';

			loading = new Element('img', {'src' : urlStatic + '/image/Admin/loaderH.gif', 'alt' : alt, 'title' : alt});

			uploadForm.appendChild(loading);
		}
	} catch (err) {
		alert('1' + err.message);
	}
}

function cropperDisplayError(error) {
	var uploadForm, errorArea;
	try {
	uploadForm = document.forms['photoCropperPhotoUpload'];
	errorArea = $('photoCropperErrorArea');

	uploadForm.firstChild.style.display = 'block';
	errorArea.innerHTML = error;

	cropperLoading(false);	} catch (err) {
		alert('2' + err.message);
	}
}

function startUpload(iframeName, obj) {
	var iframeObj;

	try {

	cropperLoading(true, 'Trwa wczytywanie zdjęcia...');

	if (obj.elements[0].value == '' || !obj.elements[0].value) {
		cropperDisplayError('Musisz wybrać plik na avatara.');
	}

	iframeObj = $(iframeName);

	iframeObj.src = obj.action;

	if (globCropperObj) {
		globCropperObj.reset();
	}
	
	obj.submit();
	} catch (err) {
		alert('3' + err.message);
	}
	
	return true;
}

function finishUpload(url, parameters) {
	var cropArea;

	if (!globCropLBox) {
		params = {'photoWidth' : parameters.photoWidth, 'photoHeight' : parameters.photoHeight, 'photoFile' : parameters.photoPath};
		createUploadLightbox(url, null, params);
	}

	cropArea = $(parameters.prefix + 'CropArea');

	if (!globImgSrc) {
		globImgSrc = cropArea.src;
	}

	cropArea.src = urlStatic + parameters.photoPath;

	initCropper(null, parameters.photoWidth, parameters.photoHeight);

	$(parameters.prefix + 'ImageFileName').value = parameters.photoPath;

	cropperLoading(false);
}

function failedUpload(fieldName, parameters) {
	var cropArea = $(globCropperParam.prefix + 'CropArea');

	if (parameters) {
		cropperDisplayError(parameters.error);
	}

	if (globImgSrc && cropArea) {
		cropArea.src = globImgSrc;
	}

	if (globCropperObj) {
		globCropperObj.setSize(300, 300);
	}
	
	destructCropper(true);
}

function refreshImage(className, photoPath) {
	var places, data, image;

	places = document.getElementsByClassName(className);

	//alert(places);

	if (places && places.length > 0) {
		data = new Date();
		image = new Element('img', {'src' : urlStatic + '/upload/' + photoPath + '?' + data.getSeconds() + data.getMilliseconds(), 'alt' : ''});

		for ( i = 0 ; i < places.length ; i++) {
			places[i].innerHTML = '';
			places[i].appendChild(image);
		}
	}
}

function doCropPhoto(formik, prefix) {

	try {
		var url = formik.action;
		
		formik.action="#";

		var myAjax = new Ajax.Request(
			url,
			{
				method: 'post',
				parameters: {
					'prefix': prefix,
					'x' : $(prefix + 'ImageCropX1').value,
					'y' : $(prefix + 'ImageCropY1').value,
					'w' : $(prefix + 'ImageCropWidth').value,
					'h' : $(prefix + 'ImageCropHeight').value,
					'photoFileName' : $(prefix + 'ImageFileName').value,
					'description' : $(prefix + 'Description').value
				},
				onSuccess: function(transport) {
					var data;
					var dir;

					try {

					destructCropper();

					data=transport.responseText.evalJSON();

					document.href = document.href + "#EtykietaZdj";

					deactivateLBox(data);
					
					} catch (e) {
						alert(e.message);
					}
				}
			}
		);
	} catch(err) {
		alert('a' + err.message);
	}
	return false;
}

function deactivateLBox(data) {

	refreshImage('_zajawkaPhoto', data['photoPath']);

	globCropLBox.deactivate();
	
	if(data['redirect']) {
		if(data['redirect'] == 'self') {
			location.reload();
		}
	}
}

//Dosia.AddAction2Run('lightboxOpen', 'createUploadLightbox();');
