////////////////////////////////////////////////////////////////////////////////
// Создаем свой контроллер
////////////////////////////////////////////////////////////////////////////////
function dmZoomControl() {
}

dmZoomControl.prototype = new GControl();

// Определяем и рисуем кнопки
dmZoomControl.prototype.initialize = function(map) {
  var container = document.createElement("div");

  var zoomTopDiv = document.createElement("div");
  this.setButtonStyle_(zoomTopDiv, 't');
  container.appendChild(zoomTopDiv);
  GEvent.addDomListener(zoomTopDiv, "click", function() {
    map.panDirection(0, 1);
  });

  var zoomLeftDiv = document.createElement("div");
  this.setButtonStyle_(zoomLeftDiv, 'l');
  container.appendChild(zoomLeftDiv);
  GEvent.addDomListener(zoomLeftDiv, "click", function() {
    map.panDirection(1, 0);
  });
  var zoomRightDiv = document.createElement("div");
  this.setButtonStyle_(zoomRightDiv, 'r');
  container.appendChild(zoomRightDiv);
  GEvent.addDomListener(zoomRightDiv, "click", function() {
    map.panDirection(-1, 0);
  });

  var zoomBottomDiv = document.createElement("div");
  this.setButtonStyle_(zoomBottomDiv, 'b');
  container.appendChild(zoomBottomDiv);
  GEvent.addDomListener(zoomBottomDiv, "click", function() {
    map.panDirection(0, -1);
  });

  var zoomInDiv = document.createElement("div");
  this.setButtonStyle_(zoomInDiv, 'i');
  container.appendChild(zoomInDiv);
  GEvent.addDomListener(zoomInDiv, "click", function() {
    map.zoomIn();
  });

  var zoomOutDiv = document.createElement("div");
  this.setButtonStyle_(zoomOutDiv, 'o');
  container.appendChild(zoomOutDiv);
  GEvent.addDomListener(zoomOutDiv, "click", function() {
    map.zoomOut();
  });

  map.getContainer().appendChild(container);
 
  return container;
}

// Позиционирование контейнера
dmZoomControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
}

// Оформление кнопок
dmZoomControl.prototype.setButtonStyle_ = function(button, who) {
  button.style.width = '15px';
  button.style.height = '15px';
  button.style.backgroundColor = '#c8141a';
  button.style.backgroundImage = 'url(/defa/images/gmap_arrows.png)';
  button.style.backgroundRepeat = 'no-repeat';
  button.style.cursor = 'pointer';
  if(who == 't') {
      button.style.backgroundPosition = '0 0';
      button.style.margin = '2px 0 0 11px';
  }
  else if(who == 'l') {
      button.style.backgroundPosition = '-30px 0';
      button.style.margin = '3px 0 0 2px';
  }
  else if(who == 'r') {
      button.style.backgroundPosition = '-45px 0';
      button.style.margin = '-15px 0 0 20px';
  }
  else if(who == 'b') {
      button.style.backgroundPosition = '-15px 0';
      button.style.margin = '3px 0 0 11px';
  }
  else if(who == 'i') {
      button.style.backgroundPosition = '-60px 0';
      button.style.margin = '6px 0 0 11px';
  }
  else if(who == 'o') {
      button.style.backgroundPosition = '-75px 0';
      button.style.margin = '3px 0 0 11px';
  }
  else {
      button.style.display = 'none';
  }
}


////////////////////////////////////////////////////////////////////////////////
// Создаем псевдо-контроллер для уголков
////////////////////////////////////////////////////////////////////////////////
/*
function dmCornerControl() {
}

dmCornerControl.prototype = new GControl();

// Определяем и рисуем уголки
dmCornerControl.prototype.initialize = function(map) {
  var container = document.createElement("div");
  container.style.cursor = 'pointer';
  var cornerTL = document.createElement('div');
  this.setButtonStyle_(cornerTL, 'tl');
  container.appendChild(cornerTL);
  
  var cornerTR = document.createElement('div');
  this.setButtonStyle_(cornerTR, 'tr');
  container.appendChild(cornerTR);
  
  var cornerBL = document.createElement('div');
  this.setButtonStyle_(cornerBL, 'bl');
  container.appendChild(cornerBL);
  
  var cornerBR = document.createElement('div');
  this.setButtonStyle_(cornerBR, 'br');
  container.appendChild(cornerBR);
  
  map.getContainer().appendChild(container);
 
  return container;
}

// Оформляем уголки
dmCornerControl.prototype.setButtonStyle_ = function(button, who) {
  button.style.width = '13px';
  button.style.height = '13px';
  button.style.backgroundImage = 'url(/defa/images/gmap_corners.png)';
  if(who == 'tl') {
      button.style.backgroundPosition = '0 0';
      button.style.margin = '-2px 0 0 -2px';
  }
  else if(who == 'tr') {
      button.style.backgroundPosition = '-13px 0';
      button.style.margin = '-13px 0 0 459px';
  }
  else if(who == 'bl') {
      button.style.backgroundPosition = '-26px 0';
      button.style.margin = '368px 0 0 -2px';
  }
  else if(who == 'br') {
      button.style.backgroundPosition = '-39px 0';
      button.style.margin = '-13px 0 0 459px';
  }
  else {
      button.style.display = 'none';
  }
}

dmCornerControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 0));
}
*/

function dmCornerControl(who) {
    this.who = who;
}

dmCornerControl.prototype = new GControl();

// Определяем и рисуем уголки
dmCornerControl.prototype.initialize = function(map) {
  var container = document.createElement("div");
  
  if(this.who == 'tl') {
      var cornerTL = document.createElement('div');
      this.setButtonStyle_(cornerTL);
      container.appendChild(cornerTL);
  }
  else if(this.who == 'tr') {
      var cornerTR = document.createElement('div');
      this.setButtonStyle_(cornerTR);
      container.appendChild(cornerTR);
  }
  else if(this.who == 'bl') {  
      var cornerBL = document.createElement('div');
      this.setButtonStyle_(cornerBL);
      container.appendChild(cornerBL);
  }
  else if(this.who == 'br') {
      var cornerBR = document.createElement('div');
      this.setButtonStyle_(cornerBR);
      container.appendChild(cornerBR);
  }
  map.getContainer().appendChild(container);
 
  return container;
}

// Оформляем уголки
dmCornerControl.prototype.setButtonStyle_ = function(button, who) {
  button.style.width = '13px';
  button.style.height = '13px';
  button.style.backgroundImage = 'url(/defa/images/gmap_corners.png)';
  button.style.margin = '-2px';
  if(this.who == 'tl') {
      button.style.backgroundPosition = '0 0';
  }
  else if(this.who == 'tr') {
      button.style.backgroundPosition = '-13px 0';
  }
  else if(this.who == 'bl') {
      button.style.backgroundPosition = '-26px 0';
  }
  else if(this.who == 'br') {
      button.style.backgroundPosition = '-39px 0';
  }
  else {
      button.style.display = 'none';
  }
}

dmCornerControl.prototype.getDefaultPosition = function() {
  if(this.who == 'tl') {
      return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0, 0));
  }
  else if(this.who == 'tr') {
      return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(0, 0));
  }
  else if(this.who == 'bl') {
      return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(0, 0));
  }
  else if(this.who == 'br') {
      return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(0, 0));
  }
}


////////////////////////////////////////////////////////////////////////////////
// Выводим карту
////////////////////////////////////////////////////////////////////////////////
dmObjectMap = function() {
}
dmObjectMap.prototype.init = function(x, y, scale, mapHolder) {
    if (typeof(mapHolder)=='undefined' || mapHolder=='') {
        mapHolder = 'dmClubMap';
    }
    var holder = document.getElementById(mapHolder);
    var point = new GLatLng(y, x);
    var marker = new GMarker(point);
    var map = new GMap2(holder);
    map.enableScrollWheelZoom();
    map.clearOverlays();
    map.setCenter(point, scale);
    map.addOverlay(marker);
    map.checkResize();
    map.addControl(new dmZoomControl());
    map.addControl(new dmCornerControl('tl'));
    map.addControl(new dmCornerControl('tr'));
    map.addControl(new dmCornerControl('bl'));
    map.addControl(new dmCornerControl('br'));
}

var dbCityMap = {
    
    init: function(x, y, scale, mapHolder) {
        if (typeof(mapHolder)=='undefined' || mapHolder=='') {
            mapHolder = 'dmClubMap';
        }
        this.holder = $(mapHolder);
        this.map = new GMap2(this.holder);
        this.map.enableScrollWheelZoom();
        this.map.clearOverlays();
        
        this.initCenter = new GLatLng(y, x);
        this.initScale = scale;
        
        this.map.setCenter(this.initCenter, this.initScale);
        this.map.checkResize();
        this.map.addControl(new dmZoomControl());
        this.map.addControl(new dmCornerControl('tl'));
        this.map.addControl(new dmCornerControl('tr'));
        this.map.addControl(new dmCornerControl('bl'));
        this.map.addControl(new dmCornerControl('br'));
        this.clubs = new Array();
        //this.customMarkerImg = new GIcon(G_DEFAULT_ICON, 'http://www.worldclass.ru/defa/images/flag.png');
        this.customMarkerImg = new GIcon();
        this.customMarkerImg.image = 'http://www.worldclass.ru/defa/images/flag.png';
        this.customMarkerImg.iconSize = new GSize(12, 19); 
        this.customMarkerImg.iconAnchor = new GPoint(6, 19);
        this.customMarkerImg.infoWindowAnchor = new GPoint(5, 1); 
    },
    
    addClub: function(x, y, scale, title, desId, printUrl) {
        //var markerOpt = new GMarkerOptions();
        var marker = new GMarker(new GLatLng(y, x), {'title': title, 'clickable': true, 'icon': this.customMarkerImg});
        this.map.addOverlay(marker);
        GEvent.addListener(marker, 'click', this.clickClubMap);
        marker.descriptionId = desId;
        marker.scale = scale;
        marker.parent = this;
        
        marker.printClick = function() {
            window.open(printUrl);
            return false;
        }
        
        this.clubs.push(marker);
    },
    
    resetSelection: function() {
        if (lastOpen!='') {
            $(lastOpen).style.height = '0px';
            this.getRelevantHref(lastOpen).className = '';
        }
    },
    
    setPrintEvent: function(handler) {
        $each($('city_switch').getElementsByTagName('A'), function(e){
            if (e.parentNode.className=='rt') {
                e.onclick = handler;
            }
        })
    },
    
    clickClubMap: function() {
        this.parent.resetSelection();
        
        var element = $(this.descriptionId);
        element.style.height = '22px';
        
        var scroll = $('clublist');
        scroll.scrollTop = element.parentNode.offsetTop - scroll.offsetTop - 20;
        
        this.parent.map.setCenter(this.getLatLng(), this.scale);
        this.openInfoWindow(this.getTitle());
        
        this.parent.getRelevantHref(this.descriptionId).className = 'turnoff';
        
        lastOpen = this.descriptionId;
        $('city_switch').style.visibility = 'visible';
    },
    
    clickClubHref: function(id) {
        this.resetSelection();
        
        var element = $(id);
        element.style.height = '22px';
        
        for (var i in this.clubs) {
            if (this.clubs[i].descriptionId == id) {
                this.map.setCenter(this.clubs[i].getLatLng(), this.clubs[i].scale);
                this.clubs[i].openInfoWindow(this.clubs[i].getTitle());
                this.setPrintEvent(this.clubs[i].printClick);
                break;
            }
        }
        
        this.getRelevantHref(id).className = 'turnoff';
        
        lastOpen = id;
        $('city_switch').style.visibility = 'visible';
        return false;
    },
    
    clickCitySwitch: function() {
        this.map.setCenter(this.initCenter, this.initScale);
        this.map.closeInfoWindow();
        
        this.resetSelection();
        
        lastOpen = '';
        
        $('city_switch').style.visibility = 'hidden';
        $('clublist').scrollTop = 0;
        return false;
    },
    
    getRelevantHref: function(id) {
        var element = $(id).parentNode;
        do {
            element = element.previousSibling;
            
        } while(typeof(element)!='undefined' && element!=null && element.tagName!='DT');
        if (typeof(element)!='undefined' && element!=null){
            var hrefs = element.getElementsByTagName('A');
            if (hrefs.length > 0) {
                return hrefs[0];
            }
        }
    }
    
}

