var directionDisplay;
var directionsService;
var map;
var pokeLatlng;

function getDirections(startFrom,mode) {
    if (mode === 'WALKING') {
        modeOfTravel = google.maps.DirectionsTravelMode.WALKING;
    } else {
        modeOfTravel = google.maps.DirectionsTravelMode.DRIVING;
    }
    var request = {
        origin: startFrom,
        destination: pokeLatlng,
        travelMode: modeOfTravel,
		region: "ch"
    };
    directionsService.route(request, function(result, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            //$('#transport').toggle();
            //$('#directions').toggle();
            directionsDisplay.setDirections(result);
        } else {
            alert('Not a valid address');
        }
    });
}

function doTheMap() {
	directionsService = new google.maps.DirectionsService();
    directionsDisplay = new google.maps.DirectionsRenderer();
    pokeLatlng = new google.maps.LatLng(47.3813988, 8.5171469);
    var myOptions = {
        center: pokeLatlng,
        disableDefaultUI: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        zoom: 15,
		styles: [ { saturation: -100 } ]		
    };
    map = new google.maps.Map(document.getElementById('map'),myOptions);
    
	var mapStyles = [ { stylers: [ { gamma: 0.81 }, { hue: "#0099ff" }, { visibility: "on" }, { saturation: -83 } ] } ];
	var styledMapType = new google.maps.StyledMapType(mapStyles, {name: "CrafftMap"});

	map.mapTypes.set('CrafftMap', styledMapType);
	map.setMapTypeId('CrafftMap');
  
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById('directions'));
	
	// Origins, anchor positions and coordinates
     var markerImage = new google.maps.MarkerImage('/contact/images/marker.png',
        new google.maps.Size(85,96),
        new google.maps.Point(0,0),
        new google.maps.Point(44,86)); 
    
    var marker = new google.maps.Marker({
        icon: markerImage,
        map: map, 
        position: pokeLatlng,
        title:"Crafft!"
    });
	
    $('.tram p a, .train p a').bind('click',function(e){
        var startFrom = $(this).data('start');
        getDirections(startFrom,'WALKING');
        e.preventDefault();
    });

    $('#btnGetDirections').bind('click',function(e){
        var startFrom = $("#getdirections");
        if ($(startFrom).val().length > 0) {
            getDirections(startFrom.val());
        } else {
            $(startFrom).val('Enter a start address');
        };
        e.preventDefault();
    });
    
    $('#directions .back').live('click',function(e){
        $('#transport').toggle();
        $('#directions').toggle();
        e.preventDefault();
    });
    
}
/*
function telHref() {
    var telNos = $('.banner .tel');
    var thisNo = $(telNos).text();
    $(telNos).removeClass('tel').html('<a href="tel:'+thisNo+'">'+thisNo+'</a>');
}

$(document).ready(function(){
    var screenWidth = screen.width;
    if(screenWidth >= 768) {
        doTheMap();
    } else {
        telHref();
    }
});
*/
