var map;

SkylineProjection = new GProjection();

SkylineProjection.mapResolutions = [256,512,1024,2048,4096]

SkylineProjection.fromLatLngToPixel=function(latlng,zoom) {
    var lng = parseInt(Math.floor((this.mapResolutions[zoom]/360)*(latlng.lng()+180)));
    var lat = parseInt(Math.floor(Math.abs((this.mapResolutions[zoom]/2/180)*(latlng.lat()-90))));
    var point = new GPoint(lng,lat);
    return point;
}

SkylineProjection.fromPixelToLatLng=function(pixel,zoom,unbounded) {
    var lat = 90-(pixel.y/(this.mapResolutions[zoom]/2/180));
    var lng = (pixel.x/(this.mapResolutions[zoom]/360))-180;
    var latlng = new GLatLng(lat,lng);
    return latlng;
}

SkylineProjection.tileCheckRange=function(tile,zoom,tileSize){
    var rez = this.mapResolutions[zoom];
    if(tile.y < 0 || tile.y * tileSize >= rez/2){ return false; }
    if(tile.x < 0 || tile.x * tileSize >= rez){
        var e = Math.floor( rez/tileSize );
        tile.x = tile.x%e;
        if(tile.x < 0){ tile.x += e; }
    }
    return true;
}

SkylineProjection.getWrapWidth=function(zoom){
    return this.mapResolutions[zoom];
}

copyrights = new GCopyrightCollection('Imagery:');
var tskyline = new GCopyright(
    'tskyline', new GLatLngBounds(new GLatLng(-90,-180),new GLatLng(90,180)),
    0, '<a href="http://www.chartermisstoronto.com/">Miss Toronto</a>');
copyrights.addCopyright(tskyline);

var SkylineTiles = new GTileLayer(copyrights,1,4);
SkylineTiles.getTileUrl = function(tile,zoom){
	if(zoom > 4 || zoom < 2)
	{
		return 'tiles/no_tiles_at_zoom_level.png';
	}
	else
	{
		return 'http://terrilldent.googlepages.com/tile.' + zoom + '.' +  (tile.x + tile.y*Math.pow(2,zoom)) + '.png';
	}
};
SkylineTiles.isPng = function() { return true; }
SkylineTiles.getOpacity = function() { return 1.0; }

var TorontoSkyline = new GMapType([SkylineTiles],SkylineProjection, 'Toronto Skyline',{
    shortName:'TS',
    tileSize:256,
    maxResolution:5,
    minResolution:0
});

//*****************************************************************
//                   CUSTOM ZOOM CONTROLS
//*****************************************************************

function TextualZoomControl() {
}
TextualZoomControl.prototype = new GControl();

TextualZoomControl.prototype.initialize = function(map) {
  var container = document.createElement("div");

  var zoomInDiv = document.createElement("div");
  this.setButtonStyleIn_(zoomInDiv);
  container.appendChild(zoomInDiv);
  zoomInDiv.appendChild(document.createTextNode(" "));
  GEvent.addDomListener(zoomInDiv, "click", function() {
    map.zoomIn();
  });

  var zoomOutDiv = document.createElement("div");
  this.setButtonStyleOut_(zoomOutDiv);
  container.appendChild(zoomOutDiv);
  zoomOutDiv.appendChild(document.createTextNode(" "));
  GEvent.addDomListener(zoomOutDiv, "click", function() {
    map.zoomOut();
  });

  map.getContainer().appendChild(container);
  return container;
}

TextualZoomControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
}

TextualZoomControl.prototype.setButtonStyleIn_ = function(button) {

  button.style.padding = "2px";
  button.style.marginBottom = "3px";
  button.style.textAlign = "center";
  button.style.width = "107px";
  button.style.height = "30px";
  button.style.cursor = "pointer";
  button.style.backgroundImage = "url(zoom_in.gif)";
}

TextualZoomControl.prototype.setButtonStyleOut_ = function(button) {

  button.style.padding = "2px";
  button.style.marginBottom = "3px";
  button.style.textAlign = "center";
  button.style.width = "107px";
  button.style.height = "30px";
  button.style.cursor = "pointer";
  button.style.backgroundImage = "url(zoom_out.gif)";
}

function initialize() {
	 map = new GMap2(
		document.getElementById("map"),{
			mapTypes:[TorontoSkyline]
	});

	map.addMapType(TorontoSkyline);
	map.addControl(new TextualZoomControl());
	map.setCenter(new GLatLng(44, 0), 3);
	
}

window.onload = initialize;