
function search_single_type_int_routines()
{
	$('.search-single-type-int a.normal').each(
		function() {
			var t = $( this );
			var p = t.parent();
			var txt = t.text();
			var txt1 = t.attr('href').match(/\?st=([a-z]+)$/)[1];
			var radio = '<input class="hidden" type="radio" name="st" value="RADIO_VALUE" />';
			t.replaceWith( txt + ' ' + radio.replace('RADIO_VALUE', txt1) );
			$('input:radio', p).uniform();
			if( txt1 == st_loaded ) {
				$('input:radio', p).attr('checked', true );
				p.find('span').addClass('checked')
			}
		}
	);
}

function loadSearchType()
{
	switch( search_main_type ) {
		case 'i':
			type_loaded = st_loaded == 'igenerale' ? 'igenerale' : 'iprocedurale';
		   	type2load = type_loaded == 'igenerale' ? 'iprocedurale' : 'igenerale';
			break;
		case 'm':
			type_loaded = st_loaded == 'mgenerale' ? 'mgenerale' : 'mtemporale';
		   	type2load = type_loaded == 'mgenerale' ? 'mtemporale' : 'mgenerale';
			break;
	}

	params = [];
	params.push( 'search_main_type=' + search_main_type );
	params.push( 'type_loaded=' + type_loaded );
	$.ajax({
		type: 'POST',
		url: SITE_PATH + 'ajax/getSearchType/',
		data: params.join('&'),
		dataType: 'json',
		success: function( result ) {
			if( typeof result['Error'] != 'undefined' ) {
				_unloading( result['Error'] );
				return false;
			}

			$('#sf-container').append( result['content'] );
			$('.search-form-container').prepend( result['ssti'] );

			search_single_type_int_routines();
			switchSearchTypeRoutines();
			fieldRoutines();
		}
	});
}

function fieldRoutines()
{
	$('.date-field').datepicker( $.datepicker.regional['it'] );
	$('#Comune').autocomplete({
		minLength: 2,
		source: SITE_PATH + 'ajax/searchAddressPart/?t=comune',
		select: function( event, ui ) {
			$('#Comune').val( ui.item.fullname );
			$('input[name=com_id]').val( ui.item.id );
			return false;
		},
		focus: function( event, ui ) {
			$('#Comune').val( ui.item.fullname );
			return false;
		}
	});
}

function topLinksRoutines()
{
	$('a.a-remove').each(
		function() {
			var t = $( this );
			t.replaceWith( t.text() );
		}
	);
}

function switchSearchTypeRoutines()
{
	var first_val = $('input[name=st]:checked').val();
	for( y = 0; y < search_types.length; y++ ) {
		$('#search-type-' + search_types[ y ]).find('input').attr('disabled', !( search_types[ y ] == first_val ));
		$('#search-type-' + search_types[ y ]).find('select').attr('disabled', !( search_types[ y ] == first_val ));
		$('#search-type-' + search_types[ y ]).hide();
	}
	$('#search-type-' + first_val).show();

	$('input[name=st]').click(
		function() {
			var _val_ = $( this ).val();
			for( y = 0; y < search_types.length; y++ ) {
				$('#search-type-' + search_types[ y ]).find('input').attr('disabled', !( search_types[ y ] == _val_ ));
				$('#search-type-' + search_types[ y ]).find('select').attr('disabled', !( search_types[ y ] == _val_ ));
				$('#search-type-' + search_types[ y ]).hide();
			}
			$('#search-type-' + _val_).show();
			fieldRoutines();
		}
	);
}

function searchRoutines()
{
	$('#st-hidden').remove();
	loadSearchType();
	//search_single_type_int_routines();

	$('.search-main-buttons a').each(
		function() {
			var t = $( this ).text();
			$( this ).replaceWith( t );
		}
	);

	$('.search-main-buttons').click(
		function() {
			var _id_ = $( this ).attr('id');
			if( _id_ == 'search-' + search_main_type ) {
				return;
			}
			search_main_type = _id_.replace('search-','');
			switch( search_main_type ) {
				case 'i':
					$('#search-container').removeClass('home-container-mobi');
					$('#ssti-mgenerale input:radio').eq( 0 ).attr('checked', false).parent().removeClass('checked');
					$('#ssti-mtemporale input:radio').eq( 0 ).attr('checked', false).parent().removeClass('checked');

					$('#ssti-igenerale input:radio').eq( 0 ).attr('checked', true).parent().addClass('checked');
					$('#ssti-mgenerale').hide();
					$('#ssti-mtemporale').hide();
					$('#ssti-igenerale').show();
					$('#ssti-iprocedurale').show();

					_val_ = 'igenerale';
					for( y = 0; y < search_types.length; y++ ) {
						$('#search-type-' + search_types[ y ]).find('input').attr('disabled', !( search_types[ y ] == _val_ ));
						$('#search-type-' + search_types[ y ]).find('select').attr('disabled', !( search_types[ y ] == _val_ ));
						$('#search-type-' + search_types[ y ]).hide();
					}
					$('#search-type-' + _val_).show();
					break;
				case 'm':
					$('#search-container').addClass('home-container-mobi');
					$('#ssti-igenerale input:radio').eq( 0 ).attr('checked', false).parent().removeClass('checked');
					$('#ssti-iprocedurale input:radio').eq( 0 ).attr('checked', false).parent().removeClass('checked');

					$('#ssti-mgenerale input:radio').eq( 0 ).attr('checked', true).parent().addClass('checked');
					$('#ssti-igenerale').hide();
					$('#ssti-iprocedurale').hide();
					$('#ssti-mgenerale').show();
					$('#ssti-mtemporale').show();

					_val_ = 'mgenerale';
					for( y = 0; y < search_types.length; y++ ) {
						$('#search-type-' + search_types[ y ]).find('input').attr('disabled', !( search_types[ y ] == _val_ ));
						$('#search-type-' + search_types[ y ]).find('select').attr('disabled', !( search_types[ y ] == _val_ ));
						$('#search-type-' + search_types[ y ]).hide();
					}
					$('#search-type-' + _val_).show();
					break;
			}
			fieldRoutines();
		}
	);
}

var search_form = [], search_types = ['igenerale', 'iprocedurale', 'mgenerale', 'mtemporale'],
type_loaded, type2load;
$( document ).ready(
	function() {
		$('input:radio', '#search-container').uniform();
		searchRoutines();
	}
);

