// interface GTileLayerOverlay(GTileLayer)
function KapTileLayerOverlay(a) { 
  b = new KapTileLayer(a);
  // b.setKapFn(a);
  GTileLayerOverlay.call(this,b);
}
// KapTileLayerOverlay.prototype = new GTileLayerOverlay(null);
KapTileLayerOverlay.prototype = new GTileLayerOverlay(new KapTileLayer());


// public KapTileLayer(kap_fn)
// example: map.addOverlay(new GTileLayerOverlay(new KapTileLayer('18773_1')));
// also supports:
// interface GTileLayer(copy,minZ,maxZ,opts)
function KapTileLayer(a,b,c,d) { 
  this.kap_fn = (typeof(a)!="object") ? a : null;
  this.extension = 'gif';
  this.opacity = 0.7;
  
  var copy = new GCopyrightCollection("(c) ");
  copy.addCopyright(new GCopyright('Nautical Charts',
    new GLatLngBounds(new GLatLng(0,-180), new GLatLng(90,0)),
    0,'&copy;2008 - University of California')
  );


  a = (typeof(a)!="object") ? copy : a;
  b = (!b) ? 0    : b;
  c = (!c) ? 18   : c;
  d = (!d) ? {}   : d;
  GTileLayer.call(this,copy,b,c,d);
}
KapTileLayer.prototype = new GTileLayer(null,0,18);


// public
KapTileLayer.prototype.setKapFn = function(kap_fn) { 
  this.kap_fn = kap_fn;
  return true;
};


// interface isPng()
KapTileLayer.prototype.isPng = function() { 
  return (this.extension=='png');
};


// interface getOpacity()
KapTileLayer.prototype.getOpacity = function() { 
  return this.opacity;
};


// public setOpacity(o) { 
KapTileLayer.prototype.setOpacity = function(o) { 
  this.opacity = (o<=1) ? o : o/100;
  return (o>100||o<0) ? false : true;
};

// interface getTileUrl()
KapTileLayer.prototype.getTileUrl = function(t,z) { 
  var zd = (z<10) ? '0'+z : z;
  var chart_id = this.kap_fn.split('_')[0];
  return 'http://sandbar.ucsd.edu/tiles/NauticalCharts/'+this.extension+
    '/'+chart_id+'/latest/'+this.kap_fn+
    '/'+zd+'/z'+z+'y'+t.y+'x'+t.x+'.'+this.extension;
};


// public setWorldBounds(GLatLngBounds)
KapTileLayer.prototype.setWorldBounds = function(bb) { 
  if( bb instanceof GLatLngBounds ) { 
    this.wbounds = bb;
    return true;
  }
  return false;
};

// public setPixelBounds(GBounds)
KapTileLayer.prototype.setPixeldBounds = function(bb) { 
  if( bb instanceof GBounds ) { 
    this.pbounds = bb;
    return true;
  }
  return false;
};


// interface minResolution() 
KapTileLayer.prototype.minResolution = function() {
  return 2;
};


// interface maxResolution() 
KapTileLayer.prototype.maxResolution = function() { 
  if( !this.wbounds || !this.pbounds ) { 
    return 18;
  }
  var wsx = this.wbounds.toSpan().lng();
  var psx = Math.abs(this.pbounds.maxX - this.pbounds.minX);
  return Math.ceil(Math.log((psx/wsx)*360/256)/Math.log(2));
};
