﻿//ATMO Rhône Alpes - 2009 - Gestion des modules cartographiques - version OPEN LAYERS 0.1
//Pour gérer d'autres type de carto, il faut implémenter les méthodes i* et inclure la bibliotheque, sans changer les vues sur le site
//nécessaire: ATMOJScript, OpenLayers>=2.8, prototype

/*google projection */ Proj4js.defs["SR-ORG:6"] = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs";
/*wgs84*/Proj4js.defs["EPSG:4326"] = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs";
/* UTM31 */Proj4js.defs["UTM31"] = "+proj=utm +zone=31 +ellps=intl +units=m +no_defs";

var worldBounds;
var maxExtent;
var centreParDefaut;
//var regionalBounds = new OpenLayers.Bounds(406000.0, 5450000.0, 800000.0, 5900000.0);

var maxZoom = 16;

initializeGlobalVars = function() {
    worldBounds = new OpenLayers.Bounds(-20037508.34, -20037508.34, 20037508.34, 20037508.34);
    maxExtent = new OpenLayers.Bounds(290000.0, 5400000.0, 850000.0, 6000000.0);
    centreParDefaut = new OpenLayers.LonLat(5.45, 45.3);
}

var iKmlLoader = OpenLayers.Class(OpenLayers.Layer.Vector, {
    initialize: function(maCarte, titre, adresseKML) { OpenLayers.Layer.Vector.prototype.initialize.apply(this, [titre]); this.url = adresseKML; this._maCarte = maCarte; this._content = new Element("DIV", { 'id': 'kmlLoader' }); var loader = new iLoadingElement(this, this.url); },
    loadComplete: function(transport) {
        try {
            var features = new OpenLayers.Format.KML({ extractStyles: true, extractAttributes: true, 'internalProjection': this._maCarte._map.baseLayer.projection, 'externalProjection': new OpenLayers.Projection("EPSG:4326") }).read(transport.responseText);
            if (features) { if (features.constructor != Array) { features = [features]; }; this.addFeatures(features); } else { this.loadFailed(); } this._maCarte._map.addLayers([this]); features = null;
            try { this._dateDonnees = transport.responseText.split("<TimeStamp>")[1]; this._dateDonnees = this._dateDonnees.split("</TimeStamp>")[0]; } catch (e) { } this._maCarte.loadEnd(this);
        } catch (e) { this.loadFailed(); }
    },
    getAjaxDestination: function() { return this._content; },
    loadFailed: function() { this._maCarte.loadFailed(this); }
});
pChargerImage = function(maCarte, adresseImage, titre, size, bounds, setBaseLayer, numZoomLevels) {
    try {
        if (size == undefined) { size = maCarte._map.size; }; if (bounds == undefined) { bounds = maCarte.getBounds(); }
        if (numZoomLevels == undefined) { numZoomLevels = 12 };
        var monLayer = new OpenLayers.Layer.Image(titre, adresseImage, bounds, size, { 'numZoomLevels': numZoomLevels, 'isBaseLayer': setBaseLayer });
        maCarte._map.addLayers([monLayer]); return monLayer;
    } catch (e) { }
}
pChargerOSM = function(maCarte, adresse, titre) { var monLayer = new OpenLayers.Layer.OSM(titre, adresse); monLayer.events.register("loadend", monLayer, function(e) { maCarte.loadEnd(e.object); }); maCarte._map.addLayers([monLayer]); return monLayer;}
pChargerGoogle = function(maCarte) { var monLayer = new OpenLayers.Layer.Google("Google", { type: G_PHYSICAL_MAP, sphericalMercator: true }); maCarte._map.addLayers([monLayer]); return monLayer; }
clickOnLayerButton = function(monEvent) {
try {
    if (monEvent.element().layer != null) {
        monEvent.element().layer.select()
    } else {
        monEvent.element().up().layer.select();
    }
} catch (e) {
alert(e.message);
} 
             }
featureSelect = function(feature) { feature.layer.baseLayer.onFeatureClick(feature); }
featureOut = function(feature) { feature.style.cursor = 'default'; feature.layer.baseLayer.onFeatureOut(feature); }
featureOver = function(feature) { feature.style.cursor = 'pointer'; feature.layer.baseLayer.onFeatureOver(feature); }
tuileVide = function(bounds) { return cheminSite + "App_themes/shared/Images/emptclickFeature: featureSelect,y.gif"; }


//Module général de carte définissant les différentes parties de la carto : boutons, controler, layers, carte...
var iMapModule = Class.create({
    initialize: function(mapZone, myLayerSwitcher, myControler, myMapDiv, defaultBackground, onLoadedAction, onZoomChange, activeZoom, locateCommuneDiv, defaultExtent, autoAdjustOnZoom) {
        try {
            if (worldBounds == null) { initializeGlobalVars(); }
            (activeZoom == undefined || activeZoom == null) ? activeZoom = false : void (0); (defaultExtent == null) ? this._defaultExtent = maxExtent : this._defaultExtent = defaultExtent; (autoAdjustOnZoom == null) ? this._autoAdjustOnZoom = true : this._autoAdjustOnZoom = autoAdjustOnZoom;
            this._mapZone = mapZone; this._layerSwitcher = myLayerSwitcher; if (this._layerSwitcher != null) { this._layerSwitcher.setMapModule(this); }; this._controlerZone = myControler; this._currentLayer = null; this._mapElement = myMapDiv; this._onZoomChange = onZoomChange;
            this._layerToTell = []; this._loadingLayer = 0; this._onLoadedAction = onLoadedAction;
            this._loadingDiv = new Element("DIV", { 'id': "loading" + this.id() }); this._loadingDiv.setStyle({ 'zIndex': 20000, 'background': '#FFFFFF', 'position': 'absolute' }); this._loadingDiv.innerHTML = "Chargement..."; this._loadingDiv.setOpacity(0.5); this._loadingDiv.hide(); this._loadingDiv.style.x = this._loadingDiv.style.x + 20;
            this._mapZone.appendChild(this._loadingDiv); this._ready = false; this._couchesACharger = [];
            if (locateCommuneDiv != null && locateCommuneDiv != undefined) { this._monCommuneLocator = new iZoneBrowser(locateCommuneDiv, this, new Array('Annecy', 'Bourg-en-Bresse', 'Chambéry', 'Grenoble', 'Lyon', 'Saint-Etienne', 'Valence'), "commune"); }
            this._options = { projection: new OpenLayers.Projection('SR-ORG:6'), numZoomLevels: maxZoom, displayProjection: new OpenLayers.Projection('EPSG:4326'), units: "m", maxExtent: this._defaultExtent, controls: [] };
            this._markers = new OpenLayers.Layer.Markers("Markers");
            if (this.initMap(defaultBackground, activeZoom)) { this._map.addLayer(this._markers); this._mapZone.observe("mouseover", beginSavingMousePosition); this._mapZone.observe("mouseout", endSavingMousePosition); this.isReady(); } else { this.info("Erreur de chargement de la carte, veuillez nous excuser", this._mapElement); }
            try { Element.clonePosition(this._loadingDiv, this._mapElement); } catch (e) { }; this._geoLocator = new iGeolocator(this); this._tips = new Array(); this._infos = new Array();
        } catch (e) { alert("Erreur de creation dans mapModule: " + e.message); }
    },
    initMap: function(myDefaultBackground, activeZoom) {
        var i = 0;
        try {
            this._map = new OpenLayers.Map(this._mapElement.id, this._options); i++; this._map.atmoMap = this; i++;
            this._dragFeature = new OpenLayers.Control.DragPan(); i++; this._dragFeature.deactivate(); i++; this._map.addControl(this._dragFeature); i++;
            this._zoomControl = new OpenLayers.Control.ZoomPanel(); i++; if (activeZoom) { this._map.addControl(this._zoomControl); } i++;
            //this._map.addControl(new OpenLayers.Control.LayerSwitcher());
            //this._map.addControl(new OpenLayers.Control.MousePosition());
            try { this._emptyBackground = pChargerImage(this, tuileVide(), "fond_blanc", new OpenLayers.Size(this._mapElement.style.width, this._mapElement.style.height), worldBounds, true); i++; } catch (e) { }
            this._map.events.on({ "click": function(e) { try { e.srcElement.map.atmoMap.clicOnMap(e.xy); } catch (e) { } }, "zoomend": function(e) { try { e.object.atmoMap.zoomChanged(e.object.getZoom()); } catch (e) { } } }); i++;
            if (!(myDefaultBackground == null)) { myDefaultBackground.parent = this; this.switchBackground(myDefaultBackground); }
            if (this._map.scales == null || this._map.scales == undefined) { this._map.setCenter(centreParDefaut.transform(new OpenLayers.Projection('EPSG:4326'), this._map.projection)); } else { this._map.setCenter(new OpenLayers.LonLat(5.45, 45.3).transform(new OpenLayers.Projection('EPSG:4326'), this._map.projection), 7); } i++;
            return true;
        } catch (e) { alert("Erreur init map ligne " + i + " : " + e.message); return false; }
    },
    getBounds: function() { return this._defaultExtent; },
    setLayerSwitcher: function(myLayerSwitcher) { this._layerSwitcher = myLayerSwitcher; },
    getLayerSwitcher: function() { return this._layerSwitcher; },
    getMap: function() { return this._map; },
    id: function() { return this._mapZone.id; },
    addLayer: function(monLayer, autoLoad, addToSwitcher) {
        if (addToSwitcher == null) { addToSwitcher = true; };
        if (autoLoad == null) { autoLoad = false }; monLayer.setParent(this);
        if (this._layerSwitcher != null && addToSwitcher) { this._layerSwitcher.addLayer(monLayer); Element.clonePosition(this._loadingDiv, this._mapElement); };
        if (autoLoad) { monLayer.select(); }
    },
    addLayers: function(layers, loadThisOne) { if (loadThisOne == null || loadThisOne == undefined) { loadThisOne = -1; } for (var i = 0; i < layers.length; i++) { this.addLayer(layers[i], (i == loadThisOne), false) }; },
    setContent: function(monContent) {
        try { if (this._currentLayer != null) { this._map.removeLayer(this._currentLayer._OLLayer); } } catch (e) { }
        if (this._ready) { this.load(); this._currentLoadingContent = monContent; this.ajouterCouche(monContent); } else { this._couchesACharger.push(monContent); }
    },
    contentLoaded: function(monLayer) {
        if (this._currentLoadingContent.getAdresse() == monLayer.url) {
            this._currentLoadingContent.setOLLayer(monLayer);
            try { if (this._currentLoadingContent.getBounds() != null && (this._currentLayer == null || this._currentLoadingContent._controlerAdress != this._currentLayer._controlerAdress) && (this._map.getExtent() == null || !this._map.getExtent().equals(this._currentLoadingContent.getBounds()))) { this._map.zoomToExtent(this._currentLoadingContent.getBounds(), this._autoAdjustOnZoom); } } catch (e) { }
            this._currentLayer = this._currentLoadingContent; this._currentLoadingContent = null;
        }
    },
    tellMeWhenClicked: function(monLayer) { this._layerToTell.push(monLayer); },
    clicOnMap: function(e) {
        this._currentClicEvent = e;
        if (this._layerToTell != null && this._layerToTell.length > 0) {
            for (var i = 0; i < this._layerToTell.length; i++) {
                this._layerToTell[i].actionWhenClicked(this._map.getLonLatFromViewPortPx(new OpenLayers.Pixel(e.x, e.y)).transform(this._map.projection, this._map.displayProjection));
            };
        }
        this._currentClicEvent = null;
    },
    cancelCurrentLoading: function() { this._loadingLayer = 0; this._loadingDiv.hide(); this._currentLoadingContent = null; },
    newLayerSelected: function() { this.cancelCurrentLoading(); if (this._controlerZone != null) { this._controlerZone.innerHTML = ""; }; this.clearAllTipMessage(); this.clearAllInfoMessage(); },
    ajouterCouche: function(monLayer, tries) {
        var maCouche; if (tries == null || tries == undefined) { tries = 0; }; if (tries > 3) { return null; }
        try {
            switch (monLayer.getType().toLowerCase()) {
                case "kml": maCouche = new iKmlLoader(this, monLayer.getTitre(), monLayer.getAdresse()); break;
                case "image": maCouche = pChargerImage(this, monLayer.getAdresse(), monLayer.getTitre(), monLayer.getSize(), monLayer.getBounds(), (monLayer.isBaseLayer != undefined && monLayer.isBaseLayer()), monLayer._numZoomLevels); if (maCouche != null) { maCouche.baseLayer = monLayer; this.loadEnd(maCouche); } else { this._couchesACharger.push(monLayer); }; return maCouche; break;
                case "osm": maCouche = pChargerOSM(this, monLayer.getAdresse(), monLayer.getTitre()); break;
                case "google": maCouche = pChargerGoogle(this); this.loadEnd(maCouche); break;
            }
        } catch (e) { maCouche = null; };
        if (maCouche == null) { tries++; return this.ajouterCouche(monLayer, tries); } else { return maCouche; }
    },
    load: function() { try { this._loadingDiv.style.display = "inline"; this._loadingDiv.height = this._mapElement.style.height; this._loadingLayer++; try { Element.clonePosition(this._loadingDiv, this._mapElement); } catch (e) { } } catch (e) { alert("Erreur map.load " + e.message); } },
    loadEnd: function(monLayer) { if (this._currentLoadingContent != null) { this.contentLoaded(monLayer); }; this._loadingLayer--; if (this._loadingLayer == 0) { this._loadingDiv.hide(); this._loadingDiv.style.height = 0; try { monLayer.baseLayer.isLoaded(); } catch (e) { } } },
    loadFailed: function(monLayer) { if (this._currentLoadingContent != null) { this.cancelCurrentLoading(); this.info("Erreur lors du chargement, veuillez nous excuser", this._loadingDiv); } },
    chargerCouches: function() { if (this._couchesACharger.length > 0) { this._couchesACharger.shift().select(); } },
    isReady: function() { this._ready = true; if (this._pageReady) { this.chargerCouches(); if (this._onLoadedAction != null) { if (Object.isString(this._onLoadedAction)) { eval(this._onLoadedAction); } else { this._onLoadedAction(); } } } },
    pageReady: function() { this._pageReady = true; this.isReady(); },
    getCenter: function(destProjection) {
        var p = new OpenLayers.LonLat(this._map.getCenter().lon, this._map.getCenter().lat);
        if (destProjection != null) { p.transform(this._map.projection, destProjection); }
        return new Array(p.lat, p.lon);
    },
    getWidthInKm: function() {
        Proj4js.defs["EPSG:23031"] = "+proj=utm +zone=31 +ellps=intl +units=m +no_defs ";
        var bottomLeftCorner = new OpenLayers.LonLat(this._map.getExtent().left, this._map.getExtent().bottom);
        bottomLeftCorner.transform(this._map.projection, new Proj4js.Proj('EPSG:23031'));
        var topRightCorner = new OpenLayers.LonLat(this._map.getExtent().right, this._map.getExtent().top);
        topRightCorner.transform(this._map.projection, new Proj4js.Proj('EPSG:23031'));
        return (topRightCorner.lon - bottomLeftCorner.lon) / 1000;
    },
    zoomChanged: function(zoomLevel) { if (zoomLevel >= 9) { try { this._dragFeature.activate(); } catch (e) { this._dragFeature.deactivate(); } } else { this._dragFeature.deactivate(); }; try { if (this._onZoomChange != null) { window[this._onZoomChange](zoomLevel); }; this._currentLayer._OLLayer.zoomChanged() } catch (e) { } },
    switchBackground: function(myNewBackground) {
        try {
            if (myNewBackground == null) { this._map.setBaseLayer(this._map.layers[0]); }
            else if (this._map.baseLayer == null || this._map.baseLayer.baseLayer == null || this._map.baseLayer.baseLayer.getAdresse() != myNewBackground.getAdresse()) {
                var dejaCharge = this.layerDejaCharge(myNewBackground); if (dejaCharge == null) { this.load(); dejaCharge = this.ajouterCouche(myNewBackground); myNewBackground.setOLLayer(dejaCharge); };
                if (myNewBackground.getType().toLowerCase() == "kml") { this._map.setBaseLayer(this._emptyBackground); this._emptyBackground.baseLayer = dejaCharge.baseLayer; dejaCharge.display(true); }
                else { this._map.setBaseLayer(dejaCharge); try { this._emptyBackground.baseLayer.display(false); } catch (e) { } }
                if (myNewBackground.getBounds() != null && (this._map.getExtent() == null || !this._map.getExtent().equals(myNewBackground.getBounds()))) { this._map.zoomToExtent(myNewBackground.getBounds(), this._autoAdjustOnZoom); }
            }
        } catch (e) { alert("erreur map.switchBackground " + e.message); }
    },
    layerDejaCharge: function(monLayer) { for (var i = 0; i < this._map.layers.length; i++) { if (this._map.layers[i].baseLayer != undefined && this._map.layers[i].baseLayer != null && this._map.layers[i].baseLayer.getAdresse() == monLayer.getAdresse()) { return this._map.layers[i]; } }; return null; },
    clearAllTipMessage: function() { while (this._tips.length > 0) { try { this._tips.pop().supprimer(); } catch (e) { } } },
    clearAllInfoMessage: function() { while (this._infos.length > 0) { try { this._infos.pop().supprimer(); } catch (e) { } } },
    tip: function(content, allowMultipleMessage, parentElement) {
        this.clearAllInfoMessage(); if (allowMultipleMessage == null || allowMultipleMessage == undefined) { allowMultipleMessage = false; }; try { if (!allowMultipleMessage) { this.clearAllTipMessage(); }; if (parentElement == null) { parentElement = this._mapElement; }; this._tips.push(new tip(content, parentElement, { 'setPosition': 'left', 'width': '400px', 'event': this._currentClicEvent })); } catch (e) { alert("erreur mapModule.tip " + e.message); }
    },
    info: function(content, parentElement) { try { this.clearAllInfoMessage(); if (content != null) { this._infos.push(new tip(null, parentElement, { 'defaultText': content, 'setPosition': 'left', 'width': '150px' })); } } catch (e) { } },
    setPosition: function(element, parentElement) { element.absolutize(); element.setStyle({ left: mouseXPosition + "px", top: mouseYPosition + "px" }) },
    locate: function(point, typeLieu, projection, addMaker) {
        if (addMaker == null) { addMaker = false; }; var monPoint = new OpenLayers.LonLat(point[0], point[1]); if (projection != null) { monPoint = monPoint.transform(new OpenLayers.Projection(projection), this._map.projection); } this._map.setCenter(monPoint);
        switch (typeLieu) { case "departement": this._map.zoomTo(9); break; case "commune": this._map.zoomTo(11); break; case "site": this._map.zoomTo(11); break; case "region": this._map.zoomTo(7); break; default: this._map.zoomTo(7); break; }
        if (addMaker) { this._markers.clearMarkers(); this._markers.addMarker(new OpenLayers.Marker(monPoint, null)); }
    },
    geolocaliser: function(lieu) { this._geoLocator.search(lieu); }
});

//conteneur de layers
var iMapLayerSwitcher = Class.create({
    initialize: function(element, typeSwitcher){try { this._element = element; this._typeSwitcher = typeSwitcher; this._layers = []; this._parent = null; } catch (e) { alert("Erreur de creation dans mapLayerSwitcher: " + e.message); }},
    setMapModule: function(myMapModule) { try { this._parent = myMapModule; } catch (e) { } },
    addLayer: function(myLayer) {
        try {
            this._layers.push(myLayer);
            switch (this._typeSwitcher) {
                case "radioSelect": this._element.appendChild(this.createRadioElement(myLayer)); break;
                case "linkSelect": this._element.appendChild(this.createLinkElement(myLayer)); break;
            };
        } catch (e) { alert("Erreur iMapLayerSwitcher.addlayer " + e.message); }
    },
    createLinkElement: function(myLayer) {
        var nouvelElement = document.createElement("A"); nouvelElement.id = "b" + myLayer.id(); nouvelElement.href = "javascript:void(0)";
        nouvelElement.innerHTML = myLayer.getTitre(); myLayer.setButton(nouvelElement); return myLayer.getButton();
    },
    createRadioElement: function(myLayer) { }
});

//layer de type controleur : pas de contenu direct, mais permet de faire changer les dates par exemple, etc..
var iMapControler = Class.create({
    id: function() { return this._id; },
    getType: function() { return this._contentType; },
    //normalement appelé sur clic d'un bouton dans le controler
    setContent: function(myLayer) {
        //on s'attend à ce que le bouton ait une action qui renvoi le contenu à charger
        myLayer.setBaseLayer(this);
        this._contentAdress = myLayer.getAdresse();
        this._actionWhenLoaded = myLayer.getActionWhenLoaded();
        this._actionWhenClicked = myLayer.getActionWhenClicked();
        this._contentType = myLayer.getType();
        this._size = myLayer.getSize();
        this._bounds = myLayer.getBounds();
        this._parent.setContent(this);
    },
    getBounds: function() { if (this._bounds == null) { return this._parent.getBounds(); } else { return this._bounds; } },
    actionWhenClicked: function(coords) {
        if (this._actionWhenClicked != null) {
            this._parent.tip(window[this._actionWhenClicked](this).replace("[%y%]", coords.lat).replace("[%x%]", coords.lon))
        }
    },
    getSize: function() { return this._size; },
    setOLLayer: function(monLayer) {
        this._OLLayer = monLayer;
        this._OLLayer.baseLayer = this;
        if (this._OLLayer != null) {
            try {
                switch (this._contentType.toLowerCase()) {
                    case "kml": var selectControl = new OpenLayers.Control.SelectFeature(this._OLLayer, { clickFeature: featureSelect, overFeature: featureOver, outFeature: featureOut, hover: false }); this._parent._map.addControl(selectControl); selectControl.activate(); break;
                    case "image": this._parent.tellMeWhenClicked(this); break;
                    default: void (0); break;
                }
            } catch (e) { alert("Erreur lors de l'ajout du selectControl sur " + this._titre); }
        }
    },
    newLayerSelected: function() { },
    onFeatureClick: function(feature) { this._parent.tip(feature.fid, false, $(feature.geometry.id)); },
    onFeatureOver: function(feature) { this._parent.info(feature.data.name, $(feature.geometry.id)); },
    onFeatureOut: function(feature) { this._parent.info(null); },
    getAdresse: function() { return this._contentAdress; },
    getActionWhenLoaded: function() { return null; },
    isLoaded: function() { if (this._actionWhenLoaded != undefined && this._actionWhenLoaded != null) { window[this._actionWhenLoaded](); } },
    getTitre: function() { return this._titre; },
    setButton: function(myElement) { this._controlButton = myElement; this._controlButton.layer = this; this._controlButton.observe("click", clickOnLayerButton); },
    getButton: function() { return this._controlButton; },
    setElement: function(myElement) { this._element = myElement; },
    setParent: function(parent) { this._parent = parent; this._element = parent._controlerZone; },
    addLayer: function(myElement, load) { myElement.setParent(this); if (load) { myElement.select(); } },
    tip: function(content, multipleDisplay, parentElement) { this._parent.tip(content, multipleDisplay, parentElement); },
    info: function(content, parentElement) { this._parent.info(content, parentElement); },
    zoomChanged: function() { void (0); },
    select: function() {
        this._element.mapControler = this; this._parent.newLayerSelected(); load(this._element.id);
        new Ajax.Updater(this._element.id, this._controlerAdress,
            {
                method: 'get',
                evalScripts: true,
                onComplete: function(transport) {
                    if (transport.status == 200) { }
                },
                onFailure: function() { }
            });
        if (!(this._background == null)) { this._parent.switchBackground(this._background); }
    },
    initialize: function(titre, adresse, id, button, background, selectionneParDefaut, numzoomlevels) {
        this._element = null;
        this._controlerAdress = adresse;
        this._controlerZone = null;
        this._parent = null;
        this._controlButton = null;
        this._id = id;
        this._titre = titre;
        this._contentAdress = null;
        this._contentType = null;
        if (button != null && button != undefined) {
            this.setButton(button);
        };
        this._background = background;
        this._size = null;
        this._bounds = null; this._numZoomLevels = numzoomlevels
    }
});

var baseLayer = Class.create({
initialize: function(titre, type, adresse, actionWhenLoaded, actionWhenClicked, size, bounds, setAsBaseLayer, scales, numzoomlevels) { this._titre = titre; this._type = type; this._adresse = adresse; this._actionWhenLoaded = actionWhenLoaded; try { this._bounds = new OpenLayers.Bounds(bounds[0], bounds[1], bounds[2], bounds[3]) } catch (e) { }; try { this._size = new OpenLayers.Size(size[0], size[1]) } catch (e) { }; this._baseLayer = null; this._actionWhenClicked = actionWhenClicked; if (this._actionWhenClicked != null) { this._clicSensible = true; }; this._isBaseLayer = setAsBaseLayer; this._scales = scales; this._parent = null; this._OLLayer = null; this._numZoomLevels = numzoomlevels },
    getActionWhenLoaded: function() { return this._actionWhenLoaded; },
    getAdresse: function() { if (Object.isFunction(this._adresse)) { return this._adresse(); } else { return this._adresse; } },
    getType: function() { return this._type; },
    getTitre: function() { return this._titre; },
    getBounds: function() { return this._bounds; },
    getSize: function() { if (this._size == null) { return new OpenLayers.Size(245, 290); } else { return this._size; } },
    getScales: function() { return this._scales; },
    setBaseLayer: function(myBaseLayer) { this.baseLayer = baseLayer; },
    getActionWhenClicked: function() { return this._actionWhenClicked; },
    actionWhenClicked: function(coords) {
        if (this._actionWhenClicked != null) {
            this._parent.tip(window[this._actionWhenClicked](this).replace("[%y%]", coords.lat).replace("[%x%]", coords.lon))
        }
    },
    isBaseLayer: function() { return this._isBaseLayer; },
    isLoaded: function() { if (this._actionWhenLoaded != undefined && this._actionWhenLoaded != null) { window[this._actionWhenLoaded](); } },
    setParent: function(parent) { this._parent = parent; },
    select: function() { this._parent.newLayerSelected(); this._parent.setContent(this); },
    display: function(visible) { this._OLLayer.setVisibility(visible); },
    setOLLayer: function(monLayer) {
        this._OLLayer = monLayer;
        this._OLLayer.baseLayer = this;
        if (this._OLLayer != null) {
            try {
                if (this._clicSensible || this._overSensible) {
                    switch (this._type.toLowerCase()) {
                        case "kml": var selectControl = new OpenLayers.Control.SelectFeature(this._OLLayer, { clickFeature: featureSelect, overFeature: featureOver, outFeature: featureOut, hover: false }); this._parent._map.addControl(selectControl); selectControl.activate(); break;
                        case "image": this._parent.tellMeWhenClicked(this); break;
                        default: void (0); break;
                    }
                }
            } catch (e) { alert("Erreur lors de l'ajout du selectControl sur " + this._titre); }
        }
    },
    setAdresse: function(adresse) { this._adresse = adresse; },
    update: function(adresse, type) { this.setAdresse(adresse); this.select(); }
});

var iMapButtonLayer = Class.create(baseLayer, {
    getAdresse: function() { try { if (this._adresse != null && this._adresse != undefined) { return this._adresse } else { return window[this.getAction()](this); } } catch (e) { } },
    id: function() { return this._id; },
    getButton: function() { return this._element; },
    setButton: function(myElement) { this._element = myElement; this._element.layer = this; switch (this._element.nodeName.toLowerCase()) { case "input": this._element.observe("click", this.buttonClicked.bindAsEventListener(this)); break; case "a": this._element.observe("click", this.buttonClicked.bindAsEventListener(this)); break; case "select": this._element.observe("change", this.buttonClicked.bindAsEventListener(this)); break; } },
    getAction: function() { return this._action; },
    getBounds: function() { if (this.bounds == null) { return this._parent.getBounds(); } else { return this._bounds } },
    onFeatureClick: function(feature) { this._parent.tip(feature.fid, this._allowMultipleSelect, $(feature.geometry.id)); },
    onFeatureOver: function(feature) { this._parent.info(feature.data.name, $(feature.geometry.id)); },
    onFeatureOut: function(feature) { this._parent.info(null); },
    initialize: function($super, titre, type, adresse, monElement, clicSensible, overSensible, action, actionWhenLoaded, actionWhenClicked, size, bounds, allowMultipleSelect) {
        try {
            //(titre, type, adresse, actionWhenLoaded, actionWhenClicked, size, bounds, setAsBaseLayer, scales) 
            $super(titre, type, adresse, actionWhenLoaded, actionWhenClicked, size, bounds, false, null);
            this._clicSensible = clicSensible; this._overSensible = overSensible; this._action = action; this._id = null; this._titre = titre; this._adresse = adresse; this._parent = null; this._layerSwitcher = null; try { this._bounds = new OpenLayers.Bounds(bounds[0], bounds[1], bounds[2], bounds[3]) } catch (e) { }; try { this._size = new OpenLayer.Size(size[0], size[1]) } catch (e) { }; this._actionWhenClicked = actionWhenClicked; (allowMultipleSelect != null) ? this._allowMultipleSelect = allowMultipleSelect : this._allowMultipleSelect = false;
            try { monElement.toLowerCase(); this._id = monElement; } catch (e) { this.setButton(monElement); this._id = monElement.id; }
            this._type = type;
        } catch (e) { "erreur creation iMapButtonLayer " + alert(e.message); }
    },
    buttonClicked: function(e) {
        try {
            if (Event.element(e).layer != null) {
                if (this._action != null) {
                    if (Object.isFunction(this._action)) {
                        this.update(this._action());
                    } else {
                        this.update(window[this._action]());
                    }
                } else { Event.element(e).layer.select() }
            } else {
                Event.element(e).up().layer.select();
            }
        } catch (e) { }
    }
});

