


var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];
var i = 0;
var fullHtml;

var gMapContainer;

function createMarker(point, markerOptions, name, html) {
	var marker = new GMarker(point, markerOptions);
	to_htmls[i] = html + '<br /><strong>' + 'get_directions'.i18nGmap() + ':</strong> <strong>' + 'to_here'.i18nGmap() + '</strong> - <a href="javascript:fromhere(' + i + ')">' + 'from_here'.i18nGmap() + '</a>' +
	   '<br />' + 'start_address'.i18nGmap() + ':<form action="http://maps.google.com/maps" method="get" accept-charset="utf-8" enctype="application/x-www-form-urlencoded" target="_blank">' +
	   '<input type="text" size="20" maxlength="80" name="saddr" id="saddr" value="" /> ' +
	   '<input type="submit" value="' + 'go'.i18nGmap() + '" class="button" />' +
	   '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + '"/><br /><a href="javascript:back(' + i + ');">' + 'back'.i18nGmap() + '</a></form>';
	from_htmls[i] = html + '<br /><strong>' + 'get_directions'.i18nGmap() + ':</strong> <a href="javascript:tohere(' + i + ')">' + 'to_here'.i18nGmap() + '</a> - <strong>' + 'from_here'.i18nGmap() + '</strong>' +
	   '<br />' + 'end_address'.i18nGmap() + ':<form action="http://maps.google.com/maps" method="get" accept-charset="utf-8" enctype="application/x-www-form-urlencoded" target="_blank">' +
	   '<input type="text" size="20" maxlength="80" name="daddr" id="daddr" value="" /> ' +
	   '<input type="submit" value="' + 'go'.i18nGmap() + '" class="button" />' +
	   '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() + '"/><br /><a href="javascript:back(' + i + ');">' + 'back'.i18nGmap() + '</a></form>';
	html = html + '<br /><strong>' + 'get_directions'.i18nGmap() + ':</strong> <a href="javascript:tohere(' + i + ')">' + 'to_here'.i18nGmap() + '</a> - <a href="javascript:fromhere(' + i + ')">' + 'from_here'.i18nGmap() + '</a>';
  GEvent.addListener(marker, 'click', function () {
    marker.openInfoWindowHtml(html);
  });
	gmarkers[i] = marker;
	htmls[i] = html;
	i = i + 1;
	fullHtml = html;
	return marker;
}

function prepareMap(lat, lon, zoom, name, initHtml) {
  var mapContainer = $('#gmap');
  try {
	  mapContainer.html('<p>'+'please_wait_gmap'.i18n()+'</p>');
    if (GBrowserIsCompatible()) {
  		var map = new GMap2(gMapContainer);
  		map.addControl(new GSmallMapControl());
  		map.setCenter(new GLatLng(lat, lon), zoom); 
  		var point = new GLatLng(lat, lon);
  		markerOptions = {};
  		var marker = createMarker(point, markerOptions, name, initHtml);
  		map.addOverlay(marker);
  		marker.openInfoWindowHtml(fullHtml);
  	}
  } catch(e) { mapContainer.html('<p>'+'error_gmap'.i18n()+'</p>'); }
}

function back(i) {
  gmarkers[i].openInfoWindowHtml(htmls[i]);
}

function tohere(i) {
  gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}

function fromhere(i) {
  gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}

function chooseLocation() {
	if (!gMapContainer) {
    return false;
  }
	var lat = 48.089679;
	var lon = 19.152677;
	var name = 'title'.i18nGmap();
	var initHtml = '<strong>' + 'title'.i18nGmap() + '</strong><br /><strong>' + 'address'.i18nGmap() + ':</strong><br />211 Dolinka<br />991 28&nbsp;&nbsp;Vinica<br />';
	prepareMap(lat, lon, 14, name, initHtml);
}

$(document).ready(function(){	
  gMapContainer = document.getElementById('gmap');
  chooseLocation();
});
$(window).unload( function () {
  GUnload();
});