var map;
var geoXml1 = new GGeoXml("http://www.gps.lt/GoogleShops/Shops.kml");
var geoXml2 = new GGeoXml("http://www.gps.lt/GoogleShops/Cities.kml");
var lat;
var lon;
var zoom;

function load() {
	if (GBrowserIsCompatible()) {
        	map = new GMap2(document.getElementById("map"));
       		map.enableDoubleClickZoom();
            	map.addControl(new GLargeMapControl());
            	map.addControl(new GMapTypeControl());
            	map.setCenter(new GLatLng(55.210222, 23.807373), 6);
		map.addOverlay(geoXml2);               

            	function printmap()
            	{}
            	printmap.prototype = new GControl();
            	printmap.prototype.initialize = function(map) {
             	  var container = document.createElement("div");
             	  var print = document.createElement("div");
             	  this.setButtonStyle_(print);
             	  container.appendChild(print);
             	  print.appendChild(document.createTextNode("Spausdinti"));
             	  GEvent.addDomListener(print, "click", function() { 
              	    printSpecial();  
             	  });
             	  map.getContainer().appendChild(container);
             	  return container;
            	}
            	printmap.prototype.getDefaultPosition = function() { 
             	  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(215, 7));
            	}
            	printmap.prototype.setButtonStyle_ = function(button) {
             	  button.style.color = "black";
             	  button.style.backgroundColor = "white";
             	  button.style.font = "small Arial";
             	  button.style.border = "1px solid black";
             	  button.style.textAlign = "center";
             	  button.style.width = "5em";
             	  button.style.cursor = "pointer";
            	}
            	map.addControl(new printmap());

            	GEvent.addListener(map, "click", function(overlay, point) {
            	  if (overlay) 
		  {
                    map.setCenter(overlay.getPoint(), 11);
            	  }
            	});

            	GEvent.addListener(map, "zoomend", function(oldLevel, newLevel) {
                  if(newLevel >= 11)
                  {
		    map.addOverlay(geoXml1);
		    geoXml2.hide();
		    geoXml1.show();
                    GEvent.clearListeners(map, "click");
                    GEvent.addListener(map, "click", function(overlay, point) {
                      if (overlay) 
		      {
                        map.setCenter(overlay.getPoint(), 15);
                      }
                    });
                  }
                  else
                  {
		    geoXml1.hide();
		    geoXml2.show();
                    GEvent.clearListeners(map, "click");
                    GEvent.addListener(map, "click", function(overlay, point) {
                      if (overlay) 
		      {
                        map.setCenter(overlay.getPoint(), 11);
                      }
                    });
                  }
                });
            /*
            	GDownloadUrl("http://www.gps.lt/GoogleShops/getCitiesKML.php", function(data, responseCode) 
            	{var xmlc = GXml.parse(data);
             	 var cities = xmlc.documentElement.getElementsByTagName("name");
                 var citycoordinates = xmlc.documentElement.getElementsByTagName("coordinates");

             	 GDownloadUrl("http://www.gps.lt/GoogleShops/getShopsKML.php", function(data, responseCode) 
             	 {var xml = GXml.parse(data);
              	  var markers = xml.documentElement.getElementsByTagName("name");
              	  var description = xml.documentElement.getElementsByTagName("description");
              	  var coordinates = xml.documentElement.getElementsByTagName("coordinates");

              	  for (var j = 1; j < cities.length; j++) {
		    var a = citycoordinates[j - 1];
		    var strc = citycoordinates[j - 1].text;
		    if(strc == undefined)
		    {
			strc = citycoordinates[j - 1].textContent;
		    }
                    var firstc = strc.indexOf(",");
                    var lonc = strc.substring(0,firstc);
                    strc = strc.substring(firstc+1,strc.length);
                    firstc = strc.indexOf(",")
                    var latc = strc.substring(0,firstc);
                    var marker = document.createElement("div");
		    var txttmp = cities[j].text;
		    if(txttmp == undefined)
		    {
			txttmp = cities[j].textContent;
		    }
                    marker.setAttribute("id", txttmp);
                    document.getElementById("shoplist").appendChild(marker);
                    document.getElementById(txttmp).innerHTML = "<hr><div style=\"background-color: #5D7B9D\"><font color=\"white\"><a href=\"javascript:zoommarker(" + lonc + "," + latc + ", 11);\" style=\"color: white\">" + txttmp + ":</a></font></div>";
              
                    for (var i = 1; i < markers.length; i++) {
                      var str = coordinates[i - 1].text;
   		      if(str == undefined)
		      {
			  str = coordinates[i - 1].textContent;
		      }
                      var first = str.indexOf(",");
                      var lon = str.substring(0,first);
                      str = str.substring(first+1,str.length);
                      first = str.indexOf(",")
                      var lat = str.substring(0,first);
		      var destmp = description[i].text;
		      if(destmp == undefined)
		      {
			destmp = description[i].textContent;
		      }
                      var city = destmp.match(txttmp);
                      var side_bar_html = "";
                      if(city == txttmp)
                      {
			var markname = markers[i].text;
			if(markname == undefined)
			{
			   markname = markers[i].textContent;
			}
                        side_bar_html += '<a href="javascript:zoommarker(' + lon + ',' + lat + ',15);">' + markname + '</a><br>';
                        document.getElementById(txttmp).innerHTML = document.getElementById(txttmp).innerHTML + side_bar_html;
                      }
                    }
                  }
                 });
                });*/
	}
}

function zoommarker(lon, lat, zoom)
{
    map.setCenter(new GLatLng(lat, lon), zoom);
    scroll(0,300);
}

function getLat()
{
    return lat;
}

function getLon()
{
    return lon;
}

function getZoom()
{
    return zoom;
}

function printSpecial() 
{
    lat = map.getCenter().lat();
    lon = map.getCenter().lng();
    zoom = map.getZoom();

    var printWin = window.open("http://www.gps.lt/GoogleShops/print.html","printWin", "width=500,height=360" );

/* 
	var gAutoPrint = true;
        if (document.getElementById != null) 
        {
                var html = '<HTML>\n<HEAD>\n'; 

                if (document.getElementsByTagName != null) 
                { 
                	var headTags = document.getElementsByTagName("head"); 
                        if (headTags.length > 0) 
                                html += headTags[0].innerHTML; 
                } 

                html += '\n</HE>\n[an error occurred while processing this directive] \n'; 

                var printReadyElem = document.getElementById("map"); 

                if (printReadyElem != null) 
                { 
                                html += printReadyElem.innerHTML; 
                } 
                else 
                { 
                        alert("Could not find the printReady function"); 
                        return; 
                } 

                html += '\n</BO>\n</HT>'; 

                var printWin = window.open("","printSpecial", "width=596,height=360" ); 

                printWin.document.open(); 
                printWin.document.write(html); 
                printWin.document.close(); 
                if (gAutoPrint) 
                        printWin.print(); 
        } 
        else 
        { 
                alert("The print ready feature is only available if you are using an browser. Please update your browswer."); 
        }
 */
}