var ie=document.all;

var map_PaddX = 150;        // отступ X
var map_PaddY = 100;        // отступ Y

var mapW = 300; 			// ширина карты без отступа
var mapH = 200; 			// высота карты без отступа
var mapSizeX = mapW + 2*map_PaddX;
var mapSizeY = mapH + 2*map_PaddY;

var level = 0;

// макс. и мин. уровни увеличения
var levelMIN = 0;
var levelMAX = 2;

var centerSegment;
var segmentLT;
var segmentT;
var segmentRT;
var segmentL;
var segmentR;
var segmentLB;
var segmentB
var segmentRB;

var mapInside;

var map_step;			// шаг при нажатии на стрелочку

// характеристики файлов картинок
var map_path = "";
var map_image_ext = ".jpg";
var map_image_sep = "_";


// работа с координатами 
// это миним. и. макс. коорд с учетом зума!
var positionMIN = new Point();
var positionMAX = new Point();

// позиция в абс. коорд ЦЕНТРА экрана!!!
var position = new Point();
// позиция в абс. коорд нужной нам точки
var cursorPosition;

// индекс картинки, высчитывается от абсолютных координат и зума
var imageIndexMAX = new Point();
var imageIndexMIN = new Point();

imageIndexMIN.x = 1;
imageIndexMIN.y = 1;

var imageIndex = new Point();
imageIndex.x= 1;
imageIndex.y = 1;
// корректировка смещения, зависит от абс. коорд., индекса картинки
var correct = new Point();
correct.x = 0;
correct.y = 0;

var isRequestSending = true;

var cityPointSize = 10;

var instrument = "pointer";

var map;
var lang;

var isNeedSityHighlight = false;

function selectInstr(type)
{
	var pointerImg = document.getElementById("pointerInstr");
	var grabImg = document.getElementById("grabInstr");
	var zoomInImg = document.getElementById("zoomIn");
	var zoomOutImg = document.getElementById("zoomOut");
	var sitePref = getFullURLPrefix();
	switch(type)
	{
		case 'zoomIn':
		instrument = type;
		map.style.cursor = "default";
		if(ie)
			map.style.cursor = sitePref + "cursors/zoom_in.cur";
		else
			map.style.cursor = "-moz-zoom-in";
		break;
		case 'grab':
		map.style.cursor = "default";
		instrument = type;
		if(ie)map.style.cursor = sitePref + "cursors/grab.cur";
		else
			map.style.cursor = "-moz-grab";
		break;
		case 'zoomOut':
		map.style.cursor = "default";
		instrument = type;
		if(ie)map.style.cursor = sitePref + "cursors/zoom_out.cur";
		else
			map.style.cursor = "-moz-zoom-out";
		break;
		default:
		instrument = "pointer";
		map.style.cursor = "crosshair";
		break;
	}
	
}

// чисто функции для админки
function onMapClick(e)
{
	var coord = new Point();
	var mapPosition = getElementPosition(map);
	
	mousePagexy = mousePageXY(e);
	if(ie)
	{
		coord.x = mousePagexy.x - mapPosition.left;
		coord.y = mousePagexy.y - mapPosition.top;
	}
	else
	{
		coord.x = e.pageX - mapPosition.left;
		coord.y = e.pageY - mapPosition.top;
	}

	switch(instrument)
	{
		case 'zoomIn':
			ZoomIn(coord.x, coord.y);
			break;
		case 'zoomOut':
			ZoomOut(coord.x, coord.y);
			break;
		case 'grab':
			grabMouseDown(coord);
			break;
		default:
			
			// границы div увеличивают его размеры, соотв. мы уменьшаем
			coord.x--;coord.y--;
			if((coord.x < 0)||(coord.y < 0)||(coord.x > mapSizeX)||(coord.y > mapSizeY))
			{
				return false;
			}
			
			cursorPosition.x = position.x + ((coord.x - mapSizeX/2)*map_Zoom_Coof);
			cursorPosition.y = position.y + ((coord.y - mapSizeY/2)*map_Zoom_Coof);
			
			
			SetCursorPosition(cursorPosition.x,cursorPosition.y);
		break;
	}

}
function SetCursorPosition(X, Y)
{
	//var position_x = document.getElementById("position_x");
	//var position_y = document.getElementById("position_y");
	
	var absX = ((X - position.x)/map_Zoom_Coof) + mapSizeX/2;
	var absY = ((Y - position.y)/map_Zoom_Coof) + mapSizeY/2;

	//position_x.value = X;
	//position_y.value = Y;
	
	var newCityPoint = document.getElementById("newCityPoint");
	
	var cityId = document.getElementById("cityId");
	if(cityId != null & cityId.value != "")
	{
		ShowElement(newCityPoint, true);
		var cityPoint = null;
		var collection = document.getElementsByTagName("div");
		for(i=0; i < collection.length; i++)
		{
			 if(collection[i].getAttribute("cid") != null)
			 {
			 	if(collection[i].cid == cityId.value)
			 	{
			 		cityPoint = collection[i];
			 		break;
			 	}
			 }
		}
		if(cityPoint == null) return;
		
		
	}
	else
	{
		cityPoint = newCityPoint;
	}
	cityPoint.style.left = (absX - cityPointSize/2) + "px";
	cityPoint.style.top = (absY - cityPointSize/2) + "px";
	//cityPoint.position_x = X;
	//cityPoint.position_y = Y;
	
	if(absX < 0 || absX > mapSizeX || absY < 0 || absY > mapSizeY)
	{
		ShowElement(cityPoint, true);
	}
	else
	{
		ShowElement(cityPoint,false);
	}
}
function UpdateLevelToShow()
{
	var level_to_show = document.getElementById("level_to_show");
	level_to_show.value = level;
}
// общие функции
function GetCenterImageSrc()
{
    return map_path + imageIndex.y + map_image_sep + imageIndex.x + map_image_ext;
}
function GetLTImageSrc()
{
    return map_path + (imageIndex.y - 1) + map_image_sep + (imageIndex.x - 1) + map_image_ext;
}
function GetTImageSrc()
{
    return map_path + (imageIndex.y -1 )+ map_image_sep + imageIndex.x + map_image_ext;
}
function GetRTImageSrc()
{
    return map_path + (imageIndex.y - 1) + map_image_sep + (imageIndex.x + 1) + map_image_ext;
}
function GetLImageSrc()
{
    return map_path + imageIndex.y + map_image_sep + (imageIndex.x - 1) + map_image_ext;
}
function GetRImageSrc()
{
    return map_path + imageIndex.y + map_image_sep + (imageIndex.x + 1) + map_image_ext;
}
function GetLBImageSrc()
{
    return map_path + (imageIndex.y + 1) + map_image_sep + (imageIndex.x - 1) + map_image_ext;
}
function GetBImageSrc()
{
    return map_path + (imageIndex.y + 1) + map_image_sep + imageIndex.x + map_image_ext;
}
function GetRBImageSrc()
{
    return map_path + (imageIndex.y + 1) + map_image_sep + (imageIndex.x + 1) + map_image_ext;
}
function Segment()
{
	var x;
	var y;
	var w;
	var h;
}
function Point()
{
    var x;
    var y;
}
function get_session_id()
{
	var ses_name = document.getElementById("_session_name");
	var ses_val = document.getElementById("_session_value");
	if(ses_name != null && ses_val!=null && ses_name.value!="" && ses_val.value != "")
	{
		return "&" + ses_name.value + "=" + ses_val.value;
	}
	return "";
}
function getFullURLPrefix()
{
	var s = document.getElementById("host");
	if(s != null && s.value != "")
	{
		return s.value;
	}
	return "";
}
function SetBackGround(element, background)
{
	if(background != element.style.backgroundImage)
	{
		element.style.backgroundImage = background;
		if(isRequestSending == false)
		{
			var cityId=document.getElementById("cityId");
			language = lang.value;
			mapDoLoad(getFullURLPrefix() + 'ajax.php?action=map_regions_load'+get_session_id()+'&x=' + position.x +'&y=' + position.y + "&level=" + level + "&cityId=" + cityId.value + "&lang=" + language);
			
			isRequestSending = true;
		}
	}
}
function RefreshPosition(isReloadNeed)
{
	isRequestSending = false;
	var mapCenter = document.getElementById("mapCenter");
	mapCenter.style.left = centerSegment.x + "px";
	mapCenter.style.top = centerSegment.y + "px";
	mapCenter.style.width = centerSegment.w + "px";
	mapCenter.style.height = centerSegment.h + "px";
	if(isReloadNeed == 1 && centerSegment.w != 0 && centerSegment.h != 0)
		SetBackGround(mapCenter, "url(" + GetCenterImageSrc() + ")");
    if(centerSegment.w == 0 || centerSegment.h == 0)
		SetBackGround(mapB, "");
			
	
	var mapLT = document.getElementById("mapLT");
	
	mapLT.style.left = segmentLT.x + "px";
	mapLT.style.top = segmentLT.y + "px";
	mapLT.style.width = segmentLT.w + "px";
	mapLT.style.height = segmentLT.h + "px";
	if(isReloadNeed == 1 && segmentLT.w!=0 && segmentLT.h!=0)
		SetBackGround(mapLT, "url(" + GetLTImageSrc() + ")");
    if(segmentLT.w == 0 || segmentLT.h == 0) 
		SetBackGround(mapLT, "");
			

	var mapT = document.getElementById("mapT");
	mapT.style.left = segmentT.x + "px";
	mapT.style.top = segmentT.y + "px";
	mapT.style.width = segmentT.w + "px";
	mapT.style.height = segmentT.h + "px";
	if(isReloadNeed == 1 && segmentT.h!=0 && segmentT.w!=0)
		SetBackGround(mapT, "url(" + GetTImageSrc() + ")");
    if(segmentT.w == 0 || segmentT.h == 0) 
		SetBackGround(mapT, "");
			
	
	var mapRT = document.getElementById("mapRT");
	mapRT.style.left = segmentRT.x + "px";
	mapRT.style.top = segmentRT.y + "px";
	mapRT.style.width = segmentRT.w + "px";
	mapRT.style.height = segmentRT.h + "px";
	
	if(isReloadNeed == 1 && segmentRT.h!=0 && segmentRT.w!=0)
		SetBackGround(mapRT, "url(" + GetRTImageSrc() + ")");
    if(segmentRT.w == 0 || segmentRT.h == 0) 
		SetBackGround(mapRT, "");
			
	
	var mapL = document.getElementById("mapL");
	mapL.style.left = segmentL.x + "px";
	mapL.style.top = segmentL.y + "px";
	mapL.style.width = segmentL.w + "px";
	mapL.style.height = segmentL.h + "px";
	if(isReloadNeed == 1 && segmentL.h!=0 && segmentL.w !=0)
		SetBackGround(mapL, "url(" + GetLImageSrc() + ")");
    if(segmentL.w == 0 || segmentL.h == 0) 
		SetBackGround(mapL, "");
			
	
	var mapR = document.getElementById("mapR");
	mapR.style.left = segmentR.x + "px";
	mapR.style.top = segmentR.y + "px";
	mapR.style.width = segmentR.w + "px";
	mapR.style.height = segmentR.h + "px";
	if(isReloadNeed == 1 && segmentR.h!=0 && segmentR.w!=0)
		SetBackGround(mapR, "url(" + GetRImageSrc() + ")");
    if(segmentR.w == 0 || segmentR.h == 0) 
		SetBackGround(mapR, "");
			
	
	var mapLB = document.getElementById("mapLB");
	mapLB.style.left = segmentLB.x + "px";
	mapLB.style.top = segmentLB.y + "px";
	mapLB.style.width = segmentLB.w + "px";
	mapLB.style.height = segmentLB.h + "px";
	if(isReloadNeed == 1 && segmentLB.h!=0 && segmentLB.w!=0)
		SetBackGround(mapLB, "url(" + GetLBImageSrc() + ")");
    if(segmentLB.w == 0 || segmentLB.h == 0) 
		SetBackGround(mapLB, "");
			
	var mapB = document.getElementById("mapB");
	mapB.style.left = segmentB.x + "px";
	mapB.style.top = segmentB.y + "px";
	mapB.style.width = segmentB.w + "px";
	mapB.style.height = segmentB.h + "px";
	if(isReloadNeed == 1 && segmentB.h!=0 && segmentB.w!=0)
		SetBackGround(mapB, "url(" + GetBImageSrc() + ")");
	if(segmentB.w == 0 || segmentB.h == 0) 
		SetBackGround(mapB, "");
	
	var mapRB = document.getElementById("mapRB");
	mapRB.style.left = segmentRB.x + "px";
	mapRB.style.top = segmentRB.y + "px";
	mapRB.style.width = segmentRB.w + "px";
	mapRB.style.height = segmentRB.h + "px";
	if(isReloadNeed == 1 && segmentRB.h!=0 && segmentRB.w!=0)
		SetBackGround(mapRB, "url(" + GetRBImageSrc() + ")");
	if(segmentRB.w == 0 || segmentRB.h == 0) 
		SetBackGround(mapRB, "");
}
function Move()
{
    Initialize(position.x,position.y);
    RefreshPosition(1);
}
function MoveDrag()
{
    Initialize(position.x,position.y);
    //RefreshPosition(1);
}

function MoveLeft()
{
    position.x = position.x + map_step;
    Move();
}
function MoveTop()
{
    position.y = position.y + map_step;
    Move();
}
function MoveRight()
{
    position.x = position.x - map_step;
    Move();
}
function MoveBottom()
{
    position.y = position.y - map_step;
    Move();
}
function MoveTopLeft()
{
    position.x = position.x - map_step;
    position.y = position.y - map_step;
    Move();
}
function MoveTopRight()
{
	position.x = position.x + map_step;
    position.y = position.y - map_step;
    Move();
}
function MoveBottomLeft()
{
    position.x = position.x - map_step;
    position.y = position.y + map_step;
    Move();
}
function MoveBottomRight()
{
	position.x = position.x + map_step;
    position.y = position.y + map_step;
    Move();
}
function ZoomIn(X,Y)
{
    level ++;
    if(level > levelMAX)
    {
    	level = levelMAX;
    	return false;
    }
    Initialize(position.x + ((X - mapSizeX/2)*map_Zoom_Coof),position.y + ((Y - mapSizeY/2)*map_Zoom_Coof));
    RefreshPosition(1);
    UpdateLevelToShow();
}
function ZoomOut(X,Y)
{
    level --;
    if(level < levelMIN)
    level = levelMIN;
    Initialize(position.x + ((X - mapSizeX/2)*map_Zoom_Coof),position.y + ((Y - mapSizeY/2)*map_Zoom_Coof));
    RefreshPosition(1);
    UpdateLevelToShow();
}
function Initialize(X,Y, currentLevel)
{
	map = document.getElementById("map");
	selectInstr(instrument);
	map.onmousedown=onMapClick;
	
	lang = document.getElementById("lang");

	if(currentLevel != null)
	{
		level = currentLevel;
	}
	
	position.x = X;
	position.y = Y;
	
	if(cursorPosition == null)
	{
		cursorPosition = new Point();
		cursorPosition.x = X;
		cursorPosition.y = Y;
	}
	
	// пересчитываем константы
	
	if(level == 0) imageIndexMAX.x = 1;// на 2 отличаются от крайних циферок
	if(level == 1) imageIndexMAX.x = 6;
	if(level == 2) imageIndexMAX.x = 30;
	if(level == 0) imageIndexMAX.y = 1;
	if(level == 1) imageIndexMAX.y = 6;
	if(level == 2) imageIndexMAX.y = 30;
	
	if(level == 0){map_step = 800;map_Zoom_Coof = 16;positionMIN.x=4800;positionMIN.y=3200;positionMAX.x=4800;positionMAX.y=3200;}
	if(level == 1){map_step = 200;map_Zoom_Coof = 4;positionMIN.x=1200;positionMIN.y=800;positionMAX.x=8400;positionMAX.y=5600;}
	if(level == 2){map_step = 50;map_Zoom_Coof = 1;positionMIN.x=300;positionMIN.y=200;positionMAX.x=9300;positionMAX.y=6200;}
	
	map_path = getFullURLPrefix() + "images/map/" + level + "/";
	
	if(level == levelMIN)
	{
	    ShowNavigation(true);
	}
	else
	{
	    ShowNavigation(false);
	}

	if(centerSegment == null)
	{
		centerSegment = new Segment();
		segmentLT = new Segment();
		segmentT = new Segment();
		segmentRT = new Segment();
		segmentL = new Segment();
		segmentR = new Segment();
		segmentLB = new Segment();
		segmentB = new Segment();
		segmentRB = new Segment();
		mapInside = new Segment();
	}
	mapInside.x = 0;
	mapInside.y = 0;
	mapInside.h = 400;
	mapInside.w = 600;
	
	CalculatePosition();
	SetCursorPosition(cursorPosition.x, cursorPosition.y);
	placeCityInMap();

	centerSegment.x = map_PaddX + correct.x;
	centerSegment.y = map_PaddY + correct.y;
	centerSegment.w = mapW;
	centerSegment.h = mapH;
	
	segmentLT.x = 0;
	segmentLT.y = 0;
	segmentLT.w = map_PaddX + correct.x;
	segmentLT.h = map_PaddY + correct.y;
	
	segmentT.x = map_PaddX + correct.x;
	segmentT.y = 0;
	segmentT.w = mapW;
	segmentT.h = map_PaddY + correct.y;
	
	segmentRT.x = mapW + map_PaddX + correct.x;
	segmentRT.y = 0;
	segmentRT.w = map_PaddX - correct.x;
	segmentRT.h = map_PaddY + correct.y;
	
	segmentL.x = 0;
	segmentL.y = map_PaddY + correct.y;
	segmentL.w = map_PaddX + correct.x;
	segmentL.h = mapH;
	
	segmentR.x = mapW + map_PaddX + correct.x;
	segmentR.y = map_PaddY + correct.y;
	segmentR.w = map_PaddX - correct.x;
	segmentR.h = mapH;
	
	segmentLB.x = 0;
	segmentLB.y = mapH + map_PaddY + correct.y;
	segmentLB.w = map_PaddX + correct.x;
	segmentLB.h = map_PaddY - correct.y;
	
	segmentB.x = map_PaddX + correct.x;
	segmentB.y = mapH + map_PaddY + correct.y;
	segmentB.w = mapW;
	segmentB.h = map_PaddY - correct.y;
	
	segmentRB.x = mapW + map_PaddX + correct.x;
	segmentRB.y = mapH + map_PaddY + correct.y;
	segmentRB.w = map_PaddX - correct.x;
	segmentRB.h = map_PaddY - correct.y;
	
	
	// ставим куки
	var in_expDate = new Date();
	in_expDate.setTime(in_expDate.getTime() + 31*24*60*60*1000);
	
	setCookie("hdnLastMapPositionX", position.x, in_expDate, "/");
	setCookie("hdnLastMapPositionY", position.y, in_expDate, "/");
	setCookie("hdnLastMapPositionZoom", level, in_expDate, "/");
	
	document.getElementById('hdnLastMapPositionX').value = position.x;
	document.getElementById('hdnLastMapPositionY').value = position.y;
	document.getElementById('hdnLastMapPositionZoom').value = level;
}
function CalculatePosition()
{
	if(position.x < positionMIN.x)position.x = positionMIN.x;
	if(position.y < positionMIN.y)position.y = positionMIN.y;
	if(position.x > positionMAX.x)position.x = positionMAX.x;
	if(position.y > positionMAX.y)position.y = positionMAX.y;
	
	var tmpPosition = new Point();
	if(level == 0)
	{
		tmpPosition.x = position.x/map_Zoom_Coof;
		tmpPosition.y = position.y/map_Zoom_Coof;
	}else if(level == 1)
	{
		tmpPosition.x = position.x/map_Zoom_Coof;
		tmpPosition.y = position.y/map_Zoom_Coof;
	}else if(level == 2)
	{
		tmpPosition.x = position.x;
		tmpPosition.y = position.y;
	}


    correct.x = tmpPosition.x%mapW;
    correct.y = tmpPosition.y%mapH;
    
    imageIndex.x = ((tmpPosition.x - correct.x) /mapW);
    imageIndex.y = ((tmpPosition.y - correct.y) / mapH);
    // для границ
    if(imageIndex.x < imageIndexMIN.x){imageIndex.x = imageIndexMIN.x;correct.x = 0;}
    if(imageIndex.y < imageIndexMIN.y){imageIndex.y = imageIndexMIN.y;correct.y = 0;}

    correct.x = -correct.x;
    correct.y = -correct.y;
    correct.x += map_PaddX;
    correct.y += map_PaddY;
    //var text = document.getElementById("text");
    //text.innerHTML = segmentB.x + ' ' + segmentB.y + "|" + segmentB.w + ' ' + segmentB.h;
}
function ShowNavigation(isShow)
{
    var navLeft = document.getElementById("navLeft");
    var navRight = document.getElementById("navRight");
    var navBottom = document.getElementById("navBottom");
    var navTop = document.getElementById("navTop");
    
    ShowElement(navLeft, isShow);
    ShowElement(navRight, isShow);
    ShowElement(navBottom, isShow);
    ShowElement(navTop, isShow);
}
function ShowElement(element, isShow)
{
    if(isShow)
    {
        element.style.dispaly = "none";
        element.style.visibility = "hidden";
    }
    else
    {
        element.style.dispaly = "";
        element.style.visibility = "";
    }    
}

function placeCityInMap()
{
	var city;
	var i = 0;
	var mapPos = getElementPosition(map);
	while((city = document.getElementById("city"+i))!=null)
	{
		var tmpCityPoint = new Point();
		tmpCityPoint.x = mapSizeX - (((position.x - city.getAttribute('position_x'))/map_Zoom_Coof) + mapSizeX/2);
		tmpCityPoint.y = mapSizeY - (((position.y - city.getAttribute('position_y'))/map_Zoom_Coof) + mapSizeY/2);
		if(tmpCityPoint.x < 3 || tmpCityPoint.x > mapSizeX - 3 || tmpCityPoint.y < 3 || tmpCityPoint.y >mapSizeY-3 )
		{
			ShowElement(city, true);
		}
		else
		{
			city.style.left = (tmpCityPoint.x - cityPointSize/2) + "px";
			city.style.top = (tmpCityPoint.y - cityPointSize/2) + "px";
			SetupCityTitlePosition(city, tmpCityPoint, mapPos);
			ShowElement(city, false);
		}
		i++;
	}
	if(isNeedSityHighlight != false)
	{
		HighlightCity(isNeedSityHighlight);
		cityToHighlight = false;
	}
}

function SetupCityTitlePosition(city, point, mapPos)
{
    var elem = city.childNodes[1];
    var elemPos = getElementPosition(elem);
    if((elemPos.left + elemPos.width > mapPos.left + mapPos.width)||
        (elemPos.left < mapPos.left)||
        (elemPos.top + elemPos.height > mapPos.top + mapPos.height)||
        (elemPos.top < mapPos.top))
    {
        ShowElement(elem, true);
    }
    else
    {
        ShowElement(elem, false);
    }
}

function onCitySynchronizeClick(cityId, x, y)
{
if(x == null || y == null || x == 0 || y == 0)
	return false;
	
	language = lang.value;
//	mapCopsDoLoad(getFullURLPrefix() + 'ajax.php?action=load_cops_in_the_city'+get_session_id() + "&lang=" + language + "&city=" + cityId);
	level = 2;
	Initialize(parseInt(x),parseInt(y));
    RefreshPosition(1);
    isNeedSityHighlight = cityId;
}

function HighlightCity(cityId)
{
	var collection = document.getElementsByTagName("div");
	var cityToHighlight;
	var cityID = cityId;

	for(i=0; i < collection.length; i++)
	{
		 if(collection[i].getAttribute("cid") != null)
		 {
		 	if(collection[i].attributes['cid'].value == cityID)
		 	{
		 		cityToHighlight = collection[i];
		 		break;
		 	}
		 }
	}

	if(cityToHighlight != null)
	{
		if(ie)
		{
			cityToHighlight.children[1].style.textDecoration='underline';
		}
		else
		{
			cityToHighlight.childNodes[1].style.textDecoration='underline'
		}
	}
}
// загрузка списка городов
function mapDoLoad(url) {
if (window.XMLHttpRequest) {  
	request = new XMLHttpRequest();  
	request.onreadystatechange = mapProcessRequestChange;  
	request.open("GET", url, true);  
	request.send(null);  
} else if (window.ActiveXObject) {  
	request = new ActiveXObject("Microsoft.XMLHTTP");  
	if (request) {  
		request.onreadystatechange = mapProcessRequestChange;  
		request.open("GET", url, true);  
		request.send();  
	}  
}  
}  

function mapProcessRequestChange() {  
	if (request.readyState == 4) {  
		if (request.status == 200) {
			var node = request.responseXML.childNodes[request.responseXML.childNodes.length-1];

			if(node == null)
			{
				alert("Не удалось получить данные");
				return;
			}
			var p = new Array();
			for(var i = 0; i < node.childNodes.length; i++)
			{
				p[i] = new Array();
				for(var j = 0; j < node.childNodes[i].childNodes.length; j++)
				{
					
					p[i][j] = ie?node.childNodes[i].childNodes[j].text:node.childNodes[i].childNodes[j].textContent;
				}
			}
			var t = "";
			for(var i = 0; i < p.length; i++)
			{
				t += "<div id='" 	+ p[i][0] + "'";
				t += " cid='" 		+ p[i][1] + "'";
				t += " class='" 	+ p[i][2] + "' ";
				t += " position_x='"+ p[i][3] + "' ";
				t += " position_y='"+ p[i][4] + "'>";
				t += "<div ";//title='" + p[i][5] + "' ";
				t += " class='" 	+ p[i][6] + "' ";
				t += "  onmouseover=\"setCursor(this);Tip('<b class=t1>" + p[i][8] + "</b><br />" + p[i][5] + "', SHADOW, true, BGCOLOR, '#EEEEEE', FADEIN, 400);\" onclick='onCityClick(" + p[i][1] + "," + p[i][3] + "," + p[i][4] + ");'></div>";
				t += "<div onmouseover=\"setCursor(this);\" nowrap='nowrap' id='" + p[i][7] + "'>";
				t += 				  p[i][8] + "</div></div>";
			}
			document.getElementById("regions_container").innerHTML = t;
			placeCityInMap();
		} else {  
			alert("Не удалось получить данные: " + request.statusText);
		}  
		ShowElement(document.getElementById("loading"),		true);
	}  
	// иначе, если идет загрузка или в процессе - показываем слой "Загружаются данные"  
	else if (request.readyState == 3 || request.readyState == 1) {  
	ShowElement(document.getElementById("loading"), false);
	}  
}  
// загрузка списка копов из города
function mapCopsDoLoad(url) {
	document.getElementById("divRegionCopList").innerHTML = "";
if (window.XMLHttpRequest) {  
	request1 = new XMLHttpRequest();  
	request1.onreadystatechange = mapCopsProcessRequestChange;
	request1.open("GET", url, true);  
	request1.send(null);  
} else if (window.ActiveXObject) {
	request1 = new ActiveXObject("Microsoft.XMLHTTP");  
	if (request1) {  
		request1.onreadystatechange = mapCopsProcessRequestChange;  
		request1.open("GET", url, true);  
		request1.send();  
	}  
}  
}  

function mapCopsProcessRequestChange() {  
	if (request1.readyState == 4) {  
		if (request1.status == 200) {  
			document.getElementById("divRegionCopList").innerHTML = request1.responseXML.text;
		} else {
			alert("Не удалось получить данные: " + request1.statusText);
		}
		ShowElement(document.getElementById("divRegionCopListLoading"),	true);
	}  
	// иначе, если идет загрузка или в процессе - показываем слой "Загружаются данные"  
	else if (request1.readyState == 3 || request1.readyState == 1) {  
	ShowElement(document.getElementById("divRegionCopListLoading"), false);
	}  
}  

// drag

var maxIndex=1;
var dragInfoDown=new drag();
var dragInfoUp=new drag();

function drag()
{ // Конструктор объекта перетаскивания:
  this.kx=0;
  this.ky=0;
}
function grabMouseDown(coord)
{ // Начало перемещения:
  dragInfoDown.kx=coord.x;
  dragInfoDown.ky=coord.y;
  
  
  dragInfoUp.kx = dragInfoDown.kx;
  dragInfoUp.ky = dragInfoDown.ky;

  document.onmousemove=mouseMove;
  document.onmouseup=mouseUp;
  
  map.style.cursor = getFullURLPrefix() + "cursors/grabbing.cur";
  
  return false;
}
function mouseMove(e)
{ // Перемещение элемента:
	
	var coord = new Point();
	var mapPosition = getElementPosition(map);
	
	mousePagexy = mousePageXY(e);
	if(ie)
	{
		coord.x = mousePagexy.x - mapPosition.left;
		coord.y = mousePagexy.y - mapPosition.top;
	}
	else
	{
		coord.x = e.pageX - mapPosition.left;
		coord.y = e.pageY - mapPosition.top;
	}
	
	
  dragInfoUp.kx=coord.x;
  dragInfoUp.ky=coord.y;
  
  MoveDrag();
  return false;
}
function mouseUp(e)
{ // Завершение перемещения:
  document.onmousemove=null;
  document.onmouseup=null;
  
  position.x = position.x + (dragInfoDown.kx - dragInfoUp.kx)*map_Zoom_Coof;
  position.y = position.y + (dragInfoDown.ky - dragInfoUp.ky)*map_Zoom_Coof;
  
  map.style.cursor = getFullURLPrefix() + "cursors/grab.cur";
  
  Move();
  return false;
}
function setCursor(e)
{
	switch(instrument)
	{
		case 'zoomIn':
		e.style.cursor = "default";
		if(ie) e.style.cursor = getFullURLPrefix() + "cursors/zoom_in.cur";
		else e.style.cursor = "-moz-zoom-in";
		break;
		case 'grab':
		e.style.cursor = "default";
		if(ie) e.style.cursor = getFullURLPrefix() + "cursors/grab.cur";
		else e.style.cursor = "-moz-grab";
		break;
		case 'zoomOut':
		e.style.cursor = "default";
		if(ie) e.style.cursor = getFullURLPrefix() + "cursors/zoom_out.cur";
		else e.style.cursor = "-moz-zoom-out";
		break;
		default:
		if(e.id == "")
		{
			//e.style.cursor = "pointer";
			e.style.cursor = "crosshair";
		}else
		{
			//e.style.cursor = "default";
			e.style.cursor = "crosshair";
		}
		break;
	}
}
