function POV(canvas)
{
	this.imageSize = 256.0;
	this.imageMode = "pov";
	this.mapId = POV.prototype.mapIdCounter;
	this.mapImageTemplate = "[mode]+[image]";
	//this.mapImageTemplate = "[image]&[mode]";
	this.mapHost = new Object();


	this.mapHost[0] = "http://pov1.luuk.co.za";
	this.mapHost[1] = "http://pov2.luuk.co.za";
	this.mapHost[2] = "http://pov3.luuk.co.za";
	this.mapHost[3] = "http://pov4.luuk.co.za";
	this.mapHost[4] = "http://pov5.luuk.co.za";
	this.mapHost[5] = "http://pov6.luuk.co.za";
	this.mapHost[6] = "http://pov7.luuk.co.za";
	this.mapHost[7] = "http://pov8.luuk.co.za";
	this.mapHost[8] = "http://pov9.luuk.co.za";
	this.mapHost[9] = "http://pov10.luuk.co.za";


	/*
	this.mapHost[0] = "http://192.168.0.69:8080";
	this.mapHost[1] = "http://192.168.0.69:8080";
	this.mapHost[2] = "http://192.168.0.69:8080";
	this.mapHost[3] = "http://192.168.0.69:8080";
	this.mapHost[4] = "http://192.168.0.69:8080";
	this.mapHost[5] = "http://192.168.0.69:8080";
	this.mapHost[6] = "http://192.168.0.69:8080";
	this.mapHost[7] = "http://192.168.0.69:8080";
	this.mapHost[8] = "http://192.168.0.69:8080";
	this.mapHost[9] = "http://192.168.0.69:8080";
	*/

	this.mapMode = "hybrid";
	//this.mapCoordX = 30.07781982421875;
	//this.mapCoordY = -29.34148119149953;
	//this.mapZ = 10;
	this.mapCoordX = 30.07781982421875;
	this.mapCoordY = -29.34148119149953;
	this.mapZ = 9;

	this.mapChanged = 0;
	this.mapOldPixX = 0;
	this.mapOldPixY = 0;
	this.mapOldPixWidth = 0;
	this.mapOldPixHeight = 0;
	this.canvasMain = canvas;
	this.imageActive = new Object();
	this.pointActive = new Object();
	this.polyActive = new Object();
	this.mapObjects[this.mapId] = this;

	POV.prototype.mapIdCounter++;

	this.eventMouseMove = new BE();
	this.eventMouseOver = new BE();
	this.eventMouseOut = new BE();
	this.eventMouseClick = new BE();
	this.eventMouseDoubleClick = new BE();
	this.eventMouseDown = new BE();
	this.eventMouseUp = new BE();
	this.eventMapViewChange = new BE();

	this.mapMouseMove = 1;

	this.mapViewCheckInterval = 100;
	this.imageHostCounter = 0;

	this.points = new Object();
	this.polys = new Object();

	if(navigator.appName=="Microsoft Internet Explorer") this.useVML = 1;
	else this.useVML = 0;
}

var p = POV.prototype;
p.imageCodes = new Object();
p.imageCodes.pov = new Object();
p.imageCodes.pov[0] = '0';
p.imageCodes.pov[1] = '1';
p.imageCodes.pov[2] = '2';
p.imageCodes.pov[3] = '3';

p.imageCodes.google = new Object();
p.imageCodes.google[0] = 'q';
p.imageCodes.google[1] = 'r';
p.imageCodes.google[2] = 't';
p.imageCodes.google[3] = 's';

p.mapObjects = new Object();
p.imageCache = new Object();

p.mapIdCounter = 1;

p.getId = function(id)
{
	return document.getElementById(this.mapId+"_"+id);
}

p.mapStart = function()
{
	var events = "";
	events += " onmousemove='POV.prototype.mapObjects["+this.mapId+"].eventMouseMove.trigger(event)'";
	events += " onmouseover='POV.prototype.mapObjects["+this.mapId+"].eventMouseOver.trigger(event)'";
	events += " onmouseout='POV.prototype.mapObjects["+this.mapId+"].eventMouseOut.trigger(event)'";
	events += " onclick='POV.prototype.mapObjects["+this.mapId+"].eventMouseClick.trigger(event)'";
	events += " ondblclick='POV.prototype.mapObjects["+this.mapId+"].eventMouseDoubleClick.trigger(event)'";
	events += " onmousedown='POV.prototype.mapObjects["+this.mapId+"].eventMouseDown.trigger(event)'";
	events += " onmouseup='POV.prototype.mapObjects["+this.mapId+"].eventMouseUp.trigger(event)'";

	var html = "";
	html += "<div style='width:100%;height:100%'>";
		html += "<div style='width:100%;height:100%;position:relative;overflow:hidden' id='"+this.mapId+"_sizecanvas' "+events+">";
			html += "<div style='position:absolute;left:0px;top:0px;width:100%;height:100%;z-index:30;background-color:transparent;font-size:3000px;cursor:pointer' id='"+this.mapId+"_eventcanvas' unselectable='on' onmousedown='return false'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>";
			html += "<div style='position:absolute;width:1px;height:1px;overflow:visible;left:0px;top:0px;z-index:25' id='"+this.mapId+"_overlaycanvas'></div>";
			html += "<div style='position:absolute;left:0px;top:0px;width:1px;height:1px;overflow:visible;z-index:20' id='"+this.mapId+"_containercanvas'>";
				html += "<div style='position:absolute;left:0px;top:0px;width:1px;height:1px;overflow:visible;z-index:10' id='"+this.mapId+"_imagecanvas'></div>";
				html += "<div style='position:absolute;left:0px;top:0px;width:1px;height:1px;overflow:visible;z-index:20' id='"+this.mapId+"_drawcanvas'></div>";
			html += "</div>";
		html += "</div>";
	html += "</div>";
	this.canvasMain.innerHTML = html;

	this.canvasSize = this.getId("sizecanvas");
	this.canvasContainer = this.getId("containercanvas");
	this.canvasImage = this.getId("imagecanvas");
	this.canvasDraw = this.getId("drawcanvas");
	this.canvasEvent = this.getId("eventcanvas");
	this.canvasOverlay = this.getId("overlaycanvas");

	var c = this.convertCoordsToXY(this.mapCoordX,this.mapCoordY,this.mapZ);
	this.canvasContainer.style.left = (c.x*-1)+"px";
	this.canvasContainer.style.top = (c.y*-1)+"px";

	this.canvasConstruction = document.createElement("SPAN");
	document.body.appendChild(this.canvasConstruction);
	this.canvasConstruction.style.position = "absolute";
	this.canvasConstruction.style.left = "-10000px";
	this.canvasConstruction.style.top = "-10000px";

	eval("window.setInterval(function(){ POV.prototype.mapObjects["+this.mapId+"].mapCheckViewChange()},"+this.mapViewCheckInterval+");");

	this.eventMapViewChange.add(this,"mapRender");
	this.eventMapViewChange.add(this,"pointRender");
	this.eventMapViewChange.add(this,"polyRender");
	this.eventMouseUp.add(this,"mapAutoDragMouseUp");
	//this.eventMouseClick.add(this,"mapAutoDragMouseUp");

	this.mapAutoDragHandle = this.eventMouseDown.add(this,"mapAutoDragMouseDown");
}

p.mapGetCentrePoint = function()
{
	var x = Math.round(this.mapPixX+(this.mapPixWidth/2));
	var y = Math.round(this.mapPixY+(this.mapPixHeight/2));
	return this.convertXYtoCoords(x,y);
}

p.mapAutoMouseDragEnable = function()
{
	if(!this.mapAutoDragHandle) this.mapAutoDragHandle = this.eventMouseDown.add(this,"mapAutoDragMouseDown");
	this.canvasEvent.style.cursor = "pointer";

}

p.mapAutoMouseDragDisable = function()
{
	if(this.mapAutoDragHandle)
	{
		this.eventMouseDown.remove(this.mapAutoDragHandle);
		this.mapAutoDragHandle = 0;
	}
}

p.mapAutoDragMouseMove = function(event)
{
	this.canvasContainer.style.left = (event.clientX - this.mouseDX) + "px";
	this.canvasContainer.style.top = (event.clientY - this.mouseDY) + "px";
}

p.mapAutoDragMouseDown = function(event)
{
	if(this.mapMouseMove==1)
	{
		this.mouseDX = event.clientX - this.canvasContainer.offsetLeft;
		this.mouseDY = event.clientY - this.canvasContainer.offsetTop;
		if(this.mouseMoveHandle) this.eventMouseMove.remove(this.mouseMoveHandle);
		this.mouseMoveHandle = this.eventMouseMove.add(this,"mapAutoDragMouseMove");
	}
}

p.mapAutoDragMouseUp = function()
{
	if(this.mouseMoveHandle)
	{
		this.eventMouseMove.remove(this.mouseMoveHandle);
		this.mouseMoveHandle = 0;
	}
}

p.mapSetCanvasVars = function()
{
	this.mapPixWidth = this.canvasSize.offsetWidth;
	this.mapPixHeight = this.canvasSize.offsetHeight;
	this.mapPixX = this.canvasContainer.offsetLeft*-1;
	this.mapPixY = this.canvasContainer.offsetTop*-1;
}

p.mapCheckViewChange = function()
{
	this.mapSetCanvasVars();
	if(this.mapPixWidth!=this.mapOldPixWidth||this.mapPixHeight!=this.mapOldPixHeight||this.mapPixX!=this.mapOldPixX||this.mapPixY!=this.mapOldPixY)
	{
		this.mapPixX2 = this.mapPixX+this.mapPixWidth;
		this.mapPixY2 = this.mapPixY+this.mapPixHeight;
		try{this.eventMapViewChange.trigger();}catch(e){}
		this.mapOldPixHeight = this.mapPixHeight;
		this.mapOldPixWidth = this.mapPixWidth;
		this.mapOldPixX = this.mapPixX;
		this.mapOldPixY = this.mapPixY;
	}
}

p.mapGetHost = function()
{
	host = this.mapHost[this.imageHostCounter];
	this.imageHostCounter++;
	if(!this.mapHost[this.imageHostCounter]) this.imageHostCounter=0;
	return host;
}

p.mapMoveCoords = function(x,y)
{
	var c = this.convertCoordsToXY(x,y);
	this.mapMoveXY(c.x,c.y);
}

p.mapMoveXY = function(x,y)
{
	this.canvasContainer.style.left = Math.round((x*-1)+this.mapPixWidth/2)+"px";
	this.canvasContainer.style.top = Math.round((y*-1)+this.mapPixHeight/2)+"px";
}

p.mapMoveApprox = function(x1,y1,x2,y2)
{
	var z = 1,c,c1;
	c = this.convertCoordsToXY(x1,y1,z);
	c1 = this.convertCoordsToXY(x2,y2,z);

	while(Math.abs(c.x-c1.x)<this.mapPixWidth&&Math.abs(c.y-c1.y)<this.mapPixHeight)
	{
		z++;
		c = this.convertCoordsToXY(x1,y1,z);
		c1 = this.convertCoordsToXY(x2,y2,z);
	}

	z--;
	c = this.convertCoordsToXY(x1,y1,z);
	c1 = this.convertCoordsToXY(x2,y2,z);

	this.mapZ = z;
	var x = Math.round((c.x+c1.x)/2);
	var y = Math.round((c.y+c1.y)/2);


	this.mapMoveXY(x,y);
}


p.imageCreateCache = function(url,x,y)
{
	this.canvasConstruction.innerHTML = "<img src='"+this.mapGetHost()+"/"+url+"' style='position:absolute;left:"+x+"px;top:"+y+"px' onload='this.loadComplete=1' onerror='this.loadComplete=0; this.src = POV.prototype.mapObjects["+this.mapId+"].mapGetHost()+\"/\"+this.cacheKey'>";
	this.imageCache[url] = this.canvasConstruction.childNodes[0];

	this.imageHostCounter++;
	if(!this.mapHost[this.imageHostCounter]) this.imageHostCounter=0;
	this.canvasConstruction.childNodes[0].cacheKey = url;

	return this.canvasConstruction.childNodes[0];
}

p.mapRender = function()
{
	var horizImageCount = Math.ceil(this.mapPixWidth/this.imageSize)+1;
	var vertImageCount = Math.ceil(this.mapPixHeight/this.imageSize)+1;
	var url,Ycount = 0,Xcount = 0, image;
	var origXpos = this.mapPixX - (this.mapPixX % this.imageSize);
	var yPos = this.mapPixY - (this.mapPixY % this.imageSize);
	for(var i in this.imageActive) this.imageActive[i].remove=1;
	for(Ycount=0;Ycount<vertImageCount;Ycount++)
	{
		var xPos = origXpos;
		for(Xcount=0;Xcount<horizImageCount;Xcount++)
		{
			url = this.mapImageTemplate.replace("[image]",this.convertXYtoURL(xPos,yPos,this.mapZ)).replace("[mode]",this.mapMode);
			image = this.imageCache[url];
			if(!image)image = this.imageCreateCache(url,xPos,yPos);
			if(!this.imageActive[url])
			{
				this.canvasImage.appendChild(image);
				this.imageActive[url] = image;
			}
			image.remove = 0;
			xPos += this.imageSize;
		}
		yPos += this.imageSize;
	}

	var i,i1=0,keys = new Object();
	for(i in this.imageActive) {keys[i1] = i; i1++};
	for(i in keys)
	{
		image = this.imageActive[keys[i]];
		if(image.remove==1)
		{
			try { this.canvasImage.removeChild(image); } catch(e){}
			this.imageActive[keys[i]] = null;
			delete(this.imageActive[keys[i]]);
			if(!image.loadComplete)
			{
				image.src = '#';
				image.onerror = null;
				image.onload = null;
				this.imageCache[keys[i]] = null;
				delete(this.imageCache[keys[i]]);
			}
			image = null;
		}
	}
}

p.convertCoordsToXY = function(x,y,z)
{
   if(!z) z = this.mapZ;
   x = parseFloat(x);
   y = parseFloat(y);
   var r = new Object();
   r.x = x + 180;
   y *= Math.PI/180;
   r.y = Math.log(Math.tan(y)+(1.0/Math.cos(y)));
   r.y *= 180/Math.PI;
   r.y = (r.y*-1)+180;
   r.x = parseInt(((Math.pow(2,z)*this.imageSize) / 360.0) * r.x);
   r.y = parseInt(((Math.pow(2,z)*this.imageSize) / 360.0) * r.y);
   return r;
}

p.convertMouseXYtoXY = function(x,y)
{
	var r = new Object();
	r.x = x+this.mapPixX;
	r.y = y+this.mapPixY;
	return r;
}

p.convertXYtoCoords = function(x,y,z)
{
   if(!z) z = this.mapZ;
   x = parseFloat(x);
   y = parseFloat(y);
   var r = new Object();
   r.x = (360 / (Math.pow(2,z)*this.imageSize)) * x;
   r.y = (360 / (Math.pow(2,z)*this.imageSize)) * y;

   r.x -= 180;
   r.y -= 180;
   r.y *= -1;
   r.y *= Math.PI/180;
   r.y = Math.atan((Math.exp(r.y)-Math.exp(-r.y))/2);
   r.y *= 180/Math.PI;
   return r;
}

p.convertCoordsToURL = function(x,y,z)
{
   if(!z) z = this.mapZ;
   x = parseFloat(x);
   y = parseFloat(y);
   var r = this.convertCoordsToXY(x,y,z);
   r = this.convertXYtoURL(r.x,r.y,z);
   return r;
}

p.convertXYtoURL = function(x,y,z)
{
   if(!z) z = this.mapZ;
   x = parseFloat(x);
   y = parseFloat(y);
   var b = Math.pow(2,z)*this.imageSize;
   var u = "";
   var xmin = 0.0;
   var xmax = b;
   var ymin = 0;
   var ymax = b;
   var xmoy = 0;
   var ymoy = 0;

   var i;
   for(i=0;i<z;i++)
   {
      xmoy=(xmax+xmin)/2;
      ymoy=(ymax+ymin)/2;
      if(y<ymoy)
      {
         ymax=ymoy;
         if(x<xmoy){u+=this.imageCodes[this.imageMode][0];xmax=xmoy}
         else{u+=this.imageCodes[this.imageMode][1];xmin=xmoy}
      }
      else
      {
         ymin=ymoy;
         if(x<xmoy){u+=this.imageCodes[this.imageMode][2];xmax=xmoy}
         else{u+=this.imageCodes[this.imageMode][3];xmin=xmoy}
      }
   }
   return u;
}

p.eventsOpen = function()
{
	this.canvasContainer.style.zIndex = 40;
	this.mapAutoMouseDragDisable();
}

p.eventsClose = function()
{
	this.mapAutoMouseDragEnable();
	this.canvasEvent.style.filter = "Alpha(opacity=0)";
	this.canvasContainer.style.zIndex = 20;
}

p.eventGetOffset = function(event)
{
	var r = new Object();
	if(event.offsetX&&event.offsetY)
	{
		r.x = event.offsetX;
		r.y = event.offsetY;
	}
	else
	{
		r.x = event.layerX;
		r.y = event.layerY;
	}
	return r;
}


p.setProperties = function(zoom,startX,startY)
{
	this.mapZ = zoom;
	this.mapCoordX = startX;
	this.mapCoordY = startY;
}



p.zoomIn = function()
{
	this.mapZ++;
	this.canvasContainer.style.left = (this.canvasContainer.offsetLeft*2) - (this.canvasSize.offsetWidth/2);
	this.canvasContainer.style.top = (this.canvasContainer.offsetTop*2) - (this.canvasSize.offsetHeight/2);
}

p.zoomOut = function()
{
	this.mapZ--;
	this.canvasContainer.style.left = (this.canvasContainer.offsetLeft/2) + (this.canvasSize.offsetWidth/4);
	this.canvasContainer.style.top = (this.canvasContainer.offsetTop/2) + (this.canvasSize.offsetHeight/4);
}

p.zoomMouseDown = function(event)
{
	var c = this.eventGetOffset(event);
	this.zoomMouseX = c.x;
	this.zoomMouseY = c.y;

	if(!this.zoomMouseMoveHandle)	this.zoomMouseMoveHandle = this.eventMouseMove.add(this,'zoomMouseMove');
}

p.zoomMouseUp = function(event)
{
	if(this.zoomMouseRecAdded)
	{
		var eo = this.eventGetOffset(event);
		var eo = this.convertMouseXYtoXY(eo.x,eo.y);
		this.zoomMouseRecAdded = 0;
		try { this.canvasZoomRec.parentNode.removeChild(this.canvasZoomRec); } catch(e){}
		this.canvasZoomRec = null;
		var c1 = this.convertXYtoCoords(eo.x,eo.y,this.mapZ);
		var eo = this.convertMouseXYtoXY(this.zoomMouseX,this.zoomMouseY);
		var c = this.convertXYtoCoords(eo.x,eo.y,this.mapZ);
		this.mapMoveApprox(c.x,c.y,c1.x,c1.y);
	}
	else
	{
		var c = this.convertMouseXYtoXY(this.zoomMouseX,this.zoomMouseY);
		this.mapMoveXY(c.x,c.y);
		this.zoomIn();
	}

	if(this.zoomMouseMoveHandle)
	{
		this.eventMouseMove.remove(this.zoomMouseMoveHandle);
		this.zoomMouseMoveHandle = 0;
	}
}

p.zoomMouseMove = function(event)
{
	var c = this.eventGetOffset(event);

	if(!this.zoomMouseRecAdded)
	{
		this.canvasConstruction.innerHTML = "<div style='filter:Alpha(opacity=50);opacity:0.5;position:absolute;left:"+this.zoomMouseX+"px;top:"+this.zoomMouseY+"px;overflow:hidden;width:1px;height:1px;background-color:#F88585;border:2px solid #F44A4A'>&nbsp;</div>";
		this.canvasZoomRec = this.canvasConstruction.childNodes[0];
		this.canvasOverlay.appendChild(this.canvasZoomRec);
		this.zoomMouseRecAdded = 1;
	}

	var x = this.zoomMouseX;
	var y = this.zoomMouseY;
	var x2;
	var y2;

	if(x>c.x)
	{
		x2 = x;
		x = c.x;
	}
	else
		x2 = c.x;

	if(y>c.y)
	{
		y2 = y;
		y = c.y;
	}
	else
		y2 = c.y;

	var w = x2-x;
	var h = y2-y;

	if(w<=0) w = 1;
	if(h<=0) h = 1;

	this.canvasZoomRec.style.left = x+"px";
	this.canvasZoomRec.style.top = y+"px";
	this.canvasZoomRec.style.width = w+"px";
	this.canvasZoomRec.style.height = h+"px";

	this.canvasZoomRec
}

p.zoomModeEnable = function()
{
	if(!this.zoomArea)
	{
		this.zoomArea = 1;
		this.eventsClose();
		this.mapAutoMouseDragDisable();
		this.canvasEvent.style.cursor = "default";
		if(!this.zoomMouseDownHandle) this.zoomMouseDownHandle = this.eventMouseDown.add(this,'zoomMouseDown');
		if(!this.zoomMouseUpHandle) this.zoomMouseUpHandle = this.eventMouseUp.add(this,'zoomMouseUp');
	}
}

p.zoomModeDisable = function()
{
	if(this.zoomArea==1)
	{
		this.zoomArea = 0;
		this.mapAutoMouseDragEnable();
		if(this.zoomMouseDownHandle)
		{
			this.eventMouseDown.remove(this.zoomMouseDownHandle);
			this.zoomMouseDownHandle = 0;
		}
		if(this.zoomMouseUpHandle)
		{
			this.eventMouseUp.remove(this.zoomMouseUpHandle);
			this.zoomMouseUpHandle = 0;
		}
	}
}

p.mapChangeMode = function(mode)
{
	this.mapMode = mode;
	this.mapRender();
}

p.pointNew = function(o)
{
	var point = new Object();
	point.image = new Object();
	point.id = o.id;
	this.points[o.id] = point;
	point.active = 0;
	point.constructed = 0;
}

p.pointConstructCanvas = function(point)
{
	var mouseover = "";

	var style = "";
	if(point.hovercaption)
	{
		mouseover = " onmouseover='this.parentNode.childNodes[1].style.visibility=\"visible\"' onmouseout='this.parentNode.childNodes[1].style.visibility=\"hidden\"'";
		style = "visibility:hidden;";
	}
	this.canvasConstruction.innerHTML = "<div style='position:absolute;left:0px;top:0px;overflow:visible;width:1px;height:1px;cursor:pointer'><img style='position:absolute;left:-10px;top:-10px' src='' onload='var obj=POV.prototype.mapObjects["+this.mapId+"]; obj.pointPack(obj.points[\""+point.id+"\"])' "+mouseover+"><div style='"+style+"position:absolute;left:-30px;top:-30px;background-color:#FFFFFF;border:1px solid #000000;white-space:nowrap;padding-left:3px;padding-right:3px;font-size:12px;font-family:Verdana' nowrap></div></div>";
	point.canvas = this.canvasConstruction.childNodes[0];
	point.canvasImage = point.canvas.childNodes[0];
	point.canvasCaption = point.canvas.childNodes[1];
	if(!point.caption) point.canvasCaption.style.visibility = 'hidden';
	else point.canvasCaption.innerHTML = point.caption;
	point.constructed = 1;

	if(point.mouseover) eval("point.canvas.onmouseover = function(){"+point.mouseover+"}");
	if(point.mouseout) eval("point.canvas.onmouseout = function(){"+point.mouseout+"}");
	if(point.mousedown) eval("point.canvas.onmousedown = function(){"+point.mousedown+"}");
	if(point.mouseup) eval("point.canvas.onmouseup = function(){"+point.mouseup+"}");
	if(point.mouseclick) eval("point.canvas.onclick = function(){"+point.mouseclick+"}");
	if(point.mousedblclick) eval("point.canvas.ondblclick = function(){"+point.mousedblclick+"}");

	this.pointPack(point);
}

p.pointMove = function(p,o)
{
	var point = this.points[o.id];
	point.x = o.x;
	point.y = o.y;
	point.converted = new Object();
}

p.pointPack = function(point)
{
	var imageHeight = point.canvasImage.offsetHeight;
	var imageWidth = point.canvasImage.offsetWidth;
	var captionHeight = point.canvasCaption.offsetHeight;
	var captionWidth = point.canvasCaption.offsetWidth;

	point.canvasImage.style.left = "-"+Math.round(imageWidth/2)+"px";
	point.canvasImage.style.top = "-"+Math.round(imageHeight/2)+"px";
	point.canvasCaption.style.left = "-"+Math.round(captionWidth/2)+"px";
	point.canvasCaption.style.top = "-"+(Math.round(imageHeight/2)+5+captionHeight)+"px";
}

p.pointRemove = function(id)
{
	var point = this.points[id];
	if(point)
	{
		point.canvasImage = null;
		point.canvasCaption = null;
		try{point.canvas.parentNode.removeChild(point.canvas);}
		catch(e){}
		point.canvas = null;
		this.points[id] = null;
		delete(this.points[id]);
		if(this.pointActive[id])
		{
			this.pointActive[id] = null;
			delete(this.pointActive[id]);
		}
		point = null;
	}
}

p.pointRemoveAll = function()
{
	var keys = new Object();
	var i1 = 0;
	for(var i in this.points){keys[i1]=i;i1++}

	for(var i in keys) this.pointRemove(keys[i]);
}

//
// id;
// x;
// y;
// z;
// image[0];
// image[1];
// image[2];
// image[3];
// image[4];
// image[5];
// image[6];
// image[..];  // specify an image for every zoom level.  If the zoom level is not found it will go down the zoom levels until it finds an image.
// caption;
//

p.pointSet = function(o)
{
	if(!this.points[o.id]) this.pointNew(o);
	var point = this.points[o.id];

	if(o.x||o.y) this.pointMove(point,o);
	if(o.z) point.z = o.z;
	if(!point.z) point.z = 0;
	if(o.image) point.image = o.image;
	if(o.caption) point.caption = o.caption;
	if(o.hovercaption) point.hovercaption = 1;
	if(o.mouseclick) point.mouseclick = o.mouseclick;
	if(o.mousedblclick) point.mousedblclick = o.mousedblclick;
	if(o.mouseover) point.mouseover = o.mouseover;
	if(o.mouseout) point.mouseout = o.mouseout;
	if(o.mousedown) point.mousedown = o.mousedown;
	if(o.mouseup) point.mouseup = o.mouseup;

	var si=point.image[0];
	for(var i=1;i<18;i++){if(point.image[i])si=point.image[i];else point.image[i]=si}

	if(o.caption) point.caption = o.caption;

	if(!this.pointRenderTimer)
		eval("this.pointRenderTimer = window.setTimeout(function(){POV.prototype.mapObjects["+this.mapId+"].pointRender()},10);");
}

p.pointRender = function()
{
	var point = null;
	var x,y,imagePath,i1,z;

	for(var i in this.pointActive) this.pointActive[i].remove = 1;
	for(var i in this.points)
	{
		point = this.points[i];
		if(!point.converted[this.mapZ]) point.converted[this.mapZ] = this.convertCoordsToXY(point.x,point.y,this.mapZ);

		x = point.converted[this.mapZ].x;
		y = point.converted[this.mapZ].y;

		if(x<this.mapPixX2&&x>this.mapPixX&&y<this.mapPixY2&&y>this.mapPixY)
		{
			point.remove = 0;
			if(point.constructed==0) this.pointConstructCanvas(point);
			if(point.canvasImage.imageSrc!=point.image[this.mapZ])
			{
				point.canvasImage.imageSrc = point.image[this.mapZ];
				point.canvasImage.src = point.image[this.mapZ];
			}

			if(point.active==0)
			{
				this.canvasDraw.appendChild(point.canvas);
				point.active = 1;
				this.pointActive[i] = point;
			}
			if(point.canvas.offsetLeft!=x||point.canvas.offsetTop!=y)
			{
				point.canvas.style.left = x+"px";
				point.canvas.style.top = y+"px";
				z = point.z;
				if(!z) z = 0;
				point.canvas.style.zIndex = z;
			}
		}
	}

	var keys = new Object();
	var i1 = 0;
	for(var i in this.pointActive){keys[i1]=i; i1++}
	for(var i in keys)
	{
		var point = this.pointActive[keys[i]];
		if(point.remove==1)
		{
			try { this.canvasDraw.removeChild(point.canvas); } catch(e) {}
			this.pointActive[keys[i]] = null;
			delete(this.pointActive[keys[i]]);
			point.active = 0;
		}
	}

	this.pointRenderTimer = 0;
}


p.polyNew = function(o)
{
	var p = new Object();
	p.id = o.id;
	this.polys[o.id] = p;
	p.canvas = new Object();
	p.active = 0;
}

//id
//coords
//		[0]
//			x
//			y
//		[1]
//			x
//			y
//z
//opacity = 0-100
//fill // if ("" or not specified) then not filled; else #FFFFFF, #FF0000, rgb(255,255,255) etc etc.
//color // if ("" or not specified) then no border; else #FFFFFF, #FF0000, rgb(255,255,255) etc etc.
//stroke //stroke width if("" or not specified) then stroke width = 1, else 1,2,3,4,5 etc. for width
//level   // at which zoom levels this object should be shown, if not specified it will show on all levels

//mouseclick
//mousedblclick
//mouseover
//mouseout
//mouseup
//mousedown

//		[1] = 1  //should show at zoom level 1
//		[2] = 1  //should show at zoom level 2
//		[3] = 1  //should show at zoom level 3
//		[4] = 0  //should not show at zoom level 4
//		[5] = 0  //should not show at zoom level 5

p.polySet = function(o)
{
	if(!this.polyHandle)
	{
		eval("this.polyHandle = window.setInterval(function(){ POV.prototype.mapObjects["+this.mapId+"].polyRender()},1);");
	}


	if(!this.polys[o.id]) this.polyNew(o);
	var p = this.polys[o.id];
	if(o.z) p.z = o.z;
	if(!p.z) p.z = 0;

	if(o.opacity) p.opacity = o.opacity;
	if(!p.opacity&&p.opacity!=0) p.opacity = 100;

	if(o.fill) p.fill = o.fill;
	if(!p.fill) p.fill = "";

	if(o.color) p.color = o.color;
	if(!p.color) p.color = "";

	if(o.stroke) p.stroke = o.stroke;
	if(!p.stroke) p.stroke = 1;

	if(o.level) p.level = o.level;


	if(o.mouseclick) p.mouseclick = o.mouseclick;
	if(o.mousedblclick) p.mousedblclick = o.mousedblclick;
	if(o.mouseover) p.mouseover = o.mouseover;
	if(o.mouseout) p.mouseout = o.mouseout;
	if(o.mousedown) p.mousedown = o.mousedown;
	if(o.mouseup) p.mouseup = o.mouseup;

	if(o.coords)
	{
		p.coords = o.coords;
		p.minX = 1000;
		p.minY = 1000;
		p.maxX = -1000;
		p.maxY = -1000;
		var c;
		for(var i in p.coords)
		{
			c = p.coords[i];
			c.x = parseFloat(c.x);
			c.y = parseFloat(c.y);
			if(p.minX>c.x) p.minX=c.x;
			if(p.minY>c.y) p.minY=c.y;
			if(p.maxX<c.x) p.maxX=c.x;
			if(p.maxY<c.y) p.maxY=c.y;
		}
		if(p.active) try{p.canvas[p.active].parentNode.removeChild(p.canvas[p.active]);}catch(e){}
		p.canvas = new Object();
		p.active = 0;
	}
}

p.polyRemove = function(i)
{
	if(this.polyActive[i])
	{
		var c = this.polyActive[i].canvas[this.polyActive[i].active];
		try{c.parentNode.removeChild(c);}catch(e){}
		this.polyActive[i] = null;
		delete(this.polyActive[i]);
	}

	if(this.polys[i])
	{
		for(i1 in this.polys[i].canvas) this.polys[i].canvas[i1] = null;
		this.polys[i] = null;
		delete(this.polys[i]);
	}
}

p.polyRemoveAll = function()
{
	for(var i in this.polyActive)
	{
		var c = this.polyActive[i].canvas[this.polyActive[i].active];
		try{c.parentNode.removeChild(c);}catch(e){}
		this.polyActive[i] = null;
	}
	this.polyActive = new Object();

	var i1;
	for(var i in this.polys)
	{
		for(i1 in this.polys[i].canvas) this.polys[i].canvas[i1] = null;
		this.polys[i] = null;
	}
	this.polys = new Object();
}

p.polyBuildCoordsString = function(c,tx,ty)
{
	var r="",p,a,b=0;
	for(var i in c)
	{
		a = c[i];
		p = this.convertCoordsToXY(a.x,a.y);
		p.x = p.x + tx;
		p.y = p.y + ty;
		if(b==0) r+=p.x+","+p.y;
		else r+= " "+p.x+","+p.y;
		b++;
	}
	return r;
}

p.polyCreateCanvas = function(p)
{
	var pos = this.convertCoordsToXY(p.minX,p.maxY);
	pos.x -= 10;
	pos.y -= 10;


	var c = this.polyBuildCoordsString(p.coords,0-pos.x,0-pos.y);
	this.canvasConstruction.innerHTML = "<div style='position:absolute;left:"+pos.x+"px;top:"+pos.y+"px;z-index:"+p.z+";width:1px;height:1px;overflow:visible'></div>";
	p.canvas[this.mapZ] = this.canvasConstruction.childNodes[0];
	if(this.useVML==1)
	{
		var fill = "filled='false'";
		if(p.fill) fill = "fillcolor='"+p.fill+"'";

		var stroke = "stroked='false'";
		if(p.color) stroke = "strokecolor='"+p.color+"'";
		if(p.stroke) stroke += " strokeweight='"+p.stroke+"px'";
		var html = "<v:polyline points='"+c+"' "+fill+" "+stroke;
		if(p.mouseclick) html += " onclick='"+p.mouseclick+"'";
		if(p.mousedblclick) html += " ondblclick='"+p.mousedblclick+"'";
		if(p.mouseover) html += " onmouseover='"+p.mouseover+"'";
		if(p.mouseout) html += " onmouseout='"+p.mouseout+"'";
		if(p.mouseup) html += " onmouseup='"+p.mouseup+"'";
		if(p.mousedown) html += " onmousedown='"+p.mousedown+"'";
		html += ">";

		p.canvas[this.mapZ].innerHTML = html;
	}
	else
	{
		var fill = p.fill;
		var stroke = p.color;
		var width = p.stroke;

		var svg = document.createElementNS("http://www.w3.org/2000/svg","polygon");
		svg.setAttributeNS(null,'points',c);
		if(fill) svg.setAttributeNS(null,'fill',fill);
		else  svg.setAttributeNS(null,'fill','none');
		svg.setAttributeNS(null,'stroke',stroke);
		svg.setAttributeNS(null,'stroke-width',width);
		svg.setAttributeNS(null,'fill-opacity',p.opacity/100);
		svg.setAttributeNS(null,'stroke-opacity',p.opacity/100);

		if(p.mouseclick) svg.setAttributeNS(null,"onclick",p.mouseclick);
		if(p.mousedblclick) svg.setAttributeNS(null,"ondblclick",p.mousedblclick);
		if(p.mouseover) svg.setAttributeNS(null,"onmouseover",p.mouseover);
		if(p.mouseout) svg.setAttributeNS(null,"onmouseout",p.mouseout);
		if(p.mouseup) svg.setAttributeNS(null,"onmouseup",p.mouseup);
		if(p.mousedown) svg.setAttributeNS(null,"onmousedown",p.mousedown);

		var pos2 = this.convertCoordsToXY(p.maxX,p.minY);
		pos2.x += 10;
		pos2.y += 10;

		var csvg = document.createElementNS("http://www.w3.org/2000/svg","svg");
		csvg.setAttributeNS(null,"width",Math.abs(pos.x-pos2.x));
		csvg.setAttributeNS(null,"height",Math.abs(pos.y-pos2.y));
		p.canvas[this.mapZ].appendChild(csvg);
		csvg.appendChild(svg);
	}
	p = null;
}

p.polyRender = function()
{
	if(this.polyHandle)
	{
		window.clearInterval(this.polyHandle);
		this.polyHandle = 0;
	}
	var i=0;
	for(i in this.polyActive) this.polyActive[i].remove = 1;
	var p;
	var min = this.convertXYtoCoords(this.mapPixX,this.mapPixY);
	var max = this.convertXYtoCoords(this.mapPixX2,this.mapPixY2);

	for(i in this.polys)
	{
		p = this.polys[i];
		if(p.minX<max.x&&p.maxX>min.x&&p.maxY>max.y&&p.minY<min.y&&(!p.level||p.level[this.mapZ]==1))
		{
			if(!p.canvas[this.mapZ]) this.polyCreateCanvas(p);
			if(p.active!=this.mapZ)
			{
				if(p.active>0) try { p.canvas[p.active].parentNode.removeChild(p.canvas[p.active]); } catch(e){}
				this.canvasDraw.appendChild(p.canvas[this.mapZ]);
				p.canvas[this.mapZ].childNodes[0].style.filter="Alpha(opacity="+p.opacity+")";
				p.active = this.mapZ;
				this.polyActive[p.id] = p;
			}
			p.remove = 0;
		}
	}

	var i1 = 0;
	var removePolys = new Object();
	for(i in this.polyActive)
	{
		if(this.polyActive[i].remove)
		{
			removePolys[i1] = i;
			i1++;
		}
	}

	for(i in removePolys)
	{
		var c = this.polyActive[removePolys[i]];
		try{c.canvas[c.active].parentNode.removeChild(c.canvas[c.active]);}catch(e){}
		c.active = 0;
		delete(this.polyActive[removePolys[i]]);
	}
}

////////////////////////
///////////////////////
////////////////////////
////////////////////////

var EVENT_SHARED = new Object();
EVENT_SHARED.handlecounter = 0;
function BE()
{
	this.data = new Array();
	this.shared = EVENT_SHARED;
};

BE.prototype.remove = function(h)
{
	var d=this.data;
	var s=d.length;
	var nd=new Array();
	var i1=0;
	for(var i=0;i<s;i++)
		if(d[i].handle!=h)
		{
			nd[i1]=d[i];
			i1++;
		}
	this.data=nd;
	d=null;nd=null;
};

BE.prototype.add=function(o,f)
{
	var i=0;
	while(this.data[i])i++;
	this.data[i]=new Object();
	this.data[i].obj=o;
	this.data[i].func=f;
	this.data[i].handle=this.newhandle();
	o=null;
	return this.data[i].handle;
};

BE.prototype.trigger=function(p0,p1,p2,p3,p4)
{
	var s,d,i;
	d=this.data;
	if(d)
	{
		s=d.length;
		for(i=0;i<s;i++)if(d[i].obj[d[i].func])d[i].obj[d[i].func](p0,p1,p2,p3,p4);
		d=null;
	}
};

BE.prototype.newhandle=function()
{
	this.shared.handlecounter++;
	return this.shared.handlecounter;
};

BE.prototype.kill=function()
{
	for(var i in this.data)
	{
		this.data[i].obj=null;
		this.data[i].func=null;
	}
	this.data=null;;
};
