//minimap
function MiniMap(width,height){
	this.miniwidth = width;                         //размеры миникарты
	this.miniheight = height;
	this.minix = map.xx + map.scrmapw - this.miniwidth;
	this.miniy = map.yy + map.scrmaph - this.miniheight;

	this.coordx;
	this.coordy;

	var z = map.scrmapw * map.zoom/map.edge0;       //размеры рамки
	var z2 = map.scrmaph * map.zoom/map.edge0;       //размеры рамки
	this.width = Math.round(this.miniwidth*z/map.width)
//	this.height = Math.round(this.width*map.scrmaph/map.scrmapw);
	this.height = Math.round(this.miniheight*z2/map.height)
	this.width0 = this.width;
	this.height0 = this.height;

	this.top = 0;                                      //координаты рамки
	this.left = 0;

	this.createmap = function(){
        this.mini = document.createElement("div");
		var img = document.createElement("img");
		img.height = this.miniwidth;
		img.width = this.miniheight;
		img.src = "/get_minimap.phtml";
		this.mini.appendChild(img);
		this.mini.id = "mini";
        this.mini.style.width = this.miniwidth + "px";
		this.mini.style.height = this.miniheight + "px";
		this.mini.style.left = this.minix + "px";
        this.mini.style.top = this.miniy + "px";

        this.mini.onmousemove = this.getminicoords;
		document.getElementById("overmap").appendChild(this.mini);
	}
	this.resizeWin = function(){
   		this.minix = map.scrmapw - this.miniwidth;
		this.miniy = map.scrmaph - this.miniheight;
		this.mini.style.left = this.minix + "px";
		this.mini.style.top = this.miniy + "px";
		this.zoomframe();
	}
    this.getminicoords = function(e){

			if (map.time != "") {
		 		clearInterval(map.time);
			}

			var posx = 0;
			var posy = 0;
            if (!e) var e = window.event;
			if (e.pageX || e.pageY) 	{
				posx = e.pageX;
				posy = e.pageY;
			}
			else if (e.clientX || e.clientY) 	{
				posx = e.clientX + document.body.scrollLeft
					+ document.documentElement.scrollLeft;
				posy = e.clientY + document.body.scrollTop
					+ document.documentElement.scrollTop;
			}
            this.coordx = posx - map.scrmapw + mini.miniwidth;
			this.coordy = posy - map.scrmaph + mini.miniheight;
    }

    this.createframe = function(){
        this.frame = this.mini.appendChild(document.createElement("div"));
		this.frame.id = "frame";
        this.zoomframe();
		miniDrag.init(this.frame);
	}

	this.show = function(){
		this.createmap();
		this.createframe();
		this.updateframe();
	}
	this.updateframe = function(){
		var sx = Math.floor(-constants['min_coord_x']/map.zoom*map.edge);
		var sy = Math.floor(-constants['min_coord_y']/map.zoom*map.edge);

        this.frame.style.left = - Math.round((map.proX - sx)*this.miniwidth/(map.width/(map.zoom/map.edge))) + "px" ;
		this.frame.style.top = - Math.round((map.proY - sy)*this.miniheight/(map.height/(map.zoom/map.edge))) + "px" ;
	}
 	this.zoomframe = function(){
		var sx = Math.floor(-constants['min_coord_x']/map.zoom*map.edge);
		var sy = Math.floor(-constants['min_coord_y']/map.zoom*map.edge);

		var z = map.scrmapw * map.zoom/map.edge;       //размеры рамки
		var z2 = map.scrmaph * map.zoom/map.edge;
		this.setParam("width", Math.ceil(this.miniwidth*z/map.width))
		this.setParam("height", Math.ceil(this.miniheight*z2/map.height))

        this.setParam("width", (this.width > this.miniwidth) ? this.miniwidth : this.width)
		this.setParam("height", (this.height > this.miniheight) ? this.miniheight : this.height)

		this.setParam("left", - Math.ceil((map.proX - sx)*this.miniwidth/(map.width/(map.zoom/map.edge))))
		this.setParam("top", - Math.ceil((map.proY - sy)*this.miniheight/(map.height/(map.zoom/map.edge))))
	}

    this.setParam = function(attr, value){             //параметры кусков карты
     	 if (attr == "width") {
     	        this.width = value;
              this.frame.style.width = value - 2 + "px";
          }
     	    if (attr == "height") {
				
     	      this.height = value;
			  if(value<2)
			  	return;
              this.frame.style.height = value - 2 + "px";
          }
          if (attr == "left") {
     	        this.left = value;
              this.frame.style.left = value+"px";
          }
     	    if (attr == "top") {
     	        this.top = value;
              this.frame.style.top = value+"px";
          }
        }
	this.hide = function(){
		this.mini.style.display = "none";
	}
	this.showMe = function(){
		this.mini.style.display = "block";
	}
    this.drag = function(nx,ny){

        mini.left = Math.round(nx);
		mini.top =  Math.round(ny);

        map.minidrag();
     }
}