﻿


var GMapCustomMethods = Class.create();

GMapCustomMethods.prototype =
            {
                initialize: function() {
                    this.NoLayerAtThatResolutionImage = "../images/map/toofar.en.png";
                    this.OutsideBounds = "../images/map/outside.png";
                    this.MaximumZoomLevel = 10;
                    this.msg_copyright = "©2009 geomatic.dk",
                    this.VARIABLE_XML_SERVER_URL = xmlServerUrl;
                    this.MAP_SERVER_URL = mapServerUrl;
                },

                getCustomLayer: function(variable, opacity, tile) {
                    var self = this;
                    var name = "";
                    if (variable != null)
                        name = variable.name;


                    var copyCollection = new GCopyrightCollection(name);
                    copyCollection.addCopyright(new GCopyright(1, new GLatLngBounds(new GLatLng(-90, -180), new GLatLng(90, 180)), 0, this.msg_copyright));
                    var tilelayers = new Array();

                    var index = 0;
                    tilelayers[index] = tile[0];
                    index++;

                    // no variable, return just the ground layer.
                    if (variable == null)
                        return tilelayers;

                    tilelayers[index] = new GTileLayer(copyCollection, 1, 18);
                    tilelayers[index].getTileUrl = function(a, b) { return self.getTileUrl(a, b, variable); };
                    tilelayers[index].getOpacity = opacity;

                    if (tile.length > 1) {
                        index++;
                        tilelayers[index] = tile[1];
                    }


                    return tilelayers;
                },

                getTileUrl: function(a, b, variable) {


                    var point1 = new GPoint(a.x * 256, (a.y + 1) * 256);
                    var point2 = new GPoint((a.x + 1) * 256, (a.y) * 256);
                    var glonglat1 = G_NORMAL_MAP.getProjection().fromPixelToLatLng(point1, b);
                    var glonglat2 = G_NORMAL_MAP.getProjection().fromPixelToLatLng(point2, b);
                    var coords1 = this.forwardMercator(glonglat1.lng(), glonglat1.lat());
                    var coords2 = this.forwardMercator(glonglat2.lng(), glonglat2.lat());
                    var params = new Array();


                    if (!GeographicTransformation.isSquareWithinDK(glonglat1, glonglat2))
                        return this.OutsideBounds;

                    if (b < this.MaximumZoomLevel) {
                        return this.NoLayerAtThatResolutionImage;
                    }



                    // cache it, its the same everytime...
                    var baseUrl = "SRS=EPSG%3A900913&FORMAT=image%2Fpng&TILED=true&TRANSPARENT=true&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&EXCEPTIONS=application%2Fvnd.ogc.se_inimage&WIDTH=256&HEIGHT=256&";

                    //params.push(["SRS", "EPSG%3A900913"]);
                    //params.push(["FORMAT", "image%2Fpng"]);
                    //params.push(["TILED", "true"]);
                    //params.push(["TRANSPARENT", "true"]);
                    //params.push(["SERVICE", "WMS"]);
                    //params.push(["VERSION", "1.1.1"]);
                    //params.push(["REQUEST", "GetMap"]);
                    //params.push(["EXCEPTIONS", "application%2Fvnd.ogc.se_inimage"]);
                    params.push(["BBOX", coords1.lon + "," + (coords1.lat) + "," + coords2.lon + "," + (coords2.lat)]);
                    //params.push(["WIDTH", 256]);
                    //params.push(["HEIGHT", 256]);

                    var xmlUrl = (this.VARIABLE_XML_SERVER_URL + "varId=" + variable.id);

                    // ouch. to change.
                    var legendtbl = $('legend_id_' + variable.id);

                    if (legendtbl != null && legendtbl.ids != null && legendtbl.ids.length > 0)
                        xmlUrl += "%26vals=" + legendtbl.ids;

                    params.push(["SLD", xmlUrl]);

                    var url = this.MAP_SERVER_URL + baseUrl;

                    for (var i = 0; i < params.length; i++)
                        url += params[i][0] + "=" + params[i][1] + "&";
                    return url;

                },

                forwardMercator: function(lon, lat) {
                    var x = lon * 20037508.34 / 180;
                    var y = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180);
                    y = y * 20037508.34 / 180;
                    return { "lon": Math.floor(x), "lat": Math.floor(y) };
                }

            }











// map close

function Custom_MapClose() { }
Custom_MapClose.prototype.selectable = Custom_MapClose.prototype.printable = Custom_MapClose.prototype.allowSetVisibility = function(o) { return false; }
Custom_MapClose.prototype.getDefaultPosition = function() { return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7)); }
Custom_MapClose.prototype.initialize = function(map) {

    var container = GetDiv(map.getContainer().id + "_closecnt");

    var btn = GetButton(container.id + "_close", "X");
    btn.className = "close";

    container.appendChild(btn);
    map.getContainer().appendChild(container);
    return container;
}





// the select box to change map overlay

function Custom_OverlaySelector() { }
Custom_OverlaySelector.prototype.selectable = Custom_OverlaySelector.prototype.printable = Custom_OverlaySelector.prototype.allowSetVisibility = function(o) { return false; }
Custom_OverlaySelector.prototype.getDefaultPosition = function() { return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7)); }
Custom_OverlaySelector.prototype.initialize = function(map) {

    var container = GetDiv(map.getContainer().id + "_cnt");
    var cnt = GetDiv(map.getContainer().id + "_cntcomplete");
    cnt.className = "legend_container";


    var header = GetDiv(map.getContainer().id + "_header");
    var body = GetDiv(map.getContainer().id + "_body");
    body.className = "window_body";

    header.className = "window_bar";


    var select = GetSelect(container.id + "_select");
    var btnmin = GetButton(container.id + "_minmax", "_");
    btnmin.className = "min_max";



    header.appendChild(btnmin);
    cnt.appendChild(header);
    cnt.appendChild(body);


    var legend = GetDiv(map.getContainer().id + "_legend");
    legend.className = "legend";

    body.appendChild(select);
    body.appendChild(legend);


    // open/close
    Event.observe(btnmin, 'click', function(e) {
        var bd = $(map.getContainer().id + "_legend");
        var o = Event.element(e);
        if (bd.style.display == 'none') {
            bd.style.display = 'block';
            o.removeClassName('minimised');
            o.addClassName('maximised');
            o.value = '_';
        } else {
            bd.style.display = 'none';
            o.value = '[]';
            o.removeClassName('maximised');
            o.addClassName('minimised');
        }

    });

    container.appendChild(cnt);

    map.getContainer().appendChild(container);

    return container;
}



function GetDiv(id) {
    var div = document.createElement("div");
    div.id = id;
    return div;
}

function GetSelect(id) {
    var select = document.createElement("select");
    select.id = id;
    return select;
}

function GetButton(id, value) {
    var btn = document.createElement("input");
    btn.type = "button";
    btn.id = id;
    btn.value = value;
    return btn;
}


function getFirstWithClass(className, obj) {
    if (!obj) obj = document;

    var eles = $(obj).getElementsByClassName(className);

    if (eles != null && eles.length > 0)
        return eles[0];
    return null;
}
