// interface GTileLayerOverlay(GTileLayer)
function SC6KTileLayerOverlay(opts) { 
  this.options_ = {};
  for ( var opt in opts ) {
    this.options_[opt] = opts[opt]; 
  }
  this.opacity = opts && typeof(opts.opacity) != "undefined"?opts.opacity:0.65;
  var b = new SC6KTileLayer();
  GTileLayerOverlay.call(this,b);
  b.opacity = this.opacity;
}
SC6KTileLayerOverlay.prototype = new GTileLayerOverlay(new SC6KTileLayer());

SC6KTileLayerOverlay.prototype.initialize = function(map) { 
  this.map = map;
  this.removeColorbar();
  this.addColorbar();
  return GTileLayerOverlay.prototype.initialize.call(this,map);
}

SC6KTileLayerOverlay.prototype.addColorbar = function () {
  if ( ! this.colorbar ) {
    this.colorbar = new GColorbarControl(true,true);
    this.colorbar.height = 41;
    this.colorbar.width = 151;
    //this.colorbar.setOpts({height:53,width:220,src:'http://www.sccoos.org/data/harbors/lalb/swell_files/currents-scale-fixed.png',layer:this});
    this.colorbar.setOpts({height:41,width:151,src:'/images/surface-currents-cb.png',style:{padding:'0px',border: 'none'}});
  }
  if ( this.options_.colorbarPosition ) {
    this.map.addControl(this.colorbar,this.options_.colorbarPosition);
  } else {
    this.map.addControl(this.colorbar);
  }
}

SC6KTileLayerOverlay.prototype.removeColorbar = function () {
  if ( typeof(this.options_.control) != "undefined" )
      this.options_.control.removeControl(this.colorbar);
}

SC6KTileLayerOverlay.prototype.repositionColorbar = function () {
  if ( typeof(this.options_.control) != "undefined" ) {
      this.options_.control.removeControl(this.colorbar);
      this.options_.control.addControl(this.colorbar);
  }
}


SC6KTileLayerOverlay.prototype.isInView = function (bbox,zoom) {
  var ov = this.getTileLayer();
  if ( ov ) {
    return ov.isInView(bbox,zoom);
  }
  return true;
}

SC6KTileLayerOverlay.prototype.hide = function() { 
  GEvent.trigger(this,'hidden');
  this.colorbar.hide();
  GTileLayerOverlay.prototype.hide.call(this);
}

SC6KTileLayerOverlay.prototype.show = function() { 
  this.colorbar.show();
  GTileLayerOverlay.prototype.show.call(this);
}

SC6KTileLayerOverlay.prototype.setOpacity = function(opacity) { 
  this.opacity = opacity;
  var ov = this.getTileLayer();
  if ( ov ) {
    ov.setOpacity(opacity);
    if ( this.map ) {
      var me = this;
      var mm = GEvent.addListener(this.map,'removeoverlay',function(overlay) {
        if ( overlay == me ) {
          GEvent.removeListener(mm);
          me.map.addOverlay(overlay);
        }  
      });
      this.map.removeOverlay(this);
    }
  }
}

SC6KTileLayerOverlay.prototype.isHidden = function() { 
  if ( ! this.map ) return true;
  return GTileLayerOverlay.prototype.isHidden.call(this);
}

SC6KTileLayerOverlay.prototype.getOpacity = function() { 
  var ov = this.getTileLayer();
  if ( ov ) {
    return ov.getOpacity();
  }
  return this.opacity;
}


// public SC6KTileLayer()
// also supports:
// interface GTileLayer(copy,minZ,maxZ,opts)
function SC6KTileLayer(a,b,c,d) { 
  this.extension = 'gif';
  this.opacity = 1.0;
  this.res = "6km";
  this.loadinterval = "600";
 
  var copy = new GCopyrightCollection("(c) ");
  copy.addCopyright(new GCopyright('Surface Currents (6km)',
    new GLatLngBounds(new GLatLng(-80,-180), new GLatLng(80,180)),
    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);

}

SC6KTileLayer.prototype = new GTileLayer(null,0,18);


SC6KTileLayer.prototype.setLevel = function(l) { 
  return false;
}

SC6KTileLayer.prototype.getBounds = function() { 
  if ( typeof(this.bounds_) == "undefined" )
    this.bounds_ = new GLatLngBounds(new GLatLng(-80,-180),new GLatLng(80,180));  
  return this.bounds_;
}

SC6KTileLayer.prototype.reload = function() { 
  return false;
}


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


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


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

// interface getTileUrl()
SC6KTileLayer.prototype.getTileUrl = function(t,z) { 
  if ( typeof(this.lastload) == "undefined" || (this.lastload - new Date().getTime() > this.loadinterval ) ) {
    this.getTileTimestamp();
  }
  //var url = "http://cordc.ucsd.edu/projects/mapping/maps/tile.php?xt=" +t.x+'&yt='+t.y+'&zt='+z +'&time='+ this.ts+'&avgs=1&res=' + this.res; 
  var url = "http://sandbar.ucsd.edu/rtv-tiles/tile.php?xt=" +t.x+'&yt='+t.y+'&zt='+z+ '&time='+ this.ts+'&avgs=1&res=' + this.res + "&rng=0,50"; 
  return url;
};

SC6KTileLayer.prototype.getTileTimestamp = function() { 
    var url = "http://www.sccoos.org/data/hfrnet/xml/tilesStatus.php";

    var request = GXmlHttp.create();
    request.open("GET",url,false);
    request.send(null);

    if (request.status != 200 ) {
        return false;
    }

    var dom = GXml.parse(request.responseText);

    var tiles = dom.getElementsByTagName("tiles");
    for ( var i = 0; i < tiles.length; i++ ) {
        if ( tiles[i].getAttribute("res") == this.res && tiles[i].getAttribute("pfx") == "a" ) {
            this.ts = tiles[i].getAttribute("time");
            this.lastload = new Date().getTime();
            break;
        }
    }
    return true;
}

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

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

// interface minResolution() 
SC6KTileLayer.prototype.minResolution = function() {
  return 5;
};


// interface maxResolution() 
SC6KTileLayer.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));
};

SC6KTileLayer.prototype.isInView = function (bbox,zoom) {
  var small = zoom >= this.minResolution();
  var large = zoom <= this.maxResolution();
  return small && large && bbox.intersects(this.getBounds());
}

