//Zoom
function Zoom(count){
	this._countdef = count; // Кол-во шагов на уменьшение
	this._count = this._countdef;
	this._counter = this._count + 1;
	this._plus = 400 / this._countdef;
	this.plus;
	this._delta = 0;
	this._zoomer = 0;
	this._direction;
	this.bproX;
	this.bproY;
	this.active = true;


	//
	//
	this.move = function(delta) {
        if (zoom.active == true) {
	   		if (delta > 0 && map.level > 1 || delta < 0 && map.level < map.levels) {          //ограничение зума

				map.xx = Math.round(map.globalcenterx*map.edge0/map.zoom);
				map.yy = Math.round(map.globalcentery*map.edge0/map.zoom);

				zoom._direction = delta;
				zoom._counter = 1;
				zoom._zoomer = setTimeout(zoom.zoomer, 10);
				zoom.active = false;
			}
		}
	}

	//	Начало увеличения/уменьшения
	//
	this.zoomer = function () {

	 	if (zoom._counter == 1) {                                          //первая фаза движения

			if (zoom._direction <0) {                                         //определения направления зума
			   zoom.plus = - zoom._plus/2;
	//		   zoom._count = Math.round(zoom._countdef / 2);
			} else {
			   zoom.plus = zoom._plus;
   //			   zoom._count = zoom._countdef;
			}
			
			zoom.bproX = map.proX;
			zoom.bproY = map.proY;

			if (map.xx > 0) {     
				var quantity = Math.ceil (map.xx / map.edge);
			} else {
	            var quantity = Math.floor (map.xx / map.edge);
			}
			var width =  quantity * map.edge;
			zoom._delta = quantity * (map.edge +  zoom._count * zoom.plus);
			zoom._movex = (zoom._delta * (map.xx/width) - map.xx) * zoom._counter / zoom._count;

			if (map.yy > 0) {
				quantity = Math.ceil (map.yy / map.edge);
			} else {
	            quantity = Math.floor (map.yy / map.edge);
			}
			var width =  quantity * map.edge;
			zoom._delta = quantity * (map.edge +  zoom._count * zoom.plus);
			zoom._movey = (zoom._delta * (map.yy/width) - map.yy) * zoom._counter / zoom._count;

			map.layerupdate();               					// обновить нижний слой
		}

	    if (zoom._counter <= zoom._count) {                                   //промежуточные фазы
	        zoom._counter++;
	        zoom._zoomer = setTimeout(zoom.zoomer, 50);

	 		map.edge = map.edge + zoom.plus;

           	map.UpdateSize();

			proX = map.proX - zoom._movex;
			proY = map.proY - zoom._movey;

			var sx = Math.floor(-constants['min_coord_x']/map.zoom*map.edge);
			var sy = Math.floor(-constants['min_coord_y']/map.zoom*map.edge);

			proX = (map.mlr + proX < map.scrmapw+sx) ? map.scrmapw+sx - map.mlr : proX;
			proY = (map.mlb + proY < map.scrmaph+sy) ? map.scrmaph+sy - map.mlb : proY;

			proX = (proX > sx) ? sx : proX;
			proY = (proY > sy) ? sy : proY;


/*			if (proX > 0) {
				proX = 0;
			}

			if (proY > 0) {
				proY = 0;
			}
*/

			map.setParam("proX",proX);
			map.setParam("proY",proY);

			map.zooming(zoom._counter);


	   } else {
	   		// Окончание движения
			map.level = map.level - zoom._direction;
			map.zoom = (zoom._direction > 0) ? map.zoom/2 : map.zoom*2;
			map.edge = map.edge0;
			map.UpdateSize();
			setTimeout("map.drag()", 50) ;                          //обновление карты
	 		setTimeout("map.update()", 50) ;

	        map.globalcenterx = Math.floor((map.scrmapw/2 - map.proX)*map.zoom/map.edge);
            map.globalcentery = Math.floor((map.scrmaph/2 - map.proY)*map.zoom/map.edge);

			map.xx = map.xx - (map.proX - zoom.bproX);
			map.yy = map.yy - (map.proY - zoom.bproY);
	   }
	}

	//	Функция обработки колеса мыши
	//

	var navName = navigator.appName;
	var navVer = navigator.appVersion;

	 this.wheel = function(event){
		if (zoom.active == true) {

			var delta = 0;
			if (!event) event = window.event;
			if (event.wheelDelta) {
				delta = event.wheelDelta;
				if (window.opera) delta = -delta;
			} else if (event.detail) {
				delta = event.detail;
			}
            delta = (delta > 0) ? 1 : -1;

            if(navName=="Netscape" || navName=="Opera"){
 				if((navVer.indexOf("Chrome")!=-1) || (navVer.indexOf("Safari")!=-1)){
	            	delta = (delta > 0) ? 1 : -1;
				}
				else{
                    delta = (delta > 0) ? -1 : 1;
				}
}


       		if (delta > 0 && map.level > 1 || delta < 0 && map.level < map.levels) {          //ограничение зума
		      zoom._direction = delta;
		      zoom._counter = 1;
			  zoom.active = false;


			  map.HideAllLayers();

		      zoom._zoomer = setTimeout(zoom.zoomer, 10);
		    }
		}
	}
}