

function onLoad(context) {
    if (context >= 2 && context <= 5) {
        if (GBrowserIsCompatible()) {
            __php_showTownMap();
        }
    }
}

function onUnload(context) {
    if (context >= 2 && context <= 5) {
        if (GBrowserIsCompatible()) {
            GUnload();
        }
    }
}


//
// townMap
//

var townMap = {
    preferences : {
        containerId             : "map",
        imageAccommodation      : "http://www.hotels-in-sofia.com/images/general/marker_accommodation_16px.png",
        imageSpot               : "http://www.hotels-in-sofia.com/images/general/marker_spot_16px.png",
        iconWidth               : 16,
        iconHeight              : 16,
        iconAnchorX             : 8,
        iconAnchorY             : 8,
        infoWindowAnchorX       : 8,
        infoWindowAnchorY       : 8
    },

    MARKER_ACCOMMODATION        : 0,
    MARKER_SPOT                 : 1,

    CENTER_CUSTOM               : 0,
    CENTER_MARKER_ACCOMMODATION : 1,
    CENTER_MARKER_SPOT          : 2,
    CENTER_MARKER_ALL           : 3,

    initialize : function() {
        this.markerAccommodationIcon = new GIcon();
        this.markerAccommodationIcon.image = this.preferences.imageAccommodation;
        this.markerAccommodationIcon.iconSize = new GSize(this.preferences.iconWidth, this.preferences.iconHeight);
        this.markerAccommodationIcon.iconAnchor = new GPoint(this.preferences.iconAnchorX, this.preferences.iconAnchorY);
        this.markerAccommodationIcon.infoWindowAnchor = new GPoint(this.preferences.infoWindowAnchorX, this.preferences.iconAnchorY);
        this.markerAccommodationOptions = { icon : this.markerAccommodationIcon };

        this.markerSpotIcon = new GIcon();
        this.markerSpotIcon.image = this.preferences.imageSpot;
        this.markerSpotIcon.iconSize = new GSize(this.preferences.iconWidth, this.preferences.iconHeight);
        this.markerSpotIcon.iconAnchor = new GPoint(this.preferences.iconAnchorX, this.preferences.iconAnchorY);
        this.markerSpotIcon.infoWindowAnchor = new GPoint(this.preferences.infoWindowAnchorX, this.preferences.iconAnchorY);
        this.markerSpotOptions = { icon : this.markerSpotIcon };

        this.div = document.getElementById(this.preferences.containerId);
        this.items = {};
        this.length = 0;
        this.centerType = this.CENTER_MARKER_ALL;
        this.latitude = 0;
        this.longitude = 0;
        this.mapZoom = 13;
        this.mapType = G_NORMAL_MAP;
        this.map = undefined;
    },

    addMarker : function(id, lat, lng, type) {
        this.items[id] = { lat:lat, lng:lng, type:type };
        this.length++;
    },

    showMap : function() {
        if (typeof this.div != "object" ||
            this.map ||
            this.length == 0) {
            throw new Error("showMap() error");
        }
        this.map = new GMap2(this.div);
        this.map.setUIToDefault();
        //this.map.addControl(new GSmallMapControl());
        //this.map.addControl(new GMapTypeControl());
        //this.map.addControl(new GScaleControl());
        this.map.addControl(new GOverviewMapControl());
        this.center(this.centerType);
        this.map.setZoom(this.mapZoom);
        this.map.setMapType(this.mapType);
        for (var id in this.items) {
            this.map.addOverlay(this.createMarker(this.map, id, this.items[id].lat, this.items[id].lng, this.items[id].type));
        }
    },

    createMarker : function(map, id, lat, lng, type) {
        var latlng = new GLatLng(lat, lng);
        var marker = new GMarker(latlng, type == this.MARKER_ACCOMMODATION ? this.markerAccommodationOptions : this.markerSpotOptions);
        var html = this.getInfoWindowHTML(id);
        GEvent.addListener(marker, "click", function() { map.openInfoWindowHtml(latlng, html) });
        return marker;
    },

    center : function(type) {
        if (type == this.CENTER_CUSTOM) {
            this.map.setCenter(new GLatLng(this.latitude, this.longitude));
        } else {
            var latSum = 0;
            var lngSum = 0;
            var c = 0;
            for (var id in this.items) {
                if ((type == this.CENTER_MARKER_ALL) ||
                    (this.items[id].type == this.MARKER_ACCOMMODATION && type == this.CENTER_MARKER_ACCOMMODATION) ||
                    (this.items[id].type == this.MARKER_SPOT && type == this.CENTER_MARKER_SPOT)) {
                    latSum += this.items[id].lat;
                    lngSum += this.items[id].lng;
                    c++;
                }
            }
            this.map.setCenter(new GLatLng(latSum / c, lngSum / c)/*, isNaN(this.map.getZoom()) ? this.mapZoom : this.map.getZoom()*/);
        }
    },

    locateOnMap : function(id) {
        if (!this.map ||
            this.length == 0 ||
            !this.items[id]) {
            throw new Error("locateOnMap() error");
        }
        this.map.openInfoWindowHtml(new GLatLng(this.items[id].lat, this.items[id].lng), this.getInfoWindowHTML(id));
    },

    getInfoWindowHTML : function(id) {
        return "<div style=\"width:400px;\">" + document.getElementById(id).innerHTML.replace(/<span.*<\/span>/i, "") + "</div>";
    }
}

//
// accommodationMap
//

var accommodationMap = {
    preferences : {
        containerId         : "map",
        iconImage           : "http://www.hotels-in-sofia.com/images/general/marker20px.png",
        iconWidth           : 20,
        iconHeight          : 20,
        iconAnchorX         : 10,
        iconAnchorY         : 10,
        infoWindowAnchorX   : 10,
        infoWindowAnchorY   : 10
    },

    initialize : function() {
        this.markerIcon = new GIcon();
        this.markerIcon.image = this.preferences.iconImage;
        this.markerIcon.iconSize = new GSize(this.preferences.iconWidth, this.preferences.iconHeight);
        this.markerIcon.iconAnchor = new GPoint(this.preferences.iconAnchorX, this.preferences.iconAnchorY);
        this.markerIcon.infoWindowAnchor = new GPoint(this.preferences.infoWindowAnchorX, this.preferences.iconAnchorY);
        this.markerOptions = { icon : this.markerIcon };
        this.div = document.getElementById(this.preferences.containerId);
        this.mapZoom = 14;
        this.mapType = G_HYBRID_MAP;
        this.map = undefined;
    },

    showMap : function(latitude, longitude) {
        if (typeof this.div != "object" || this.map) {
            throw new Error("showMap() error");
        }
        var ll = new GLatLng(latitude, longitude);
        this.map = new GMap2(this.div);
        this.map.addControl(new GSmallMapControl());
        this.map.addControl(new GMapTypeControl());
        this.map.addControl(new GScaleControl());
        this.map.addControl(new GOverviewMapControl());
        this.map.setCenter(ll, this.mapZoom, this.mapType);
        this.map.addOverlay(new GMarker(ll, this.markerOptions));
    }
}