
var zusatz1 = '';
var zusatz2 = '';
var zusatz3 = '';

var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var from;
function getSelectedText(elementId) {
    var elt = document.getElementById(elementId);

    if (elt.selectedIndex == -1)
	return null;

    return elt.options[elt.selectedIndex].text;
}
function getZip(elementId) {
    var elt = document.getElementById(elementId);

    if (elt.selectedIndex == -1)
	return null;

    var blabla = elt.options[elt.selectedIndex].text.split(" ");
    return blabla[0];
}
$("#map_container").dialog({
    autoOpen: false,
    show: {effect: 'fade', duration: 700},
    hide: {effect: 'fade', duration: 700},
    width: 'auto',
    height: 'auto',
    modal: true,
    draggable: false,
    resizable: false,
    dialogClass: 'dialog_fixed',
    resizeStop: function (event, ui) {
	google.maps.event.trigger(map, 'resize')
    },
    open: function (event, ui) {
	google.maps.event.trigger(map, 'resize');
    }

});



$(".ui-dialog-titlebar").hide();

$(document).on('click', '#mapview', function () {
    $("#map_container").dialog("open");
    directionsDisplay.setMap(map);
    return false;
});
$(document).on('click', '#mapview2', function () {
    $("#map_container").dialog("open");
    directionsDisplay.setMap(map);
    return false;
});

$('#closedialog').click(function () {
    $("#map_container").dialog('close');
});

function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    var vienna = new google.maps.LatLng(48.208174, 16.373819);
    var mapOptions = {
	zoom: 16,
	center: vienna
    }
    map = new google.maps.Map(document.getElementById('mapa'), mapOptions);
    directionsDisplay.setMap(map);

}

function calcRoute() {




    var end = from;
    var start = 'Flughafen Wien, Schwechat';




    if (zusatz1 === 'none' || zusatz1 === '') {
	zusatzX1 = '';
    } else {
	zusatzX1 = zusatz1;
    }
    if (zusatz2 === 'none' || zusatz2 === '') {
	zusatzX2 = '';
    } else {
	zusatzX2 = zusatz2;
    }
    if (zusatz3 === 'none' || zusatz3 === '') {
	zusatzX3 = '';
    } else {
	zusatzX3 = zusatz3;
    }
    // document.getElementById('aaa1').innerHTML = zusatzX1;
    //  document.getElementById('aaa2').innerHTML = zusatzX2;
    // document.getElementById('aaa3').innerHTML = zusatzX3;

    var checkboxArray = [zusatzX1, zusatzX2, zusatzX3];



    var waypts = [];
    for (var i = 0; i < checkboxArray.length; i++) {
	var adresanesto = checkboxArray[i];
	if (adresanesto !== "") {

	    waypts.push({
		location: adresanesto,
		stopover: true});
	}
    }




    var request = {
	origin: start,
	destination: end,
	waypoints: waypts,
	optimizeWaypoints: false,
	travelMode: google.maps.TravelMode.DRIVING};
    // document.getElementById('aaa4').innerHTML = waypts;

    var totaldistance = 0;
    var totalduration = 0;

    directionsService.route(request, function (response, status) {
	if (status == google.maps.DirectionsStatus.OK) {
	    directionsDisplay.setDirections(response);
	    var route = response.routes[0];

	    // For each route, display summary information.
	    for (var i = 0; i < route.legs.length; i++) {
		totaldistance = totaldistance + route.legs[i].distance.value;
		totalduration = totalduration + route.legs[i].duration.value;
	    }
	    var seconds = totalduration;


	    var date = new Date(seconds * 1000);
	    var hh = date.getUTCHours();
	    var mm = date.getUTCMinutes();
	    var ss = date.getSeconds();
	    if (hh < 10) {
		hh = "0" + hh;
	    }
	    if (mm < 10) {
		mm = "0" + mm;
	    }
	    if (ss < 10) {
		ss = "0" + ss;
	    }
	    var mins = Math.floor(seconds / 60);



	    document.getElementById('km').innerHTML = (totaldistance / 1000).toFixed(1);
	    document.getElementById('min').innerHTML = mins;
	    // document.getElementById('km2').innerHTML = (totaldistance / 1000).toFixed(1);
	    // document.getElementById('min2').innerHTML = mins;

//			   document.querySelector('#route_distance').innerHTML = (totaldistance/1000).toFixed(1)+" Km lang ";
//	   document.querySelector('#route_time').innerHTML = "Dauer ca. "+hh+" Stunden, und "+mm+" Minuten";
	    /*document.F1.km.value = (totaldistance/1000).toFixed(1);
	     Die Route ist Km lang und in Dauer von ca. Stunden und Minuten
	     /*document.F1.price.value = '156';*/
	}
    });
}
google.maps.event.addDomListener(window, 'load', initialize);

$(document).ready(function () {
    $('#city').change(function () {

	from = getSelectedText('street') + ' ' + document.getElementById('housenumber').value + ', ' + getZip('zip') + ' ' + getSelectedText('city');
	calcRoute();
    });
    $('#zip').change(function () {
	from = getSelectedText('street') + ' ' + document.getElementById('housenumber').value + ', ' + getZip('zip') + ' ' + getSelectedText('city');

	calcRoute();
    });
    $('#street').change(function () {

	from = getSelectedText('street') + ' ' + document.getElementById('housenumber').value + ', ' + getZip('zip') + ' ' + getSelectedText('city');
	calcRoute();
    });


    $('#housenumber').keyup(function () {
	from = getSelectedText('street') + ' ' + document.getElementById('housenumber').value + ', ' + getZip('zip') + ' ' + getSelectedText('city');
	calcRoute();
    });

    $('#adresse').change(function () {
	setTimeout(bla, 1000);

    });

    function bla() {

	from = getSelectedText('street') + ' ' + document.getElementById('housenumber').value + ', ' + getZip('zip') + ' ' + getSelectedText('city');
	calcRoute();
    }


    $('#aa1zip').change(function () {
	zz1_zip = $(this).val();
	if (zz1_zip === 'none') {
	    zusatz1 = '';
	} else {
	    zusatz1 = getZip('aa1zip').substring(0, 4) + ' Wien';
	}
	calcRoute();
    });

    $('#aa1street').change(function () {
	zz1_street = $(this).val();
	if (zz1_street === 'none') {
	    zusatz1 = getZip('aa1zip').substring(0, 4) + ' Wien';
	} else {
	    zusatz1 = getSelectedText('aa1street') + ' ' + document.getElementById('aa1housenumber').value + ', ' + getZip('aa1zip').substring(0, 4) + ' Wien';
	}
	calcRoute();
    });


    $('#aa1housenumber').keyup(function () {
	aa1_zip = getZip('aa1zip').substring(1, 4);
	if (aa1_zip !== '') {

	    zusatz1 = getSelectedText('aa1street') + ' ' + document.getElementById('aa1housenumber').value + ', ' + getZip('aa1zip').substring(0, 4) + ' Wien';
	    calcRoute();
	}

    });


    $('#aa2zip').change(function () {
	zz2_zip = $(this).val();
	if (zz2_zip === 'none') {
	    zusatz2 = '';
	} else {
	    zusatz2 = getZip('aa2zip').substring(0, 4) + ' Wien';
	}
	calcRoute();
    });

    $('#aa2street').change(function () {
	zz2_street = $(this).val();
	if (zz2_street === 'none') {
	    zusatz2 = getZip('aa2zip').substring(0, 4) + ' Wien';
	} else {
	    zusatz2 = getSelectedText('aa2street') + ' ' + document.getElementById('aa2housenumber').value + ', ' + getZip('aa2zip').substring(0, 4) + ' Wien';
	}
	calcRoute();
    });


    $('#aa2housenumber').keyup(function () {
	aa2_zip = getZip('aa2zip').substring(1, 4);
	if (aa1_zip !== '') {

	    zusatz2 = getSelectedText('aa2street') + ' ' + document.getElementById('aa2housenumber').value + ', ' + getZip('aa2zip').substring(0, 4) + ' Wien';
	    calcRoute();
	}

    });


    $('#aa3zip').change(function () {
	zz3_zip = $(this).val();
	if (zz3_zip === 'none') {
	    zusatz3 = '';
	} else {
	    zusatz3 = getZip('aa3zip').substring(0, 4) + ' Wien';
	}
	calcRoute();
    });

    $('#aa3street').change(function () {
	zz3_street = $(this).val();
	if (zz3_street === 'none') {
	    zusatz3 = getZip('aa3zip').substring(0, 4) + ' Wien';
	} else {
	    zusatz3 = getSelectedText('aa3street') + ' ' + document.getElementById('aa3housenumber').value + ', ' + getZip('aa3zip').substring(0, 4) + ' Wien';
	}
	calcRoute();
    });


    $('#aa3housenumber').keyup(function () {
	aa3_zip = getZip('aa3zip').substring(1, 4);
	if (aa3_zip !== '') {
	    zusatz3 = getSelectedText('aa3street') + ' ' + document.getElementById('aa3housenumber').value + ', ' + getZip('aa3zip').substring(0, 4) + ' Wien';
	    calcRoute();
	}
    });

    $('#zusatzadresse').change(function () {
	aaddress = $(this).val();
	if (aaddress === '2') {
	    zusatz3 = '';
	    calcRoute();

	} else if (aaddress === '1') {
	    zusatz2 = '';
	    zusatz3 = '';
	    calcRoute();

	} else if (aaddress === '0') {
	    zusatz1 = '';
	    zusatz2 = '';
	    zusatz3 = '';
	    calcRoute();

	}

    });



    /*	function custrout() {
     from = getSelectedText('street')+' '+document.getElementById('housenumber').value+', '+getZip('zip')+' '+getSelectedText('city');
     calcRoute();
     }*/
    /*   $('#id_sc_field_customer_route').change(function() {
     setTimeout(custrout, 3000);
     });*/

    /*$('#id_sc_field_customer_route').change(function () {
     from = getSelectedText('id_sc_field_street')+document.getElementById('id_sc_field_house_number').value+', '+getSelectedText('id_sc_field_zip')+' '+getSelectedText('id_sc_field_city');
     calcRoute();
     });	*/


    /*
     $('#zz1_select').change(function () {
     zz1val = $(this).val();
     if (zz1val === 'none'){
     zusatz1='';
     } else{
     zusatz1 = zz1val;
     }
     calcRoute();
     document.querySelector('#zusatz_right1').innerHTML = zz1val;
     });
     $('#zz2_select').change(function () {
     zz2val = $(this).val();
     if (zz2val === 'none'){
     zusatz2='';
     } else{
     zusatz2 = zz2val;
     }
     calcRoute();

     document.querySelector('#zusatz_right2').innerHTML = zz2val;
     });
     $('#zz3_select').change(function () {
     zz3val = $(this).val();
     if (zz3val === 'none'){
     zusatz3='';
     } else{
     zusatz3 = zz3val;
     }
     calcRoute();

     document.querySelector('#zusatz_right3').innerHTML = zz3val;
     });
     */


    //setTimeout(custrout, 3000);
});






