/*
 * The very first increment of Droppables&Draggables demo. The code is going to
 * be more concise (remove unnecessary code repetitions etc.). And imho the
 * photo management is a good candidate for demonstration of more jQuery UI
 * components (sortables, selectables...). More to come...
 *
 */

$(window).bind('load', function() {
										
	// make images in the gallery draggable
	$('#oggetti_list li img').addClass('img_content').draggable({
		helper: 'clone'
	});	$('#box_cr_17 img').addClass('img_trash').draggable({
		helper: 'clone'
	});$('#box_cr_xy img').addClass('img_trash').draggable({
		helper: 'clone'
	});
	

	// make the scrigno box droppable, accepting images from the content section only
	$('#box_cr_17').droppable({
		accept: '.img_content',
		activeClass: 'active',
		
		drop: function(ev, ui) {
			var $that = $(this);
			ui.draggable.fadeIn('slow', function() {
				ui.draggable
				.clone()
				
				//	.hide()
					 
					.appendTo($that)
					
					.fadeIn('slow')
					.animate({
						width: '50px',
						height: '35px'
					})
					.removeClass('img_content')
					.addClass('img_trash')
		.draggable({
		helper: 'clone'
	});
				;
				valore_oggetto = Number($(ui.draggable).attr('rel'));
				val_cont  = Number($('#cont_val').val());
				var somma = (val_cont + valore_oggetto);
				$('#cont_val').val(somma);
				$('#ammontare_donazione').val(somma);

				$('#cont_val_frm').val(somma);
				
				var contenuto_scr_17=$('#box_cr_17').html();
				$('#box_cr_17_frm').val(escape(contenuto_scr_17));
				
					var contenuto_scr_xy=$('#box_cr_xy').html();
				$('#box_cr_xy_frm').val(escape(contenuto_scr_xy));
		
			});
		}
	});$('#box_cr_xy').droppable({
		accept: '.img_content',
		activeClass: 'active',
		
		drop: function(ev, ui) {
			var $that = $(this);
			ui.draggable.fadeIn('slow', function() {
				ui.draggable
				.clone()
				
				//	.hide()
					 
					.appendTo($that)
					
					.fadeIn('slow')
					.animate({
						width: '50px',
						height: '35px'
					})
					.removeClass('img_content')
					.addClass('img_trash')
		.draggable({
		helper: 'clone'
	});
				;
				valore_oggetto = Number($(ui.draggable).attr('rel'));
				val_cont  = Number($('#cont_val').val());
				var somma = (val_cont + valore_oggetto);
				$('#cont_val').val(somma);
				$('#cont_val_frm').val(somma);
				$('#ammontare_donazione').val(somma);
				
					var contenuto_scr_17=$('#box_cr_17').html();
				$('#box_cr_17_frm').val(escape(contenuto_scr_17));
				
					var contenuto_scr_xy=$('#box_cr_xy').html();
				$('#box_cr_xy_frm').val(escape(contenuto_scr_xy));
		
			});
		}
	});



// make the shredder box droppable, accepting images from both content and trash sections
	$('#cestino_oggetti').droppable({
		accept: '.img_trash',
		activeClass: 'active',
		drop: function(ev, ui) {
			var $that = $(this);
			
		 if (ui.draggable.hasClass('img_trash')) {
				ui.draggable
					.appendTo($that)
					.animate({
						width: '0',
						height: '0'
					}, 'slow', function(){
						$(this).remove();
						
					var_rif = "#cont_val";
						var_rif2 = "#cont_val_frm";
						
				valore_oggetto = Number($(ui.draggable).attr('rel'));
				val_cont  = Number($(var_rif).val());
				var somma = (val_cont - valore_oggetto);
				$(var_rif).val(somma);
				
				$(var_rif2).val(somma);
				$('#ammontare_donazione').val(somma);


				var contenuto_scr_17=$('#box_cr_17').html();
				$('#box_cr_17_frm').val(escape(contenuto_scr_17));
				
					var contenuto_scr_xy=$('#box_cr_xy').html();
				$('#box_cr_xy_frm').val(escape(contenuto_scr_xy));
		
					});
			}
		}
	});



	
	// handle the trash icon behavior
	$('a.tb_trash').livequery('click', function() {
		var $this = $(this);
		var $img = $this.parent().siblings('img');
		var $item = $this.parents('li');
			
		$item.fadeOut('slow', function() {
			$img
				.hide()
				.appendTo('#trash div')
				.fadeIn('slow')
				.animate({
					width: '72px',
					height: '54px'
				})
				.removeClass('img_content')
				.addClass('img_trash');
			$(this).remove();
		});

		return false;
	});

	// handle the magnify button
	$('a.tb_supersize').livequery('click', function() {
		$('<img width="576" height="432">')
			.attr('src', $(this).attr('href'))
			.appendTo('#body_wrap')
			.displayBox();
		return false;
	});
});

function createGalleryItem(img) {
	var title = img.getAttribute('alt');
	var href = img.getAttribute('src').replace(/thumbs\//, '');

	var $item = $('<li><p>'+title+'</p><div><a href="#" title="Trash me" class="tb_trash">Trash me</a><a href="'+href+'" title="See me supersized" class="tb_supersize">See me supersized</a></div></li>').hide();
	$item.prepend($(img));

	return $item;
}
