var map;
var adUnit;
var directionDisplay;
var directionsService;
var stepDisplay;
var markerArray = [];

function initialize() {
  //alert('started');
  // Instantiate a directions service.
  directionsService = new google.maps.DirectionsService();

  // Create a map and center it on Manhattan.
  var manhattan = new google.maps.LatLng(40.746256,17.424116);
  var myOptions = {
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: manhattan
  }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
 

  // Create a renderer for directions and bind it to the map.
  var rendererOptions = {
    map: map
  }
  directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions)

  // Instantiate an info window to hold step text.
  stepDisplay = new google.maps.InfoWindow();
  
  
  calcRoute();
 
}

function calcRoute() {
  //alert("works");
  // First, clear out any existing markerArray
  // from previous calculations.
  for (i = 0; i < markerArray.length; i++) {
    markerArray[i].setMap(null);
  }

  // Retrieve the start and end locations and create
  // a DirectionsRequest using DRIVING directions.
  
  var start = document.getElementById("start").value;
  
  //var end = document.getElementById("end").value;
  var end = "Cisternino BR";
  var request = {
      origin: start,
      destination: end,
      travelMode: google.maps.TravelMode.DRIVING
  };

  directionsService.route(request, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      addAd();
      directionsDisplay.setDirections(result);
      
    }
  });
}

function addAd() {

//Place Advert
  var adUnitDiv = document.createElement('div');
  
  var adUnitOptions = {
    format: google.maps.adsense.AdFormat.VERTICAL_BANNER,
    position: google.maps.ControlPosition.LEFT,
    map: map,
    visible: true,
    publisherId: 'pub-0384300060170963'
  }
  adUnit = new google.maps.adsense.AdUnit(adUnitDiv, adUnitOptions);
  


}


//FACEBOOK POSTS
function streamPublish(name, description, hrefTitle, hrefLink, userPrompt){
                FB.ui({ method : 'feed',
                        message: userPrompt,
                        link   :  hrefLink,
                        caption:  hrefTitle,
                        picture: 'https://fbcdn-photos-a.akamaihd.net/photos-ak-snc1/v27562/209/134268223284173/app_1_134268223284173_1883.gif'
               });
               //http://developers.facebook.com/docs/reference/dialogs/feed/
 
}
function publishStream(){
	streamPublish("Cisternino in Fuga 4.0!", 'Individua i cistranesi in fuga intorno a te per incontrali o... evitarli!', 'Sto usando Cisternino in Fuga 4.0!', 'http://www.cisterninoinfuga.it', "Che idea originale!");
	alert("ok");
} 
