var $uploadHandler;

function filtratabella() {

	on = $("#filtropv option:selected").html();
	if (on!='tutte') {
		$('#dati tr[rel!='+on+']').hide();
		$('#dati tr[rel^='+on+']').show();
	} else {
		$('#dati tr').show();
	}
}

function showNetFunForm($form) {
	$('#loading').hide();
	$('#text').hide();
	$('#media').hide();

	$('#type').val($form.toUpperCase());

	if ($form!="text") {
		$('#media').fadeIn();		
	} else {
		$('#'+$form).fadeIn();
	}
}

function insertNetFunContent() {
	$type = $('#type').val();
	$err = "";
	
	var $action = $('#action').val();
	var $id = $('#id').val();
	
	if ($type=="TEXT") {
		var $title = $('#title1').val();
		var $text = $('#text1').val();
		
		if ($title=="") {
			$err += "Inserisci un titolo.<br/>";
		}
		if ($text=="") {
			$err += "Inserisci un testo.<br/>";
		}
	} else {
		var $title = $('#title2').val();
		
		if ($title=="") {
			$err += "Inserisci un titolo.<br/>";
		}
	}
	
	if ($err=="") {
		if ($type=="TEXT") $('#text').hide();
		
		if ($type=="TEXT") {
			$('#loading').fadeIn();
			var $data = {
				action: $action,
				type: $type,
				id: $id,
				title: $title,
				text: $text
			}
			$.post(
				'/ajax-salva-contenuto-net-fun.php',
				$data,
				insertNetFunContentCallback,
				'json'
			);
		} else {
			var $fileUpload = $('input[name="attachment"]').val();
			if ($fileUpload == undefined || $fileUpload == '') {
				$err = "Inserire il file da inviare.<br/>";
				$('#basic-modal-content').html($err);
				$('#basic-modal-content').modal();
			} else {
				if ($('#error').val()=="FALSE") {
					$('#media').hide();
					$('#loading').fadeIn();
					
					var $data = {
						action: $action,
						type: $type,
						id: $id,
						title: $title
					}
					$uploadHandler.setData($data);
					$uploadHandler.submit();			
				} else {
					$err = "Il file selezionato non &egrave; nel formato corretto<br/>";
					$('#basic-modal-content').html($err);
					$('#basic-modal-content').modal();
				}
			}
		
		}
	} else {
		$('#basic-modal-content').html($err);
		$('#basic-modal-content').modal();
	}
}

function insertNetFunContentCallback($data) {
	$('#loading').fadeOut();
	if ($data.success) {
		$('#basic-modal-content').html($data.output);
	} else {
		$('#basic-modal-content').html($data.error);
	}
	$('#basic-modal-content').modal();
}

function insertNetFunContentFile() {
	$id = "";
	$uploadHandler = new AjaxUpload (
		'addNewFile', 
		{
			action: '/ajax-salva-contenuto-net-fun.php',
			autoSubmit: false,
			name: 'attachment',
			responseType: 'json',
			onChange: function (file, ext) {
				$err = "";
				$type = $('#type').val();
				if ($type=="VIDEO") {
					if (!(ext && /^(mpg|avi|mov|mp4|3gp|wmv)$/i.test(ext))){
						$err = "Il video selezionato non &egrave; nel formato corretto.";
					}
				} else {
					if (!(ext && /^(jpg|gif|bmp|png|tiff)$/i.test(ext))){
						$err = "La fotografia selezionata non &egrave; nel formato corretto.";
					}
				}	
					
				if ($err!="") {
					$('#basic-modal-content').html($err);
					$('#basic-modal-content').modal();
					$('#error').val("TRUE");
				} else {
					$('#error').val("FALSE");
				}
			},
			onSubmit: function(file, ext) {
			},
			onComplete : function(file, response) {
				insertNetFunContentCallback(response);
			}			
		}
	);
}

function showSlideshowContent($id) {
	if (($id==8)||($id==10)||($id==15)) {
		$w = 400;
		$h = 328;
	} else if (($id==7)||($id==9)||($id==11)||($id==12)||($id==14)) {
		$w = 600;
		$h = 500;
	} else {
		$w = 800;
		$h = 625;
	}
	
	$w += 50;
	$h += 20;
	
	var $html = $('#user-content-'+$id).html();
	
//	var $w = $('#user-content-'+$id).width()+50;
//	var $h = $('#user-content-'+$id).height()+20;

	$('#basic-modal-content').html($html);
	$('#basic-modal-content').modal({
		containerCss:{
			height: $h,
			width: $w
		}
	});
}

