var crop_api={};

function trim(x){
	return x.replace(/^\s+/, '').replace(/\s+$/, '');
}

function changeCrop(c){
	$('#crop_x').val(c.x);
	$('#crop_y').val(c.y);
	$('#crop_x2').val(c.x2);
	$('#crop_y2').val(c.y2);
	$('#crop_w').val(c.w);
	$('#crop_h').val(c.h);
};

function initializeLightbox(obj){
	$('#lightbox-content').html('Lade...');

	// TODO: lightbox content generated by Backend!
	var x=$(obj).attr('title');

	$.get('?q=_backend/crop/generate.html&file='+x,function(data){
		$('#lightbox-content').html(data);

		var img=new Image();
		$(img).attr('src','?q=_backend/crop/image.html&file='+x).load(function(){

			//$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
			$(this).hide().attr('id','cropbox');
			$('#cropbox-left').prepend(this);
			$(this).fadeIn();

			var c_pleft=(($('#cropbox-left').width()-$(this).width())/2);
			var c_ptop=(($('#cropbox-left').height()-$(this).height())/2);

			var c_width=($('#cropbox-left').width() - c_pleft);
			var c_height=($('#cropbox-left').height() - c_ptop);

			$('#cropbox-left').css({
				'padding-top':c_ptop,
				'padding-left':c_pleft,
				'width':c_width,
				'height':c_height,
			});

			if(!$('#lightbox .formats li:not(.impossible)').length > 0){
				$('#lightbox .save').remove();
				return;
			}
			crop_api = $.Jcrop('#cropbox');

			crop_api.setOptions({
				onChange: changeCrop,
				onSelect: changeCrop
			});

			if($(obj).find('.cropdata').length){
				var data = $(obj).find('.cropdata').text().split(';');
				var co = data[0].split(',');
				co[2] = parseInt(co[0],10)+parseInt(co[2],10);
				co[3] = parseInt(co[1],10)+parseInt(co[3],10);
				//var co = [50,100,200,250];
				crop_api.animateTo([co[0],co[1],co[2],co[3]]);

				var ratio = data[1].split('/');
				var min   = data[2].split(',');
				crop_api.setOptions({
					minSize: [min[0], min[1]],
					aspectRatio: ratio[0]/ratio[1]
				});

				$('#lightbox .formats').each(function(){
					var typ=$(this).find('li.header').html();
					if(trim(typ)==trim(data[3])){
						$(this).find('li:not(.impossible)').each(function(){
							if(trim(data[4])==trim($(this).text())){
								$(this).addClass('active');
							}
						});
					}
				});
			}else if($('#lightbox .formats li:not(.impossible)').length){
				updateCrop($('#lightbox .formats li:not(.impossible):first'));
			}else{
				$('#lightbox .save').remove();
			}

		}).error(function(){
			// notify the user that the image could not be loaded
			$('#cropbox-left').html('<p>Es trat ein Ladefehler auf.</p><p>Bitte klicken Sie auf Schließen und nochmals auf die Datei, um es erneut zu versuchen.</p>')
		});

		$('#lightbox').show();
	});
};

function updateCrop(ele){
	var x = $(ele).attr('title').split(',');
	var y = $(ele).parent().find('li:first').html()+';'+$(ele).html();
	var min = x[0].split('x');
	var ratio = x[1].split('/');

	crop_api.setOptions({
		minSize: [ min[0], min[1] ],
		aspectRatio: ratio[0]/ratio[1]
	});

	$('#crop_min').val(min[0]+","+min[1]);
	$('#crop_ratio').val(ratio[0]+"/"+ratio[1]);
	$('#crop_format').val(y);

	$('#lightbox').find('.formats li').removeClass('active');
	$(ele).addClass('active');

	crop_api.animateTo([0,0,min[0],min[1]]);
}

function checkReadyFiles(){
	if($('#files tbody tr').length==$('#files tbody tr.ready').length){
		alert('yes, you are ready!!!');
	}
}

if(!$('#hints').html()==""){
	$('#hints').slideDown('fast').focus();
}
