jQuery(document).ready(function(){
	/*
	$.ajaxSetup({
		'beforeSend' : function(xhr) {
			if (xhr && xhr.overrideMimeType) xhr.overrideMimeType('text/html; charset=ISO-8859-1');
		},
	});
	*/

	$('a.scrollto').click(function() {
		var pos = $(this).attr('rel');
		if (pos) {
			$.scrollTo('#' + pos, 1000, {offset: -111});
		}
	});

	set_form_submit();

	function set_form_submit() {
		$('.hidden_data .form_data').each(function () {
			var form_id = $(this).find('.form_id').text();
			var form_url = $(this).find('.form_url').text();

			set_form_submit_single(form_id, form_url);
		});
	}

	function set_form_submit_single(form_id, form_url) {
		$('form#' + form_id + ' input[type=checkbox]').checkbox({cls:'jquery-checkbox', empty: 'immagini/form/empty.gif'});

		$('form#' + form_id + ' div.form .formsubmit').css('cursor', 'pointer');
		$('form#' + form_id + ' div.fieldinput4').css('opacity', '1.0');
		$('form#' + form_id + ' div.contentloader').hide();

		$('form#' + form_id + ' div.fieldinput4').click(function() {

			$('form#' + form_id + ' div.form .formsubmit').css('cursor', 'auto');
			$('form#' + form_id + ' div.fieldinput4').css('opacity', '0.5');
			$('form#' + form_id + ' div.fieldinput4').click(function() {});
			$('form#' + form_id + ' div.contentloader').show();

			var form_data = $('form#' + form_id).serialize();
			$.post(form_url, form_data, function(data) {
				$('form#' + form_id).html(data);
				if ($('form#' + form_id + ' .error:first').html()) {
					$.scrollTo('form#' + form_id + ' .error:first', 1000, {offset: -111});
				}
				else {
					if ($('form#' + form_id + ' .message_sent_ok').text()) $.scrollTo('form#' + form_id + ' .message_sent_ok', 1000, {offset: -111});
				}
				set_form_submit_single(form_id, form_url);
				tb_init('form#' + form_id + ' a.thickbox, form#' + form_id + ' area.thickbox, form#' + form_id + ' input.thickbox');//pass where to apply thickbox
				if (form_id == 'segnala_locale_form') {
					$('a.scrollto').click(function() {
						var pos = $(this).attr('rel');
						if (pos) {
							$.scrollTo('#' + pos, 1000, {offset: -111});
						}
					});
				}
			});
		});
	}
});