function TranslateJStext(name) {
	return JStext[name] || name;
}


var JStext = new Array();
JStext['stadtGemeinde'] = 'Stadt/Gemeinde';
JStext['haltestellePoiStrasse'] = 'Haltestelle/Adresse/POI';

$(function() {

	handler = (ajaxhandler ? ajaxhandler : "");

    if($('#departure_city').length>0) {
        txt_city_dep = new Autocomplete(
        {
            textfield: $('#departure_city'),
            gkz: $('#departure_city_gkz'),
            include_delfi: $('#include_delfi'),
            label: TranslateJStext('stadtGemeinde'),
            mode: 'city',
            resetValues: [$('#departure_station_type'), $('#departure_station_id')]
        }
        , acStaticItems, masterLink+'script=hssc/ajax', function(){},function(){}, handler);

        txt_station_dep = new Autocomplete(
        {
            textfield: $('#departure_station'),
            type: $('#departure_station_type'),
            station_id: $('#departure_station_id'),
            label: TranslateJStext('haltestellePoiStrasse'),
            mode: 'stop',
            types: 'stop,address,poi'
        }, [], masterLink+'script=hssc/ajax', appendCityParams('departure'),function(){}, handler);

        txt_city_dep.reinit();
        txt_station_dep.reinit();
    }

    if($('#arrival_city').length>0) {
        txt_city_arr = new Autocomplete(
        {
            textfield: $('#arrival_city'),
            gkz: $('#arrival_city_gkz'),
            include_delfi: $('#include_delfi'),
            label: TranslateJStext('stadtGemeinde'),
            mode: 'city',
            resetValues: [$('#arrival_station_type'), $('#arrival_station_id')]
        }, acStaticItems, masterLink+'script=hssc/ajax',function(){},function(){}, handler);

        txt_station_arr = new Autocomplete(
        {
            textfield: $('#arrival_station'),
            type: $('#arrival_station_type'),
            station_id: $('#arrival_station_id'),
            label: TranslateJStext('haltestellePoiStrasse'),
            mode: 'stop',
            types: 'stop,address,poi'
        }, [], masterLink+'script=hssc/ajax', appendCityParams('arrival'),function(){}, handler);

        txt_city_arr.reinit();
        txt_station_arr.reinit();
    }

    if($('#via_city').length>0) {
        txt_city_via = new Autocomplete(
        {
            textfield: $('#via_city'),
            gkz: $('#via_city_gkz'),
            include_delfi: $('#include_delfi'),
            label: TranslateJStext('stadtGemeinde'),
            mode: 'city',
            resetValues: [$('#via_station_type'), $('#via_station_id')]
        }
        , acStaticItems, masterLink+'script=hssc/ajax',function(){},function(){}, handler);

        txt_station_via = new Autocomplete(
        {
            textfield: $('#via_station'),
            type: $('#via_station_type'),
            station_id: $('#via_station_id'),
            label: TranslateJStext('haltestellePoiStrasse'),
            mode: 'stop',
            types: 'stop,address,poi'
        }, [], masterLink+'script=hssc/ajax', appendCityParams('via'),function(){}, handler);

        txt_city_via.reinit();
        txt_station_via.reinit();
    }

    

    

    $('.ac_check').bind('submit', function() {
        for(var i in Autocomplete.INSTANCES)
            Autocomplete.INSTANCES[i].beforeSubmitCallback();
    });

});

function appendCityParams(type) {
    return function() {
        var p = {};
        p.city_text = $('#'+type+'_city').attr('value');
        p.city_gkz = $('#'+type+'_city_gkz').attr('value');

        var f,s;

        f = p.city_gkz.substr(0, 1);
        s = p.city_gkz.substr(0, 2);

        if (!p.city_gkz || (f != '5' && s != '26' && s != '27') )
            throw {};

        return p;
    };
}
