var lon = 17.01670;
var lat = 50.99109;
var zoom = 9;
var map;
var track, gsat, gter, mapnik, cycle;

OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;
//OpenLayers.Util.onImageLoadErrorColor = "transparent";

function init(){
  var options = {
    projection: new OpenLayers.Projection("EPSG:900913"),
    units: "m",
    maxResolution: 156543.0339,
    maxExtent: new OpenLayers.Bounds(-20037508.34, -20037508.34,
                                     20037508.34, 20037508.34),
    controls: [
      new OpenLayers.Control.PanZoomBar(),
      new OpenLayers.Control.MouseDefaults(),
      new OpenLayers.Control.LayerSwitcher(),
      new OpenLayers.Control.ScaleLine(),
      new OpenLayers.Control.Permalink()
    ]
  };

  map = new OpenLayers.Map( 'map', options );

  gter = new OpenLayers.Layer.Google(
    "Google Physical Map",
    {type: G_PHYSICAL_MAP, 'sphericalMercator': true, numZoomLevels: 22}
  );

  gsat = new OpenLayers.Layer.Google(
    "Google Satellite",
    {type: G_SATELLITE_MAP, 'sphericalMercator': true, numZoomLevels: 22}
  );

  var TILECACHE = "http://apps.rodney.id.au/tilecache/tilecache.cgi?";
  track = new OpenLayers.Layer.WMS( "Bike Tour 2008",
    TILECACHE, {layers: 'gtrack', format:
                'image/png' } );
  track.isBaseLayer = false;

   mapnik = new OpenLayers.Layer.TMS(
     "OpenStreetMap",
     "http://a.tile.openstreetmap.org/",
     {
       type: 'png', getURL: osm_getTileURL,
       displayOutsideMaxExtent: true
     }
   );

  cycle = new OpenLayers.Layer.TMS("OSM Cycle Map",
    ["http://a.andy.sandbox.cloudmade.com/tiles/cycle/",
     "http://b.andy.sandbox.cloudmade.com/tiles/cycle/",
     "http://c.andy.sandbox.cloudmade.com/tiles/cycle/"],
    { type: 'png', getURL: getTileURL, displayOutsideMaxExtent: true,
      transitionEffect: 'resize'});

  map.addLayers([gter, gsat, mapnik, cycle, track]);

  var lonLat = new OpenLayers.LonLat(lon, lat).transform(
    new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());

  if (!map.getCenter())
    map.setCenter(lonLat, zoom);
}

function osm_getTileURL(bounds) {
  var res = this.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 = this.map.getZoom();
  var limit = Math.pow(2, z);

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

function getTileURL(bounds) {
  var res = this.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 = this.map.getZoom();
  var limit = Math.pow(2, z);

  if (y < 0 || y >= limit)
  {
    return null;
  }
  else
  {
    x = ((x % limit) + limit) % limit;

    var url = this.url;
    var path = z + "/" + x + "/" + y + ".png";

    if (url instanceof Array) {
      url = this.selectUrl(path, url);
    }
    return url + path;

  }
}

window.onload = init;
