// js for offer detail page
var maps = Array();

function initMap(id, data) {
    //init map
    var map = new YMaps.Map(document.getElementById("YMapsID" + id));

    map.setCenter(new YMaps.GeoPoint(data.lng, data.lat), 17);
    map.addControl(new YMaps.SmallZoom(), new YMaps.ControlPosition(YMaps.ControlPosition.TOP_LEFT, new YMaps.Size(5, 5)));


    var point = new YMaps.GeoPoint(data.lng, data.lat);
    var placemark = new YMaps.Placemark(point, {style: "default#lightblueSmallPoint", hasBalloon: false, hasHint : false, interactive : false});
    placemark.Events = null;
    map.addOverlay(placemark);

    maps[id] = map
}

jQuery(document).ready(function() {
    $('#otherOffers').css("display", "none");

    for (i in locIds) {
        $('#YMapsID' + locIds[i]).css("display", "none");
    }

    jQuery('#othersTable tr').live("mouseover", function (event) {
        setBlockBg(true, this, "");
    });
    jQuery('#othersTable tr').live("mouseout", function (event) {
        setBlockBg(false, this, "transparent");
    });

//    jQuery('#latestOffer').live("mouseover", function (event) {
//        setBlockBg(true, this, "");
//    });
//    jQuery('#latestOffer').live("mouseout", function (event) {
//        setBlockBg(false, this, "#f1f1ed");
//    });

    jQuery('#otherOffersLink').live("click", function(event) {
        if ($(this).text() == "Другие предложения компании") {
            $('#otherOffers').css("display", "");
            $(this).text("Скрыть предложения");
            $(this).css("color", "rgb(255, 98, 49)");
        } else {
            $('#otherOffers').css("display", "none");
            $(this).css("color", "rgb(34, 123, 187)");
            $(this).text("Другие предложения компании");
        }
    });

    jQuery('#mapLink span').live("click", function(event) {
        var id = $(this).attr('id');

        if ($(this).text() == "показать на карте") {
            $(this).text("убрать карту");
            $(this).css("color", "rgb(255, 98, 49)");

            $('#YMapsID' + id).css("display", "");

            if (maps[id] == undefined) {
                $.ajax({
                    type: "GET",
                    url: "/ajax/locationRequest",
                    data: {'id' : id},
                    dataType: "json",
                    success: function(data) {
                        initMap(id, data);
                    }
                });
            }
        } else {
            $(this).text("показать на карте");
            $(this).css("color", "rgb(34, 123, 187)");

            $('#YMapsID' + id).css("display", "none");
        }
    });
});
