﻿var mzGMapLayer = new Class({

    options: {
        startvisible: false,
        initmarkers: [],
        initkmls: [],
        onLoad: Class.empty
    },

    initialize: function(map, options) {

        this.setOptions(options);

        this.overlays = {};
        this.map = map;

        GEvent.addListener(this.map, "click", function(overlay, latlng, overlaylatlng) {
            if (overlay) {
                for (var lyr in this.overlays) {
                    if (this.overlays[lyr]["ly"] == ((overlay["parentGeoXml"]) ? overlay["parentGeoXml"] : overlay)) this.map.openInfoWindowHtml(overlaylatlng, this.overlays[lyr]["ct"]);
                }
            }
        } .bind(this));

        for (var i = 0; i < this.options.initmarkers.length; i++) {
            this.addMarker(this.options.initmarkers[i].id, this.options.initmarkers[i].lat, this.options.initmarkers[i].lng, this.options.initmarkers[i].opt, this.options.initmarkers[i].contHTML);
        }

        for (var i = 0; i < this.options.initkmls.length; i++) {
            this.addKml(this.options.initkmls[i].id, this.options.initkmls[i].url, this.options.initkmls[i].contHTML);
        }

        this.visible = false;
        if (this.options.startvisible) this.showAll();

    },

    addMarker: function(id, lat, lng, opt, contHTML) {
        this.overlays[id] = new Object({ ly: new GMarker(new google.maps.LatLng(lat, lng), opt), ob: false, ld: true, ct: contHTML });
    },

    addKml: function(id, url, contHTML) {
        this.overlays[id] = new Object({ ly: new GGeoXml(url), ob: false, ld: false, ct: contHTML });
        GEvent.addListener(this.overlays[id]["ly"], "load", function() {
            this.overlays[id]["ld"] = true;
            this.checkLoad();
        } .bind(this));
    },

    removeOverlay: function(id) {
        this.map.removeOverlay(this.overlays[id]["ly"]);
        delete this.overlays[id];
    },

    removeAllOverlays: function() {
        for (var lyr in this.overlays) {
            this.removeOverlay(lyr);
        }
    },

    refreshOverlay: function(id) {
        if (!this.overlays[id]["ob"]) {
            this.map.addOverlay(this.overlays[id]["ly"]);
            this.overlays[id]["ob"] = true;
            this.checkLoad();
        }
    },

    checkLoad: function() {
        for (var lyr in this.overlays) {
            if (this.overlays[lyr]["ob"] == false || this.overlays[lyr]["ld"] == false) return false;
        }
        this.fireEvent('onLoad');
        return true;
    },

    showOverlay: function(id) {
        this.overlays[id]["ly"].show();
        this.refreshOverlay(id);
    },

    hideOverlay: function(id) {
        this.overlays[id]["ly"].hide();
    },

    toggleOverlay: function(id) {
        (this.overlays[id]["ly"].isHidden()) ? this.showOverlay(id) : this.hideOverlay(id);
    },

    showAll: function() {
        for (var lyr in this.overlays) {
            this.showOverlay(lyr);
        }
        this.visible = true;
    },

    hideAll: function() {
        for (var lyr in this.overlays) {
            this.hideOverlay(lyr);
        }
        this.visible = false;
    },

    toggleAll: function() {
        (this.visible) ? this.hideAll() : this.showAll();
    },

    getBounds: function() {
        var bounds = new GLatLngBounds();
        for (var lyr in this.overlays) {
            if (this.overlays[lyr]["ob"]) {
                if (!this.overlays[lyr]["ly"].getTileLayerOverlay) {
                    bounds.extend(this.overlays[lyr]["ly"].getLatLng());
                } else {
                    bounds.extend(this.overlays[lyr]["ly"].getDefaultBounds().getSouthWest());
                    bounds.extend(this.overlays[lyr]["ly"].getDefaultBounds().getNorthEast());
                }
            }
        }
        return bounds;
    }

});

mzGMapLayer.implement(new Events, new Options);

