// G5 Framework
// http://Framework.GregBabula.info
// http://GregBabula.info/framework.php


progress_key = 0;




$(document).ready(function() {
/*
$('#sel_spazi').click(function(){
		$('#mappa_spazi').removeClass("hidden");
	
	});
*/


	function cerca_tipo(ext) {	
		ext = ext.toLowerCase();
		switch (ext) {
		case "jpg":
		case "jpeg": tipo = "immagine"; break;
		case "pdf":
		case "docx":
		case "doc": tipo = "documento"; break;
		case "flv":
		case "mp4":
		case "m4v":
		case "f4v": tipo = "video"; break;
		case "mp3": tipo = "audio"; break;
		default: tipo = "no"; break;
		} 
		return tipo;	
	}


function traverseFiles(el) {

	files = el.files;

	if (typeof files !== "undefined") {
		if(parseInt(files[0].size / 1024 /1024, 10)>parseInt($(el).attr('maxsize'))) 
		{
			alert("Attenzione! Dimensione massima: "+$(el).attr('maxsize')+"MB")
			$('#'+el.attr('name')).val("");
		}
}

else 
	{
		
	}
}


	// PAGINATION POSITIONING BEGIN
	
	$pagination = $('ul#pagination');
	
	$pagination.each( function()
	{
		if (! $(this).hasClass('compact'))
		{			
			pagination_width = 0;
	
			$(this).children('li').each( function()
			{
				pagination_width += $(this).outerWidth(true);
			});
			
			$(this).width(pagination_width);
			$(this).css('margin-left', $(this).parent().width() / 2 - $(this).outerWidth(true) / 2 + 0);
		}
	});

	// PAGINATION POSITIONING END
	
	
	

//ELIMINA FAKE PATH
$('input[type=file]').change(function(){
	
	
	if (window.File && window.FileReader && window.FileList && window.Blob) {
  	//alert($(this).target.files[0].size)
  	traverseFiles(this);
	}
		nomefile = $(this).val().replace('C:\\fakepath\\', '')
		$('#'+$(this).attr('name')).val(nomefile);
		allowed = $(this).attr('allowed');

		if (/\.\w+$/.test($(this).val())) {
      var m = $(this).val().match(/([^\/\\]+)\.(\w+)$/);
      if(m) {
      	tipo_ext = cerca_tipo(m[2]);
      	if(tipo_ext!=allowed) {
      		alert("Estensione non supportata per un file "+allowed)
      		$('#'+$(this).attr('name')).val("");
      		$(this).val("");
      	}
      	}
		}
		
	})


$('.attivatrattini').change(function(){
		if($(this).attr('checked')==true)
		{
			$(this).closest('li').addClass("trattini")
		}	else $(this).closest('li').removeClass("trattini")
});


 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("data-rel") == "blank")
     anchor.target = "_blank";
 }
 
 
 
$('input[data-rel!=""]').change(function() {

	if($(this).attr("checked")==true) 
	{	
		$('#'+$(this).attr("data-rel")).removeClass('hidden');
		$('#'+$(this).attr("data-rel")+' input[type="radio"]').attr('required','required');
	}
	else
	{
		$('#'+$(this).attr("data-rel")).addClass('hidden');
		$('#'+$(this).attr("data-rel")+' input[type="radio"]').removeAttr('required');	
	}
	
	if($("#espNo").attr("checked")==true) 
	{
		$("#anni").addClass('hidden');
		$("#anni"+' input[type="radio"]').removeAttr('required');
	}
});
	
	//Smooth Scroll To Top
	$(".return-top").click(function() {
		$("html, body").animate({
			scrollTop: $($(this).attr("href")).offset().top + "px"
		}, {
			duration: 250,
			easing: "swing"
		});
		return false;
	});

    //Nav IE last-child fix
     $("header#top nav ul li:last-child a").css("margin-right","0");

    //Remove padding from last paragraph
     $(".cols p:last-child").css("padding-bottom","0");

/*
    //Tipsy
  	$(function() {
	    $('.tooltip').tipsy();
	    
	    $('.tooltip-north').tipsy({gravity: 'n'});
	    $('.tooltip-south').tipsy({gravity: 's'});
	    $('.tooltip-east').tipsy({gravity: 'e'});
	    $('.tooltip-west').tipsy({gravity: 'w'});
	    
	    $('.tooltip-fade').tipsy({fade: true});
  	});
*/

	//HTML5 Placeholder Fallback

	// Create input element to do tests
	var input = document.createElement('input');

	// Add placeholder support for non HTML5 browsers
	var support_placeholder = 'placeholder' in input;
	if(!support_placeholder){
		$(':input[placeholder]').each(function() {
			var $$ = $(this);
			if($$.val() === '') {
		   
				$$.addClass('placeholder');
				$$.val($$.attr('placeholder'));
			}
			$$.focus(function() {
				$$.addClass('focus');
				if($$.val() === $$.attr('placeholder')) {
					$$.val('');
					$$.removeClass('placeholder');
				}
			}).blur(function() {
				$$.removeClass('focus');
				if($$.val() === '') {
					$$.addClass('placeholder');
					$$.val($$.attr('placeholder'));
				}
			});
		});
	}

	// Add autofocus support for non HTML5 browsers
	var support_autofocus = 'autofocus' in input;
	if(!support_autofocus){
		$('input[autofocus]').eq(0).focus();
	}else{
		// Fix for opera
		$('input[autofocus]').eq(0).val('');	
		$('input[autofocus]').eq(0).removeClass('placeholder');
	}

	// Handler form validation
	$('input,textarea').keyup(function() {
		validate(this);
	});

	// Validate an element
	function validate(element){
		var $$ = $(element);
		var validator = element.getAttribute('type'); // Using pure javascript because jQuery always returns text in none HTML5 browsers
		var valid = true;
		var apply_class_to = $$;
		
		var required = element.getAttribute('required') == null ? false : true;
		switch(validator){
			case 'email': valid = is_email($$.val()); break;
			case 'url': valid = is_url($$.val()); break;
			case 'number': valid = is_number($$.val()); break;
		}
		
		// Extra required validation
		if(valid && required && $$.val().replace($$.attr('placeholder'), '') == ''){
			valid = false;
		}
		
		// Set input to valid of invalid
		if(valid || (!required && $$.val() == '')){
			apply_class_to.removeClass('invalid');
			apply_class_to.addClass('valid');
			return true;
		}else{
			apply_class_to.removeClass('valid');
			apply_class_to.addClass('invalid');
			return false;
		}
	}

	// Add required class to inputs
	$(':input[required]').addClass('required');

	// Block submit if there are invalid classes found
	$('form').submit(function() {
		$('input,textarea').each(function() {
			validate(this);
		});
		if(($(this).find(".invalid").length) == 0){
			// Delete all placeholder text
			$('input,textarea').each(function() {
				if($(this).val() == $(this).attr('placeholder')) $(this).val('');
			});
			if(progress_key!=0) beginUpload();
			return true;
		}else{
			return false;
		}
	});

	function is_email(value){
		return (/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])(([a-z0-9-])*([a-z0-9]))+(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/).test(value);
	}

	function is_url(value){
		return (/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i).test(value);
	}

	function is_number(value){
		return (typeof(value) === 'number' || typeof(value) === 'string') && value !== '' && !isNaN(value);
	}

});




