//this javascript file contains 3 examples, how you can add custom map layers to cmaps
//in one javascript file it is possible to add more custom layers
//
//
//Easist way is to use cmaps internal function:
//AddCustomLayer(newlayername,newlayerurl,newlayertype,newlayermaxzoom,newlayerminzoom);
//Parameters: 
//newlayername - string, layer name (do not use blank space character in layer name)
//newlayerurl - URL address of map tiles
//              for x,y,z tile coordinates in URL address use capital characters in brackets:  (X) (Y) (Z) 
//              for tile server balancing in URL use server names in brackets with | separator:   (server1|server2|server3|server4|..|..)
//  
//newlayertype - string, values: "base" or "over"     layer can be base layer or overlay
//newlayermaxzoom - integer  from 0 to 20 - minimal supported zoom level (Google map standard)
//newlayerminzoom - integer form 0 to 20 - maximal supported zoom level (Google map standard)
//
//note: 
//if custom map is type "base" and is zoomed outside supported zoom level, OSM cycle map tiles are displayed.
//if custom map is type "over" and is zoomed outside supported zoom level, transparent tiles are displayed. 
//
//AddCustomLayer function support only maps with google compliant tile scheme. 
//This function is also used in Basic definition form in CMAPS.
//
//
//






//example 1 - first custom layer
//add overlay with castles location in SLovak Republic from http://www.freemap.sk server
AddCustomLayer("FreemapSK_Castles","http://tiles.freemap.sk/X~Z/(Z)/(X)/(Y).png","over",17,8);


//example 2
//add OSM mapnik map as base layer, server balancing within 3 servers defined:  a , b and c
AddCustomLayer("OSM_Mapnik","http://(a|b|c).tile.openstreetmap.org/(Z)/(X)/(Y).png","base",18,16);




//Example 3, add base layer using standard Openlayers functions
//Openlayers documentation: http://trac.openlayers.org/wiki/Documentation

            var mapnik = new OpenLayers.Layer.XYZ(
                "OpenStreetMap_Mapnik",
                "http://tile.openstreetmap.org/",
                {
                    projection: p900913, 
                    'sphericalMercator': true,
                    type: 'png', getURL: osm_getTileURL,
                    displayOutsideMaxExtent: true,
                    attribution: '<a href="http://www.openstreetmap.org/">OpenStreetMap</a>'
                }
            );
           

  
            map.addLayers([mapnik]);


          
        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;
            }
       }
