/// utils
	function GetElem(idOrName) 
	{
		return document.getElementById(idOrName);
	}

	
	function IsComplete(elem) 
	{
		if (elem.readyState) {
			return elem.readyState == "complete";
		}
		else {
			return elem.complete;
		}
	}

  
  function LoadXMLDocument(url, obj, handlerName) 
  {
  	var req;


    var handlerRef = function() {
      if (req.readyState == 4) {
        if (req.status == 200) {
        	if (handlerName) {
	  		  	obj[handlerName](req.responseXML);
	  		  }
	  		  else {
	  		  	obj(req.responseXML);
	  		  }
        } 
        else {
				  document.write(req.responseText);
          alert("There was a problem retrieving the XML data:\n" + req.statusText); 
        }
      }
    }

    if (window.XMLHttpRequest) {
      req = new XMLHttpRequest();
      req.onreadystatechange = handlerRef;
      req.open("GET", url, true);
      req.send(null);
    } 
    else if (window.ActiveXObject) {
      req = new ActiveXObject("Microsoft.XMLHTTP");
      if (req) {
        req.onreadystatechange = handlerRef;
        req.open("GET", url, true);
        req.send();
      }
    }
  }


  function GetNodeText(node) 
  {
    var s = "";
    var childs = node.childNodes;
   	for (var i = 0; i < childs.length; i++) {
   		if (childs[i].nodeType == 3) {
   			s += childs[i].nodeValue;
   		}
   	}
   	return s;
  }


	function ShowElementProperties(elem) 
	{
		var s = "";
		for (var i in elem) {
			s += i + " ";
		}
		alert(s);
	}



/// Point
  function Point(x, y)
  {
  	this.X = x;
  	this.Y = y;
  }




  Point.prototype.Assign = function (x, y)
  {
  	this.X = x;
  	this.Y = y;
  }


///Rect
  function Rect(x1, y1, x2, y2)
  {
  	this.X1 = x1;
  	this.Y1 = y1;
  	this.X2 = x2;
  	this.Y2 = y2;
  }


  Rect.prototype.Assign = function (x1, y1, x2, y2)
  {
  	this.X1 = x1;
  	this.Y1 = y1;
  	this.X2 = x2;
  	this.Y2 = y2;
  }


  Rect.prototype.Dx = function ()
  {
  	return this.X2-this.X1;
  }


  Rect.prototype.Dy = function ()
  {
  	return this.Y2-this.Y1;
  }


  Rect.prototype.Offset = function (x, y)
  {
  	this.X1 += x;
  	this.Y1 += y;
  	this.X2 += x;
  	this.Y2 += y;
  }

  function Point(x, y)
  {
  	this.X = x;
  	this.Y = y;
  }

  Rect.prototype.GetCenter = function ()
  {
  	return new Point((this.X1 + this.X2)/2, (this.Y1 + this.Y2)/2);
  }


/// Matrix
  function Matrix()
  {
  	this.Clear();
  }


  Matrix.prototype.Assign = function (a, b, c, d, e, f)
  {
  	this.A = a; this.B = b; this.C = c;
  	this.D = d; this.E = e; this.F = f;
  }


  Matrix.prototype.Clear = function ()
  {
  	this.Assign(1, 0, 0, 0, 1, 0);
  }


  Matrix.prototype.Mul = function (a, b, c, d, e, f)
  {
  	this.Assign(
  		this.A*a + this.D*b,
  		this.B*a + this.E*b,
  		this.C*a + this.F*b + c,
  		this.A*d + this.D*e,
  		this.B*d + this.E*e,
  		this.C*d + this.F*e + f);
  }


  Matrix.prototype.AddTranslation = function (x, y)
  {
  	this.Mul(1, 0, x, 0, 1, y);
  }


  Matrix.prototype.AddScaling = function (x, y)
  {
  	this.Mul(x, 0, 0, 0, y, 0);
  }


  Matrix.prototype.AddRotation = function (a)
  {
  	var sin_a = Math.sin(a);
  	var cos_a = Math.cos(a);
  	this.Mul(cos_a, -sin_a, 0, sin_a, cos_a, 0);
  }


  Matrix.prototype.AssignBackward = function (m)
  {
  	var d = m.A*m.E - m.B*m.D;
  	if(d == 0)
  		this.Clear();
  	else 
  		this.Assign(
  			m.E/d, -m.B/d, (m.B*m.F - m.C*m.E)/d,
  			-m.D/d, m.A/d, (m.C*m.D - m.A*m.F)/d);
  }


  Matrix.prototype.ProjectPoint = function (p)
  {
  	p.Assign(
  		this.A*p.X + this.B*p.Y + this.C,
  		this.D*p.X + this.E*p.Y + this.F);
  }


/// Layer
  function Layer(name, caption, image)
  {
  	this.Name = name;
  	this.Caption = caption;
  	this.Image = image;
  }


/// MapImage
  function MapImage(db)
  {
  	this.Width = 500;
  	this.Height = 500;
  	this.Dpi = 120;
  	this.CenterX = 0;
  	this.CenterY = 0;
  	this.YMirror = true;
  	this.XAngle = Math.PI/2.0;
  	this.ZoomScale = 1/10000;
  	this.Projection = new Matrix();
  	this.BackwardProjection = new Matrix();
  	this.PaperRect = new Rect();
  	this.SelectedObject = "";

	this.Db = db;

	this.DragMode = false;
	this.DragStarted = false;
	this.DragStartX = 0;
	this.DragStartY = 0;
	this.DragStartImageLeft = 0;
	this.DragStartImageTop = 0;
/////////////////////BERDNIKOV///////////////
        this.moveend;
        this.infoWindow;
/////////////////////BERDNIKOV///////////////
   }


  MapImage.prototype.PaperToDeviceScale = function () 
  {
  	return this.Dpi/0.0254;
  }


  MapImage.prototype.SetPaperOffset = function (x, y)
  {
  	this.PaperRect.Offset(x - this.PaperRect.X1, y - this.PaperRect.Y1);
  }


  MapImage.prototype.PointPaperToDevice = function (p) 
  {
  	p.Assign(
  		(p.X - this.PaperRect.X1)*this.PaperToDeviceScale(),
  		this.Height - (p.Y - this.PaperRect.Y1)*this.PaperToDeviceScale());
  }


  MapImage.prototype.PointDeviceToPaper = function (p) 
  {
  	p.Assign(
  		p.X/this.PaperToDeviceScale() + this.PaperRect.X1,
  		(this.Height - p.Y)/this.PaperToDeviceScale() + this.PaperRect.Y1);
  }


  MapImage.prototype.PointWorldToDevice = function (p) 
  {
  	this.Projection.ProjectPoint(p);
  	this.PointPaperToDevice(p);
  }


  MapImage.prototype.PointDeviceToWorld = function (p) 
  {
  	this.PointDeviceToPaper(p);
  	this.BackwardProjection.ProjectPoint(p);
  }


  MapImage.prototype.ProjectionChanged = function () 
  {
        showLoading();
  	this.PaperRect = new Rect(0, 0, this.Width/this.PaperToDeviceScale(), this.Height/this.PaperToDeviceScale());
  	this.Projection.Clear();
  	if(this.YMirror) {
  		this.Projection.AddScaling(1, -1);
  	}
  	this.Projection.AddScaling(this.ZoomScale, this.ZoomScale);
  	this.Projection.AddRotation(this.XAngle);
  	var paperCenter = new Point(this.CenterX, this.CenterY);
  	this.Projection.ProjectPoint(paperCenter);
  	this.SetPaperOffset(paperCenter.X - this.PaperRect.Dx()/2, paperCenter.Y - this.PaperRect.Dy()/2)	
  	this.BackwardProjection.AssignBackward(this.Projection);
  	var layerNames = "";

  	var visibleLayers = this.Db.LayerList.GetVisibleLayers();
  	for (var i = 0; i < visibleLayers.length; i++) {
 			if(layerNames != "") {
 				layerNames += ",";
 			}
 			layerNames += visibleLayers[i].Name;
  	}

  	if (this.ImgElement != null) {
  		this.ImagePlaceholder.style.width = this.Width;
  		this.ImagePlaceholder.style.height = this.Height;
    	this.ImgElement.src = 
    		this.Db.Config["page_image"] +  
    		"?Db=" + this.Db.Name + 
    		"&Layers=" + layerNames +
    		"&CenterX=" + this.CenterX + 
    		"&CenterY=" + this.CenterY +
    		"&Width=" + this.Width +
    		"&Height=" + this.Height +
    		"&XAngle=" + this.XAngle +
    		"&YMirror=" + this.YMirror +
    		"&ZoomScale=" + this.ZoomScale +
    		"&SelectedObject=" + this.SelectedObject;
    }
  }


  MapImage.prototype.Scroll = function (hor, ver) 
  {
  	var center = this.PaperRect.GetCenter();
  	center.X += this.PaperRect.Dx()/4*hor;
  	center.Y += this.PaperRect.Dy()/4*ver;
  	this.BackwardProjection.ProjectPoint(center);
  	this.CenterX = center.X;
  	this.CenterY = center.Y;
  	this.ProjectionChanged();
  }


  MapImage.prototype.ScrollDelta = function (dx, dy) 
  {
//        showLoading();
  	var center = new Point(this.Width/2, this.Height/2);
  	center.X -= dx;
  	center.Y += dy;
  	this.PointDeviceToPaper(center);
  	this.BackwardProjection.ProjectPoint(center);
  	this.CenterX = center.X;
  	this.CenterY = center.Y;
  	this.ProjectionChanged();
  }

  
  MapImage.prototype.Zoom = function (multiplier)
  {
//        showLoading();
  	this.ZoomScale *= multiplier;
  	this.ProjectionChanged();
  }

  
  MapImage.prototype.SelectedObject = function (selectedObject)
  {
  	this.SelectedObject = selectedObject;

  }

	
	MapImage.prototype.Navigate = function (x, y, objectId) 
	{
    this.CenterX = x;
    this.CenterY = y;
    this.SelectedObject = objectId;
    this.ProjectionChanged();
  }

////////////////Berdnikov////////////

	MapImage.prototype.Point2LatLng = function (point) 
	{
           var x = point.X;
           var y = point.Y;
 
           var x1 = 23654;
           var y1 = 24774;
           var lng1 = 49.3511;
           var lat1 = 53.5362933333333;
           var angle = 0.0231283356450711;
 
           var lat = (y - y1) * Math.sin(angle) + (x - x1) * Math.cos(angle);
           var lng = (y - y1) * Math.cos(angle) - (x - x1) * Math.sin(angle);

           var LL = new ILatLng();
           LL.Lat = lat1 + (lat * 360) / (40008600);
           LL.Lng = lng1 + (lng * 360) / (40075700 * Math.cos(Math.PI * lat1/180));

           return LL;
        } 

	MapImage.prototype.LatLng2Point = function (LL) 
	{
 
           var x1 = 23654;
           var y1 = 24774;
           var lng1 = 49.3511;
           var lat1 = 53.5362933333333;
           var angle = 0.0231283356450711;

           var lat = LL.Lat - lat1;
           var lng = LL.Lng - lng1;

           lat = 40008600 * lat/360;
           lng = (40075700 * Math.cos(Math.PI * lat1/180)) * lng/360;

           var x = x1 + lng * Math.sin(-angle) + lat * Math.cos(-angle);
           var y = y1 + lng * Math.cos(-angle) - lat * Math.sin(-angle);

           var point = new Point(x,y);
           return point;
        } 

	MapImage.prototype.addMarker = function (marker) 
	{
           if(!marker){return;}
           this.ImagePlaceholder.appendChild(marker.div);
//           marker.div.style.zIndex   = "5";
           marker.map = this;
        }

        MapImage.prototype.removeMarker = function(marker)
        {
           if(!marker){return;}
           this.ImagePlaceholder.removeChild(marker.div);
           marker.map = false;
        }

        MapImage.prototype.chkMarkers = function()
        {
//           var markers = this.ImagePlaceholder.getElementsByName('div');
             if(pointers && pointers.length > 0)
             {
                for(var i = 0; i < pointers.length;i++)
                {
                   if(pointers[i].marker) {this.setLatLng(pointers[i].marker);}
                }     
             }
        }

        MapImage.prototype.setLatLng = function(marker,LL)
        {
           if(LL)
           {
              marker.LatLng = LL; 
           }
           else
           {
              LL = marker.LatLng; 
           }  
           var point = this.LatLng2Point(LL);
           this.PointWorldToDevice(point);

           var x = parseInt(point.X) - parseInt(marker.icon.clientWidth/2);
           var y = parseInt(point.Y) - parseInt(marker.icon.clientHeight/2);
           marker.div.style.left = x + 'px';
           marker.div.style.top  = y + 'px';

//           marker.div.style.left = parseInt(point.X) - parseInt(marker.icon.style.width/2) +'px';
//           marker.div.style.top  = parseInt(point.Y) - parseInt(marker.icon.style.height/2) + 'px';
//alert(marker.icon.clientHeight);
//           marker.div.style.left = parseInt(point.X) + 'px';
//           marker.div.style.top  = parseInt(point.Y) + 'px';

        }

	IMarker = function(icon)
        {
           if(!icon)
           {
              icon = new IIcon('/pointers/903.ico');
           }

           this.icon = icon;
           this.image;

           this.LatLng = new ILatLng();
           this.Point  = new Point();
           this.IMEI;
           this.time;
           this.map;

           var div = document.createElement('div');
           div.style.position = 'absolute';
           div.style.left = '-100px';
           div.style.top  = '-100px';
           div.style.zIndex   = "3";
//           div.onclick    = infoWindow;

           div.appendChild(this.icon);

           this.div = div;

           this.setLatLng = function(LatLng)
           {
              this.LatLng = LatLng;

              if(this.map)
              {
                 this.map.setLatLng(this,LatLng);
              }

           }
           this.setPoint = function(LatLng)
           {
              this.setLatLng(LatLng);
           }

           this.getLatLng = function()
           {
              return this.LatLng; 
           }

           this.getPoint = function()
           {
              return this.getLatLng();
           }

           this.setImage = function(url)
           {
              if(this.image)
              {
                 if(!url)
                 {
                    this.div.removeChild(this.image);
                    this.image = false;
                 }
                else
                {
                   this.image.src = url;
                }
             }
             else
             {
                var img = document.createElement('img');
                img.src = url;
                img.style.position = 'absolute';
                img.style.width = this.icon.style.width;
                img.style.height= this.icon.style.height;

                if(this.icon){img.style.cursor = this.icon.style.cursor;}                 
                
                this.div.appendChild(img);
                this.image = img;
             }
           }
        }

        function IIcon(url,width,height)
        {
           var img = document.createElement('img');
           img.src = url;
           img.style.position = 'absolute';
           if(width)
           {
              img.style.width  = width+'px';
           }
           if(height)
           {
              img.style.height = height+'px';
           }
           return img;
        }

///ILatLng
  function ILatLng(lat,lng)
  {
     this.Lat = lat;
     this.Lng = lng;
  }

  function IPoint(x, y)
  {
  	this.X = x;
  	this.Y = y;
  }
  IPoint.prototype.Assign = function (x, y)
  {
  	this.X = x;
  	this.Y = y;
  }

///end mapImage methods (Berdnikov)



	function ImgElement_OnDragStart() 
	{
		window.event.returnValue = false;
	}


	function ImgElement_OnMouseDown(ev) 
	{
		var mapImage = this["DataObject"];
                if(!ev)ev=window.event;    

		var x = ev.offsetX;
		var y = ev.offsetY;

 		mapImage.DragMode = true;
 		mapImage.DragStartX = ev.clientX;
 		mapImage.DragStartY = ev.clientY;
 		mapImage.DragStartImageLeft = parseInt(mapImage.ImgElement.style.left + 0);
 		mapImage.DragStartImageTop  = parseInt(mapImage.ImgElement.style.top + 0);

 		mapImage.DragStarted = false;
                return false;	
	}


	function ImgElement_EndDrag (mapImage, evt) 
	{
		if (mapImage.DragMode) {
	 		mapImage.DragMode = false;
			if (mapImage.DragStarted == true) {
				mapImage.DragStarted = false;
				var dx = evt.clientX - mapImage.DragStartX;
				var dy = evt.clientY - mapImage.DragStartY;
				mapImage.ScrollDelta(dx, -dy);
                                if(mapImage.moveend){mapImage.moveend();}
			}
			else {
				if (mapImage.Db.OnClick) {
					var onClickEvt = new Object();
					onClickEvt.Db = mapImage.Db;
					onClickEvt.X = evt.offsetX;
					onClickEvt.Y = evt.offsetY;

//alert('mapImage.Db.onclick')
//					mapImage.Db.OnClick(onClickEvt);
				}
				else {
//					mapImage.Db.ObjectList.SearchByCoordinates(evt.offsetX, evt.offsetY);
                                        mapImage.infoWindow.div.style.display = 'none' ;
                                        showCoords(mapImage,evt.clientX,evt.clientY);
                                        
				}
			}
		}
	}



//////////////////////////////////
function showCoords(image,x,y)
{
   var center = new Point(x, y);

//   image.PointDeviceToPaper(center);
//   image.BackwardProjection.ProjectPoint(center);
     image.PointDeviceToWorld(center);

//   var lat = 53.3234280843118 + center.X * 360 / 40008600;
//   var lng = 48.9770941478634 + (center.Y * 360) / (40075700 * Math.cos(Math.PI * 53.5362233333333/180));

//   var ll = xy2LatLng(center.X,center.Y);

   var ll = image.Point2LatLng(center);
   var point = image.LatLng2Point(ll);


//   var icon   = new IIcon('/pointers/Cars20.ico',24);
//   var marker = new IMarker(icon);

//   image.addMarker(marker);

//   var lng1 = 49.3511;
//   var lat1 = 53.5362933333333;

//   var lng1 = 49.2830583333333;
//   var lat1 = 53.5375766666667;

//   var LL = new ILatLng(lat1,lng1)

//   image.setLatLng(marker,LL);
   
   var info = document.getElementById('info');
   if (info)
   {
//   document.getElementById('info').innerHTML= 'x='+parseInt(center.X) + '  y=' + parseInt(center.Y) + ' ZoomScale=1/'+parseInt(1/image.ZoomScale) + ' Lat='+ll.Lat+' Lng='+ll.Lng + 'X='+point.X + 'Y='+point.Y;
//   info.innerHTML= 'x='+parseInt(center.X) + '  y=' + parseInt(center.Y) + ' ZoomScale=1/'+parseInt(1/image.ZoomScale);

//   info.innerHTML= 'x='+1.00310324869 * center.X/(Math.cos(Math.PI*center.Y/180)) + '  y=' + center.Y + ' ZoomScale='+image.ZoomScale;
//   info.innerHTML= 'x='+center.X/(Math.cos(Math.PI*center.Y/180)) + '  y=' + center.Y + ' ZoomScale='+image.ZoomScale;
   info.innerHTML= 'x='+center.X + '  y=' + center.Y + ' ZoomScale='+image.ZoomScale + ' Lat='+ll.Lat +' Lng='+ll.Lng + ' X=' + point.x + ' Y='+point.y;
   }
}

function xy2LatLng(x,y)
{
   var x1 = 23654;
   var y1 = 24774;
   var lng1 = 49.3511;
   var lat1 = 53.5362933333333;
   var angle = 0.0231283356450711;
 
   var lat = (y - y1) * Math.sin(angle) + (x - x1) * Math.cos(angle);
   var lng = (y - y1) * Math.cos(angle) - (x - x1) * Math.sin(angle);

   var X = x1 + lng * Math.sin(-angle) + lat * Math.cos(-angle);
   var Y = y1 + lng * Math.cos(-angle) - lat * Math.sin(-angle);

//   var Y = y1 + lat*Math.sin(angle) - lng*Math.cos(angle) + y1*(Math.sin(angle)*Math.sin(angle) - Math.cos(angle)*Math.cos(angle) ) + 2*x1*Math.cos(angle)*Math.sin(angle);

   var LL = new LatLng();
   LL.Lat = lat1 + (lat * 360) / (40008600);
   LL.Lng = lng1 + (lng * 360) / (40075700 * Math.cos(Math.PI * lat1/180));
   LL.Y = Y;
   LL.X = X;

   return LL;
}

function LatLng()
{
   this.Lat = 0;
   this.Lng = 0;
   this.Y =0;
   this.X =0;
}


//////////////////////////////////




	function ImgElement_OnMouseUp (ev) 
	{
                if(!ev)ev=window.event;    
		ImgElement_EndDrag(this["DataObject"], ev);
	}

	
	function ImgElement_OnMouseLeave() 
	{
		ImgElement_EndDrag(this["DataObject"], window.event);
	}


	function ImgElement_OnMouseMove(ev) {
		var shift = 5;
                if(!ev)ev=window.event;    

		var mapImage = this["DataObject"];

		if (mapImage.DragMode == true) {
			var dx = ev.clientX - mapImage.DragStartX;
			var dy = ev.clientY - mapImage.DragStartY;

			if (mapImage.DragStarted == false) {
				if (Math.abs(dx) > shift || Math.abs(dy) > shift) {
					mapImage.DragStarted = true;
				}
			}
			
			if (mapImage.DragStarted == true) {
//ImagePlaceholder
				mapImage.ImagePlaceholder.style.left = mapImage.DragStartImageLeft + dx;
				mapImage.ImagePlaceholder.style.top  = mapImage.DragStartImageTop + dy;

//				mapImage.ImgElement.style.left = mapImage.DragStartImageLeft + dx;
//				mapImage.ImgElement.style.top  = mapImage.DragStartImageTop + dy;
			}
                        return false;
		}
	}

	function ImgElement_OnWheel(ev) {
                if(!ev)ev=window.event;    
		var mapImage = this["DataObject"];
                if(document.all)
                {
                   mapImage.Zoom(1-(ev.wheelDelta/1200));
                }
                else
                {  
                   mapImage.Zoom(1+(ev.detail/24));
                }
                if(mapImage.moveend){mapImage.moveend();}
	}


	function ImgElement_OnReadyStateChange() {
		if (IsComplete(this)) {

                        var mapImage = this["DataObject"];

			mapImage.ImagePlaceholder.style.left = 0;
			mapImage.ImagePlaceholder.style.top = 0;

//			this.style.left = 0;
//			this.style.top = 0;
                        
                        mapImage.chkMarkers();
                        mapImage.setInfoWindowLatLng();    

                        var loading = document.getElementById('loading');
                        if(loading){loading.style.display = 'none';}
		}
	}


function showLoading()
{
                        var loading = document.getElementById('loading');
                        if(loading){loading.style.display = '';}
}


/// MapObjectList 
	function MapObjectList(db) {
		this.Db = db;
	}


	MapObjectList.prototype.TextSearch = function (filter) 
	{
  	if(filter == "") {
  		this.Placeholder.innerHTML = "<font color='red'><b>Не задана строка поиска</b></font>";
  	}
  	else {
  		LoadXMLDocument(this.Db.Config["page_search"] + "?db=" + this.Db.Name + "&filter=" + escape(filter), this, "TextSearch_XmlRequestHandler");
  	}
	}

  MapObjectList.prototype.SearchByCoordinates = function (deviceX, deviceY) {
 		var p = new Point(deviceX, deviceY);
 		this.Db.Image.PointDeviceToWorld(p);
 		                             	
  	LoadXMLDocument(this.Db.Config["page_search"] + "?db=" + this.Db.Name + "&X=" + p.X + "&Y=" + p.Y + "&ZoomScale=" + this.Db.Image.ZoomScale, 
  		this, "SearchByCoordinates_XmlRequestHandler");
  }

  MapObjectList.prototype.NavigateToObject = function (objectID) {		                             	
  	LoadXMLDocument(this.Db.Config["page_search"] + "?db=" + this.Db.Name + "&ObjectId=" + objectID, 
  		this, "SelectObjects_XmlRequestHandler");
  }

	var GetNewElement = function (image, node) {
        var id = node.getAttribute("Id");
  	var x = node.getAttribute("X");
  	var y = node.getAttribute("Y");

 		var table = document.createElement("table");
 		var tableBody = document.createElement("tbody");
 		table.appendChild(tableBody);

 		var tr = document.createElement("tr");
 		tableBody.appendChild(tr);

 		var td = document.createElement("td");
 		tr.appendChild(td);

   	var img = document.createElement("img");
   	img.src = "images/go_rtl.png";
   	img.title = "";
   	img.style.cursor = "hand";
  	img.onclick = function () { image.Navigate(x, y, id); }
   	td.appendChild(img);

 		td = document.createElement("td");
 		tr.appendChild(td);
 		td.innerHTML = GetNodeText(node);

  	return table;
  }


	MapObjectList.prototype.AfterSearch = function(xmlDoc, doSelect, centerObject)
	{
		if (this.Placeholder != null) {
			this.Placeholder.innerHTML = "";

     	var objectList = xmlDoc.documentElement.getElementsByTagName('Object');
     	var head = document.createElement("span"); 
     	var totalFound = Number(xmlDoc.documentElement.getAttribute("TotalFound"));

     	if (totalFound > objectList.length) {
	     	head.innerHTML = "Показано " + objectList.length + " объектов из " + totalFound + " найденных<br>";
     	} 
     	else  {
	     	head.innerHTML = "Найдено объектов: <b>" + objectList.length + "</b><br>";
     	}
     	
     	this.Placeholder.appendChild(head);

     	for (var i = 0; i < objectList.length; i++) {
     		this.Placeholder.appendChild(GetNewElement(this.Db.Image, objectList[i]));
     	}

   		if (objectList.length > 0) {
   			var node1 = objectList[0];

				if (doSelect) {
					this.Db.Image.SelectedObject = node1.getAttribute("Id");
				}

				if (centerObject) {
	   			this.Db.Image.CenterX = node1.getAttribute("X");
		 			this.Db.Image.CenterY = node1.getAttribute("Y");
		 		}
   		}

      this.Db.Image.ProjectionChanged();
 		}
	}
	
	
	MapObjectList.prototype.TextSearch_XmlRequestHandler = function (xmlDoc) 
	{
	  this.AfterSearch(xmlDoc, false, false);
	}

	MapObjectList.prototype.SearchByCoordinates_XmlRequestHandler = function (xmlDoc) 
	{
	  this.AfterSearch(xmlDoc, true, false);
	}

	MapObjectList.prototype.SelectObjects_XmlRequestHandler = function (xmlDoc) 
	{
	  this.AfterSearch(xmlDoc, true, true);
	}


/// MapLayerList
	function MapLayerList(db) {
		this.Db = db;
  	this.Layers = new Array();
  	this.LayersVisibility = new Array();

		LoadXMLDocument(this.Db.Config["page_layers"] + "?db=" + this.Db.Name , this, "InitLayerList");
	}


	MapLayerList.prototype.InitLayerList = function (xmlDoc) 
	{
  	this.Layers = new Array();

   	var objectList = xmlDoc.documentElement.getElementsByTagName('Layer');

   	for (var i = 0; i < objectList.length; i++) {
   		var node =  objectList[i];
   		this.Layers[i] = new Layer(node.getAttribute("Name"), node.getAttribute("Caption"), node.getAttribute("Image"));
   		this.LayersVisibility[this.Layers[i].Name] = true;
   	}
   	
   	this.BindLayerList();
  }


	MapLayerList.prototype.BindLayerList = function () 
	{
	  var GetLayerControl = function (layerListControl, layerName) {
   		var cb = document.createElement("input");
   		cb.type= "checkbox";

   		cb.onclick  = function () {
   			layerListControl.LayersVisibility[layerName] = this.checked;
   			layerListControl.Db.Image.ProjectionChanged();
   		}

   		return cb;
	  }
		
		if (this.Placeholder != null) {
			this.Placeholder.innerHTML = "";

   		var table = document.createElement("table");
   		this.Placeholder.appendChild(table);
   		
   		var tableBody = document.createElement("tbody");
   		table.appendChild(tableBody);

     	for (var i = 0; i < this.Layers.length; i++) {
     		var layer = this.Layers[i];

     		var tr = document.createElement("tr");
     		tableBody.appendChild(tr);

     		var td = document.createElement("td");
     		tr.appendChild(td);
     		
     		var cb = GetLayerControl(this, layer.Name);
     		td.appendChild(cb);
     		cb.checked = true;

     		var label = document.createElement("span");
     		label.innerHTML = layer.Caption;
     		td.appendChild(label);
     	}

     	this.Db.Image.ProjectionChanged();
    }
	}


	MapLayerList.prototype.GetVisibleLayers = function () 
	{
	  var result = new Array();
	  for (var i = 0; i < this.Layers.length; i++) {
	  	var layer = this.Layers[i];
	  	if (this.LayersVisibility[layer.Name] == true) {
	  		result.push(layer);
	  	}
	  }
   	return result;
	}


/// MapDatabase
	function MapDatabase(name) {
		this.Name = name;

		this.Config = new Array();
		this.Config["page_image"] = "/ingeoweb/ingeoimage.asp";
		this.Config["page_layers"] = "/ingeoweb/ingeolayers.asp";
		this.Config["page_search"] = "/ingeoweb/ingeosearch.asp";

		this.ObjectList = new MapObjectList(this);
		this.Image = new MapImage(this);
		this.LayerList = new MapLayerList(this);
	}

	
	MapDatabase.prototype.BindImageControl = function (elem) 
	{
                var backGround = document.createElement('div');
		elem.appendChild(backGround);
                backGround.style.backgroundColor = '#FFFFFF';
		backGround.style.position = "relative";
		backGround.style.zIndex   = "0";
                backGround.style.width  = '100%';
                backGround.style.height = '100%';
                
		var imagePlaceholder = document.createElement("div");
		elem.appendChild(imagePlaceholder);
		this.Image.ImagePlaceholder = imagePlaceholder;
		imagePlaceholder.style.overflow = "hidden";
		imagePlaceholder.style.position = "absolute";
		imagePlaceholder.style.zIndex   = "1";
		imagePlaceholder.style.top    = "0px";
		imagePlaceholder.style.left   = "0px";

		var image = document.createElement("img");
		imagePlaceholder.appendChild(image);
        	this.Image.ImgElement = image;
		image["DataObject"] = this.Image;
		image.style.position = "relative";
//		image.style.cursor = 'move';

		image.ondragstart = ImgElement_OnDragStart;
		image.onmousedown = ImgElement_OnMouseDown;
		image.onmouseup = ImgElement_OnMouseUp;
		image.onmousemove = ImgElement_OnMouseMove;
		image.onmouseleave = ImgElement_OnMouseLeave;
		image.onreadystatechange = ImgElement_OnReadyStateChange;
		image.onload = ImgElement_OnReadyStateChange;
		image.ondblclick = ImgElement_OnDblClick;

                if(document.all)
                {
                   image.onmousewheel = ImgElement_OnWheel;
                }
                else
                {  
                   image.addEventListener('DOMMouseScroll',ImgElement_OnWheel,false);
                }   



                var loading = document.createElement('div');
                loading.setAttribute('id','loading');
//                loading.style.backgroundColor = '#00ff00';
                loading.style.position = 'absolute';
		loading.style.zIndex   = "99";
                loading.style.left = '50%';
                loading.style.top  = '50%';
//                loading.style.borderStyle  = 'outset';
//                loading.style.borderWidth  = '1px';
//                loading.innerHTML = ' <br/>&nbsp; Loading ... <br/> &nbsp;';
                loading.innerHTML = '<img src="/img/bola.gif"height="36">';
                 
                elem.appendChild(loading);

                var infoWindow = new IInfoWindow() 
                imagePlaceholder.appendChild(infoWindow.div);
                this.Image.infoWindow = infoWindow; 

                return this.Image;
	}


	MapDatabase.prototype.BindObjectListControl = function (blockElement) 
	{
  	this.ObjectList.Placeholder = blockElement;
  	return this.ObjectList;
	}


	MapDatabase.prototype.BindLayerListControl = function (blockElement) 
	{
  	this.LayerList.Placeholder = blockElement;

  	this.LayerList.BindLayerList();
  
  	return this.LayerList;
	}





/////////////////////////Append by Berdnikov/////////
      function IshowInfoWindow(marker,html)
      {
          if(marker)
          {
             map.image.infoWindow.marker = marker;
             map.image.infoWindow.LatLng = marker.LatLng;

          }
//          map.image.infoWindow.div.innerHTML = html;
          map.image.infoWindow.display.innerHTML = html;

          var top  = parseInt(marker.div.style.top) - parseInt(map.image.infoWindow.div.clientHeight) + parseInt(marker.icon.style.height)/2;
          var left = parseInt(marker.div.style.left) + parseInt(marker.icon.style.width)/2;
          map.image.infoWindow.div.style.left = left + 'px';
          map.image.infoWindow.div.style.top  = top + 'px';
//          map.image.infoWindow.div.style.zIndex  = 1;
          map.image.infoWindow.div.style.display ='';
          var top  = parseInt(marker.div.style.top) - parseInt(map.image.infoWindow.div.clientHeight) + parseInt(marker.icon.style.height)/2;
          map.image.infoWindow.div.style.top  = top + 'px';

//alert(top);
      }

function IInfoWindow()
{
   this.marker;
   this.point;
   this.LatLng;

   var infoWindow = document.createElement('div');
   infoWindow.setAttribute('id','infoWindow');
//   infoWindow.style.backgroundColor = 'lemonchiffon';
   infoWindow.style.position = 'absolute';
   infoWindow.style.zIndex   = "98";

   infoWindow.style.left = '50%';
   infoWindow.style.top  = '45%';

                infoWindow.style.width = '200px';
//                infoWindow.style.height  = '200px';

//                infoWindow.style.borderStyle  = 'outset';
//                infoWindow.style.borderWidth  = '1px'; 

   infoWindow.innerHTML = '<table cellpadding="0" cellspacing="0" class="infowindow" width="100%"><tr><td id="infoWindowDispaly" style="background-color:lemonchiffon;border-color:#000000;border-style:solid;border-width:1;">&nbsp;</td></tr><tr><td style="border-color:#000000;border-style:solid;border-width:1;border-top-width:0px;border-bottom-width:0px;border-right-width:0px;">&nbsp;</td></tr></table>';

   this.display = infoWindow.getElementsByTagName('td').item(0);

   infoWindow.onclick  = function(){infoWindow.style.display='none';};               
   infoWindow.style.display = 'none';
   this.div = infoWindow;

   this.hide = function(){this.div.style.display = 'none';}
 


}      

        MapImage.prototype.setInfoWindowLatLng = function(LL)
        {
           if(LL)
           {
              this.infoWindow.LatLng = LL; 
           }
           else
           {
              if(!this.infoWindow.LatLng){return;}
              LL = this.infoWindow.LatLng; 
           }  
           var point = this.LatLng2Point(LL);
           this.PointWorldToDevice(point);

           var x = parseInt(point.X);
           var y = parseInt(point.Y) - parseInt(this.infoWindow.div.clientHeight);

           this.infoWindow.div.style.left = x + 'px';
           this.infoWindow.div.style.top  = y + 'px';

        }

function ImgElement_OnDblClick(ev)
{
   if(!ev){ev = window.event;}
   var mapImage = this["DataObject"];
   var center = new IPoint();
   center.X = ev.clientX;
   center.Y = ev.clientY;
   mapImage.PointDeviceToWorld(center);
   mapImage.CenterX = center.X;
   mapImage.CenterY = center.Y;
   mapImage.Zoom(2);
   if(map)
   {
      map.zoom = map.zoom +1;
   }
   if(mapImage.moveend){mapImage.moveend();}
//   mapImage.ProjectionChanged();
}



