﻿function IsoMap(container,w,h,tilemap,tileset,height,top,tw,th) {
	var tile, self = this;
	var i,j,img,a,atop,aleft,inij,endj,x,y;
	var size = w+h-1, middle;
	if (container.split)
		var container = document.getElementById(container);
	middle = (h-1)>>1;
	inij = endj = middle;
	this.map = [];
	for (i = 0; i < size; ++i) {
		if (i < h) {
			x = 0;
			y = i;
		}
		else {
			x = i-h+1;
			y = h-1;
		}
		for (j = Math.floor(inij); j <= Math.floor(endj); ++j) {
			tile = new Image();
			if (!tilemap[y] || !tilemap[y][x])
				tileid = 0;
			else
				tileid = tilemap[y][x];
			tile.src = tileset[tileid];
			tile.border = 0;
			a = document.createElement('a');
			a.className = 'tile';
			a.href = '#';
			a.appendChild(tile);
			a.onclick = function (e) {self.onClick(e);};
			a.onmousedown = function (e) {self.onMouseDown(e);};
			a.onmouseup = function (e) {self.onMouseUp(e);};
			a.onmousemove = function (e) {self.onMouseMove(e);};
			if (tile.width) tw = tile.width;
			if (tile.height) th = tile.height;
			aleft = (tw>>1)+container.offsetLeft+((tw>>1)*(i%2)+tw*j);
			a.style.left = aleft+'px';
			atop = top+container.offsetTop+(((th-height)>>1)*i);
			a.style.top = atop+'px';
			if (!this.map[y]) this.map[y] = [];
			this.map[y][x] = tile.tile = a.tile = {x:x,y:y,z:i,id:tileid,left:aleft,top:atop,bottom:atop+th-height,middle:atop+((th-height)>>1),width:tw,center:aleft+(tw>>1)};
			container.appendChild(a);
			x++; y--;
		}
		if (i < h-1) inij -= 0.5;
		else inij += 0.5;
		if (i < w-1) endj += 0.5;
		else endj -= 0.5;
	};
	this.putAt = function(elm,x,y,offsetX,offsetY) {
		var tile = this.map[y][x];
		if (!offsetX) offsetX = 0;
		if (!offsetY) offsetY = 0;
		elm.style.position = 'absolute';
		elm.style.left = (tile.center+offsetX)+'px';
		elm.style.top = (tile.middle+offsetY)+'px';
		elm.style.zIndex = tile.z+1;
		elm.style.cursor = 'pointer';
		elm.tile = tile;
		container.appendChild(elm);
	}
	this.onClick = function (e) {};
	this.onMouseDown = function (e) {};
	this.onMouseUp = function (e) {};
	this.onMouseMove = function (e) {};
}
