var map;

// 初期表示時の中心座標 佐倉駅

var defaultCenter = gp(35.7097401, 140.226037);

// マーカーを管理する配列

var markers = new Array();



var baseIcon = new GIcon();

baseIcon.shadow = "/images/gicon_shadow.png";

baseIcon.iconSize = new GSize(22, 37);

baseIcon.shadowSize = new GSize(53, 37);

baseIcon.iconAnchor = new GPoint(11, 37);

baseIcon.infoWindowAnchor = new GPoint(9, 2);

baseIcon.infoShadowAnchor = new GPoint(18, 25);



function gp(x, y){

    return new GLatLng(x, y);

}



function load(id, ctr1, ctr2, ctr3, z){

    map = new GMap2($('#' + id)[0]);



    switch (ctr1) {

        case "L":

            map.addControl(new GLargeMapControl());

            break;

        case "S":

            map.addControl(new GSmallMapControl());

            break;

    }



    if (ctr2 == 1) {

        map.addControl(new GScaleControl())

    };

    if (ctr3 == 1) {

        map.addControl(new GMapTypeControl())

    };

    var zoom = (zoom == "") ? "13" : z;

    map.setCenter(defaultCenter, zoom);

}



function addMarker(lat, lng, id, link){

    // マーカー画像の指定

	var icon = new GIcon(baseIcon);

	icon.image = "/images/gicon.png";

    var marker = new GMarker(new GPoint(lng, lat), icon);



    if (link != null) {

        GEvent.addListener(marker, "click", function(){

            window.location.href = link;

        });

    }



    if (id != null) {

        GEvent.addListener(marker, "mouseover", function(){

            openInfoWindow(id);

        });

        GEvent.addListener(marker, "mouseout", function(){

            $('#hid' + id).hide();

        });

    }



    map.addOverlay(marker);

    markers.push(marker);

}



function openInfoWindow(id){

    var bb = map.getBounds();



    if (bb.contains(markers[id].getPoint())) {

        var tlcLatLng = map.fromContainerPixelToLatLng(new GPoint(0, 0), true);

        var tlcDivPixel = map.fromLatLngToDivPixel(tlcLatLng);

        var pointDivPixel = map.fromLatLngToDivPixel(markers[id].getPoint());

        var c = new GPoint(pointDivPixel.x - tlcDivPixel.x, pointDivPixel.y - tlcDivPixel.y);

        var container = map.getContainer();

        var container_id = container.id;

        var offset = $('#' + container_id).offset();

        var t = (offset.top + c.y);

        var l = (offset.left + c.x);

        var id = "hid" + id;

        $('#' + id)

        $('#' + id).css("top", t + "px");

        $('#' + id).css("left", l + "px");

        $('#' + id).show();

    }

}



function putMarkerRange(zlv){

    var gb;

    var first = 1;



    // マーカーが無ければ何もしない

    if (markers.length < 1) {

        return;

    }



    // 1個目の

    for (var index = 0; index < markers.length; index++) {

        var marker = markers[index];

        if (first) {

            gb = new GLatLngBounds(marker.getPoint(), marker.getPoint());

            first = 0;

        }

        else {

            var point = marker.getPoint();

            gb.extend(point);

        }

    }



    map.setCenter(gb.getCenter(), map.getBoundsZoomLevel(gb) + zlv);

}



function clearMarker(){

    // 配列で保持しているマーカーを順次削除

    while (markers.length > 0) {

        var m = markers.shift();

        // マーカーのイベントを削除

        GEvent.clearInstanceListeners(m);

        // マーカーを削除

        map.removeOverlay(m);

    }

}



$(window).unload(function(){

    GUnload();

});


