var map;
var iconOptions = {
    width: 20,
    height: 20,
    primaryColor: "#FF0000",
    shape: "circle"
};
function myOnload(a) {
    if (GBrowserIsCompatible()) {
        var b = new GMap2($("map"));
        gOpts = b.getDefaultUI();
        gOpts.zoom.scrollwheel = false;
        gOpts.zoom.doubleclick = false;
        gOpts.keyboard = false;
        gOpts.controls.largemapcontrol3d = false;
        gOpts.controls.smallzoomcontrol3d = false;
        gOpts.controls.maptypecontrol = false;
		gOpts.controls.hierarchicalmaptypecontrol = false;
        gOpts.controls.menumaptypecontrol = false;
        gOpts.controls.scalecontrol = false;
        b.setUI(gOpts);
        GEvent.addListener(b, "load", function () {
            var c = a.catIds;
            map = new ClusterMarker(b);
            map.site = a.site;
            map.borderPadding = typeof(a.padding) != "undefined" ? a.padding : 256;
            map.intersectPadding = typeof(a.overlap) != "undefined" ? a.overlap : 1;
            map.first = 1;
            map.pin = new PinMenu("accordion", b, c);
            if ( !! a.poiActive) {
                map.poiShowing = a.poiActive;
                $("poiClose").onclick = function () {
                    hidePoi(1)
                };
                adustPoiTextHeight()
            }
            ajaxGetMarkers(c)
        });
        b.setCenter(new GLatLng(a.lat, a.lng), a.zoom, G_NORMAL_MAP)
    }
}
function ajaxGetMarkers(b) {
    var surl = (document.location == 'http://areadigital.homeunix.com/visitsweden/') ? '/visitsweden/' : '/';
    var a = surl + "layout/set/ajax/map/list/id/" + b.join(",") + "/site/" + map.site;
    new Ajax.Request(a, {
        method: "get",
        onSuccess: function (c) {
            map._cats = map._cats.concat(b);
            c = c.responseText.evalJSON();
            if ( !! c.line) {
                map.addLines(c.line)
            }
            loadMarkers(c.poi)
        }
    })
}
function loadMarkers(d) {
    var b, a = [];
    for (var c = 0; c < d.length; c++) {
        b = newMarker(new GLatLng(d[c].posn[0], d[c].posn[1]), d[c]);
        a.push(b)
    }
    map.addMarkers(a);
    if (map.first) {
        map.first = 0;
        map.lat = map.map.getCenter().lat();
        map.lng = map.map.getCenter().lng();
        map.zoom = map.map.getZoom()
    }
    map.refresh()
}
function newMarker(b, l) {
    var surl = (document.location == 'http://areadigital.homeunix.com/visitsweden/') ? '/visitsweden/' : '/';
    var j = l.t[0];
    var c = surl + "extension/gmaplocation/design/standard/images/markers2/";
    var a = c + "themes/";
    var m = a + l.t[1] + "bk.png";
    var h = new Image();
    h.src = c + j + ".png";
    var g = new Image();
    g.src = m;
    var e = new Image();
    e.src = c + j + "ov.png";
    var d = new Image();
    d.src = a + l.t[1] + "click.png";
    var f = new Image();
    f.src = a + l.t[1] + "ov.png";
    var k;
    if ( !! map._iconCache[j]) {
        k = map._iconCache[j]
    } else {
        k = new GIcon();
        k.iconSize = new GSize(26, 40);
        k.iconAnchor = new GPoint(15, 36);
        k.image = c + j + ".png";
        map._iconCache[j] = k
    }
    var i = new GMarker(b, {
        icon: k
    });
    i.id = l.id;
    i.cat = l.cat;
    i._noCluster = !!l.nc;
    GEvent.addListener(i, "mouseover", function () {
        this.mOv = true;
        var n = this.clicked ? a + l.t[1] + "ov.png" : c + j + "ov.png";
        this.setImage(n);
        this.origZ = GOverlay.getZIndex(this.getPoint().lat());
        var o = $("mtgt_" + this.id);
        o.style.zIndex = 100;
        var p = new Element("span", {
            id: "newDiv",
            style: "background-repeat: no-repeat; background-position: center right; background-image: url(" + m + "); font-family: Arial; white-space: nowrap;font-size: 11px; color:#6c6c6c;height:23px; padding: 7px 15px 0 3px;position: absolute; z-index: 100; top:" + (parseInt(o.style.top)) + "px; left:" + (parseInt(o.style.left) + 26) + "px"
        }).update(l.name);
        o.parentNode.appendChild(p)
    });
    GEvent.addListener(i, "mouseout", function () {
        this.mOv = false;
        if (this.clicked) {
            this.setImage(a + l.t[1] + "click.png")
        } else {
            this.setImage(c + j + ".png")
        }
        if (!this.clicked) {
            $("mtgt_" + this.id).style.zIndex = this.origZ
        }
        while ($("newDiv")) {
            $("newDiv").remove()
        }
    });
    GEvent.addListener(i, "click", function (n) {
        if (map.lastClick && map.lastClick != this) {
            map.clearLastClickIcon()
        }
        if (typeof(l.url) != "undefined") {
            while ($("newDiv")) {
                $("newDiv").remove()
            }
            this.setImage(c + j + ".png");
            map.poiShowing = 0;
            $("poi").setStyle({
                display: "none"
            });
            document.location.href = l.url
        } else {
            map.lastClick = this;
            map.lastBk = c + j + ".png";
            $("mtgt_" + this.id).style.zIndex = GOverlay.getZIndex(this.getPoint().lat()) + 8 * 50000;
            if (!this.clicked) {
                this.setImage(a + l.t[1] + "click.png")
            }
            this.clicked = true;
            openPoi(l.id, n)
        }
    });
    return i
}
function toggleClustering() {
    map.clusteringEnabled = !map.clusteringEnabled;
    map.refresh(true)
}
function showArr(b) {
    var c = "";
    for (var d = 0; d < b.length; d++) {
        c += b[d] + ", "
    }
    return c
}
function openPoi(b, a) {
    showPoi(b, a)
}
function hidePoi(a) {
    map.poiShowing = 0;
    map.clearLastClickIcon();
    if (a) {
        $("poi").fade({
            duration: 0.5
        })
    } else {
        $("poi").setStyle({
            display: "none"
        })
    }
}
function showPoi(b, c) {
    var surl = (document.location == 'http://areadigital.homeunix.com/visitsweden/') ? '' : '/';
    var f = !map.poiShowing;
    var d = map.map;
    var j = d.getZoom();
    var e = d.getCurrentMapType().getProjection().fromLatLngToPixel(new GLatLng(c.lat(), c.lng()), j);
    var g = e.y;
    var h = parseInt(e.x - 70);
    var i = d.getCurrentMapType().getProjection().fromPixelToLatLng(new GPoint(h, g), j);
    d.panTo(new GLatLng(i.lat(), i.lng()));
    if (map.poiShowing == b) {
        return
    }
    map.poiShowing = b;
    var a = surl + "layout/set/ajax/map/info/id/" + b + "/site/" + map.site;
    new Ajax.Request(a, {
        method: "get",
        onSuccess: function (l) {
            var k = l.responseText;
            $("poi").update(k);
            $("poiClose").onclick = function () {
                hidePoi(1)
            };
            if (f) {
                $("poi").appear({
                    duration: 0.5,
                    afterFinish: function () {
                        adustPoiTextHeight()
                    }
                })
            } else {
                adustPoiTextHeight()
            }
            
            trackExternalLink();
            externalUrlReplace();

        }
    })
}
function adustPoiTextHeight() {
    var a = $("poiText").getHeight() - 12;
    if (a > 313) {
        $("moreTextLink").observe("click", function () {
            if ($("poiText").getStyle("top").replace("px", "") != 0) {
                new Effect.Move($("poiText"), {
                    y: 0,
                    mode: "absolute"
                })
            } else {
                new Effect.Move($("poiText"), {
                    y: (285 - a),
                    mode: "absolute"
                })
            }
        });
        $("moreTextLink").show()
    }
    $("poiText").show()
};