
	var prevEl = null;
	var prevTool = null;
	function showExtLabel(o)
	{
		//if(labels.length==0)return;
            var info  = xGetElementById ("maplabel");   
            if (info==null)
            {
               info =  createLabel();
            }
                  
            var mc = xGetElementById (umap.mapCanvas);
            
            var rect = eval('['+o.imagemap+']');
            var x = parseInt(rect[0])+(parseInt(rect[2])- parseInt(rect[0]))/2;
            var y = parseInt(rect[1])+(parseInt(rect[3])- parseInt(rect[1]))/2;
            
       
            var html ='<table border=0 cellcpacing=0 cellpadding=0><tr>';
            
            //if (typeof(labels[0].extimage)!='undefined')
            //		html+='<td valign=top style="padding:5px;"><img width="150" src="images/'+labels[0].extimage+'-small.jpg"/></td>';
            //for(var i=0; i < labels.length; i++)
            {
            	html+='<td valign=top>';
            	//if (typeof(labels[0].label_old)!='undefined')
            		//html+='<div style="color:#004477;font-weight:bold;">&nbsp;' + o.name+'&nbsp;</div>' ; 
            	html+='<div style="color:#004477;">&nbsp;' + o.name+'&nbsp;</div>' ; 
            	
          //  	if (typeof(labels[0].url)!='undefined')
            		html+='<a style="color:#004477;" target="_blank" href="'+o.url +'">&nbsp;Перейти&nbsp;</div>' ; 
            	
            	
            }
            html+='</td>'
            html+='<td valign=top><img class="labelClose" onclick="hideLabel()" src="images/i-close.gif" border="0" alt="Закрыть" title="Закрыть" width="12px" height="12px"/></td>'
            html+='</tr></table>';
            
                       
            setLabel (xPageX(mc) +  x ,xPageY(mc)+ y, html);        
	}
	function icon_click(i)
	{
		var idx = i.id;
		
		if (typeof(petrsu.objects[idx].extimage)!='undefined')
		{
				document.prevTool = umap.mapTool;
				var im = createImage ();
				umap.showLoading();
				
				im.src = "images/" + petrsu.objects[idx].extimage+"-small.jpg";
				
				if (petrsu.objects[idx].imagemap)
				{
					xGetElementById('object_extimage_map').innerHTML = '<area title="'+petrsu.objects[idx].name+'" shape="rect" coords="' + petrsu.objects[idx].imagemap+'" href="'+petrsu.objects[idx].url+'" target="_blank"/>'
					}
				im.useMap = '#object_extimage_map';
				umap.setupTool ('EXT_IMAGE');
				xShow('object_extimage')
				hideLabel();
				xGetElementById ("mapname").innerHTML = petrsu.objects[idx].name;
				showExtLabel(petrsu.objects[idx]);
		}
		else
		{
			createObjectLabel(i.id);
			umap.onlabelchange(umap.labels);
		}
	}
	function extimage_click(e)
	{
		//alert(e.screenPix[0] + ' ' + e.screenPix[1]);
	}
	function image_load()
	{
		umap.hideLoading();
	}
	function hideExtImage ()
	{
			hideLabel();
		xGetElementById ("mapname").innerHTML = petrsu.maps[document.prevMap].name;
		xHide('object_extimage');umap.onlabelchange(umap.labels);umap.setupTool(document.prevTool||'PAN');
	}
	function  createImage ()
	{
			var i = xGetElementById ('object_extimage_image');
			if (i==null)
			{
				var m = xGetElementById (umap.mapCanvas);
				
				var idiv = document.createElement("div");
				idiv.id = 'object_extimage';
				
				
				var idivclose = document.createElement("div");
				
				var iclose = new Image();
				idivclose.style.float = 'right';
				
				iclose.src = 'images/arrow_left_blue.gif';
				iclose.style.cursor='painter';
				idivclose.style.position = "absolute";
				//idivclose.style.float='right';
				idivclose.style.marginLeft='350px';
				idivclose.style.marginTop='10px';
				//idivclose.style.marginRight=10;
				//idivclose.style.marginTop=10;
				idivclose.style.cursor='painter';
				iclose.style.cursor='painter';
				iclose.title =iclose.alt = 'Вернуться к карте';
				
				iclose.onclick = hideExtImage;
				xAppendChild (idivclose, iclose);
				
				
				var i = new Image();
				i.onload = image_load;
				i.id = 'object_extimage_image';
				idiv.style.position = "absolute";
				i.border='0';
		
				idiv.style.width = xWidth(m);
				idiv.style.height = xHeight(m);
				idiv.style.zIndex = ++umap.maxzindex;
				idiv.style.left = 0;
				idiv.style.top = 0;
				idiv.style.backgroundColor="#ffffff";
				//idiv.style.overflow='auto';
				
				i.style.width = xWidth(m);
				i.style.height = xHeight(m);
				var imap = document.createElement("map");
				imap.id = 'object_extimage_map';
				imap.name = 'object_extimage_map';
				
				i.useMap = 'object_extimage_map';
				
				xAppendChild (xGetElementById (umap.mapCanvas+"_MAP"), idiv);
				xAppendChild (idiv, idivclose);
				xAppendChild (idiv, i);
				xAppendChild (document.body, imap);
			}
			
			return i;
	};
	function createObjectLabel(idx)
	{
		 if (typeof(umap.labels)=="undefined"||umap.labels==null)
		        {
		        	umap.labels =new Array ();
		        }
		        
			umap.labels[0] = {};
			umap.labels[0].x = petrsu.objects[idx].x; 		  
			umap.labels[0].y = petrsu.objects[idx].y;
			umap.labels[0].label_old = petrsu.objects[idx].name;
			umap.labels[0].label = petrsu.objects[idx].label;
			umap.labels[0].id = idx;
			
			if (typeof(petrsu.objects[idx].extimage)!='undefined')
				umap.labels[0].extimage = petrsu.objects[idx].extimage;
				
			if (typeof(petrsu.objects[idx].url)!='undefined')
				umap.labels[0].url = petrsu.objects[idx].url;
			
	}
	function showMap (idx)
	{
		if (umap.mapTool=='EXT_IMAGE')
		{
			hideExtImage();
		}
		if (document.currentMap == idx) return;
		
		if (petrsu.maps[idx].id == umap.mapfile)
		{	
			xGetElementById("objects-"+idx).innerHTML = renderPetrsuObjects(idx);
			
			if (typeof(document.prevMap)!='undefined')
				xGetElementById("objects-"+document.prevMap).innerHTML = "";
				
      document.prevMap = idx;
			document.mapToShow = null;
			umap.removeIcons();
		 	for(var r=0; r < petrsu.objects.length;r++)
          {           
						if (petrsu.objects[r].map==idx)
						{
							var icon = new UserIcon (r, petrsu.objects[r].x, petrsu.objects[r].y, 'images/' + petrsu.objects[r].icon, 12,12, petrsu.objects[r].name);
	        
	      			icon.onclick = icon_click;
	      			umap.addIcon (icon);
						}            
          }
      
    }
    else
    {
    		document.mapToShow = idx;							
    		umap.removeIcons();
    		hideLabel();
				umap.openMap (petrsu.maps[idx].id);
    }
	};
	function showObject(idx)
	{
		if (umap.mapTool=='EXT_IMAGE')
		{
			hideExtImage();
		}
		var el = xGetElementById("o_" + idx);
		document.objectToShow = idx;
		if (prevEl)
			prevEl.style.fontWeight = "";
			
		if (umap.mapfile ==petrsu.maps[petrsu.objects[idx].map].id )
		{
			//	umap.queryObject (petrsu.objects[idx].id,petrsu.objects[idx].name );	
			
				document.objectToShow = null;			
		  umap.cx 	= petrsu.objects[idx].x;
			umap.cy 	= petrsu.objects[idx].y;
			umap.zoom = petrsu.objects[idx].zoom;
									
			createObjectLabel(idx);
			
			//umap.removeIcons();
			
			//var icon = new UserIcon (idx, petrsu.objects[idx].x, petrsu.objects[idx].y, 'images/' + petrsu.objects[idx].icon , 12,12, petrsu.objects[idx].name);
        
      //icon.onclick = icon_click;
      //umap.addIcon (icon);
			umap.draw();
			//umap.onobjectresults(ob);
		}
		else
			{
				document.mapToShow = petrsu.objects[idx].map;	
				document.objectToShow = idx;							
				hideLabel();
				umap.openMap (petrsu.maps[petrsu.objects[idx].map].id);
				
			}
			el.style.fontWeight="bold";
			prevEl = el;
				
	};
	function selectTool (t)
	{
			umap.setupTool (t);			
			
	};
  function renderPetrsuObjects (idx)
    {
    		
    	 		var txt ="<table border=0 cellpadding=0 cellspacing=2 valign=top>";
    	 		
          for(var r=0; r < petrsu.objects.length;r++)
          {
            if (petrsu.objects[r].map == idx)
            {
            	txt+="<tr align=right><td nowrap><a id='o_"+ r+"'class=menu_m2 href='#top' onclick='showObject("+r +")'>";
            	txt+=petrsu.objects[r].name + '&nbsp;<img src="images/'+petrsu.objects[r].icon+'" valign="middle" border="0"/>';
            	txt+="</a></td></tr>"
            }         
            
          }          
					txt+="</td></tr>"
					txt+="</table>";
          document.currentMap = idx;         
          return  txt;
          
    };
    function renderMaps()
    {
    	 		var txt ="<table border=0 cellpadding=3 cellspacing=5 valign=top>";
    	 		
          for(var r=0; r < petrsu.maps.length;r++)
          {
            
            txt+="<tr align=left><td nowrap><a id='o_"+ r+"'class='menu_m2'  href='#' onclick='showMap("+r +")'>";
            txt+=petrsu.maps[r].name;
            txt+="</a>";
            txt+="</td></tr>";
            txt+="<tr><td id='objects-"+ r+"'>&nbsp;";
            //txt+=renderPetrsuObjects(r);
            txt+="</td></tr>";
            
                      
            
          }
          
					txt+="</td></tr>"
					txt+="</table>";
                   
          document.getElementById ('objects').innerHTML = txt;
          
          
          
    };
    
    
     
    var umap = new UserMap();
    var urefmap = new UserRefMap();
    
   
    window.onload= function ()
    {
    	 umap.initialize ('map',450,450);
    	 urefmap.initialize (umap,'refmap', 133,133,"#0000FF");
    	renderMaps();
    	showMap(0);
    	umap.setupTool ("PAN");
    	umap.addCustomTool('EXT_IMAGE',extimage_click, '');
    };
    
    umap.onobjectresults=function(ob)
		{
		    if (ob)
		    {
		    	  if (typeof(this.labels)=="undefined"||this.labels==null)
		        {
		        	this.labels =new Array (ob);
		        }
		        	
		        var idx = this.labels.length;
		        this.labels[0] = ob;
		        this.labels[0].label_old = ob.qtable;// + "<br>" + this.labels[0].label ;		        
		        		       
		       petrsu.objects[document.objectToShow].x = ob.x;
		       petrsu.objects[document.objectToShow].y = ob.y;
		       petrsu.objects[document.objectToShow].zoom = ob.zoom;
		      petrsu.objects[document.objectToShow].label = ob.label;
						//document.getElementById ("r").innerHTML +="," + encode(petrsu.objects[document.objectToShow]); 		       
		        this.onlabelchange(this.labels);        
		    }
		};
		umap.onsetuptool = function (tool)
		{
				var el = xGetElementById(tool);			
				if (el)
				{
					xShow('mapTools');
					el.className = "tool";
					if (prevTool ==  null)
						prevTool = xGetElementById('PAN');
						
					if (prevTool.id!=el.id)
						prevTool.className = "inactive-tool";
					
					prevTool = el;
			}
			else
				xHide('mapTools');
		};
	umap.onstatechanged=function(state) 
{
    switch(state)
    {
        
        case 2:
        {
                
            if (typeof(this.labels)!="undefined")
            {
                this.onlabelchange(this.labels);
            }
            break;
        }
        default:
        {        
          //  xHide ("maplabel");
            break;
        }
     }
            
};
 umap.onmapload = function(m)
    {
    	if (document.mapToShow!=null&&typeof(document.mapToShow)!='undefined')
    	{
    		xGetElementById ("mapname").innerHTML = petrsu.maps[document.mapToShow].name;
    		showMap (document.mapToShow);    	
    		if (document.objectToShow!=null&&typeof(document.objectToShow)!='undefined')
    			showObject (document.objectToShow);	
    	}
    	else
    	{	
    		xGetElementById ("mapname").innerHTML = petrsu.maps[petrsu.objects[document.objectToShow].map].name;
    		showObject (document.objectToShow);
    	}
    };
function hideLabel()
{
  document.UserMap.labels= null;  
  xHide ("maplabel");  
};
function createLabel()
{
 var info = document.createElement("div");
 info.id = "maplabel";
 info.style.position= "absolute";  
 info.style.visibility="hidden";
 
                 
 var lImage = new Image();
 lImage.className = "label";
 lImage.src = "petrsumap/i-legend_point.gif";
 lImage.style.width = "15px";
 lImage.style.height = "11px";
// lImage.style.position= "absolute";  
 lImage.style.marginTop = "-1px";  
 //lImage.style. = "3px";
 
 
 
 var info_text = document.createElement("div"); 
 info_text.style.border="1px solid";
 info_text.style.backgroundColor="#ffffff";
 info_text.className = "f1 label";
 xAppendChild (info, info_text); 
 xAppendChild (info, lImage);
 xAppendChild (document.body, info);
 
 return info;
};
function setLabel(x, y, text)
{
 var info  = xGetElementById ("maplabel");
 						if (info==null)
            {
               info =  createLabel();
            }
 if (info)
 {
    var mc = xGetElementById (document.UserMap.mapCanvas);
    info.childNodes[0].innerHTML = text;
    
    if (x < xPageX(mc) || y <  xPageY(mc) )
        xHide(info);
    else if (x > xPageX(mc) + xWidth(mc) || y >  xPageY(mc) + xHeight(mc) ) xHide(info);
    else
    {
        xMoveTo (info,x  ,y-xHeight(info.childNodes[0])-11);                                               
   // xMoveTo (info.childNodes[0],x ,y+xHeight(info.childNodes[0]));                                       
  
        xShow(info);
    }
    
 }
 
                                   
};
umap.onlabelchange = function (labels)
{
        if (labels!=null)
        {
        
            if(labels.length==0)return;
            var info  = xGetElementById ("maplabel");   
            if (info==null)
            {
               info =  createLabel();
            }
                  
            var mc = xGetElementById (this.mapCanvas);
         
            var html ='<table border=0 cellcpacing=0 cellpadding=0><tr>';
            
            //if (typeof(labels[0].extimage)!='undefined')
            //		html+='<td valign=top style="padding:5px;"><img width="150" src="images/'+labels[0].extimage+'-small.jpg"/></td>';
            //for(var i=0; i < labels.length; i++)
            {
            	html+='<td valign=top>';
            	if (typeof(labels[0].label_old)!='undefined')
            		html+='<div style="color:#004477;font-weight:bold;">&nbsp;' + labels[0].label_old+'&nbsp;</div>' ; 
            	html+='<div style="color:#004477;">&nbsp;' + labels[0].label+'&nbsp;</div>' ; 
            	
            	if (typeof(labels[0].url)!='undefined')
            		html+='<a style="color:#004477;" target="_blank" href="'+labels[0].url +'">&nbsp;Перейти&nbsp;</div>' ; 
            	
            	
            }
            html+='</td>'
            html+='<td valign=top><img class="labelClose" onclick="hideLabel()" src="images/i-close.gif" border="0" alt="Закрыть" title="Закрыть" width="12px" height="12px"/></td>'
            html+='</tr></table>';
            
            
            var pt = this.geoToPix(labels[0].x, labels[0].y);
            setLabel (xPageX(mc) +  pt[0] ,xPageY(mc)+ pt[1], html);                                       
                                               
         }
};
umap.onrullerchange=function(current, total)
{
        var mc = xGetElementById (this.mapCanvas);         
        var info  = xGetElementById (this.mapCanvas+"_INFO");   
              
        xShow(info);
        
        var d=total.toFixed(2);;
        var d0=current.toFixed(2);;
        var du="м";
        var d0u="м";
        if (d / 1000 > 1)
        {
            d/=1000;
            du="км";
            d = d.toFixed(2);
        }
        if (d0/1000> 1)
        {
            d0/=1000;
            d0u="км";
            d0 = d0.toFixed(2);
        }
            
        var html = "<table border=0 cellcpacing=0 cellpadding=0><tr><td valign='top'></td><td class='f1'>Расстояние:&nbsp;" +d0+ "&nbsp;"+d0u+"<br>Всего:&nbsp;"+d+"&nbsp;"+du+"</td><td valign='top'><img class='labelClose' src='images/i-close.gif' onclick='document.UserMap.showRuller(true);' border='0' alt='Закрыть' width='12px' height='12px'></td></tr></table>";
        
        info.innerHTML =  html;
      /*  var pt = document.UserMap.geoToPix(this.object[this.objectIndex][0], this.object[this.objectIndex][1]);
        if (pt[0] - xWidth(info) - 10 < xPageX(mc) )
            pt[0] = 0;
        if( pt[1] - xHeight(info) - 10 <  xPageY(mc) )
            pt[1] = 0;
        if (pt[0] + xWidth(info) + 10> xPageX(mc) + xWidth(mc) )
            pt[0] = xWidth(mc);                    
        if(pt[1] +  xHeight(info) + 10>  xPageY(mc) + xHeight(mc) ) 
             pt[1] = xHeight(mc);*/
        xMoveTo (info,xPageX(mc) /*+  pt[0]*/ +2 , xPageY(mc)/* + pt[1]*/  +2 );                                       
        
};


function redrawMap()
{
		umap.cx = umap.states[0].cx;
		umap.cy = umap.states[0].cy;
		umap.zoom = umap.states[0].zoom;
		umap.draw();
}

