var queryString;
var ajax_case;
var pager_result_per_page;
var last_record;


$('#form_submit').click(function(){
	last_record = 0;
	queryString = $('#searchForm').formSerialize();
	wowAjaxRequest(queryString);
	return false;
});


function wowInit() {
	ajax_case = $('#ajax_case').val();
	pager_result_per_page = $('#pager_result_per_page').val();
	last_record = $('#last_record').val();
	queryString = $('#searchForm').formSerialize();
	wowAjaxRequest(queryString);
}


function wowAjaxRequest(queryString) {
	atlDisplayLoading('#paline_list');

	$.ajax({
		type:'POST',
		url:'index.php?page=ajax_requests',
		data:'ajax_case='+ajax_case+'&pager_result_per_page='+pager_result_per_page+'&last_record='+last_record+'&'+queryString,
		success:function(response){
			$('#results_list').html(response);
		}
	});
}


function atlDisplayLoading(divId) {
	$(divId).html('<img src="include/loading.gif" />');
}






function atlGetBusstopAddress(busstop_id) {
	var result = false;
	
	$.ajax({
		async:false,
		type:'POST',
		url:'index.php?page=ajax_requests',
		data:'ajax_case=atl_retrive_busstop_address&busstop_id='+busstop_id,
		success:function(response){
			result = response;
		}
	});
	
	return result;
}


function atlAjaxRetriveLinee() {
	$('#linee_list').show();
	atlDisplayLoading('#linee_list');

	var departure_busstop_id = $('#departure_busstop').val();
	var arrival_busstop_id = $('#arrival_busstop').val();

	$.ajax({
		type:'POST',
		url:'index.php?page=ajax_requests',
		data:'ajax_case=atl_retrive_linee&departure_busstop_id='+departure_busstop_id+'&arrival_busstop_id='+arrival_busstop_id,
		success:function(response){
			//atlDisplayResponse(response,secondPalinaName);
			
			$('#linee_list').html(response);
		}
	});
}

function ajaxRetriveRadiosStatus() {
	var result = 1;
	
	$.ajax({
		async:false,
		type:'POST',
		url:'index.php?page=ajax_requests',
		data:'ajax_case=atl_retrive_radios_status',
		success:function(response){
			result = response;
		}
	});
	
	return result;
}

function atlDisplayRadios() {
	
	var radiosStatus = ajaxRetriveRadiosStatus();
	
	var orari_radios_html = '';
	if (radiosStatus == 1 || radiosStatus == 2) orari_radios_html += '<input type="radio" class="orari_radios_festivo" name="festivo" value="0" checked="checked" /> feriale';
	else orari_radios_html += '<input type="radio" class="orari_radios_festivo" name="festivo" value="0" /> feriale';
	orari_radios_html += '<br />';
	if (radiosStatus == 3 || radiosStatus == 4) orari_radios_html += '<input type="radio" class="orari_radios_festivo" name="festivo" value="1" checked="checked" /> festivo';
	else orari_radios_html += '<input type="radio" class="orari_radios_festivo" name="festivo" value="1" /> festivo';
	orari_radios_html += '<br /><br />';
	if (radiosStatus == 1 || radiosStatus == 3) orari_radios_html += '<input type="radio" class="orari_radios_stagione" name="stagione" value="i" checked="checked" /> invernale';
	else orari_radios_html += '<input type="radio" class="orari_radios_stagione" name="stagione" value="i" /> invernale';
	orari_radios_html += '<br />';
	if (radiosStatus == 2 || radiosStatus == 4) orari_radios_html += '<input type="radio" class="orari_radios_stagione" name="stagione" value="e" checked="checked" /> estivo';
	else orari_radios_html += '<input type="radio" class="orari_radios_stagione" name="stagione" value="e" /> estivo';
	
	$('#orari_radios').html(orari_radios_html);
}

$('.linea_li').livequery('click',function(){
	var linea_id = $(this).attr('id');
	$('#selected_linea_id').val(linea_id);
	
	$('#orari_radios').empty();
	$('#linea_notes').empty();
	
	atlDisplayRadios();
		
	atlAjaxRetriveOrari();
	atlAjaxRetriveNotes();
});

$('.orari_radios_festivo:not(:checked), .orari_radios_stagione:not(:checked)').livequery('click',function(){
	atlAjaxRetriveOrari();
	atlAjaxRetriveNotes()
});

function atlAjaxRetriveOrari() {
	$('#orari_radios').show();
	$('#orari_list').show();
	atlDisplayLoading('#orari_list');
	
	var linea_id = $('#selected_linea_id').val();
	var departure_busstop_id = $('#departure_busstop').val();

	var festivo = $('.orari_radios_festivo:checked').val();
	var stagione = $('.orari_radios_stagione:checked').val();
	
	$.ajax({
		type:'POST',
		url:'index.php?page=ajax_requests',
		data:'ajax_case=atl_retrive_orari&linea_id='+linea_id+'&departure_busstop_id='+departure_busstop_id+'&festivo='+festivo+'&stagione='+stagione,
		success:function(response){
			$('#orari_list').html(response);
		}
	});
}


function atlAjaxRetriveNotes() {
	$('#linea_notes').show();
		
	var linea_id = $('#selected_linea_id').val();

	$.ajax({
		type:'POST',
		url:'index.php?page=ajax_requests',
		data:'ajax_case=atl_retrive_notes&linea_id='+linea_id,
		success:function(response){
			$('#linea_notes').html(response);
		}
	});
}


function atlAjaxRetriveRelated() {
	$('#related_list').show();
	
	var departure_busstop_id = $('#departure_busstop').val();
	var arrival_busstop_id = $('#arrival_busstop').val();

	$.ajax({
		type:'POST',
		url:'index.php?page=ajax_requests',
		data:'ajax_case=atl_retrive_related&departure_busstop_id='+departure_busstop_id+'&arrival_busstop_id='+arrival_busstop_id,
		success:function(response){
			//atlDisplayResponse(response,secondPalinaName);
			
			$('#related_list').html(response);
		}
	});
}






function relatedLinksAutomation(palina_id) {
	$('#primary_select').children('option').each(function(i){
		var currentPalinaId = $(this).val();
		if (currentPalinaId == palina_id) {
			$(this).attr('selected','selected');
		}
	})

	atlPrimarySelectRetriveLinee(palina_id);
}












/*
function atlDisplayResponse(response,secondPalinaName) {
	var selected_palina = $('#primary_select option:selected').text();
	if (secondPalinaName=='' || secondPalinaName=='- seleziona fermata di arrivo -') $('#paline_list').html('<p>Elenco delle linee che passano dalla fermata "' + selected_palina + '":</p>' + response);
	else $('#paline_list').html('<p>Elenco delle linee che passano dalla fermata "' + selected_palina + '" e dalla fermata "' + secondPalinaName + '":</p>' + response);
}

function atlPrimarySelectRetriveLinee(palinaId) {
	if (palinaId != '') {
		$.ajax({
			async:false,
			type:'POST',
			url:'index.php?page=ajax_requests',
			data:'ajax_case=atl_prepare_secondary_select_manipulation&palina_id='+palinaId,
			dataType:'json',
			success:function(response){
				atlRemovePalinaIds(response);
			}
		});

		atlRestoreSecondarySelect();
		atlRemoveSecondarySelectPalinaId(palinaId);
		atlHandleSecondarySelectDisplay();
		atlAjaxRetriveLinee(false,palinaId,'','');

	} else {
		$('#secondary_select_span select').remove();
		$('#paline_list').empty();
	}
}

function atlHandleSecondarySelectDisplay() {
	var secondarySelectLength = $('#secondary_select option').length;
	if (secondarySelectLength == 1) $('#secondary_select').hide();
	else if (secondarySelectLength == 2) $('#secondary_select option:first').remove();
}

function atlSecondarySelectRetriveLinee(palinaId) {
	var firstPalinaId = $('#primary_select option:selected').val();
	var secondPalinaId = palinaId;
	var secondPalinaName = $('#secondary_select option:selected').text();

	atlAjaxRetriveLinee(true,firstPalinaId,secondPalinaId,secondPalinaName);
}

// override
function atlAjaxRetriveLinee(loading,firstPalinaId,secondPalinaId,secondPalinaName) {
	if (loading) atlDisplayLoading('#paline_list');

	$.ajax({
		type:'POST',
		url:'index.php?page=ajax_requests',
		data:'ajax_case=atl_retrive_linee&first_palina_id='+firstPalinaId+'&second_palina_id='+secondPalinaId,
		success:function(response){
			atlDisplayResponse(response,secondPalinaName);
		}
	});
}

function atlRestoreSecondarySelect() {
	atlDisplayLoading('#paline_list');

	$.ajax({
		async:false,
		type:'POST',
		url:'index.php?page=ajax_requests',
		data:'ajax_case=atl_restore_secondary_select',
		success:function(response){
			$('#secondary_select_span').html(response);
		}
	});
}

function atlRemoveSecondarySelectPalinaId(palinaId) {
	$('#secondary_select').children('option').each(function(i){
		var currentPalinaId = $(this).val();
		if (currentPalinaId == palinaId) $(this).remove();
	})
}

function atlRemovePalinaIds(idsArrayToPreserve) {
	$('#secondary_select').children('option').each(function(i){
		var currentPalinaId = $(this).val();
		if (jQuery.inArray(currentPalinaId,idsArrayToPreserve) == -1 && currentPalinaId != '') $(this).remove();
	})
}
*/
