var maplint;
var custom_map_types = {
    'mapnik' : {
        'id' : 'mapnik',
        'label' : 'CloudMade',
        'copyright' : '&copy; 2008 CloudMade - Map data CCBYSA 2008 OpenStreetMap.org contributors',
        'prefix' : 'Maps: ',
        'callback' : function( a, b ) {
            return 'http://tile.cloudmade.com/d1cb08000e175526b1dccb9148c9ddbb/1/256/'+(b-1)+'/'+a.x+'/'+a.y+'.png'
            return 'http://b.tile.openstreetmap.org/'+(b-1)+'/'+a.x+'/'+a.y+'.png';
        }
    },
    'noname' : {
        'id' : 'noname',
        'label' : 'No Names',
        'copyright' : '&copy; 2008 CloudMade - Map data CCBYSA 2008 OpenStreetMap.org contributors',
        'prefix' : '',
        'callback' : function( a, b ) {
            return 'http://tile.cloudmade.com/d1cb08000e175526b1dccb9148c9ddbb/3/256/'+(b-1)+'/'+a.x+'/'+a.y+'.png'
            return 'http://b.tile.openstreetmap.org/'+(b-1)+'/'+a.x+'/'+a.y+'.png';
        }
    },
    'mobile' : {
        'id' : 'mobile',
        'label' : 'Mobile',
        'copyright' : '&copy; 2008 CloudMade - Map data CCBYSA 2008 OpenStreetMap.org contributors',
        'prefix' : '',
        /*'size' : 64,*/
        'callback' : function( a, b ) {
            return 'http://tile.cloudmade.com/d1cb08000e175526b1dccb9148c9ddbb/2/256/'+(b-1)+'/'+a.x+'/'+a.y+'.png'
            return 'http://b.tile.openstreetmap.org/'+(b-1)+'/'+a.x+'/'+a.y+'.png';
        }
    },
    'osmarender' : {
        'id' : 'osmarender',
        'label' : 'Tiles@Home',
        'copyright' : 'Open Street Map',
        'prefix' : 'Maps: ',
        'callback' : function( a, b ) {
            return 'http://a.tah.openstreetmap.org/Tiles/tile/'+(b-1)+'/'+a.x+'/'+a.y+'.png';
        }
    },
    'testonly' : {
        'id' : 'testonly',
        'label' : 'Test Only Map',
        'copyright' : 'JMCK',
        'prefix' : 'Maps: ',
        'callback' : function( a, b ) {
            return 'http://johnmckerrell.com/map/tiles/london/'+(b-1)+'/'+a.x+'/'+a.y+'.png';
        }
    },
    'npe' : {
        'id' : 'npe',
        'label' : 'NPE',
        'maxResolution' : 15,
        'copyright' : 'npemap.org.uk',
        'prefix' : 'Maps: ',
        'callback' : function( a, b ) {
            return 'http://richard.dev.openstreetmap.org/npe/'+(b-1)+'/'+a.x+'/'+a.y+'.jpg';
            return 'http://b.tile.openstreetmap.org/'+(b-1)+'/'+a.x+'/'+a.y+'.png';
        }
    }
    /*
    'maplint' : {
        'id' : 'maplint',
        'label' : 'maplint',
        'copyright' : 'Open Street Map',
        'prefix' : 'Maps: ',
        'callback' : function( a, b ) {
            return 'http://a.tah.openstreetmap.org/Tiles/maplint/'+(b-1)+'/'+a.x+'/'+a.y+'.png';
        }
    },
    'oldosmarender' : {
        'id' : 'oldosmarender',
        'label' : 'Old OsmaRender',
        'copyright' : 'Open Street Map',
        'prefix' : 'Maps: ',
        'callback' : function( a, b ) {
            return 'http://dev.openstreetmap.org/~ojw/Tiles/tile.php/'+(b-1)+'/'+a.x+'/'+a.y+'.png';
        }
    }
    */
};

function makeMapLint( ) {
    var copyright = new MMCopyright( 1, [ new MMBounds( new MMLatLon( -90, -180 ), new MMLatLon( 90, 180 ) ) ], 1, 'Open Street Map' );
    var copyrightCollection = new MMCopyrightCollection( 'Maps: ' );
    copyrightCollection.addCopyright( copyright );

    var tilelayers = [];
    var obj = {};
    // Create one tile layer for the overlay
    var tilelayer = new MMTileLayer( copyrightCollection, obj.minResolution ? obj.minResolution : 1, 18 );
    tilelayer.getTileUrl = custom_map_types['osmarender']['callback'];
    tilelayers.push( tilelayer );
    
    tilelayer = new MMTileLayer( copyrightCollection, obj.minResolution ? obj.minResolution : 1, 17 );
    tilelayer.getTileUrl = function( a, b ) {
            return 'http://a.tah.openstreetmap.org/Tiles/maplint/'+(b-1)+'/'+a.x+'/'+a.y+'.png';
        };
    tilelayer.getOpacity = function() { return 1.0; };
    tilelayers.push( tilelayer );

    if( ! obj.projection )
        obj.projection = new MMMercatorProjection(20);

    if( obj.axis )
        obj.projection.getAxisMultiplier = function() { return obj.axis; };

    obj.map = new MMMapType( tilelayers, obj.projection, 'Maplint', { urlArg : 'maplint' } );

    /*
    var li = document.createElement( 'li' );
    var label = document.createElement( 'label' );
    var input = document.createElement( 'input' );
    input.onclick = function() {
        var myinput = input;
        if( input.checked ) {
            mapviewer.addOverlay( obj.overlay );
        } else {
            mapviewer.removeOverlay( obj.overlay );
        }
    }
    input.setAttribute( 'type', 'checkbox' );
    label.appendChild( input );
    label.appendChild( document.createTextNode( ' '+obj.label ) );
    li.appendChild( label );
    document.getElementById( 'overlays' ).appendChild( li );
    */

    maplint = obj;
    return obj.map;
}

function makeTestLayer( ) {
    var copyright = new MMCopyright( 1, [ new MMBounds( new MMLatLon( -90, -180 ), new MMLatLon( 90, 180 ) ) ], 1, 'JMCK' );
    var copyrightCollection = new MMCopyrightCollection( 'Maps: ' );
    copyrightCollection.addCopyright( copyright );

    var tilelayers = [];
    var obj = {};
    // Create one tile layer for the overlay
    var tilelayer = new MMTileLayer( copyrightCollection, obj.minResolution ? obj.minResolution : 1, 18 );
    tilelayer.getTileUrl = custom_map_types['mapnik']['callback'];
    tilelayers.push( tilelayer );
    
    tilelayer = new MMTileLayer( copyrightCollection, obj.minResolution ? obj.minResolution : 1, 18 );
    tilelayer.getTileUrl = function( a, b ) {
            return 'http://johnmckerrell.com/map/tiles/london/'+(b-1)+'/'+a.x+'/'+a.y+'.png';
        };
    tilelayer.getOpacity = function() { return 0.8; };
    tilelayers.push( tilelayer );

    if( ! obj.projection )
        obj.projection = new MMMercatorProjection(20);

    if( obj.axis )
        obj.projection.getAxisMultiplier = function() { return obj.axis; };

    obj.map = new MMMapType( tilelayers, obj.projection, 'Test', { urlArg : 'test' } );

    testlayer = obj;
    return obj.map;
}


function makeMap( obj ) {
    var copyright = new MMCopyright( 1, [ new MMBounds( new MMLatLon( -90, -180 ), new MMLatLon( 90, 180 ) ) ], 1, obj['copyright'] );
    var copyrightCollection = new MMCopyrightCollection( obj['prefix'] );
    copyrightCollection.addCopyright( copyright );

    // Create one tile layer for the overlay
    var tilelayer = new MMTileLayer( copyrightCollection, obj.minResolution ? obj.minResolution : 1, obj.maxResolution ? obj.maxResolution : 18 );
    tilelayer.getTileUrl = obj['callback'];
    tilelayer.getOpacity = function() { return 0.65; };
    tilelayer.isClipped = function() { return true; };
    obj.overlay = new MMTileLayerOverlay( tilelayer );
    
    tilelayer = new MMTileLayer( copyrightCollection, obj.minResolution ? obj.minResolution : 1, obj.maxResolution ? obj.maxResolution : 18 );
    tilelayer.getTileUrl = obj['callback'];
    var tilelayers = [ tilelayer ];

    if( ! obj.projection )
        obj.projection = new MMMercatorProjection(20);

    if( obj.axis )
        obj.projection.getAxisMultiplier = function() { return obj.axis; };

    obj.map = new MMMapType( tilelayers, obj.projection, obj.label, { urlArg : obj.id } );
    if( obj.size ) {
        obj.map.getTileSize = function() { return obj.size };
    }

    /*
    var li = document.createElement( 'li' );
    var label = document.createElement( 'label' );
    var input = document.createElement( 'input' );
    input.onclick = function() {
        var myinput = input;
        if( input.checked ) {
            mapviewer.addOverlay( obj.overlay );
        } else {
            mapviewer.removeOverlay( obj.overlay );
        }
    }
    input.setAttribute( 'type', 'checkbox' );
    label.appendChild( input );
    label.appendChild( document.createTextNode( ' '+obj.label ) );
    li.appendChild( label );
    document.getElementById( 'overlays' ).appendChild( li );
    */

    return obj.map;
}

function addOSM( map ) {
    for( var type in custom_map_types ) {
        map.addMapType( custom_map_types[type].map );
    }
    map.addMapType( makeMapLint() );
    map.addMapType( makeTestLayer() );
}

(function() {
    for( var type in custom_map_types ) {
        makeMap( custom_map_types[type] );
    }
})();
