
var osm_map, gml;

        function style_osm_feature(feature) {
            feature.style = OpenLayers.Util.extend({'fill':'white'}, OpenLayers.Feature.Vector.style['default']);

            if (feature.attributes.kct_red) {
                feature.style.strokeColor = "red";
                feature.style.strokeWidth = 4;
            } else if (feature.attributes.kct_green) {
                feature.style.strokeColor = "green";
                feature.style.strokeWidth = 4;
            } else if (feature.attributes.kct_yellow) {
                feature.style.strokeColor = "yellow";
                feature.style.strokeWidth = 4;
            } else if (feature.attributes.kct_blue) {
                feature.style.strokeColor = "blue";
                feature.style.strokeWidth = 4;
            } else if (feature.attributes.highway == "primary") {
                feature.style.strokeColor = "red";
            } else if (feature.attributes.highway == "secondary") {
                feature.style.strokeColor = "orange";
            } else if (feature.attributes.highway) {
                feature.style.strokeColor = "black";
            } else  {
                feature.style.strokeColor = "black";
            }
        }


        function osm_getTileURL(bounds) {
            var res = osm_map.getResolution();
            var x = Math.round((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
            var y = Math.round((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
            var z = osm_map.getZoom();
            var limit = Math.pow(2, z);

            if (y < 0 || y >= limit) {
                return "404.png";
            } else {
                x = ((x % limit) + limit) % limit;
                return this.url + z + "/" + x + "/" + y + "." + this.type;
            }
        }

function check_zoom() { 
var zoom = osm_map.getZoom();
if (zoom >= 11) { return true; }
if (zoom >= 9) { return confirm("Loading this amount of data may slow your browser. Are you sure you want to do this?"); }
$("status").innerHTML = "Area too large. Zoom in to load data. (Current zoom level: "+ zoom + ". Must be at zoom 9+.)";
return false;
}

function new_data() 
{
  if (!check_zoom()) { return; }
  clear_data();
  gml.loaded = false;
  kokot = osm_map.getExtent().transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
//gml.url = "http://www.openstreetmap.org/api/0.5/map?bbox="+kokot.toBBOX();
  gml.url = "http://osmxapi.hypercube.telascience.org/api/0.5/way[kct_red|kct_blue|kct_green|kct_yellow][bbox="+kokot.toBBOX()+"]";
//alert("Loading more data..."+gml.url);
  gml.loadGML();
}

function clear_data() 
{
  gml.destroyFeatures();
}

function osm_cz_init()
{
  OpenLayers.ProxyHost = "cgi-bin/proxy.cgi?url=";
  OpenLayers.IMAGE_RELOAD_ATTEMPTS=3;
  osm_map = new OpenLayers.Map('osm_map', {
                controls: [
                    new OpenLayers.Control.PanZoomBar(),
                    new OpenLayers.Control.MouseDefaults(),
                    new OpenLayers.Control.LayerSwitcher({'ascending':false}),
                    new OpenLayers.Control.ScaleLine(),
                    new OpenLayers.Control.Permalink('permalink'),
                    new OpenLayers.Control.MousePosition(),
//                    new OpenLayers.Control.OverviewMap(),
                    new OpenLayers.Control.KeyboardDefaults()                    ],
                maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
                numZoomLevels: 18,
                maxResolution: 156543.0399,
                units: 'm',
                projection: new OpenLayers.Projection("EPSG:900913"),
                displayProjection: new OpenLayers.Projection("EPSG:4326")
              });

  var layerMapnik = new OpenLayers.Layer.OSM.Mapnik("Mapnik");
  var layerTah    = new OpenLayers.Layer.OSM.Osmarender("Tiles@Home");
  var layerCycle  = new OpenLayers.Layer.OSM.CycleMap("Cycle map");
  osm_map.addLayers([layerMapnik,layerTah,layerCycle]);

  var layer_osmcz2 = new OpenLayers.Layer.TMS(
      "osm.cz TMS",
      "http://openstreetmap.cz/tms/",
      {   
          layername: 'osmcz',
          type: 'png', getURL: osm_getTileURL,
          displayOutsideMaxExtent: true,
          attribution: '<a href="http://www.openstreetmap.org/">OpenStreetMap</a>'
      }
  );
  osm_map.addLayer(layer_osmcz2);
  var layer_osmcz2 = new OpenLayers.Layer.TMS(
                "hajnet TMS",
                "http://osm.hajnet.cz/turist/",
                {   
		    layername: 'hajnet',
                    type: 'png', getURL: osm_getTileURL,
                    displayOutsideMaxExtent: true,
                    attribution: '<a href="http://www.openstreetmap.org/">OpenStreetMap</a>'
                }
            );
  osm_map.addLayer(layer_osmcz2);

  var layer_kctcz = new OpenLayers.Layer.TMS(
                "Turisticke stezky",
                "http://openstreetmap.cz/kct_tiles/",
                {   
		    isBaseLayer:false,
		    layername: 'kctcz',
                    opacity:0.6,
		    type: 'png', 
		    getURL: osm_getTileURL,
                    displayOutsideMaxExtent: true,
                    attribution: '<a href="http://www.openstreetmap.org/">OpenStreetMap</a>'
                }
            );
  osm_map.addLayer(layer_kctcz);

//            gml = new OpenLayers.Layer.GML("KCT", "http://openstreetmap.cz/kct.osm", {format: OpenLayers.Format.OSM, projection: map.displayProjection});
            gml = new OpenLayers.Layer.GML("KCT", "http://openstreetmap.cz/kct.osm", {format: OpenLayers.Format.OSM, projection: osm_map.displayProjection});
            gml.events.register("loadstart", null, function() { alert("Loading...") });
            gml.events.register("loadend", null, function() { alert("Done"); gml.setVisibility(true);});
            osm_map.addLayer(gml);
            gml.setVisibility(false);
    gml.preFeatureInsert = style_osm_feature; 

  osm_map.setCenter(new OpenLayers.LonLat(17.07,49.7).transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913")), 12);
  return osm_map;
}
