$(document).ready(function() {
	
	// q tip initilize
	$('a.qtip').each(function()
	   {
	      // Create image content using websnapr thumbnail service
	      var content = '<img src="';
	      content += $(this).attr('href');
	      content += '" alt="Loading thumbnail..." />';
	      
	      // Setup the tooltip with the content
	      $(this).qtip({
	         content: content,	        
		 position: {
		     corner: {
			target: 'bottomMiddle', // Position the tooltip above the link
			tooltip: 'topMiddle'
		     },
		     adjust: {
			screen: true // Keep the tooltip on-screen at all times
		     }
		  },
		  show: {
		     when: 'mouseover',
		     solo: true // Only show one tooltip at a time
		  },
		  hide: {
		     when: 'mouseout',
		     fixed: true
		  },
		  style: {
			when: 'mouseout',
		     tip: true, // Apply a speech bubble tip to the tooltip at the designated tooltip corner
		     border: {
			width: 0,
			radius: 4
		     },
		     name: 'light', // Use the default light style
		     width: 380 // Set the tooltip width
        	  }
	      });
	   });



	
	// bottone per iscrizione newsletter
	$('#go').click(function() {
		$('#dialogNewsletter').html('Vuoi iscriverti alla nostra newsletter?');
		$('#dialogNewsletter').dialog('open');
	});
	
	// box dialog newsletter
	$("#dialogNewsletter").dialog({
		show: 'blind',
		bgiframe: true,
		autoOpen: false,
		height: 200,
		width:500,
		modal: true,
		buttons: {
			'Iscriviti': function() {
			 datiNewsletter = $("#newsletterForm").serialize();
			 $.ajax({
				type: "POST",
				url: "form/sendnewsletter.php",
				data: datiNewsletter,
				success: function(data) {
					$("#dialogNewsletter").html("Ciao " + data + "<br>Grazie per esserti iscritto alla nostra newsletter!");
					$('#dialogNewsletter').dialog('option', 'buttons', { "Chiudi": function() { $('#dialogNewsletter').dialog("close"); } });
				}

			 });
			 return false;

			},
			'Indietro': function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			$("#menuMessage").empty();
		}
	});

	// ###################################
	// menu portfolio per scelta categoria
	// ###################################

	$("#menuTendina").hide();
	$("#buttonMenuPortfolio").toggle(
		  function () {
			$("#menuTendina").fadeIn();
		  },
		  function () {
			$("#menuTendina").fadeOut();
		  }
	);

	//accordion init
	$("#accordion").accordion({
		collapsible: true,
		autoHeight: false,
		animated: false
	});

	//progressbar
	$("#progressbar").progressbar({ value: 25 });
	$(".content25").show();


	// progress bsr on click sui titoli di sezione in basso
	$('.progressButton').click(function() {
		$('#progressbar').stopTime('controlled');
		var percent = $(this).attr("name");
		$('#progressbar').progressbar('option', 'value', percent);

		alunni =new Array ("25","50","75","100");
		i=0;
		while(i<alunni.length) {
			if(alunni[i]==percent){
				$(".content"+alunni[i]).fadeIn();
			}
			else{
				$(".content"+alunni[i]).hide();
			}
		i++;
		}

		return false;
	});


	// prgress bar atuomatica al caricamento di pagina
	var active = false;
	percent = 25;
	$('#progressbar').everyTime(10000, 'controlled', function() {
		percent = percent + 25;
		$('#progressbar').progressbar('option', 'value', percent);

		alunni =new Array ("25","50","75","100");
		i=0;
			while(i<alunni.length) {
				if(alunni[i]==percent){
					$(".content"+alunni[i]).fadeIn();
				}
				else{
					$(".content"+alunni[i]).hide();
				}
			i++;
		}

		percent = percent;
		if(percent=='100'){
			$(this).stopTime('controlled');
		}
	});


	// progress bar on mouse click su start
	$('.start').css("cursor", "pointer").click(function() {
		if (!active) {
			$('#progressbar').progressbar('option', 'value', 25);
			$(".content25").show();

			active = !active;
			percent = 25;
			$(this).everyTime(3000, 'controlled', function() {
				percent = percent + 25;
				$('#progressbar').progressbar('option', 'value', percent);

				alunni =new Array ("25","50","75","100");
				i=0;
					while(i<alunni.length) {
						if(alunni[i]==percent){
							$(".content"+alunni[i]).fadeIn();
						}
						else{
							$(".content"+alunni[i]).hide();
						}
					i++;
				}

				percent = percent;
				if(percent=='100'){
					$(this).stopTime('controlled');
				}
			});
		}
		else{
			percent = percent;
			$(this).everyTime(3000, 'controlled', function() {
				percent = percent + 25;
				$('#progressbar').progressbar('option', 'value', percent);

				alunni =new Array ("25","50","75","100");
				i=0;
					while(i<alunni.length) {
						if(alunni[i]==percent){
							$("#content"+alunni[i]).fadeIn();
						}
						else{
							$("#content"+alunni[i]).hide();
						}
					i++;
				}

				percent = percent;
				if(percent=='100'){
					$(this).stopTime('controlled');
				}
			});
		}

	});

	$('.stop').css("cursor", "pointer").click(function() {
		if (active) {
			active = active;
			$(this).stopTime('controlled');
		}
	});

	// innerfade init
	$('#textPromo').innerfade({ animationtype: 'fade', speed: 250, timeout: 8000, containerheight: '200px' });

});