function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


function activateButton(what) {
    var btn = document.getElementById(what);
    btn.style.color = "#FFFFFF";
    btn.style.background = "#FF6600 url(/images/map/active-button.gif) top left";
    btn.style.padding = "2px 1px 1px 2px";
    btn.style.fontWeight = "bold";
}

function deactivateButton(what) {
    var btn = document.getElementById(what);
    btn.style.color = "#000000";
    btn.style.background = "#FFFFFF url(/images/map/inactive-button.gif) bottom right";
    btn.style.padding = "1px 2px 2px 1px";
    btn.style.fontWeight = "normal";
}

function zoomToObject(lon, lat) {
    map.panTo(new GLatLng(lon, lat));
    map.setZoom(18);
}

function RussianMapTypeControl() { }

RussianMapTypeControl.prototype = new GControl();

RussianMapTypeControl.prototype.initialize = function(map) {

    var container = document.createElement("div");

    var typeMapDiv = document.createElement("div");
    typeMapDiv.id = "map-button";
    container.appendChild(typeMapDiv);
    this.setButtonStyle_(typeMapDiv);
    typeMapDiv.appendChild(document.createTextNode("Карта"));
    GEvent.addDomListener(typeMapDiv, "click", function() {
        map.setMapType(G_NORMAL_MAP);
        activateButton("map-button");
        deactivateButton("sat-button");
        deactivateButton("hyb-button");
    });

    var typeSatDiv = document.createElement("div");
    typeSatDiv.id = "sat-button";
    this.setButtonStyle_(typeSatDiv);
    container.appendChild(typeSatDiv);
    typeSatDiv.appendChild(document.createTextNode("Спутник"));
    GEvent.addDomListener(typeSatDiv, "click", function() {
        map.setMapType(G_SATELLITE_MAP);
        deactivateButton("map-button");
        activateButton("sat-button");
        deactivateButton("hyb-button");
    });

    var typeHybDiv = document.createElement("div");
    typeHybDiv.id = "hyb-button";
    this.setButtonStyle_(typeHybDiv);
    container.appendChild(typeHybDiv);
    typeHybDiv.appendChild(document.createTextNode("Вместе"));
    GEvent.addDomListener(typeHybDiv, "click", function() {
        map.setMapType(G_HYBRID_MAP);
        deactivateButton("map-button");
        deactivateButton("sat-button");
        activateButton("hyb-button");
    });

    map.getContainer().appendChild(container);
    activateButton("map-button");
    return container;
}

RussianMapTypeControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7));
}

RussianMapTypeControl.prototype.setButtonStyle_ = function(button) {
    button.setAttribute("style","float:left");
    button.style.styleFloat = "left";
    button.style.color = "#000000";
    button.style.background = "#FFFFFF url(/images/map/inactive-button.gif) bottom right";
    button.style.padding = "2px 2px 2px 2px";
    button.style.fontFamily = "Tahoma, sans-serif";
    button.style.fontSize = "95%";
    button.style.border = "1px solid #333";
    button.style.width = "6em";
    button.style.marginLeft = "3px";
    button.style.textAlign = "center";
    button.style.cursor = "pointer";
}

function createInfoTabs(html1, html2) {
    var tabs;
    if (html2.length == 0) {
        tabs = [new GInfoWindowTab('', html1)];
    } else {
        tabs = [new GInfoWindowTab('Адрес', html1), new GInfoWindowTab('Фильмы', html2)];
    }
    return tabs;
}

function createMarker2(point, html1, html2, icon) {
    var marker = new GMarker(point, icon);
    if (html2.length == 0) {
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(html1);
        });
    } else {
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowTabsHtml(createInfoTabs(html1, html2));
        });
    }
    return marker;
}

function wheelevent(e) {
    if (!e) { e = window.event;    }
    if (e.preventDefault) { e.preventDefault() }
    e.returnValue = false;
}

