var est0 = est0 || [],
    est1 = est0 || [];

$(document).ready(function(){
	$.each ($('.s_estacion:eq(0) optgroup'), function (i, val) { est0[i] = $(this).html(); });
	$.each ($('.s_estacion:eq(1) optgroup'), function (i, val) { est1[i] = $(this).html(); });
	
	//
		$('.s_linea:eq(0)').attr('name','origen_linea');
		$('.s_linea:eq(0) option[value='+(typeof origen_linea == 'undefined' ? '':origen_linea)+']').attr('selected','selected');
		$('.s_estacion:eq(0) option[value='+(typeof origen == 'undefined' ? '':origen)+']').attr('selected','selected');
		$('.s_estacion:eq(0)').attr('name','origen');   
	//
	
	$('.s_estacion:eq(0) option:eq(0)').html('Estaci&oacute;n de Origen');
	$('.s_linea:eq(0)').change(function () {
	  v = $(this).val();
	  $('.s_estacion:eq(0)').html('');
	  $('.s_estacion:eq(0) optgroup').remove();
	  $('.s_estacion:eq(0)').append('<optgroup>'+est0[v]+'</optgroup>');
	  if (v > 0) { $('.s_estacion:eq(0) option[value='+(typeof origen == 'undefined' ? '':origen)+']').attr('selected','selected'); origen = '';}
	}).change();
	
	//
		$('.s_linea:eq(1)').attr('name','destino_linea');
        
        if (conf.jqueryVersionMayor >= 1 && conf.jqueryVersionMinor >= 6) {
            $('.s_linea:eq(1) option[value='+(typeof destino_linea == 'undefined' ? '':destino_linea)+']').prop('selected', true);
            $('.s_estacion:eq(1) option[value='+(typeof destino == 'undefined' ? '':destino)+']').attr('selected', true);
        } else {
            $('.s_linea:eq(1) option[value='+(typeof destino_linea == 'undefined' ? '':destino_linea)+']').attr('selected','selected');
            $('.s_estacion:eq(1) option[value='+(typeof destino == 'undefined' ? '':destino)+']').attr('selected','selected');
        }
        $('.s_estacion:eq(1)').attr('name','destino');
	//
	
	$('.s_estacion:eq(1) option:eq(0)').html('Estaci&oacute;n de Destino');
	$('.s_linea:eq(1)').change(function () {
        v = $(this).val();
        $('.s_estacion:eq(1)').html('');
        $('.s_estacion:eq(1) optgroup').remove();
        $('.s_estacion:eq(1)').append('<optgroup>'+est1[v]+'</optgroup>');
        
        if (v > 0) { 
            if (conf.jqueryVersionMayor >= 1 && conf.jqueryVersionMinor >= 6) {
                $('.s_estacion:eq(1) option[value='+(typeof destino == 'undefined' ? '':destino)+']').prop('selected', true);
            } else {
                $('.s_estacion:eq(1) option[value='+(typeof destino == 'undefined' ? '':destino)+']').attr('selected','selected');
            }
            destino = '';
        }
	}).change();
	
	$('#ruta').submit(function () {
        $('#ruta').attr('action', 'planificador/'+$('.s_estacion:eq(0)').val()+'/'+$('.s_estacion:eq(1)').val());
	});
    
});

