var elementsinlayers=[];
var deltat=10;
var ie=false;
var eTITLE;
var sTITLE;
if(document.all){
  ie=true;
  deltat=33;
}
/* for my localhost:
src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAuamPA-_XA1Ky_fKD5esMQxQZnrwjgXEDrhB247t0iRABcfzgghR_0dzywVhW6LvIkhB8HlqYoMm_Gg" type="text/javascript"

*/
var theHint;
var lon_min = 1000;
var lon_max = -1000;
var lat_min = 1000;
var lat_max = -1000;
//******************************************************************************
// A MMMarker is a simple overlay that outlines a lat/lng bounds on the
// map. It has a border of the given weight and color and can optionally
// have a semi-transparent background color.
var ie=false;
if(document.all){
  ie=true;
}
var ua=navigator.userAgent;
if(ua.indexOf('Opera')!=-1){ie=false;}
var picext='.png';if(ie){picext='.gif';}
var fadeOpacity=100;
var fadeIncrementValue=18;
var fadeIncrement=-fadeIncrementValue;
var TF;
var TBlink;
var blinkstate='none';
function startBlink(callerID){
	document.getElementById('divMMMarker'+callerID).style.display=blinkstate;
document.getElementById("bulina"+callerID).style.width='12px';
document.getElementById("bulina"+callerID).style.height='12px';
document.getElementById("bulina"+callerID).style.zIndex=100;

	if(blinkstate=='none'){blinkstate='block';}else{blinkstate='none';}
	setTimeout("startBlink("+callerID+");",300);
}
var allMMMmarkers=[];
var allMMMmarkers2=[];
function     startFade(callerID){
if(ie){fadeIncrementValue=30;}
	var O=fadeOpacity;
	fadeOpacity=fadeOpacity + fadeIncrement;
	if(fadeOpacity<20){fadeIncrement=fadeIncrementValue;}
	if(fadeOpacity>90){fadeIncrement=-fadeIncrementValue;}
	var O2=O/100;var f2=fadeOpacity/100;
	if(ie){
document.getElementById('divMMMarker'+callerID).style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+fadeOpacity+")";
TF=setTimeout("startFade("+callerID+");",140);
	}
	else{
	document.getElementById('divMMMarker'+callerID).style.opacity=O2;
	TF=setTimeout("startFade("+callerID+");",60);
	}
}
function     stopFade(callerID){
	clearTimeout(TF);
	fadeOpacity=100;
	fadeIncrement=-fadeIncrementValue;
	if(ie){
document.getElementById('divMMMarker'+callerID).style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=100)";
	}
	else{
	document.getElementById('divMMMarker'+callerID).style.opacity=1;
	}
}

function MMMarker(coords,dbID,imgsrc,markerWidth,markerHeight) {
  var rectBounds = new GLatLngBounds(
    new GLatLng(coords.lat(), coords.lng() ),
    new GLatLng(coords.lat() , coords.lng()));
  this.intCOORDS = coords;
  this.bounds_ = rectBounds;
  this.weight_ = 0;
  this.color_ = '#0F0';
  this.intID=dbID;
  this.intIMGSRC=imgsrc;
  this.intMARKERHEIGHT= markerHeight;
  this.intMARKERWIDTH = markerWidth;
  allMMMmarkers[dbID]=this;
  allMMMmarkers2.push(dbID);
}

MMMarker.prototype = new GOverlay();

// Creates the DIV representing this rectangle.
MMMarker.prototype.initialize = function(map) {
  // Create the DIV representing our rectangle
  var div = document.createElement("div");
  div.id='divMMMarker'+this.intID;
  div.style.border = "0px solid " + this.color_;
  div.background = 'none';
  div.onMouseOver='alert(5)';
  div.style.position = "absolute";
  var imght=getPNG( this.intIMGSRC ,this.intMARKERWIDTH,this.intMARKERHEIGHT,this.intID);
  div.innerHTML='<a href="javascript:MMMmouseclick('+this.intID+')" '+
                ' onMouseOut="stopFade('+this.intID+');MMMmouseout('+this.intID+')" '+
                ' onMouseOver="startFade('+this.intID+');MMMmouseover('+this.intID+')" '+
                ' style="border:0p solid #000">'+
                imght+
                '</a>';
  // Our rectangle is flat against the map, so we add our selves to the
  // MAP_PANE pane, which is at the same z-index as the map itself (i.e.,
  // below the marker shadows)

  map.getPane(G_MAP_MAP_PANE).appendChild(div);

  this.map_ = map;
  this.div_ = div;
}

// Remove the main DIV from the map pane
MMMarker.prototype.remove = function() {
  this.div_.parentNode.removeChild(this.div_);
}

// Copy our data to a new MMMarker
MMMarker.prototype.copy = function() {
  return new MMMarker(this.intCOORDS, this.weight_, this.color_,
                       this.backgroundColor_, this.opacity_);
}

// Redraw the rectangle based on the current projection and zoom level
MMMarker.prototype.redraw = function(force) {
  // We only need to redraw if the coordinate system has changed
  if (!force) return;

  // Calculate the DIV coordinates of two opposite corners of our bounds to
  // get the size and position of our rectangle
  var c1 = this.map_.fromLatLngToDivPixel(this.bounds_.getSouthWest());
  var c2 = this.map_.fromLatLngToDivPixel(this.bounds_.getNorthEast());

  // Now position our DIV based on the DIV coordinates of our bounds
  this.div_.style.width = this.intMARKERWIDTH  + "px";
  this.div_.style.height = this.intMARKERHEIGHT + "px";
  if(isNaN(c2.x)){this.div_.style.display='none';return;}
  this.div_.style.left = (Math.min(c2.x, c1.x) - this.weight_ -this.intMARKERWIDTH/2 ) + "px";
  this.div_.style.top = (Math.min(c2.y, c1.y) - this.weight_ -this.intMARKERHEIGHT ) + "px";
}

function getPNG(imgsrc,iw,ih,idd){
	var imght='';
	var imgwidth = 12;if(iw){imgwidth=iw;}
	var imgheight = 12;if(ih){imgheight=ih;}
	var id="";if(idd){id='id="bulina'+idd+'"';}
	var imgStyle = "cursor:hand;display:inline-block;" ;

	if(ie){	// height:" + imgheight + "px;
/*    imght ="<span "+id+
          " style=\"" + "width:" + imgwidth + "px;" + imgStyle + ";" +
           "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"   +
           "(src=\'" +imgsrc + "\', sizingMethod='crop');\"></span>" ;
*/
		imght =   '<img src="'+imgsrc+'" '+id+
			      ' border="0" style="cursor: pointer;cursor: hand;'+
				  'width:'+imgwidth+'px;height:'+imgheight+'px;" />';
	}
	else
	{
		imght =   '<img src="'+imgsrc+'" '+id+
			      ' border="0" style="cursor: pointer;cursor: hand;'+
				  'width:'+imgwidth+'px;height:'+imgheight+'px;" />';
	}
  return imght;
}

//******************************************************************************

function plotGrid2(T,Lag,colLat,colLon,Flag,Shadow,silentOnErrors,skip,displayProcentage){
      if(map==null){
        if(!silentOnErrors) document.write('<br>GMap not init-ed. use initMap(div_id) first!');
        return false;
      }

      var R=T.rows[0];
      var hh=R.innerHTML;
      hh=hh.toUpperCase();
      if(hh.indexOf('</TH>')==-1){
        if(!silentOnErrors) document.write('Bad ID : "'+gridID+'", not an ASP.NET (basic) grid');
        return false;
      }
      var P;
      var oldprocent = -1;
      for(i=1+skip;i<T.rows.length;i++){

        R=T.rows[i];
        var Clat=R.cells[colLat];
        var lat=Clat.innerHTML;
        lat=lat.replace(/ /g,'');
        lat=lat.replace(/&nbsp;/g,'');
        lat=lat.replace(/,/g,'.'); //bullsht german windows
        var Clon=R.cells[colLon];
        var lon=Clon.innerHTML;
        lon=lon.replace(/ /g,'');
        lon=lon.replace(/&nbsp;/g,'');
        lon=lon.replace(/,/g,'.');

        if((lat==null)||(lat=='')){
          if(!silentOnErrors) document.write('<br>Missing Latitude information, entry nr.<b>'+i+'</b>');
        }
        if((lon==null)||(lon=='')){
          if(!silentOnErrors) document.write('<br>Missing Longitude information, entry nr.<b>'+i+'</b>');
        }
        lat=parseFloat(lat);
        lon=parseFloat(lon);
        if (lon_min > lon) lon_min=lon;
        if (lat_min > lat) lat_min=lat;
        if (lon_max < lon) lon_max=lon;
        if (lat_max < lat) lat_max=lat;
        var dlt=Lag+i;
        var fRun = 'map.addOverlay(new MMMarker(new GLatLng('+lat+','+lon+'),'+dlt+',"'+Flag+'",12,20));';
        var procent = parseInt(100 * i/ T.rows.length);
        if((displayProcentage==1)&&(oldprocent!=procent)&&((procent % 5)==0)){
           oldprocent=procent;
           fRun = fRun + "eTITLE.innerHTML = sTITLE + ' ("+procent+"% loaded )';";
        }
//        map.addOverlay(new MMMarker(new GLatLng(lat,lon),Lag+i,Flag,12,20));
        setTimeout(fRun,parseInt(deltat*i));
     }
     if(displayProcentage){
         var fRun2 = "eTITLE.innerHTML = sTITLE + ' (100% loaded )';setTimeout('eTITLE.innerHTML = sTITLE ',5000);";
         setTimeout(fRun2,parseInt(deltat*T.rows.length+deltat*10));
     }
     return true;
}
function pngFix(){
var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])

if ((version >= 5.5) && (document.body.filters))
{
   for(var i=0; i<document.images.length; i++)
   {
      var img = document.images[i]
      var imgName = img.src.toUpperCase()
      if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
      {
         var imgID = (img.id) ? "id='" + img.id + "' " : ""
         var imgClass = (img.className) ? "class='" + img.className + "' " : ""
         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
         var imgStyle = "display:inline-block;" + img.style.cssText
         if (img.align == "left") imgStyle = "float:left;" + imgStyle
         if (img.align == "right") imgStyle = "float:right;" + imgStyle
         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
         var strNewHTML = "<span " + imgID + imgClass + imgTitle
         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
         img.outerHTML = strNewHTML
         i = i-1
      }
   }
}
}


// A MMMhint is a simple overlay that outlines a lat/lng bounds on the
// map. It has a border of the given weight and color and can optionally
// have a semi-transparent background color.
function MMMhint(coords, opt_weight, opt_color) {
  var rectBounds = new GLatLngBounds(
    new GLatLng(coords.lat(), coords.lng() ),
    new GLatLng(coords.lat() , coords.lng()));
  this.intCOORDS = coords;
  this.bounds_ = rectBounds;
  this.weight_ = opt_weight || 2;
  this.color_ = opt_color || "#888888";
}
MMMhint.prototype = new GOverlay();

var mapHeight=300;
var mapWidth=400;
// Creates the DIV representing this rectangle.
MMMhint.prototype.initialize = function(map) {
  // Create the DIV representing our rectangle
  var div = document.createElement("div");
  mapHeight=parseInt(document.getElementById("map").style.height+0);
  mapWidth=parseInt(document.getElementById("map").style.width+0);
  div.style.border = this.weight_ + "px solid " + this.color_;
  div.style.className = 'blurry';
  div.style.display = 'none';
  div.style.position = "absolute";
  div.id='MMMhint';
  // Our rectangle is flat against the map, so we add our selves to the
  // MAP_PANE pane, which is at the same z-index as the map itself (i.e.,
  // below the marker shadows)
  map.getPane(G_MAP_MAP_PANE).appendChild(div);

  this.map_ = map;
  this.div_ = div;
}

// Remove the main DIV from the map pane
MMMhint.prototype.remove = function() {
  this.div_.parentNode.removeChild(this.div_);
}

// Copy our data to a new MMMhint
MMMhint.prototype.copy = function() {
  return new MMMhint(this.bounds_, this.weight_, this.color_,
                       this.backgroundColor_, this.opacity_);
}

// Redraw the rectangle based on the current projection and zoom level
MMMhint.prototype.redraw = function(force) {
  // We only need to redraw if the coordinate system has changed
  if (!force) return;

  // Calculate the DIV coordinates of two opposite corners of our bounds to
  // get the size and position of our rectangle
  var c1 = this.map_.fromLatLngToDivPixel(this.intCOORDS);
  var c2 = this.map_.fromLatLngToDivPixel(this.intCOORDS);

  // Now position our DIV based on the DIV coordinates of our bounds
  this.div_.style.width = "200px";
  this.div_.style.height =  "auto";
  var x = parseInt( (Math.min(c2.x, c1.x) - this.weight_+10) );
  var y  =  parseInt((Math.min(c2.y, c1.y) - this.weight_ - 100));
	var bounds = this.map_.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var SWpx=this.map_.fromLatLngToDivPixel(southWest);
	var NEpx=this.map_.fromLatLngToDivPixel(northEast);
	var CNTR = this.map_.fromLatLngToDivPixel(this.map_.getCenter());
  var ox= parseInt(x);var oy= parseInt(y);
  x+=20;
  if(x>CNTR.x+50){x=x-250;}
 y= parseInt(Math.max(y, parseInt(NEpx.y)+40)); y= parseInt(Math.min(y, parseInt(SWpx.y)-160));
 //x= parseInt(Math.max(x, parseInt(SWpx.x)+60)); x= parseInt(Math.min(x, parseInt(NEpx.x)-210));
  this.div_.style.left = x + "px";
  this.div_.style.top = y + "px";

}


function getPNG(imgsrc){
var imght='';
if(ie){
   var imgStyle = "cursor:hand;display:inline-block;" ;
   var imgwidth = 12;
   var imgheight = 20;
    imght ="<span " +
          " style=\"" + "width:" + imgwidth + "px; height:" + imgheight + "px;" + imgStyle + ";" +
           "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"   +
           "(src=\'" +imgsrc + "\', sizingMethod='scale');\"></span>" ;

  }
  else {
    imght =   '<img src="'+imgsrc+'" '+
                ' border="0" style="cursor: pointer;cursor: hand" '+
                '/>';
  }
  return imght;
}

