// directions.js: Copyright Remon Versteeg
// http://remonversteeg.nl
var  $$ = jQuery.noConflict();
$$(window).load(function(){
        // Google map
        if ($$('#gmap').length > 0)
           return initialize();
		   // return loadMap();
    });


var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var infowindow;

function initialize() {
	var coords = $$('#gmap').attr("title").split(",");
	var point = new google.maps.LatLng(parseFloat(coords[0]), parseFloat(coords[1]));
	
	directionsDisplay = new google.maps.DirectionsRenderer();
	var myOptions = {
	zoom:15,

	scaleControl: true,

	mapTypeControl: true,
	mapTypeControlOptions: {
	  style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
	},
	navigationControl: true,
    navigationControlOptions: {
        style: google.maps.NavigationControlStyle.ZOOM_PAN,
        position: google.maps.ControlPosition.TOP_LEFT
    },
	
	mapTypeId: google.maps.MapTypeId.ROADMAP,
	center: point
	}
	map = new google.maps.Map(document.getElementById("gmap"), myOptions);
	//icons
	
	$$.ajax({
		url: window.location.protocol + "//" + window.location.host+"/modules/route/js/adres_loader.php",
		success: function(data) {
			infowindow = new google.maps.InfoWindow({
				content: data
			});
		}
	});

	var sitename = $$('#routename').val();
	var image = window.location.protocol + "//" + window.location.host+"/includes/images/markers/company.png";
    var marker = new google.maps.Marker({
        position: point,
		icon: image,
        map: map,
        title: sitename
    });

	//homebutton
	var homeControlDiv = document.createElement('DIV');
	var homeControl = new HomeControl(homeControlDiv, map);
	
	homeControlDiv.index = 1;
	map.controls[google.maps.ControlPosition.TOP_RIGHT].push(homeControlDiv);
	
	//directions
	directionsDisplay.setMap(map);
	directionsDisplay.setPanel(document.getElementById("gdirections"));
	
	google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map,marker);
    });

}

function calcRoute() {
	var start = $$('#routefrom').val();
	var end = $$('#routeto').val();
	var request = {
	origin:start, 
	destination:end,
	travelMode: google.maps.DirectionsTravelMode.DRIVING
	};
	directionsService.route(request, function(response, status) {
	if (status == google.maps.DirectionsStatus.OK) {
	  directionsDisplay.setDirections(response);
	}
  });
}
$$(document).ready(function(){
	//$$()							
});

function HomeControl(controlDiv, map) {

  var coords = $$('#gmap').attr("title").split(",");
  var point = new google.maps.LatLng(parseFloat(coords[0]), parseFloat(coords[1]));
  controlDiv.style.padding = '5px';

  // Set CSS for the control border
  var controlUI = document.createElement('DIV');
  controlUI.style.backgroundColor = 'white';
  controlUI.style.borderStyle = 'solid';
  controlUI.style.borderWidth = '2px';
  controlUI.style.cursor = 'pointer';
  controlUI.style.textAlign = 'center';
  controlUI.title = 'Click to set the map to Home';
  controlDiv.appendChild(controlUI);

  // Set CSS for the control interior
  var controlText = document.createElement('DIV');
  controlText.style.fontFamily = 'Arial,sans-serif';
  controlText.style.fontSize = '12px';
  controlText.style.paddingLeft = '4px';
  controlText.style.paddingRight = '4px';
  controlText.innerHTML = '<b>Home</b>';
  controlUI.appendChild(controlText);

  // Setup the click event listeners: simply set the map to
  // Chicago
  google.maps.event.addDomListener(controlUI, 'click', function() {
    map.setCenter(point)
  });

}


