//<!--
var map;

var to_htmls = [];
var from_htmls = [];
var gmarkers = [];
var startMarker = [];
var bounds = new GLatLngBounds();


function addGMMarker(lat, lng, zoom, name, description, showInfo, setCenter, markerInfoWidth)
{
	var lat = lat;
	var lng = lng;
	var showInfo = showInfo;
	var setCenter = setCenter;
	var name = name;
	var markerInfoWidth = markerInfoWidth;
	if (name=='') return false;
	
	var description = ( (description && description!='') ? '<br><br>'+description : '');
	var zoom = ( zoom ? eval(zoom) : 15);
	var icon = new GIcon();
	var i = gmarkers.length;
	
	startMarker['lat'] = lat;
	startMarker['lng'] = lng;
	startMarker['zoom'] = zoom;
	startMarker['name'] = name;
	startMarker['description'] = description;
	startMarker['showInfo'] = showInfo;
	startMarker['setCenter'] = setCenter;
	startMarker['markerInfoWidth'] = markerInfoWidth;
	
	//icon.image = '/images/company.png';
	//icon.image = 'http://maps.google.com/mapfiles/kml/pal3/icon56.png';
	//icon.iconSize = new GSize(32, 32);
	icon.image = '/images/map-home.png';
	icon.iconSize = new GSize(32, 37);
	icon.iconAnchor = new GPoint(16, 32);
	//icon.shadow = "/images/shadow.png";
	icon.shadow = "/images/map-shadow.png";
	icon.infoWindowAnchor = new GPoint(15,12);
	
	// The info window version with the "to here" form open
    to_htmls[i] = '<br>Dojazd: <b>Do tego miejsca<\/b> - <a href="javascript:fromhere(' + i + ')">Z tego miejsca<\/a>' +
       '<br>Początek trasy:<form action="javascript:getDirections()">' +
       '<input type="text" style="width:'+(startMarker['markerInfoWidth']-5)+'px" MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
       '<INPUT value="Pokaż trasę" TYPE="SUBMIT"><br>' +
       'Spacer <input type="checkbox" name="walk" id="walk" /> &nbsp; Unikaj autostrad <input type="checkbox" name="highways" id="highways" />' +
       '<input type="hidden" id="daddr" value="'+name+"@"+ lat + ',' + lng + 
       '"/>';
    // The info window version with the "from here" form open
    from_htmls[i] = '<br>Dojazd: <a href="javascript:tohere(' + i + ')">Do tego miejsca<\/a> - <b>Z tego miejsca<\/b>' +
       '<br>Koniec trasy:<form action="javascript:getDirections()">' +
       '<input type="text" style="width:'+(startMarker['markerInfoWidth']-5)+'px" MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
       '<INPUT value="Pokaż trasę" TYPE="SUBMIT"><br>' +
       'Spacer <input type="checkbox" name="walk" id="walk" /> &nbsp; Unikaj autostrad <input type="checkbox" name="highways" id="highways" />' +
       '<input type="hidden" id="saddr" value="'+name+"@"+ lat + ',' + lng +
       '"/>';
    // The inactive version of the direction info
    html = '<br><br>Dojazd: <a href="javascript:tohere('+i+')">Do tego miejsca<\/a> - <a href="javascript:fromhere('+i+')">Z tego miejsca<\/a>';
	
	var marker	=	new GMarker(new GLatLng(lat,lng),{title: name, icon: icon});
	marker.txt	=	'<div style="width:'+markerInfoWidth+'px"><strong>'+name+'</strong>'+description+html+'</div>';
	bounds.extend(marker.getPoint());
	map.addOverlay(marker);
	
	if (setCenter==true) map.setCenter(new GLatLng(lat, lng), zoom);
	if (showInfo==true) marker.openInfoWindowHtml(marker.txt);
	
	GEvent.addListener(marker,"click",function()
	{
		marker.openInfoWindowHtml(marker.txt);
		directoryVisible(0);
	});
	gmarkers.push(marker);
	
	return marker;
}

var added = 0;

function createMarker(point, name, index, description, homeMarker) {

	var baseIcon = new GIcon();
	var description = (description==undefined ? '' : '<br><br>'+description);
    baseIcon.iconSize=new GSize(32,37);
    baseIcon.shadowSize=new GSize(0,0);
    baseIcon.iconAnchor=new GPoint(16,32);
    baseIcon.infoWindowAnchor=new GPoint(15,12);

    var letteredIcon = new GIcon(baseIcon);
    //var placesIcon = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal4/icon9.png", null, "http://maps.google.com/mapfiles/kml/pal4/icon9.png");
    if (homeMarker==true) var placesIcon = new GIcon(baseIcon, "/images/map-home.png", null, "/images/map-shadow.png"); 
    else var placesIcon = new GIcon(baseIcon, "/images/map-branches.png", null, "/images/map-shadow.png");
    markerOptions = { icon:placesIcon, title:name };
    
    var i = gmarkers.length;
    
    // The info window version with the "to here" form open
    to_htmls[i] = '<br>Dojazd: <b>Do tego miejsca<\/b> - <a href="javascript:fromhere(' + i + ')">Z tego miejsca<\/a>' +
       '<br>Początek trasy:<form action="javascript:getDirections()">' +
       '<input type="text" style="width:'+(startMarker['markerInfoWidth']-5)+'px" MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
       '<INPUT value="Pokaż trasę" TYPE="SUBMIT"><br>' +
       'Spacer <input type="checkbox" name="walk" id="walk" /> &nbsp; Unikaj autostrad <input type="checkbox" name="highways" id="highways" />' +
       '<input type="hidden" id="daddr" value="'+name+"@"+ point.lat() + ',' + point.lng() + 
       '"/>';
    // The info window version with the "from here" form open
    from_htmls[i] = '<br>Dojazd: <a href="javascript:tohere(' + i + ')">Do tego miejsca<\/a> - <b>Z tego miejsca<\/b>' +
       '<br>Koniec trasy:<form action="javascript:getDirections()">' +
       '<input type="text" style="width:'+(startMarker['markerInfoWidth']-5)+'px" MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
       '<INPUT value="Pokaż trasę" TYPE="SUBMIT"><br>' +
       'Spacer <input type="checkbox" name="walk" id="walk" /> &nbsp; Unikaj autostrad <input type="checkbox" name="highways" id="highways" />' +
       '<input type="hidden" id="saddr" value="'+name+"@"+ point.lat() + ',' + point.lng() +
       '"/>';
    // The inactive version of the direction info
    html = '<br>Dojazd: <a href="javascript:tohere('+i+')">Do tego miejsca<\/a> - <a href="javascript:fromhere('+i+')">Z tego miejsca<\/a>';

    var marker = new GMarker(point, markerOptions);
    marker.txt	=	'<div style="width:'+startMarker['markerInfoWidth']+'px"><strong>'+name+'</strong>'+description+'<br>'+html+'</div>';
    bounds.extend(marker.getPoint());
    map.addOverlay(marker);
    
    GEvent.addListener(marker, "click", function() {
    	marker.openInfoWindowHtml(marker.txt);
    	directoryVisible(0);
    });
    gmarkers.push(marker);
    
    return marker;
}

function changeZoom(zoom) {
	map.setCenter(new GLatLng(52.2319, 19.01), zoom);
	map.closeInfoWindow();
}
function setMapZoom(zoom) {
	//var center = map.getCenter();
	//alert(center)
	map.setCenter(map.getCenter(), zoom);
}
function showAllBounds() {
	map.setZoom(map.getBoundsZoomLevel(bounds));
    map.setCenter(bounds.getCenter());
}

var gdir;
// ===== request the directions =====
function getDirections() {
  // ==== Set up the walk and avoid highways options ====
  var opts = {};
  if (document.getElementById("walk").checked) {
     opts.travelMode = G_TRAVEL_MODE_WALKING;
  }
  if (document.getElementById("highways").checked) {
     opts.avoidHighways = true;
  }
  // ==== set the start and end locations ====
  var saddr = document.getElementById("saddr").value
  var daddr = document.getElementById("daddr").value
  gdir.load("from: "+saddr+" to: "+daddr, opts);
  
  directoryVisible(1);
  map.closeInfoWindow();
}

//functions that open the directions forms
function tohere(i) {
  gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
function fromhere(i) {
  gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}
function directoryVisible(x) {
	document.getElementById("PathDirections").style.display = (x==1 ? 'block' : 'none');
}

function clearPoints() {
	to_htmls = [];
	from_htmls = [];
	gmarkers = [];
	map.clearOverlays();
	
	addGMMarker(startMarker['lat'], startMarker['lng'], startMarker['zoom'], startMarker['name'], startMarker['description'], startMarker['showInfo'], startMarker['setCenter'], startMarker['markerInfoWidth'])
	directoryVisible(0);
	added = 0;
}



//-->
