
/************************************************
    Gmap Tile Overlay


************************************************/
function WNTileOverlay(mapper, settings) {
    this.mapper = mapper;
    this.map = mapper.getMap();
    this.hidden = false;
    var defaults = {
        id:         'WNTileOverlay_' + Math.floor(Math.random()*10000),
        mapElement: '',
        width:      256,
        height:     256,
        url:        '',
        points:     null,
        opacity:    60,
        copyright: '',
        minZoom:    0,
        maxZoom:    22,
        zPriority: 22,
        display: 'show'
    };
    this.options = $.extend({}, defaults, settings);
    this.id = this.options.id;

    var that = this;
    if (this.map) {

        var copyright = new GCopyright(1, new GLatLngBounds(new GLatLng(-90,-180), new GLatLng(90,180)), 0, "copyleft");
        var copyrightCollection = new GCopyrightCollection(this.copyright);
        copyrightCollection.addCopyright(copyright);


        var tilelayer = new GTileLayer(copyrightCollection, this.options.minZoom, this.options.maxZoom, {opacity: this.options.opacity/100, isPng: true});
        tilelayer.isPng = function() {return true;};
        tilelayer.getOpacity = function() { return that.options.opacity/100;};
        tilelayer.getTileUrl = function (a,b) {

        	var lULP = new GPoint(a.x*256,(a.y)*256);
        	var lLRP = new GPoint((a.x+1)*256,(a.y+1)*256);
        	var lUL = G_NORMAL_MAP.getProjection().fromPixelToLatLng(lULP,b);
        	var lLR = G_NORMAL_MAP.getProjection().fromPixelToLatLng(lLRP,b);


        	var url = that.options.url;
        	url = url.replace(/\{Z\}/,b);
        	url = url.replace(/\{X\}/,a.x);
        	url = url.replace(/\{Y\}/,a.y);
        	url = url.replace(/\{LAT1\}/,lUL.y);
        	url = url.replace(/\{LON1\}/,lUL.x);
        	url = url.replace(/\{LAT2\}/,lLR.y);
        	url = url.replace(/\{LON2\}/,lLR.x);
        	url = url.replace(/\{WIDTH\}/,that.options.width);
        	url = url.replace(/\{HEIGHT\}/,that.options.height);

        	return url;
        }


        this.tileLayerOverlay = new GTileLayerOverlay(tilelayer, {zPriority:this.options.zPriority} );
        if (this.options.display != 'hidden') this.map.addOverlay(this.tileLayerOverlay);

    }

}
WNTileOverlay.prototype.hide = function () { if (!this.hidden) {this.hidden = true; this.tileLayerOverlay.hide(); }};
WNTileOverlay.prototype.show = function () { if (this.hidden) {this.hidden = false; this.tileLayerOverlay.show(); }};
WNTileOverlay.prototype.getID = function () { return this.id;};
WNTileOverlay.prototype.setOpacity = function (opacity) {

    if (opacity <= 0) opacity = 0;
    else if (opacity > 100) opacity = 100;
    this.options.opacity = opacity;

    var tileLayer = this.tileLayerOverlay.getTileLayer.opacity = opacity;
    this.map.removeOverlay(this.tileLayerOverlay);
    this.map.addOverlay(this.tileLayerOverlay);
};




/************************************************
    Gmap Image Overlay


************************************************/
function WNImageOverlay(mapper,settings) {
    this.mapper = mapper;
    this.hidden = false;

    var defaults = {
        id:         'WNImageOverlay_' + Math.floor(Math.random()*10000),
        mapElement: '',
        width:      0,
        height:     0,
        url:        '',
        opacity:    100,
        zindex:     0,
        minZoom:    0,
        maxZoom:    22
    };
    this.options = $.extend({}, defaults, settings);
    this.id = this.options.id;

    if (this.mapper) {
        this.reDraw();
        var that = this;
        this.mapper.addEventListener('moveend', function() { that.reDraw();});
    }

};

WNImageOverlay.prototype.getSRC = function () { return this.options.url;};
WNImageOverlay.prototype.getID = function () { return this.id;};
WNImageOverlay.prototype.hide = function () { this.hidden = true; $('#' + this.id).hide()};
WNImageOverlay.prototype.show = function () { this.hidden= false; this.reDraw();$('#' + this.id).show(); };

WNImageOverlay.prototype.setOpacity = function (opacity) {
    if (opacity <= 0) opacity = 0;
    else if (opacity > 100) opacity = 100;
    this.options.opacity = opacity;
    $('#' + this.id).fadeTo('fast', opacity/100);
};

WNImageOverlay.prototype.reDraw = function (bForce) {

    if (bForce || !this.hidden) {
        var url = this.getSRC();

        if (url) {
            var boundingBox = mapstraction.getBounds();
            var northEast = boundingBox.getNorthEast();
            var southWest = boundingBox.getSouthWest();

            var img = new Image;

            var that = this;
            $(img).load(function() {
                $('img').remove('#' + that.id);
                that.mapper.addImageOverlay(that.id,url,that.options.opacity,southWest.lon, southWest.lat, northEast.lon, northEast.lat);
                if (that.options.zindex != 0) {

                    $('#' + that.id).css('z-index', that.options.zindex);
                }
            });

            img.src = url;
        }
    }

};

WNImageOverlay.prototype.getSRC = function () {
    var boundingBox = this.mapper.getBounds();
    var northEast = boundingBox.getNorthEast();
    var southWest = boundingBox.getSouthWest();
    var zoomLevel = this.mapper.getZoom();
    var mapWidth = (this.options.width > 0) ? this.options.width  : $('#' + this.options.mapElement).width();
    var mapHeight =(this.options.height > 0) ?  this.options.height : $('#' + this.options.mapElement).height();


	var url = this.options.url;
	url = url.replace(/\{Z\}/,zoomLevel);
	url = url.replace(/\{NELAT\}/,northEast.lat);
	url = url.replace(/\{NELON\}/,northEast.lon);
	url = url.replace(/\{SWLAT\}/,southWest.lat);
	url = url.replace(/\{SWLON\}/,southWest.lon);
	url = url.replace(/\{WIDTH\}/,mapWidth);
	url = url.replace(/\{WIDTH\}/,that.options.width);
	url = url.replace(/\{HEIGHT\}/,that.options.height);

	return url;
}


