//mapMArkers
var mapMarkers = new Array();

function DrawMarkers(start_id)	{
  //partea care se refera la margine
  var mapBounds = new GLatLngBounds(mapSW, mapNE);
  //GLog.write(mapBounds);
  map.setZoom(map.getBoundsZoomLevel(mapBounds));
  //GLog.write(map.getZoom());
  //daca am mai multe puncte suprapuse tre sa redefinesc zoom levelul
  if (map.getZoom() >= 15 && p_html_ar.length >= 1) {
    map.setZoom(13);
  }
  //daca e article ii dam zumuri in minus
  if (articleON) {
    map.setZoom(map.getZoom()-3);
  }
  //daca nu e nici un item ii dam un zoom 13  
  if (!p_html_ar.length)  {
    map.setZoom(13);
  }
   
  //fct zoom in cazul in care utilizatorul il modifica in pagina  
  GEvent.addListener(map, 'zoomend', function() {
    zoom_level = map.getZoom();
    var mapMarker;
    //regenerarea tooltipurilor pt noul zoom
    for (i=0;i<p_html_ar.length;i++)	{
      mapMarker = mapMarkers[i];
      //GLog.write(mapMarker.oTooltip);
      mapMarker.oTooltip = getTooltip(mapMarker, mapMarker.oTooltip);
    }
  });
  //alert(p_html_ar);

  //generarea markerrelor
  for (i=0;i<p_html_ar.length;i++) {
    //GLog.write(p_html_ar[i]);
    oTR = parent.document.getElementById('trId' + i);
    var marker = AddMarker(p_x_ar[i], p_y_ar[i], p_html_ar[i], p_clickHtml_ar[i], oTR);
    map.addOverlay(marker);
    mapMarkers.push(marker);
    if (oTR)  {
      oTR.marker = marker;
    }
  }
  
}

function AddMarker(x, y, html, dhtml, oTR)	{
	var icon = new GIcon();
	
	icon.image = 'html/newspaper_icon.png';
	icon.iconSize = new GSize(30, 30);
	icon.iconAnchor = new GPoint(15, 15);
	icon.infoWindowAnchor = new GPoint(15, 15);


	var point = new GLatLng(parseFloat(y), parseFloat(x));
	var marker = new GMarker(point, icon);
	
  marker.tooltip = '<div class="pointTooltip">' + html + '</div>';
	var tooltipDIV = document.createElement('div');
	tooltipDIV.style.visibility = 'hidden';
	map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltipDIV);
	tooltipDIV = getTooltip(marker, tooltipDIV);
	marker.oTooltip = tooltipDIV;
	
	//GLog.write(marker.tooltip);
	
	GEvent.addListener(marker, 'mouseover', function() {
    tooltipDIV.style.visibility = 'visible';
    if (oTR)  {
      oTR.className = 'markerMouseOver';
    }
	});
	
	GEvent.addListener(marker, 'mouseout', function() {
    tooltipDIV.style.visibility = 'hidden';
    if (oTR)  {
      oTR.className = 'markerMouseOut';
    }
	});
  
  GEvent.addListener(marker, 'click', function() {
		//GLog.write(dhtml);
    //marker.openInfoWindowHtml(dhtml);
    top.location = dhtml;
	}); 

	return marker;
}

function PopupMarker(oTR, mid, direction)  {
  if (oTR.marker) {
    var tooltipDIV = oTR.marker.oTooltip;
    if (direction)  {
      tooltipDIV.style.visibility = 'visible';
    }
    else  {
      tooltipDIV.style.visibility = 'hidden';
    }
  }
  if (direction)  {
    oTR.className = 'markerMouseOver'
  }
  else  {
    oTR.className = 'markerMouseOut';
  }
}

function getTooltip(marker, current_tooltip) {

  mover_offset_x = -7;
  mover_offset_y = 7;
  
 	current_tooltip.innerHTML = marker.tooltip;
 	
	var point = map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true), map.getZoom());
	var offset = map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(), map.getZoom());
	
	var anchor = marker.getIcon().iconAnchor;
	var width = marker.getIcon().iconSize.width;
	var height = current_tooltip.clientHeight;
	
	var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width + mover_offset_x, offset.y - point.y -anchor.y -height + mover_offset_y));
  
	pos.apply(current_tooltip);
	
	return current_tooltip;
}

function FollowLink(oTR)  {
  if (oTR.childNodes[1])  {
    //alert(oTR.childNodes[1].childNodes[1].href);
    //document.location = oTR.childNodes[1].childNodes[1].href;
    document.location = oTR.childNodes[1].childNodes[1].href;
    return;
  }
  if (oTR.childNodes[0])  {
    document.location = oTR.childNodes[0].childNodes[1].href;
    return;
  }
}
