var centreLat = 31.9921875;
var centreLon = -97.734375;
var initialZoom=2;
var imageWraps=false;
var map;
var myTileSize = 188;

function CustomProjection(a,b){
  this.imageDimension=65536;
  this.pixelsPerLonDegree=[];
  this.pixelOrigin=[];
  this.tileBounds=[];
  this.tileSize=256;
  this.isWrapped=b;
  var b=this.tileSize;
  var c=1;
  for(var d=0;d<a;d++){
    var e=b/2;
    this.pixelsPerLonDegree.push(b/360);//360
    this.pixelOrigin.push(new GPoint(e,e));
    this.tileBounds.push(c);
    b*=2;
    c*=2
  }
}

CustomProjection.prototype=new GProjection();

CustomProjection.prototype.fromLatLngToPixel=function(latlng,zoom){
  var c=Math.round(this.pixelOrigin[zoom].x+latlng.lng()*this.pixelsPerLonDegree[zoom]);
  var d=Math.round(this.pixelOrigin[zoom].y+(-2*latlng.lat())*this.pixelsPerLonDegree[zoom]);
  return new GPoint(c,d)
};

CustomProjection.prototype.fromPixelToLatLng=function(pixel,zoom,unbounded){
  var d=(pixel.x-this.pixelOrigin[zoom].x)/this.pixelsPerLonDegree[zoom];
  var e=-0.5*(pixel.y-this.pixelOrigin[zoom].y)/this.pixelsPerLonDegree[zoom];
  return new GLatLng(e,d,unbounded)
};

CustomProjection.prototype.tileCheckRange=function(tile,zoom,tilesize){
  var tileBounds=this.tileBounds[zoom];
  if (tile.y<0 || tile.y >= tileBounds) {return false;}
  if (this.isWrapped) {
    if (tile.x<0 || tile.x>=tileBounds) { 
      tile.x = tile.x%tileBounds; 
      if (tile.x < 0) {tile.x+=tileBounds} 
    }
  }
  else { 
    if (tile.x<0 || tile.x>=tileBounds) {return false;}
  }  
  return true;
}

CustomProjection.prototype.getWrapWidth=function(zoom) {
  return this.tileBounds[zoom]*this.tileSize;
}

function customGetTileURL(a,b) {
  var c=Math.pow(2,b);

  var d=a.x;
  var e=a.y;
  var f="t";
  for(var g=0;g<b;g++){
    c=c/2;
    if(e<c){
      if(d<c){f+="q"}
      else{f+="r";d-=c}
    }
    else{
      if(d<c){f+="t";e-=c}
      else{f+="s";d-=c;e-=c}
    }
  }
  return "tiles/"+f+".jpg"
}


function getWindowHeight() {
  if (window.self&&self.innerHeight) {
    return self.innerHeight;
  }
  if (document.documentElement&&document.documentElement.clientHeight) {
    return document.documentElement.clientHeight;
  }
  return 0;
}

function load() {
  if (GBrowserIsCompatible()) {

    var pic_tileLayers = [ new GTileLayer()];
    pic_tileLayers[0].getTileUrl = customGetTileURL;
    pic_tileLayers[0].isPng = function() { return false; };
    pic_tileLayers[0].getOpacity = function() { return 1.0; };
    var proj=new CustomProjection(10,imageWraps);

    var pic_customMap = new GMapType(pic_tileLayers, proj, "Pic",
      {maxResolution:9, minResolution:2, errorMessage:"",tileSize: myTileSize});

    map = new GMap2(document.getElementById("map"),{mapTypes:[pic_customMap],backgroundColor:"black"});
    map.enableDoubleClickZoom();
    map.enableContinuousZoom();
    map.enableScrollWheelZoom();

    map.addControl(new GSmallMapControl());

    map.setCenter(new GLatLng(centreLat, centreLon), initialZoom, pic_customMap);

    if($("code").value!=""){
      fromCodeToXY($("code").value);
    }

    GEvent.addListener(map, "click", function(overlay, latlng, overlaylatlng){
    });

  }
}

function goto(x,y){
  map.setZoom(9);
  map.panTo(map.getCurrentMapType().getProjection().fromPixelToLatLng(new GPoint(x*94+47,y*94+47),9,false));
}
