switch (kununu.environment) {
  case 'development':
      kununu.GOOGLE_MARKER_PNG = "/images/images_front/gmap/google_marker.png";
      kununu.GOOGLE_MARKER_GIF = "/images/images_front/gmap/google_marker.gif";
      kununu.GOOGLE_GREYED_MARKER_PNG = "/images/images_front/gmap/google_greyed_marker.png";
      kununu.GOOGLE_GREYED_MARKER_GIF = "/images/images_front/gmap/google_greyed_marker.gif";
      kununu.GOOGLE_JUMPER = "/images/images_front/gmap/google_active_jumper.gif";
      break;
  case 'production':
      kununu.GOOGLE_MARKER_PNG = kununu.baseurl + "images/images_front/gmap/google_marker.png";
      kununu.GOOGLE_MARKER_GIF = kununu.baseurl + "images/images_front/gmap/google_marker.gif";
      kununu.GOOGLE_GREYED_MARKER_PNG = kununu.baseurl + "images/images_front/gmap/google_greyed_marker.png";
      kununu.GOOGLE_GREYED_MARKER_GIF = kununu.baseurl + "images/images_front/gmap/google_greyed_marker.gif";
      kununu.GOOGLE_JUMPER = kununu.baseurl + "images/images_front/gmap/google_active_jumper.gif";
      break;
}

kununu.GOOGLE_MAP_MARKER_WIDTH  = 29;
kununu.GOOGLE_MAP_MARKER_HEIGHT = 37;

/*
   inherited from GxMarker version 1.2
   http://code.toeat.com/gxmarker.html
*/
function GxMarkerNamespace() {
    var n4=(document.layers);
    var n6=(document.getElementById&&!document.all);
    var ie=(document.all);
    var o6=(navigator.appName.indexOf("Opera") != -1);
    var safari=(navigator.userAgent.indexOf("Safari") != -1);
    var currentSpan = new GBounds();

    function setCursor( container, cursor ) {
        try {
            container.style.cursor = cursor;
        }
        catch ( c ) {
            if ( cursor == "pointer" )
                setCursor("hand");
        }
    };

    function GxMarker( a, b, tooltipHtml ) {
        this.inheritFrom = GMarker;
        this.inheritFrom(a,b);
        if(!currentSpan.minX || a.x < currentSpan.minX) currentSpan.minX = a.x;
        if(!currentSpan.maxX || a.x > currentSpan.maxX) currentSpan.maxX = a.x;
        if(!currentSpan.minY || a.y < currentSpan.minY) currentSpan.minY = a.y;
        if(!currentSpan.maxY || a.y > currentSpan.maxY) currentSpan.maxY = a.y;
        if(typeof tooltipHtml != "undefined") {
            this.setTooltip( tooltipHtml );
        }
    }

    GxMarker.prototype = new GMarker(new GLatLng(1, 1));

    GxMarker.prototype.setTooltip = function( string ) {
        this.removeTooltip();
        this.tooltip = new Object();
        this.tooltip.opacity = 100;
        this.tooltip.contents = string;
    };

    GxMarker.prototype.initialize = function( a ) {
        try {
            GMarker.prototype.initialize.call(this, a);
            // Setup the mouse over/out events
            GEvent.addListener(this, "mouseover", this.onMouseOver);
            GEvent.addListener(this, "mouseout", this.onMouseOut);
            this.map = a;
        } catch(e) {
            console.error(e);
        }
    }

    GxMarker.prototype.setCursor = function( cursor ) {
        var c = this.iconImage;
        // Use the image map for Firefox/Mozilla browsers
        if(n6 && this.icon.imageMap && !safari) {
            c = this.imageMap;
        }
        // If we have a transparent icon, use that instead of the main image
        else if(this.transparentIcon && typeof this.transparentIcon != "undefined") {
            c = this.transparentIcon;
        }
    }

    GxMarker.prototype.remove = function( a ) {
        GMarker.prototype.remove.call(this);
        this.removeTooltip();
    }

    GxMarker.prototype.removeTooltip = function() {
        if(this.tooltipElement) {
            try {
                document.body.removeChild(this.tooltipElement);
            } catch(e) {
                console.error(e);
            }
            this.tooltipElement = null;
        }
    }

    GxMarker.prototype.onInfoWindowOpen = function() {
        this.hideTooltip();
        GMarker.prototype.onInfoWindowOpen.call(this);
    }

    GxMarker.prototype.onMouseOver = function() {
        this.showTooltip();
    };

    GxMarker.prototype.onMouseOut = function() {
        this.hideTooltip();
    };

    GxMarker.prototype.showTooltip = function() {
        if(this.tooltip) {
            if(!this.tooltipElement) {
                var opacity = this.tooltip.opacity / 100;
                this.tooltipElement = document.createElement("div");
                this.tooltipElement.style.display    = "none";
                this.tooltipElement.style.position   = "absolute";
                this.tooltipElement.style.background = "#fff";
                this.tooltipElement.style.padding    = "0";
                this.tooltipElement.style.margin     = "0";
                this.tooltipElement.style.MozOpacity = opacity;
                this.tooltipElement.style.filter     = "alpha(opacity=" + this.tooltip.opacity + ")";
                this.tooltipElement.style.opacity    = opacity;
                this.tooltipElement.style.zIndex     = 50000;
                this.tooltipElement.innerHTML        = this.tooltip.contents;

                document.body.appendChild(this.tooltipElement);
            }

            markerPosition = calculateMarkerPosition(this.map, this.getPoint());
            try {
                this.tooltipElement.style.top  = markerPosition.top  - (this.getIcon().iconAnchor.y + 0) + "px";
                this.tooltipElement.style.left = markerPosition.left + (this.getIcon().iconSize.width - this.getIcon().iconAnchor.x - 5) + "px";
                this.tooltipElement.style.display = "block";
            } catch(e) {
                console.error(e);
            }
        }
    }

    function calculateMarkerPosition(map, markerLatLngPoint) {
        gmapTopLeftLatLng = map.fromContainerPixelToLatLng(new GPoint(0,0));
        gmapTopLeftPixel  = map.fromLatLngToDivPixel(gmapTopLeftLatLng);
        myMarkerDivPixel  = map.fromLatLngToDivPixel(markerLatLngPoint);

        markerTop  = getAbsoluteTop(map.getContainer())  - gmapTopLeftPixel.y + myMarkerDivPixel.y;
        markerLeft = getAbsoluteLeft(map.getContainer()) - gmapTopLeftPixel.x + myMarkerDivPixel.x;

        return {top:markerTop, left:markerLeft};
    }

    GxMarker.prototype.hideTooltip = function() {
        if(this.tooltipElement) {
            this.tooltipElement.style.display = "none";
        }
    }

    function makeInterface(a) {
        var b = a || window;
        b.GxMarker = GxMarker;
    }

    makeInterface();
}

// Utility functions
function getAbsoluteLeft(o) {
    var oLeft = o.offsetLeft;       // Get left position from the parent object
    var oParent;
    var o;
    while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
        oParent = o.offsetParent      // Get parent object reference
        oLeft += oParent.offsetLeft   // Add parent left position
        o = oParent
    }
    return oLeft
}

function getAbsoluteTop(o) {
    var oTop = o.offsetTop;         // Get top position from the parent object
    var oParent;
    var o;
    while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
        oParent = o.offsetParent      // Get parent object reference
        oTop += oParent.offsetTop     // Add parent top position
        o = oParent
    }
    return oTop
}

var kununuMap = {
    the_map: null,
    the_bounds: null,

    map: function() {
        if (this.the_map == null) this.the_map = new GMap2($('#gmap')[0]);
        return this.the_map;
    },

    bounds: function() {
        if (this.the_bounds == null) this.the_bounds = new GLatLngBounds();
        return this.the_bounds;
    },

    init: function(mapControl, latitude, longitude, zoom) {
        if (mapControl == 'Small') {
            this.map().addControl(new GSmallMapControl());
            this.map().addControl(new GMapTypeControl(true));
            this.map().getContainer().style.overflow="hidden";
        } else if(mapControl == 'SmallZoom'){
            this.map().addControl(new GSmallZoomControl());
            this.map().getContainer().style.overflow="hidden";
        } else {
            this.map().addControl(new GLargeMapControl());
            this.map().addControl(new GMapTypeControl());
        }        
        
        if(latitude && longitude && zoom){
            var center = new GLatLng(latitude, longitude)
            this.map().setCenter(center, zoom);
        } else{
            var center = new GLatLng(0, 0);
            this.map().setCenter(center);
        }
    },

    setZoomAndCenter: function(level) {
        var center = this.bounds().getCenter();
        this.map().setCenter(center);
        this.map().setZoom(level);
    },

    addPlace: function(html, url, latitude, longitude) {
        var point = new GLatLng(latitude, longitude);
        this.map().addOverlay(this.createMarker(point, html, url));
        this.bounds().extend(point);
    },

    addGreyedPlace: function(html, url, latitude, longitude) {
        var point = new GLatLng(latitude, longitude);
        this.map().addOverlay(this.createGreyedMarker(point, html, url));
        this.bounds().extend(point);
    },

    createMarker: function(point, html, url) {
        var icon = new GIcon();
        icon.image = kununu.GOOGLE_GREYED_MARKER_PNG;
        icon.printImage = kununu.GOOGLE_GREYED_MARKER_GIF;
        icon.mozPrintImage = kununu.GOOGLE_GREYED_MARKER_GIF;
        //icon.shadow = "/images/google_marker_shadow.png";
        icon.iconSize = new GSize(kununu.GOOGLE_MAP_MARKER_WIDTH, kununu.GOOGLE_MAP_MARKER_HEIGHT);
        icon.shadowSize = new GSize(kununu.GOOGLE_MAP_MARKER_WIDTH, kununu.GOOGLE_MAP_MARKER_HEIGHT);
        icon.iconAnchor = new GPoint(13, 34);
        icon.infoWindowAnchor = new GPoint(19, 21);

        var marker = new GxMarker(point, icon, html);

        if(url)
            GEvent.addListener(marker, "click", function() {
                window.location = url;
            });

        return marker;
    },

    createGreyedMarker: function(point, html, url) {
        var icon = new GIcon();
        icon.image = kununu.GOOGLE_MARKER_PNG;
        icon.printImage = kununu.GOOGLE_MARKER_GIF;
        icon.mozPrintImage = kununu.GOOGLE_MARKER_GIF;
        icon.iconSize = new GSize(kununu.GOOGLE_MAP_MARKER_WIDTH, kununu.GOOGLE_MAP_MARKER_HEIGHT);
        icon.shadowSize = new GSize(kununu.GOOGLE_MAP_MARKER_WIDTH, kununu.GOOGLE_MAP_MARKER_HEIGHT);
        icon.iconAnchor = new GPoint(13, 34);
        icon.infoWindowAnchor = new GPoint(19, 21);

        var marker = new GxMarker(point, icon, html);

        if (url) GEvent.addListener(marker, "click", function() {
            window.location = url;
        });

        return marker;
    },

    setCenter: function(latitude, longitude, zoom) {
        this.map().setCenter(new GLatLng(latitude,longitude),zoom);
    },

    centerAndZoom: function() {
        var bounds = this.bounds();
        var center = bounds.getCenter();

        this.map().setCenter(center);
        var zoom = Math.min(this.map().getBoundsZoomLevel(this.bounds()), 15);
        this.map().setZoom(zoom);
    },

    clear: function() {
        this.map().clearOverlays();
        this.the_bounds = null;
    }
}

GMapDetail = {};
jQuery.extend(GMapDetail, kununuMap);
jQuery.extend(GMapDetail, {

    max_zoom: 17,
    all_markers: new Array(),
    highlighted_marker: null,
    aktive_marker: null,

    init: function(mapControl, latitude, longitude, zoom) {
        if (mapControl == 'Small') {
            this.map().addControl(new GSmallMapControl());
        } else {
            this.map().addControl(new GLargeMapControl());
            this.map().addControl(new GMapTypeControl());
        }       
        
        //this.map().enableScrollWheelZoom();
        
        if(latitude && longitude && zoom){        	
            var center = new GLatLng(latitude, longitude)            
            this.map().setCenter(center, zoom);            
        } else {        	
            var center = new GLatLng(0, 0);
            this.map().setCenter(center);
        }
    },

    addPlace: function(html, url, latitude, longitude, place_id, greyed) {
        var point  = new GLatLng(latitude, longitude);
        if (greyed == 'true') {
            var marker = this.createGreyedMarker(point, html, url);
        } else {
            var marker = this.createMarker(point, html, url);
        }
        this.map().addOverlay(marker);
        this.all_markers[place_id+''] = marker;
        this.bounds().extend(point);
    },

    highlightPlace: function(place_id){
        var marker = this.all_markers[place_id+''];
        if(marker) {
            this.highlighted_marker = this.createGreyedJumber(marker.getPoint(), '', '');
            this.map().addOverlay(this.highlighted_marker);
            this.aktive_marker = marker;
            this.map().removeOverlay(marker);
        }
    },

    unhighlightPlace: function(){
        if(this.highlighted_marker){
            this.map().removeOverlay(this.highlighted_marker);
            this.highlighted_marker = null;
        }
        if(this.aktive_marker){
            this.map().addOverlay(this.aktive_marker);
            this.aktive_marker = null;
        }
    },

    createGreyedJumber: function(point, html, url) {
        var icon = new GIcon();
        icon.image = kununu.GOOGLE_JUMPER;
        icon.iconSize = new GSize(32, 70);
        icon.shadowSize = new GSize(32, 70);
        icon.iconAnchor = new GPoint(15, 67);
        icon.infoWindowAnchor = new GPoint(20, 32);

        var marker = new GxMarker(point, icon, html);

        if (url) GEvent.addListener(marker, "click", function() {
            window.location = url;
        });

        return marker;
    },

    clear: function() {
        this.map().clearOverlays();
        this.the_bounds = null;
        this.all_markers = new Array();
    }
});

function debug(msg) {
    var d = document.getElementById("debug") ?
    $("debug")[0] : document.body;
    d.appendChild(document.createTextNode("[" + new Date() + "] "));
    d.appendChild(document.createTextNode(msg));
    d.appendChild(document.createElement("br"));
}