$('document').ready(function(){
	
	/*** CLICK SU UN ALBUM **/
	$("img[name^='album_']").live('click', function() {
		var id = $(this).attr("name").replace('album_','');

		$.get('/fotografia/fotoalbum/id/'+id, function(data) {
			$("#show_foto").hide();
			$("#tit_foto").hide();
			$("#show_album").after(data);

			$("a[rel^=vis_]").fancybox({
				'type' 				: 'image',
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'titlePosition' 	: 'over',
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-over">' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
				}
			});
		});

		return false;
	});
	
	function getFotoChecked() {         
		var allVals = [];
	    $("input[name^='check_foto_']:checked").each(function() {
	    	allVals.push($(this).val());
	    });
	    
	    return allVals;
	}
	
	/*** ASSOCIA FOTO AD UN ALBUM **/
	$("#associa-foto-album").live('click', function() {
		var id_album_origine = $("input[name='id_album_shown']").val();
		var id_album_destinazione = $("input[name='gruppo_album']:checked").val();
		
	    var res = getFotoChecked();
	    
	    if(id_album_origine != "" && id_album_destinazione > 0 && res != ""){
	    	if(id_album_origine != id_album_destinazione){ // C'è spostamento
		    	$.ajax({
		    		type: "POST",
		    		url: '/fotografia/associafotoalbum',
		    		data: ({id_album_o : ''+id_album_origine,id_album_d : ''+id_album_destinazione,photos : ''+res}),
		    		success: function(html){
						window.open('/fotografia/index','_self');
					}
					,error:function(err){}
		    	});
	    	}
	    	$("#errore").html('');
	    }
	    else{
	    	$("#errore").html('Seleziona un album e almeno una foto');
	    }
	    
	});
	
	/*** TAG CLIENTE **/
	$("#tag_cliente").live('click', function() {
		var id_user = $("#lista_clienti").val();
	    var res = getFotoChecked();
	    
	    if(id_user != "" && res != ""){
	    	$.ajax({
	    		type: "POST",
	    		url: '/fotografia/tagfotocliente',
	    		data: ({id_user : ''+id_user, photos : ''+res}),
	    		success: function(html){
	    			$("#msg").html('Utente taggato');
	    			$("#errore_tag").html('');
				}
				,error:function(err){}
	    	});	    	
	    }
	    else{
	    	$("#errore_tag").html('Seleziona un utente e almeno una foto');
	    }
	    
	});
	
	/*** CANCELLA FOTO **/
	$("#canc_foto").live('click', function() {
	    var res = getFotoChecked();
	    
	    if(res != ""){
	    	$.fancybox({
				'content' 			: "<div><strong>Vuoi cancellare la/e foto selezionata/e ?</strong><br><br><div style='text-align:center;'><input id='canc_si' class='button_reset' type='button' value='Si' name='reset_password_' /><input id='canc_no' class='button_reset' type='button' value='No' name='chiudi_fancy' /></div></div>",
				'width'				: 450,
				'height'			: 'auto',
				'titleShow'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none'
			});
	    }
	    else{
	    	$("#msg_err").html('Seleziona almeno una foto');
	    }
	    
	});
	
	/*** CLICK SU SI **/
	$("input[id='canc_si']").live('click', function() {
		var res = getFotoChecked();
		
		$.ajax({
    		type: "POST",
    		url: '/fotografia/cancellafoto',
    		data: ({photos : ''+res}),
    		success: function(html){
				window.open('/fotografia/index','_self');
			}
			,error:function(err){}
    	});
    	
	});
	
	/*** CLICK SU NO **/
	$("input[id='canc_no']").live('click', function() {
		$("#fancybox-close").click();
	});

});
