$(document).ready(function () {

// top bar
	$(".connect").mouseover(function () {
		$(this).animate({
			"opacity" : "0.8"
		});

	});
	$(".connect").mouseout(function () {
		$(this).animate({
			"opacity" : "1.0"
		});

	});
	$(".connect").click(function () {
		$("#header").slideToggle("slow")
	});



// news right column
	$('.news_img_home img').live('mouseover', function() {
		$(this).animate({
			"opacity" : "0.6"
		});

	});
	$('.news_img_home img').live('mouseout', function() {
		$(this).animate({
			"opacity" : "1.0"
		});
	});



// photogallery right column
	$('.gallery_img_home img').live('mouseover', function() {
		$(this).animate({
			"opacity" : "0.6"
		});

	});
	$('.gallery_img_home img').live('mouseout', function() {
		$(this).animate({
			"opacity" : "1.0"
		});
	});



// thumbnails photogallery e news
	$('.foto_img_home img').live('mouseover', function() {
		$(this).animate({
			"opacity" : "0.6"
		});
		$(".pino").show();
	});
  
	$('.foto_img_home img').live('mouseout', function() {
		$(this).animate({
			"opacity" : "1.0"
		});
	
	});




	$("#uno.foto_img_home img").mouseover(function () {
		$(this).animate({
			"opacity" : "0.6"
		});
		$(".foto_separatore").animate({
			top : "135px"
		});
	});
	$("#uno.foto_img_home img").mouseout(function () {
		$(this).animate({
			"opacity" : "1.0"
		});
		$(".foto_separatore").animate({
			top : "130px"
	 
		});
	});
	$("#due.foto_img_home img").mouseover(function () {
		$(this).animate({
			"opacity" : "0.6"
		});
		$(".foto_separatore2").animate({
			top : "135px"

		});
	});
  	$("#due.foto_img_home img").mouseout(function () {
		$(this).animate({
			"opacity" : "1.0"
		});
		$(".foto_separatore2").animate({
			top : "130px"

		});
	});
	$("#tre.foto_img_home img").mouseover(function () {
		$(this).animate({
			"opacity" : "0.6"
		});
		$(".foto_separatore3").animate({
			top : "135px"
		});
	});
  	$("#tre.foto_img_home img").mouseout(function () {
		$(this).animate({
			"opacity" : "1.0"
		});
		$(".foto_separatore3").animate({
			top : "130px"
		});
	});
	$("#quattro.foto_img_home img").mouseover(function () {
		$(this).animate({
			"opacity" : "0.6"
		});
		$(".foto_separatore4").animate({
			top : "135px"
		});
	});
	$("#quattro.foto_img_home img").mouseout(function () {
		$(this).animate({
			"opacity" : "1.0"
		});
		$(".foto_separatore4").animate({
			top : "130px"
		});
	});
});

// gallery grande home
$(document).ready(function () {
	$(".contract a").click(function () {
		$("#gallery_main").slideToggle("slow")
	})
});



$(window).load(function() {
	$('#slider').nivoSlider();
});



//per menù
$(document).ready(function() {

	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();

		//Calculate width of all ul's
		(function($) {
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {
					rowWidth += $(this).width();
				});
			};
		})(jQuery);

		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;
			//Calculate each row
			$(this).find(".row").each(function() {
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({
				'width' :biggestRow
			});
			$(this).find(".row:last").css({
				'margin':'0'
			});

		} else { //If row does not exist...

			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({
				'width' : rowWidth
			});
		}
	}

	function megaHoverOut(){
		$(this).find(".sub").stop().fadeTo('fast', 0, function() {
			$(this).hide();
		});
	}



	var config = {
		sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
		interval: 100, // number = milliseconds for onMouseOver polling interval
		over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
		timeout: 500, // number = milliseconds delay before onMouseOut
		out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};

	$("ul#topnav li .sub").css({
		'opacity':'0'
	});
	$("ul#topnav li").hoverIntent(config);
});



// per bubble
$(function () {
	$('.bubbleInfo').each(function () {
		var distance = 10;
		var time = 250;
		var hideDelay = 500;

		var hideDelayTimer = null;

		var beingShown = false;
		var shown = false;
		var trigger = $('.trigger', this);
		var info = $('.popup', this).css('opacity', 0);


		$([trigger.get(0), info.get(0)]).mouseover(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				// don't trigger the animation again
				return;
			} else {
				// reset position of info box
				beingShown = true;

				info.css({
					top: 77,
					left: 222,
					display: 'block'
				}).animate({
					top: '-=' + distance + 'px',
					opacity: 1
				}, time, 'swing', function() {
					beingShown = false;
					shown = true;
				});
			}

			return false;
		}).mouseout(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				info.animate({
					top: '-=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					shown = false;
					info.css('display', 'none');
				});

			}, hideDelay);

			return false;
		});
	});
});



// added by pèx 9/12/11



// type == 0 == chi_siamo.php (id_docente)
function dialog_docente_info_request(id_docente) {
	var post_data = 'type=0&id_docente=' + id_docente;

	$('#dialog_1').data('post_data', post_data);
	$('#dialog_1').dialog('open');
}

// type == 1 == corsi.php (id_docente, id_corso, corso_type, id_sede)
function dialog_corso_info_request(id_docente, id_corso, corso_type, id_sede) {
	var post_data = 'type=1&id_docente=' + id_docente + '&id_corso=' + id_corso + '&corso_type=' + corso_type + '&id_sede=' + id_sede;

	$('#dialog_2').data('post_data', post_data);
	$('#dialog_2').dialog('open');

// ho preferito mantenere una dialog differente (_2) per le richieste di informazioni relative ai corsi (aggiungere select con orari, giorni, .. per maggior specificità)

}

// type == 2 == sedi_page_right.php
function dialog_sede_info_request(email_type, id_sede) {
	var post_data = 'type=2&email_type=' + email_type + '&id_sede=' + id_sede;

	$('#dialog_1').data('post_data', post_data);
	$('#dialog_1').dialog('open');
}



$(function() {

// type == 3 == index_pulsanti_down.php
	$('a.dialog_generic_info_request').live('click', function() {
		var post_data = 'type=3';

		$('#dialog_1').data('post_data', post_data);
		$('#dialog_1').dialog('open');
	});

	$('#dialog_1').dialog({
		autoOpen : false,
		height: 510,
		modal: true,
		draggable : true,
		width: 360,
		zIndex: 3200,
		disabled: false,
		buttons: {

			'Ok': function() {

				dialog_btns();
			
				var arr_return = get_dialog_params(1);
				var post_data = $('#dialog_1').data('post_data') + '&' + arr_return[1];
				var err_text = arr_return[0];				

				if (err_text != '') {
					$('#dialog_1_oper_text').empty().fadeIn('slow').append(err_text);
					window.setTimeout(function() { 
						$('#dialog_1_oper_text').fadeOut('slow');
						dialog_btns(true);
					}, 1500);

				} else {

					var dialog_text = "Invio in corso..";
					$('#dialog_1_oper_text').empty().fadeIn('slow').append(dialog_text);

					$.ajax({
						type: 'POST',
						url: 'email_send.php',
						data: post_data,
						cache: false,
  						success: function(data, textStatus, jqXHR) { 

							if ($.trim(data) == '1') {
								dialog_text = 'Messaggio correttamente inviato, grazie!<br>';
							} else if ($.trim(data) == '0') {
								dialog_text = 'Errore. Raggiunto il limite massimo di messaggi inviabili.';
							} else {
								dialog_text = 'Errore. Si prega di riprovare in seguito.';
							}

							$('#dialog_1_oper_text').empty().fadeIn('slow').append(dialog_text);

							window.setTimeout(function() { 
								$('#dialog_1').dialog('close');	
							}, 3000);
						},
						error: function(jqXHR, textStatus, errorThrown) {
							dialog_text = 'Errore. Si prega di riprovare in seguito.'; 
							$('#dialog_1_oper_text').empty().fadeIn('slow').append(dialog_text);

							window.setTimeout(function() { 
								$('#dialog_2').dialog('close');	
							}, 3000);
						}
					});
				}
			},
			'Cancel': function() {
				$(this).dialog('close');
			}
		},
		open: function() {

			dialog_btns(true);

			$('.dialog_1_input_text').val('');
			$('#dialog_1_oper_text').empty();
		}
	});

	$('#dialog_2').dialog({
		autoOpen : false,
		height: 510,
		modal: true,
		draggable : true,
		width: 360,
		zIndex: 3200,
		disabled: false,
		buttons: {

			'Ok': function() {

				dialog_btns();
			
				var arr_return = get_dialog_params(2);
				var post_data = $('#dialog_2').data('post_data') + '&' + arr_return[1];
				var err_text = arr_return[0];				

				if (err_text != '') {
					$('#dialog_2_oper_text').empty().fadeIn('slow').append(err_text);
					window.setTimeout(function() { 
						$('#dialog_2_oper_text').fadeOut('slow');
						dialog_btns(true);
					}, 1500);

				} else {

					var dialog_text = "Invio in corso..";
					$('#dialog_2_oper_text').empty().fadeIn('slow').append(dialog_text);

					$.ajax({
						type: 'POST',
						url: 'email_send.php',
						dataType: 'json',
						data: post_data,
						cache: false,
  						success: function(data, textStatus, jqXHR) { 
							if ($.trim(data) == '1') {
								dialog_text = 'Messaggio correttamente inviato, grazie!<br>';
							} else if ($.trim(data) == '0') {
								dialog_text = 'Errore. Raggiunto il limite massimo di messaggi inviabili.';
							} else {
								dialog_text = 'Errore. Si prega di riprovare in seguito.';
							}

							$('#dialog_2_oper_text').empty().fadeIn('slow').append(dialog_text);

							window.setTimeout(function() { 
								$('#dialog_2').dialog('close');	
							}, 3000);
						},
						error: function(jqXHR, textStatus, errorThrown) {
							dialog_text = 'Errore. Si prega di riprovare in seguito.'; 
							$('#dialog_2_oper_text').empty().fadeIn('slow').append(dialog_text);

							window.setTimeout(function() { 
								$('#dialog_2').dialog('close');	
							}, 3000);
						}
					});
				}
			},
			'Cancel': function() {
				$(this).dialog('close');
			}
		},
		open: function() {

			dialog_btns(true);

			$('.dialog_2_input_text').val('');
			$('#dialog_2_oper_text').empty();
		}
	});
});



function get_dialog_params(dialog_n) {

	var blank_fields = new Array;
	var err_fields = new Array;
	var post_data = '';
				
	$('.dialog_' + dialog_n + '_input_text').each(function(i) {
		var current_id = $(this).attr('id');
		var current_val = $(this).val();

		if (current_val.length == 0) {
			blank_fields.push(current_id);

		} else if (current_id == 'dialog_' + dialog_n + '_email') {
			if ( ! check_email($.trim(current_val)) ) {
				err_fields.push(current_id);
			}
		}

// es: dialog_1_nome
		var str_arr = current_id.split('_');
		post_data += str_arr[2] + '=' + encodeURIComponent(current_val) + '&';
	});

	var err_text = '';
	if (blank_fields.length > 0) {
		err_text = 'Tutti i campi sono obbligatori.';		
	}

	if ($.inArray('dialog_' + dialog_n + '_email', err_fields) >= 0) {
		if (err_text != '') {
			err_text += '<br>';
		}
		err_text += 'Indirizzo email non corretto.';
	}

	var arr_return = new Array;
	arr_return.push(err_text);
	arr_return.push(post_data);

	return arr_return;
}



function check_email(email) {

	var atom="\[^\\s"+"\\(\\)><@,;:\\\\\\\"\\.\\[\\]"+"\]"+"+";
	var word="("+atom+"|"+"(\"[^\"]*\")"+")";
	var regExpDominio=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var checkUser=new RegExp("^"+word+"(\\."+word+")*$");
	var arrEmail=email.match(/^(.+)@(.+)$/);
	if (arrEmail == null) {
		return false;
	}

	var user=arrEmail[1];
	var dominio=arrEmail[2];
	for (var i=0;i<user.length;i++) {
		if (user.charCodeAt(i)>127) {
			return false;
		}
	}

	for (var i=0;i<dominio.length;i++) {
		if (dominio.charCodeAt(i)>127) {
			return false;
		}
	}

	if (user.match(checkUser) == null) {
		return false;
	}

	var arrIP=dominio.match(regExpDominio);
	var arrDominio=dominio.split(".");
	if (arrIP != null) {
		for (var i=1;i<=4;i++) {
			if (arrIP[i]>255) {
				return false;
			}
		}
		return true;
	}

	var regExpAtom=new RegExp("^"+atom+"$");
	var len=arrDominio.length;
	for (var i=0;i<len;i++) {
		if (arrDominio[i].search(regExpAtom) == -1) {
			return false;
		}
	}

	if (len<2) {
		return false;
	}
	return true;
}



function dialog_btns(enable) {
	var btn = $('.ui-dialog-buttonpane').find('button');

	if (enable) {
		btn.button('option', 'disabled', false)
		btn.removeClass('ui-state-disabled');
	} else {
        	btn.button('option', 'disabled', true)
		btn.addClass('ui-state-disabled');
	}
}
