function DrawRect_isInitialized()
{return this.initialized
}

function DrawRect_initMap()
{
this.map_img = this.owner.images[this.map_name];

if (this.coord_field_present)
	{this.LeftLongDeg = this.owner.getElementById(this.LLoDe_name);
	this.LeftLongMin = this.owner.getElementById(this.LLoM_name);
	this.LeftLongDirect = this.owner.getElementById(this.LLoDi_name);
	
	this.RightLongDeg = this.owner.getElementById(this.RLoDe_name);
	this.RightLongMin = this.owner.getElementById(this.RLoM_name);
	this.RightLongDirect = this.owner.getElementById(this.RLoDi_name);
	
	this.TopLatDeg = this.owner.getElementById(this.TLaDe_name);
	this.TopLatMin = this.owner.getElementById(this.TLaM_name);
	this.TopLatDirect = this.owner.getElementById(this.TLaDi_name);
	
	this.BotLatDeg = this.owner.getElementById(this.BLaDe_name);
	this.BotLatMin = this.owner.getElementById(this.BLaM_name);
	this.BotLatDirect = this.owner.getElementById(this.BLaDi_name);
	
	this.LLoDe_name = null;
	this.LLoM_name = null;
	this.LLoDi_name = null;
	
	this.RLoDe_name = null;
	this.RLoM_name = null;
	this.RLoDi_name = null;
	
	this.TLaDe_name = null;
	this.TLaM_name = null;
	this.TLaDi_name = null;
	
	this.BLaDe_name = null;
	this.BLaM_name = null;
	this.BLaDi_name = null;
	}

this.x_cod = 0;
this.y_cod = 0;
this.map_scale=1;

this.resetBound();
	
this.map_width = this.map_img.width;
this.map_height = this.map_img.height;

this.map_img.onmousedown = eventOnMouseDown;

document.onmousemove = eventOnMouseMove;
document.onmouseup = eventOnMouseUp;


this.onResize();
this.initialized = true;
this.onChange();
}

function DrawRect_resetBound()
{this.vLeft = ((45*this.x_cod)/this.map_scale)/*%360*/ - 180;
this.vRight = ((45*(this.x_cod+8))/this.map_scale)/*%360*/ - 180;
this.vTop = (22.5*(this.y_cod+8))/this.map_scale - 90;
this.vBottom = (22.5*this.y_cod)/this.map_scale - 90;
}

function DrawRect_onChange(text_field, max_value)
{if (!this.initialized) return;

if (text_field != null && max_value != null)
	if (isNaN(parseInt(text_field.value)) || parseInt(text_field.value)==0 || parseInt(text_field.value)<0)
		text_field.value = 0;
	else
		if (parseInt(text_field.value)>max_value)
			text_field.value = max_value;
		else
			text_field.value = parseInt(text_field.value);

if (this.coord_field_present)
	if(isNaN(parseInt(this.LeftLongDeg.value))||
		isNaN(parseInt(this.LeftLongMin.value))||
		isNaN(parseInt(this.LeftLongDirect.value))||
		isNaN(parseInt(this.RightLongDeg.value))||
		isNaN(parseInt(this.RightLongMin.value))||
		isNaN(parseInt(this.RightLongDirect.value))||
		isNaN(parseInt(this.TopLatDeg.value))||
		isNaN(parseInt(this.TopLatMin.value))||
		isNaN(parseInt(this.TopLatDirect.value))||
		isNaN(parseInt(this.BotLatDeg.value))||
		isNaN(parseInt(this.BotLatMin.value))||
		isNaN(parseInt(this.BotLatDirect.value)))
		{this.drawRect(0,0,0,0);
		return;
		}
	else
		{this.cLeft = (parseInt(this.LeftLongDeg.value)+parseInt(this.LeftLongMin.value)/60)*
			(parseInt(this.LeftLongDirect.value)?1:-1);
		this.cRight = (parseInt(this.RightLongDeg.value)+parseInt(this.RightLongMin.value)/60)*
			(parseInt(this.RightLongDirect.value)?1:-1);
		this.cBottom = (parseInt(this.BotLatDeg.value)+parseInt(this.BotLatMin.value)/60)*
			(parseInt(this.BotLatDirect.value)?-1:1);
		this.cTop = (parseInt(this.TopLatDeg.value)+parseInt(this.TopLatMin.value)/60)*
			(parseInt(this.TopLatDirect.value)?-1:1);
		}


if (this.cTop < this.cBottom)
	this.drawRect(0,0,0,0)
else
	this.drawRect(this.cLeft,this.cTop,this.cRight,this.cBottom);
}


function DrawRect_PrintSelection(new_Left,new_Top,new_Right,new_Bottom)
{if (!this.initialized) return;

if (isNaN(parseInt(new_Left))||
	isNaN(parseInt(new_Top))||
	isNaN(parseInt(new_Right))||
	isNaN(parseInt(new_Bottom)))
	{this.drawRect(0,0,0,0);
	return;
	}

this.cLeft = new_Left;
this.cRight = new_Right;
this.cBottom = new_Bottom;
this.cTop = new_Top;

if (this.cTop < this.cBottom)
	this.drawRect(0,0,0,0)
else
	this.drawRect(this.cLeft,this.cTop,this.cRight,this.cBottom);
}

function DrawRect_getLayerStyle(id)// Доступ к Стилю Слоя
{if (this.isIE)
	return document.all[id].style;
else
	return document.getElementById(id).style;
}

function DrawRect_onResize()// Обновление параметров при перемещении карты в броузере
{var i;
var obj = this.map_img;
var x = obj.offsetLeft;
var y = obj.offsetTop;

while ((obj=obj.offsetParent)!=null && obj.tagName!='BODY') //Перебираем все слои, пока на доберемся до конца
	{x += obj.offsetLeft; // суммируем смещения
	y += obj.offsetTop;
	if (isNaN(obj.border)) // если есть обрамление, добавляем его толщину
		continue;
	x += parseInt(obj.border);
	y += parseInt(obj.border);
	}

obj = this.LayerZoomBox;
for(i in obj)
	{obj[i].width = this.map_width;
	obj[i].height = this.map_height;
	}

for(i in obj)
	{obj[i].left = x+(parseInt(obj[i].left)-this.map_x);
	obj[i].top = y+(parseInt(obj[i].top)-this.map_y);
	}

this.map_x = x;
this.map_y = y;
}

function DrawRect_setClip()
{var zleft=0, zright=0, zbottom=0, ztop=0;

if (this.firstX>this.secondX)
	{zright=this.firstX;
	zleft=this.secondX;
	}
else
	{zleft=this.firstX;
	zright=this.secondX;
	}

if (this.firstY>this.secondY)
	{zbottom=this.firstY;
	ztop=this.secondY;
	}
else
	{ztop=this.firstY;
	zbottom=this.secondY;
	}

if (this.firstX == this.secondX || this.firstY == this.secondY)
	return;

var BorderSize = 1;
this.getLayerStyle('TopBorder').clip = 'rect('+ztop+'px '+zright+'px '+Number(ztop+BorderSize)+'px '+zleft+'px)';
this.getLayerStyle('LeftBorder').clip = 'rect('+ztop+'px '+Number(zleft+BorderSize)+'px '+zbottom+'px '+zleft+'px)';
this.getLayerStyle('RightBorder').clip = 'rect('+ztop+'px '+zright+'px '+zbottom+'px '+Number(zright-BorderSize)+'px)';
this.getLayerStyle('BottomBorder').clip = 'rect('+Number(zbottom-BorderSize)+'px '+zright+'px '+zbottom+'px '+zleft+'px)';
}

function DrawRect_drawRect(c_x1,c_y1,c_x2,c_y2) // Рисование области с заданными координатами
{if (c_x1 > 180)
	c_x1 -= 360;
if (c_x2 > 180)
	c_x2 -= 360;

var CrossMap = false;
var CrossLeft = false;
var CrossRight = false;

if (c_x1 > c_x2) //Переход через 180
	{if (this.vLeft < c_x1 && c_x2+360 < this.vRight)
		{// Без пересечения
		c_x2 += 360;
		}
	else
		{if (c_x1 <= this.vLeft && c_x2+360 >= this.vRight)
			CrossMap = true; //Пересечение всей карты
		else
			{if (this.vLeft < c_x2 && c_x2 < this.vRight || this.vLeft < c_x2+360 && c_x2+360 < this.vRight)
				{CrossLeft = true; //Пересечение с левой границей
				if (c_x2+360 < this.vRight)
				c_x2 += 360;
				}
			if (this.vLeft < c_x1 && c_x1 < this.vRight)
				CrossRight = true; //Пересечение с правой границей
			}
		}
	}
else//Без перехода через 180
	{if (this.vLeft < c_x1 && c_x2 < this.vRight || this.vLeft < c_x1+360 && c_x2+360 < this.vRight)
		{// Без пересечения
		if (c_x2+360 < this.vRight)
			{c_x1 += 360;
			c_x2 += 360;
			}
		}
	else
		{if (c_x1 <= this.vLeft && c_x2 >= this.vRight/* || c_x1+360 <= this.vLeft && c_x2+360 >= this.vRight*/)
			{CrossMap = true; //Пересечени всей карты
			/*if (c_x1+360 <= this.vLeft)
				{c_x1 += 360;
				c_x2 += 360;
				}*/
			}
		else
			{if (this.vLeft < c_x2 && c_x2 < this.vRight/* || this.vLeft < c_x2+360 && c_x2+360 < this.vRight*/)
				{CrossLeft = true; //Пересечение с левой границей
				/*if (c_x2+360 < this.vRight)
					c_x2 += 360;*/
				}
			if (this.vLeft < c_x1 && c_x1 < this.vRight || this.vLeft < c_x1+360 && c_x1+360 < this.vRight)
				{CrossRight = true; //Пересечение с правой границей
				if (c_x1+360 < this.vRight)
					c_x1 += 360;
				}
			}
		}
	}



var BorderSize = 1;
var draw_x1 = (c_x1-this.vLeft)*this.map_width/(this.vRight-this.vLeft);
var draw_y1 = (this.vTop-c_y1)*this.map_height/(this.vTop-this.vBottom);
var draw_x2 = (c_x2-this.vLeft)*this.map_width/(this.vRight-this.vLeft);
var draw_y2 = (this.vTop-c_y2)*this.map_height/(this.vTop-this.vBottom);



if (CrossMap)
	{this.getLayerStyle('TopBorder').clip = 'rect('+draw_y1+'px '+this.map_width+'px '+Number(draw_y1+BorderSize)+'px '+0+'px)';
	this.getLayerStyle('LeftBorder').clip = 'rect('+0+'px '+0+'px '+0+'px '+0+'px)';
	this.getLayerStyle('RightBorder').clip = 'rect('+0+'px '+0+'px '+0+'px '+0+'px)';
	this.getLayerStyle('BottomBorder').clip = 'rect('+Number(draw_y2-BorderSize)+'px '+this.map_width+'px '+draw_y2+'px '+0+'px)';
	this.getLayerStyle('TopSplitBorder').clip = 'rect('+0+'px '+0+'px '+0+'px '+0+'px)';
	this.getLayerStyle('BottomSplitBorder').clip = 'rect('+0+'px '+0+'px '+0+'px '+0+'px)';
	}
else if (CrossLeft || CrossRight)
		{this.getLayerStyle('TopBorder').clip = 'rect('+0+'px '+0+'px '+0+'px '+0+'px)';
		this.getLayerStyle('LeftBorder').clip = 'rect('+0+'px '+0+'px '+0+'px '+0+'px)';
		this.getLayerStyle('RightBorder').clip = 'rect('+0+'px '+0+'px '+0+'px '+0+'px)';
		this.getLayerStyle('BottomBorder').clip = 'rect('+0+'px '+0+'px '+0+'px '+0+'px)';
		this.getLayerStyle('TopSplitBorder').clip = 'rect('+0+'px '+0+'px '+0+'px '+0+'px)';
		this.getLayerStyle('BottomSplitBorder').clip = 'rect('+0+'px '+0+'px '+0+'px '+0+'px)';
		if (CrossLeft)
			{this.getLayerStyle('RightBorder').clip = 'rect('+draw_y1+'px '+draw_x2+'px '+draw_y2+'px '+Number(draw_x2-BorderSize)+'px)';
			this.getLayerStyle('TopSplitBorder').clip = 'rect('+draw_y1+'px '+draw_x2+'px '+Number(draw_y1+BorderSize)+'px '+0+'px)';
			this.getLayerStyle('BottomSplitBorder').clip = 'rect('+Number(draw_y2-BorderSize)+'px '+draw_x2+'px '+draw_y2+'px '+0+'px)';
			}
		if (CrossRight)
			{this.getLayerStyle('LeftBorder').clip = 'rect('+draw_y1+'px '+Number(draw_x1+BorderSize)+'px '+draw_y2+'px '+draw_x1+'px)';
			this.getLayerStyle('TopBorder').clip = 'rect('+draw_y1+'px '+this.map_width+'px '+Number(draw_y1+BorderSize)+'px '+draw_x1+'px)';
			this.getLayerStyle('BottomBorder').clip = 'rect('+Number(draw_y2-BorderSize)+'px '+this.map_width+'px '+draw_y2+'px '+draw_x1+'px)';
			}
		}
	else
		{this.getLayerStyle('TopBorder').clip = 'rect('+draw_y1+'px '+draw_x2+'px '+Number(draw_y1+BorderSize)+'px '+draw_x1+'px)';
		this.getLayerStyle('LeftBorder').clip = 'rect('+draw_y1+'px '+Number(draw_x1+BorderSize)+'px '+draw_y2+'px '+draw_x1+'px)';
		this.getLayerStyle('RightBorder').clip = 'rect('+draw_y1+'px '+draw_x2+'px '+draw_y2+'px '+Number(draw_x2-BorderSize)+'px)';
		this.getLayerStyle('BottomBorder').clip = 'rect('+Number(draw_y2-BorderSize)+'px '+draw_x2+'px '+draw_y2+'px '+draw_x1+'px)';
		this.getLayerStyle('TopSplitBorder').clip = 'rect('+0+'px '+0+'px '+0+'px '+0+'px)';
		this.getLayerStyle('BottomSplitBorder').clip = 'rect('+0+'px '+0+'px '+0+'px '+0+'px)';
		}




/*if (c_x1>c_x2)
	{this.getLayerStyle('TopBorder').clip = 'rect('+draw_y1+'px '+this.map_width+'px '+Number(draw_y1+BorderSize)+'px '+draw_x1+'px)';
	this.getLayerStyle('LeftBorder').clip = 'rect('+draw_y1+'px '+Number(draw_x1+BorderSize)+'px '+draw_y2+'px '+draw_x1+'px)';
	this.getLayerStyle('RightBorder').clip = 'rect('+draw_y1+'px '+draw_x2+'px '+draw_y2+'px '+Number(draw_x2-BorderSize)+'px)';
	this.getLayerStyle('BottomBorder').clip = 'rect('+Number(draw_y2-BorderSize)+'px '+this.map_width+'px '+draw_y2+'px '+draw_x1+'px)';
	this.getLayerStyle('TopSplitBorder').clip = 'rect('+draw_y1+'px '+draw_x2+'px '+Number(draw_y1+BorderSize)+'px '+0+'px)';
	this.getLayerStyle('BottomSplitBorder').clip = 'rect('+Number(draw_y2-BorderSize)+'px '+draw_x2+'px '+draw_y2+'px '+0+'px)';
	}*/
}

function DrawRect_msBegin(e) // Начало рисования рамки
{if (this.isIE)
	e = event;
	
if (this.isIE && e.button!=1)
	return false;
this.insideMap=1;

this.getImageXY(e);
this.firstX = this.imageX;
this.firstY = this.imageY;
this.secondX = this.imageX;
this.secondY = this.imageY;

this.getLayerStyle('TopBorder').clip = 'rect('+this.firstY+'px '+this.secondX+'px '+this.secondY+'px '+this.firstX+'px)';
this.getLayerStyle('LeftBorder').clip = 'rect('+this.firstY+'px '+this.secondX+'px '+this.secondY+'px '+this.firstX+'px)';
this.getLayerStyle('RightBorder').clip = 'rect('+this.firstY+'px '+this.secondX+'px '+this.secondY+'px '+this.firstX+'px)';
this.getLayerStyle('BottomBorder').clip = 'rect('+this.firstY+'px '+this.secondX+'px '+this.secondY+'px '+this.firstX+'px)';
this.getLayerStyle('TopSplitBorder').clip = 'rect('+0+'px '+0+'px '+0+'px '+0+'px)';
this.getLayerStyle('BottomSplitBorder').clip = 'rect('+0+'px '+0+'px '+0+'px '+0+'px)';

this.getLayerStyle('TopBorder').visibility = 'visible';
this.getLayerStyle('LeftBorder').visibility = 'visible';
this.getLayerStyle('RightBorder').visibility = 'visible';
this.getLayerStyle('BottomBorder').visibility = 'visible';
this.getLayerStyle('TopSplitBorder').visibility = 'visible';
this.getLayerStyle('BottomSplitBorder').visibility = 'visible';
return false;
}

function DrawRect_updateField(с_x1, с_y1, с_x2, с_y2)
{if (!this.coord_field_present)
	return;
this.LeftLongDeg.value = Math.abs(с_x1 - с_x1%1);
this.LeftLongMin.value = Math.abs((с_x1%1)*60 - ((с_x1%1)*60)%1);
this.LeftLongDirect.selectedIndex = с_x1<0?0:1;

this.TopLatDeg.value = Math.abs(с_y1 - с_y1%1);
this.TopLatMin.value = Math.abs((с_y1%1)*60 - ((с_y1%1)*60)%1);
this.TopLatDirect.selectedIndex = с_y1<0?1:0;

this.RightLongDeg.value = Math.abs(с_x2 - с_x2%1);
this.RightLongMin.value = Math.abs((с_x2%1)*60 - ((с_x2%1)*60)%1);
this.RightLongDirect.selectedIndex = с_x2<0?0:1;

this.BotLatDeg.value = Math.abs(с_y2 - с_y2%1);
this.BotLatMin.value = Math.abs((с_y2%1)*60 - ((с_y2%1)*60)%1);
this.BotLatDirect.selectedIndex = с_y2<0?1:0;
}

function DrawRect_onEndDraw()
{this.insideMap=0; //Окончание рисования области
//Вычисление позиций на рисунке
var m_x1 = Math.min(this.firstX,this.secondX);
var m_x2 = Math.max(this.firstX,this.secondX);

var m_y1 = Math.min(this.firstY,this.secondY);
var m_y2 = Math.max(this.firstY,this.secondY);

if (m_x1<0)	m_x1=0;
if (m_y1<0)	m_y1=0;

if (m_x2 > this.map_width)
	m_x2 = this.map_width;
if (m_y2 > this.map_height)
	m_y2 = this.map_height;

//Вычисление координат	
this.cLeft = ((this.vRight-this.vLeft)*m_x1/this.map_width + this.vLeft);
this.cRight = ((this.vRight-this.vLeft)*m_x2/this.map_width + this.vLeft);
this.cBottom = this.vTop - ((this.vTop-this.vBottom)*m_y2/this.map_height);
this.cTop = this.vTop - ((this.vTop-this.vBottom)*m_y1/this.map_height);

if (this.cLeft > 180)
	this.cLeft -= 360;
if (this.cRight > 180)
	this.cRight -= 360;
this.updateField(this.cLeft, this.cTop, this.cRight, this.cBottom)
}

function DrawRect_msStop(e)
{if (!this.insideMap || this.isIE && event.button!=1)
 	return;

 status = "";
 return this.onEndDraw();
}

function DrawRect_getImageXY(e) // Получение позиции мыши при перемещении
{if (this.isIE)
	e = event;

this.imageX = e.clientX + document.body.scrollLeft - this.map_x - 2;
this.imageY = e.clientY + document.body.scrollTop - this.map_y - 2;

if (this.imageX<0)
	this.imageX = 0;
else
	if (this.imageX>this.map_width)
		this.imageX = this.map_width;

if (this.imageY<0)
	this.imageY = 0;
else
	if (this.imageY>this.map_height)
		this.imageY = this.map_height;
}

function DrawRect_onMouseMove(e) 
{if (!this.insideMap)
	return;

this.getImageXY(e);
this.secondX = this.imageX;
this.secondY = this.imageY;

this.setClip();

status = Math.round(((this.vRight-this.vLeft)*this.secondX/this.map_width + this.vLeft)*10)/10 +' x '+
	Math.round((this.vTop - ((this.vTop-this.vBottom)*this.secondY/this.map_height))*10)/10;
}

function DrawRect_SetCreatorName(new_creator_name)
{if (!this.initialized || this.creator_name == new_creator_name)
	return
this.creator_name = new_creator_name;
this.map_img.src = this.creator_name+"mx="+this.x_cod+"&my="+this.y_cod+"&ms="+this.map_scale;
}






function DrawRect_In ()
{if (!this.initialized) return;

if (Math.round(this.map_scale*2)>32)
	return;
this.map_scale = Math.round(this.map_scale*2);
this.x_cod = Math.round((this.x_cod+4)*2)-4;
this.y_cod = Math.round((this.y_cod+4)*2)-4;

if (this.x_cod >= this.map_scale*8)
	this.x_cod = this.map_scale*8 - 1;
else
	if (this.x_cod < 0)
		this.x_cod = 0;
	
if (this.y_cod >= (this.map_scale-1)*8)
	this.y_cod = (this.map_scale-1)*8 - 1;
else
	if (this.y_cod < 0)
		this.y_cod = 0;

this.map_img.src = this.creator_name+"mx="+this.x_cod+"&my="+this.y_cod+"&ms="+this.map_scale;
this.resetBound();
this.onChange();
}

function DrawRect_Out ()
{if (!this.initialized) return;

if (Math.round(this.map_scale)==1)
	return;
this.map_scale=Math.round(this.map_scale/2);
this.x_cod=Math.round((this.x_cod+4)/2)-4;
this.y_cod=Math.round((this.y_cod+4)/2)-4;

if (this.x_cod >= this.map_scale*8)
	this.x_cod = this.map_scale*8 - 1;
else
	if (this.x_cod < 0)
		this.x_cod = 0;
	
if (this.y_cod >= (this.map_scale-1)*8)
	this.y_cod = this.map_scale==1?0:(this.map_scale-1)*8 - 1;
else
	if (this.y_cod < 0)
		this.y_cod = 0;

this.map_img.src = this.creator_name+"mx="+this.x_cod+"&my="+this.y_cod+"&ms="+this.map_scale;
this.resetBound();
this.onChange();
}

function DrawRect_StepLeft ()
{if (!this.initialized) return;

if (this.x_cod == 0)
	this.x_cod = this.map_scale * 8 - 1;
else
	this.x_cod--;

this.map_img.src = this.creator_name+"mx="+this.x_cod+"&my="+this.y_cod+"&ms="+this.map_scale;
this.resetBound();
this.onChange();
}

function MapStepRight ()
{if (!this.initialized) return;

if (this.x_cod == this.map_scale * 8 - 1)
	this.x_cod = 0;
else
	this.x_cod++;

this.map_img.src = this.creator_name+"mx="+this.x_cod+"&my="+this.y_cod+"&ms="+this.map_scale;
this.resetBound();
this.onChange();
}

function DrawRect_StepBottom ()
{if (!this.initialized) return;

if (this.y_cod == 0)
	return;
this.y_cod--;

this.map_img.src = this.creator_name+"mx="+this.x_cod+"&my="+this.y_cod+"&ms="+this.map_scale;
this.resetBound();
this.onChange();
}

function DrawRect_StepTop ()
{if (!this.initialized) return;

if (this.y_cod == (this.map_scale-1)*8 || this.map_scale == 1)
	return;
this.y_cod++;

this.map_img.src = this.creator_name+"mx="+this.x_cod+"&my="+this.y_cod+"&ms="+this.map_scale;
this.resetBound();
this.onChange();
}

function DrawRect_JumpLeft()
{if (!this.initialized) return;

if (this.x_cod - (this.x_cod%4?this.x_cod%4:4) < 0)
	this.x_cod = (this.map_scale*8) - 4;
else
	this.x_cod-= this.x_cod%4?this.x_cod%4:4;

this.map_img.src = this.creator_name+"mx="+this.x_cod+"&my="+this.y_cod+"&ms="+this.map_scale;
this.resetBound();
this.onChange();
}

function DrawRect_JumpRight()
{if (!this.initialized) return;

if (this.x_cod + (4 - this.x_cod%4) >= this.map_scale * 8)
	this.x_cod = 0;
else
	this.x_cod+= 4 - this.x_cod%4;

this.map_img.src = this.creator_name+"mx="+this.x_cod+"&my="+this.y_cod+"&ms="+this.map_scale;
this.resetBound();
this.onChange();
}

function DrawRect_JumpBottom()
{if (!this.initialized) return;

if (this.y_cod == 0)
	return;
this.y_cod-= this.y_cod%4?this.y_cod%4:4;

this.map_img.src = this.creator_name+"mx="+this.x_cod+"&my="+this.y_cod+"&ms="+this.map_scale;
this.resetBound();
this.onChange();
}

function DrawRect_JumpTop()
{if (!this.initialized) return;

if (this.y_cod == (this.map_scale-1) * 8 - 1 || this.map_scale == 1)
	return;
if (this.y_cod + (4 - this.y_cod%4) > (this.map_scale-1) * 8)
	this.y_cod = (this.map_scale-1) * 8
else
	this.y_cod += 4 - this.y_cod%4;
this.map_img.src = this.creator_name+"mx="+this.x_cod+"&my="+this.y_cod+"&ms="+this.map_scale;
this.resetBound();
this.onChange();
}

function DrawRect_BoundCoord ()
{if (!this.initialized)
	return;

if (this.cRight < this.cLeft)
		this.cRight += 360;

if ((this.cLeft >= -180) && (this.cLeft <= 180) &&
	(this.cTop >= -90) && (this.cTop <= 90) &&
	(this.cRight >= -180) && (this.cRight <= 360) &&
	(this.cBottom >= -90) && (this.cBottom <= 90) &&
	(this.cLeft <= this.cRight) && (this.cBottom <= this.cTop) &&
	(this.cRight - this.cLeft <= 360))
	{if (this.cLeft == this.cRight && this.cTop == this.cBottom)
		this.map_scale = 32;
	else // Вычисление промежуточного масштаба
		{if ((this.cRight - this.cLeft)/360 > (this.cTop - this.cBottom)/180)
			this.map_scale = Math.round(Math.pow(2,Math.floor(Math.log(360/(this.cRight-this.cLeft))/Math.LN2)));
		else
			this.map_scale = Math.round(Math.pow(2,Math.floor(Math.log(180/(this.cTop-this.cBottom))/Math.LN2)));
		if (this.map_scale > 32)
			this.map_scale = 32;
		//Проверка допустимости масштаба и корректировка (при необходимости)
		if (Math.floor((this.cRight + 180)*this.map_scale/45) - 
			Math.floor((this.cLeft + 180)*this.map_scale/45) >= 8 ||
			Math.floor((this.cTop + 90)*this.map_scale/22.5) - 
			Math.floor((this.cBottom + 90)*this.map_scale/22.5) >= 8)
			this.map_scale /= 2;
		if (this.map_scale == 0) // Такое может случиться, если запросят карту шириной 360 градусов, у которой края не ложаться на границы изображений
			this.map_scale = 1;
		}

	this.x_cod = (this.map_scale*8 + Math.round(((this.cLeft+this.cRight)/2 + 180)*this.map_scale/45) - 4) % (this.map_scale*8);
	this.y_cod = (this.map_scale*8 + Math.round(((this.cTop+this.cBottom)/2 + 90)*this.map_scale/22.5) - 4) % (this.map_scale*8);
	if (this.map_scale == 1)
		this.y_cod = 0
	else if (this.y_cod > (this.map_scale-1)*8)
			this.y_cod = (this.map_scale-1)*8;


	this.map_img.src = this.creator_name+"mx="+this.x_cod+"&my="+this.y_cod+"&ms="+this.map_scale;
	this.resetBound();
	this.onChange();

	}
}

function DrawRectClass(new_owner, new_map_name, new_creator_name, new_LLoDe_name, new_LLoM_name, new_LLoDi_name, new_TLaDe_name, new_TLaM_name, new_TLaDi_name, new_RLoDe_name, new_RLoM_name, new_RLoDi_name, new_BLaDe_name, new_BLaM_name, new_BLaDi_name)
{
this.initialized = false;

this.coord_field_present = new_LLoDe_name!=null && new_LLoM_name!=null && new_LLoDi_name!=null &&
	new_TLaDe_name!=null && new_TLaM_name!=null && new_TLaDi_name!=null &&
	new_RLoDe_name!=null && new_RLoM_name!=null && new_RLoDi_name!=null &&
	new_BLaDe_name!=null && new_BLaM_name!=null && new_BLaDi_name!=null;

this.owner = new_owner;
this.map_name = new_map_name;

this.creator_name = new_creator_name;

if (this.coord_field_present)
	{
	this.LLoDe_name = new_LLoDe_name;
	this.LLoM_name = new_LLoM_name;
	this.LLoDi_name = new_LLoDi_name;
	
	this.RLoDe_name = new_RLoDe_name;
	this.RLoM_name = new_RLoM_name;
	this.RLoDi_name = new_RLoDi_name;
	
	this.TLaDe_name = new_TLaDe_name;
	this.TLaM_name = new_TLaM_name;
	this.TLaDi_name = new_TLaDi_name;
	
	this.BLaDe_name = new_BLaDe_name;
	this.BLaM_name = new_BLaM_name;
	this.BLaDi_name = new_BLaDi_name;
	}

this.map_x=0;
this.map_y=0;
this.map_width = 0;
this.map_height = 0;

this.vLeft = 0;
this.vRight = 0;
this.vTop = 0;
this.vBottom = 0;

this.cLeft = 0;
this.cRight = 0;
this.cTop = 0;
this.cBottom = 0;

this.insideMap = 0;
this.LayerZoomBox = new Array ();
this.isIE = document.all != null;
this.firstX = 0;
this.firstY = 0;
this.secondX = 0;
this.secondY = 0;
this.imageX = 0;
this.imageY = 0;

this.x_cod = 0;
this.y_cod = 0;
this.map_scale=0;


this.isInitialized = DrawRect_isInitialized;
this.initMap = DrawRect_initMap;
this.resetBound = DrawRect_resetBound;
this.getLayerStyle = DrawRect_getLayerStyle;
this.onResize = DrawRect_onResize;
this.setClip = DrawRect_setClip;
this.msBegin = DrawRect_msBegin;
this.msStop = DrawRect_msStop;
this.onEndDraw = DrawRect_onEndDraw;
this.getImageXY = DrawRect_getImageXY;
this.onMouseMove = DrawRect_onMouseMove;
this.drawRect = DrawRect_drawRect;
this.updateField = DrawRect_updateField;
this.onChange = DrawRect_onChange;
this.SetCreatorName = DrawRect_SetCreatorName;
this.PrintSelection = DrawRect_PrintSelection;


this.In = DrawRect_In;
this.Out = DrawRect_Out;
this.StepLeft = DrawRect_StepLeft;
this.StepRight = MapStepRight;
this.StepBottom = DrawRect_StepBottom;
this.StepTop = DrawRect_StepTop;
this.JumpLeft = DrawRect_JumpLeft;
this.JumpRight = DrawRect_JumpRight;
this.JumpBottom = DrawRect_JumpBottom;
this.JumpTop = DrawRect_JumpTop;
this.BoundCoord = DrawRect_BoundCoord;


var borderId = new Array('TopBorder', 'LeftBorder', 'RightBorder', 'BottomBorder', 'TopSplitBorder', 'BottomSplitBorder');
for (var i in borderId)//
	{document.writeln('<div id="'+borderId[i]+'" style="visibility:visible;position:absolute;z-index:98;left:0;top:0;background-color:#ff0000"></div>');
	this.getLayerStyle(borderId[i]).clip = 'rect('+0+'px '+0+'px '+0+'px '+0+'px)';
	this.LayerZoomBox[this.LayerZoomBox.length] = this.getLayerStyle(borderId[i]);
	}
}