// Кусок карты
//
function MapPart(x, y){

    this._numX = x; // Номер куска карты по X
    this._numY = y;
//    this.relx;//координаты относительно куска
//    this.rely;

		this.createmapimg = function(){                                 //куски первого слоя
			this.img = document.createElement("img");
			this.img.style.position = "absolute";
			this.img.width = map.edge;
			this.img.height = map.edge;
			this.setParam("top", (this._numY-1)*map.edge);
			this.setParam("left", (this._numX-1)*map.edge);
	   		this.setSrc(this._numX,this._numY,map.level);
			map.layer1.appendChild(this.img);
		}
        this.createmapimg2 = function(){                                //куски второго слоя
 			this.img = document.createElement("img");
			this.img.style.position = "absolute";
 			this.img.width = map.edge;
			this.img.height = map.edge;
			this.setParam("top", (this._numY-1)*map.edge);
			this.setParam("left", (this._numX-1)*map.edge);
  			this.setnull();
			map.layer2.appendChild(this.img);
		}
        this.setParam = function(attr, value){             //параметры кусков карты
          if (attr == "left") {
     	        this.left = value;
              this.img.style.left = value+"px";
          }
     	  if (attr == "top") {
     	        this.top = value;
              this.img.style.top = value+"px";
          }
          if (attr == "_top") {
              this._top = value;
          }
          if (attr == "_left") {
              this._left = value;
          }
          if (attr == "_plusx") {
              this._plusx = value;
          }
          if (attr == "_plusy") {
              this._plusy = value;
          }
        }
        this.setSrc = function(a,b,s){


	 		this.img.src = "/getmap.php?a="+a+"&b="+b+"&s="+s;

/*	 		switch (s){
            	case 4:
				if((0<a && a<=2) && (0<b && b<=2))
               		this.img.src = "/getmap.php?a="+a+"&b="+b+"&s="+s;
				break;
            	case 3:
				if((0<a && a<=4) && (0<b && b<=4))
                	this.img.src = "/getmap.php?a="+a+"&b="+b+"&s="+s;
				break;
            	case 2:
				if((0<a && a<=8) && (0<b && b<=8))
                	this.img.src = "/getmap.php?a="+a+"&b="+b+"&s="+s;
				break;
            	case 1:
				if((0<a && a<=16) && (0<b && b<=16))
               		this.img.src = "/getmap.php?a="+a+"&b="+b+"&s="+s;
				break;
			}
			this.img.parentNode.onload = function(){alert("OK")}        */
        }
        this.getParam = function(attr) {      //получение параметров куска карты
            if (attr == "width") {
                return this.width;
            }
            if (attr == "height") {
                return this.height;
            }
            if (attr == "left") {
                return this.left;
            }
            if (attr == "top") {
                return this.top;
            }
            if (attr == "_top") {
                return this._top;
            }
            if (attr == "_left") {
                return this._left;
            }
            if (attr == "_plusx") {
                return this._plusx;
            }
            if (attr == "_plusy") {
                return this._plusy;
            }
            return 0;
        }
   		this.dragging = function(x,y){            //изменения параметров кусков при драге
			var change = 0;

			if (this._numX < map.nx) {
             	this._numX = this._numX + map.numx * Math.ceil( (map.nx - this._numX) / map.numx);
				change = 1;
			}
			if (this._numX > map.nx + map.numx) {
             	this._numX = this._numX - map.numx * Math.floor( (this._numX - map.nx) / map.numx);
				change = 1;
			}

 			if (this._numY < map.ny) {
             	this._numY = this._numY + map.numy * Math.ceil( (map.ny - this._numY) / map.numy);
				change = 1;
			}

			if (this._numY > map.ny + map.numy) {
             	this._numY = this._numY - map.numy * Math.floor( (this._numY - map.ny) / map.numy);
				change = 1;
			}

         //   look("ban3",(-Math.floor((y - map.scrmaph)/map.edge0)))

/*            var x0 = -Math.floor(x/map.edge0)
            var y0 = -Math.floor(y/map.edge0)
            var xx = -Math.floor((x - map.scrmapw)/map.edge0)
            var yy = -Math.floor((y - map.scrmaph)/map.edge0)

            if((x0>this._numX || y0>this._numY) || (xx<this._numX || yy<this._numY)){
				this.img.style.display = "none";
				}
			else{

				this.img.style.display = "block";

                	this.img.onload = function(){      how many?
						map.ok = map.ok+1;
						look("ban3", map.ok)
                        }
				}                               */

	            if (change == 1) {
	 				this.update();
				}


     /*
            if((-Math.floor((x - map.scrmapw)/map.edge0)<this._numX) || (-Math.floor((y - map.scrmaph)/map.edge0)<this._numY))
				this.img.style.display = "none";
			else
            	this.img.style.display = "block";




			if (change == 1) {
 				this.update();
			}
	*/	}
		this.zooming = function(){              //изменения параметров кусков при зуме
 			this.img.width = map.edge;
			this.img.height = map.edge;
			this.setParam("left", (this._numX - 1)*map.edge);
			this.setParam("top", (this._numY - 1)*map.edge);
		}
        this.update = function(){                //общие изменения параметров кусков
 			this.zooming();
 	  		this.setSrc(this._numX, this._numY, map.level);
		}
		this.setnull = function(){               //обнуление нижнего слоя
 		  	this.img.src = "/images/0.gif";
		}
}