if (! info){
    var info = {};
}

if (! info.aaronland){
    info.aaronland = {};
}

if (! info.aaronland.geo){
    info.aaronland.geo = {};
}

info.aaronland.geo.Tilespum = function(layer, host){
    this.layer = (layer === undefined) ? 'dithering' : layer;
    this.host = (host === undefined) ? 'http://woe.spum.org/t' : host;

    this.height = window.innerHeight;
    this.width = window.innerWidth;
    this.mapid = 'map';

    this.map = undefined;
};

info.aaronland.geo.Tilespum.prototype.drawCenterZoom = function(lat, lon, zoom){

    var host = this.host;
    var layer = this.layer;

   	var uri = new info.aaronland.URI();

    	if (uri.query.contains('layer')){
    		layer = uri.query.get('layer');
    	}

    	if (uri.query.contains('host')){
    		host = uri.query.get('host');
    	}

    	if (uri.query.contains('zoom')){
    		zoom = uri.query.get('zoom');
    	}

	var template = host + '/' + layer + '/{Z}/{X}/{Y}.png';

        if ((lat && lon && zoom) && (! uri.query.contains('findme'))){
    		this.load_map(template, lat, lon, zoom);
    		return;
    	}

        var _self = this;

	var doThisOnSuccess = function(lat, lon){

            if ((zoom === undefined) || (zoom < 16)){
		zoom = 16;
	    }

            _self.load_map(template, lat, lon, zoom);
	};

    	var doThisIfNot = function(){
	    // http://www.flickr.com/photos/goldberg/277474865/
	    var map = document.getElementById(this.mapid);	       
	    map.innerHTML = '<img src="sadface_d.png" />';

            alert('Unable to determine your location. Sad face.');
	};

    	var loc = new info.aaronland.geo.Location({});
	loc.findMyLocation(doThisOnSuccess, doThisIfNot);

	return;
};

info.aaronland.geo.Tilespum.prototype.load_map = function(template, lat, lon, zoom){

    	var mm = com.modestmaps;

    	var provider = new mm.TemplatedMapProvider(template);
	var loc = new mm.Point(this.width, this.height);

	var handlers = undefined;

	if (document.ontouchmove !== undefined){
		var touch = new com.modestmaps.TouchHandler();
		var handlers = new Array();
		handlers.push(touch);
	}

	map = new mm.Map(this.mapid, provider, loc, handlers);
    	map.setCenterZoom(new mm.Location(lat, lon), zoom);

    	var container = document.getElementById('container');

	if (container){
    		window.onresize = function() {
        		map.setSize(container.offsetWidth, container.offsetHeight);
    		};
	}

	this.map = map;
};

info.aaronland.geo.Tilespum.prototype.drawExtent = function(swlat, swlon, nelat, nelon){

    var host = this.host;
    var layer = this.layer;

    var template = host + '/' + layer + '/{Z}/{X}/{Y}.png';

    this.load_map_by_extent(template, swlat, swlon, nelat, nelon);
}

info.aaronland.geo.Tilespum.prototype.load_map_by_extent = function(template, swlat, swlon, nelat, nelon){

    	var mm = com.modestmaps;

    	var provider = new mm.TemplatedMapProvider(template);
	var loc = new mm.Point(this.width, this.height);

	// console.log(loc);
	var handlers = undefined;

	if (document.ontouchmove !== undefined){
		var touch = new com.modestmaps.TouchHandler();
		var handlers = new Array();
		handlers.push(touch);
	}

	var sw = new mm.Location(swlat, swlon);
	var ne = new mm.Location(nelat, nelon);

	var locs = [ sw, ne ];

	map = new mm.Map(this.mapid, provider, loc, handlers);
    	map.setExtent(locs);

    	var container = document.getElementById('container');

	if (container){
    		window.onresize = function() {
        		map.setSize(container.offsetWidth, container.offsetHeight);
    		};
	}

	this.map = map;
};

