function initMap() {
    if (map == undefined) {
        //init map
        map = new YMaps.Map(document.getElementById("YMapsID"));

        map.addControl(new YMaps.ScaleLine(), new YMaps.ControlPosition(YMaps.ControlPosition.TOP_RIGHT, new YMaps.Size(5, 10)));
        map.addControl(new YMaps.ToolBar());

        var searchControl = new YMaps.SearchControl({resultsPerPage : 1, width : 250});
        map.addControl(searchControl, new YMaps.ControlPosition(YMaps.ControlPosition.TOP_LEFT, new YMaps.Size(150, 5)));

        var zoom = new YMaps.Zoom({
            customTips: [
                {
                    index: 10,
                    value: "Город"
                },
                {
                    index: 13,
                    value: "Улица"
                },
                {
                    index: 16,
                    value: "Дом"
                }
            ]
        });
        map.addControl(zoom);
        map.setCenter(new YMaps.GeoPoint(82.925717, 55.000081), 10);
    }
}

function showPoints(result) {
    var points = new Array(result.locations.length);

    for (var x in result.locations) {
        var point = new YMaps.GeoPoint(result.locations[x].lng, result.locations[x].lat);
        var placemark = new YMaps.Placemark(point, {style:"commonStyle"});
        placemark.id = result.id;
        placemark.company = result.company;
        placemark.name = result.name
        placemark.address = result.locations[x].address;
        placemark.setIconContent(result.company);
        map.addOverlay(placemark);

        points[x] = placemark
    }

    placemarks[result.id] = points;
}

var map;
var placemarks = new Array();


jQuery(document).ready(function() {
    var balloonContentStyle = new YMaps.BalloonContentStyle(new YMaps.Template("<strong><a href='../offer/$[id]'>$[company]</a></strong><div class='offer_title'>$[name]</div><br/>Адрес: $[address]"));
    var s = new YMaps.Style();
    s.balloonContentStyle = balloonContentStyle;
    YMaps.Styles.add("commonStyle", s);

    $('#map').css("display", "none");    

    jQuery('.offer').live("mouseover", function (event) {
        setBlockBg(true, this, "");
    });
    jQuery('.offer').live("mouseout", function (event) {
        setBlockBg(false, this, "transparent");
    });

    jQuery('#close_map_link span').live("click", function(event) {
        if ($(this).text() == "показать карту") {
            $(this).text("скрыть карту");
            jQuery('#map').css("display", "");
            initMap();
        } else {
            $(this).text("показать карту");
            jQuery('#map').css("display", "none");
        }
    });

    jQuery('#offer_map_link a').live("click", function(event) {
        var id = $(this).attr('id');

        if ($(this).text() == "показать на карте") {
            $(this).text("убрать с карты");
            $(this).css("color", "rgb(255, 98, 49)");
            jQuery('#close_map_link span').text("скрыть карту");

            $('#map').css("display", "");
            initMap();
            
            $.ajax({
                type: "GET",
                url: "/ajax/offerRequest",
                data: {'id' : id},
                dataType: "json",
                success: function(data) {
                    showPoints(data);
                }
            });

        } else {
            $(this).text("показать на карте");
            $(this).css("color", "rgb(34, 123, 187)");

            for (var x in placemarks[id]) {
                map.removeOverlay(placemarks[id][x]);
            }
        }

        return true;
    });
});
