
function SparseTileLayerOverlay()
{
	this.Q={alpha:[],level:[]};
}

SparseTileLayerOverlay.prototype=new google.maps.OverlayView;

SparseTileLayerOverlay.prototype.offset=268435456;

SparseTileLayerOverlay.prototype.radius=268435456/Math.PI;

SparseTileLayerOverlay.prototype.degree=180/Math.PI;

SparseTileLayerOverlay.prototype.LToX=function(x)
{
	return Math.round(this.offset+this.radius*x/this.degree);
}

SparseTileLayerOverlay.prototype.LToY=function(y)
{
	return Math.round(this.offset-this.radius*Math.log((1+Math.sin(y/this.degree))/(1-Math.sin(y/this.degree)))/2);
}

SparseTileLayerOverlay.prototype.XToL=function(x)
{
	return this.degree*(Math.round(x)-this.offset)/this.radius;
}

SparseTileLayerOverlay.prototype.YToL=function(y)
{
	return this.degree*(Math.PI/2-2*Math.atan(Math.exp((Math.round(y)-this.offset)/this.radius)));
}

SparseTileLayerOverlay.prototype.setUrl=function(xy,z){return [];}

SparseTileLayerOverlay.prototype.setStyle=function(offset,properties)
{
	var a,e,i,l,q,w;

	l=this.L;
	q=this.Q;

	if (l)
	{
		i=offset;

		for (a in properties)
		{
			if (q[a]) q[a][i]=properties[a];
		}

		for (a=0;l.childNodes[a];a++)
		{
			if (e=l.childNodes[a].childNodes[i])
			{
				if (q.alpha[i]==null) q.alpha[i]=1;
				if (q.level[i]==null) q.level[i]=0;

				e.style.display=q.alpha[i] ? "" : "none";
				e.style.zIndex =q.level[i];
			}
		}
	}
}

SparseTileLayerOverlay.prototype.idle=function()
{
	this.repair();
}

SparseTileLayerOverlay.prototype.draw=function()
{
	this.repair();
}

SparseTileLayerOverlay.prototype.repair=function()
{
	var a,b,c,d,e,f,g,h,i,j,k,l,o,p,q,r,s,t,u,v,w,x,y,z;

	var X=0,Y=0,X$=0,Y$=0,X_=0,Y_=0,X0=0,X1=0,Y0=0,Y1=0;

	w=this.getMap();
	u=this.getPanes();
	v=this.getProjection();

	if (!w) return;
	if (!u) return;
	if (!v) return;

	l=w.getDiv();

	q=w.getBounds();
	c=w.getCenter();
	z=w.getZoom();

	var Q=8;

	var Z=21-z;

	q={x:(this.LToX(c.lng())+(1<<Z>>1))>>Z,y:(this.LToY(c.lat())+(1<<Z>>1))>>Z};

	c=v.fromLatLngToDivPixel(c);

	X0=q.x-((l.offsetWidth +1)>>1);
	X1=q.x+((l.offsetWidth +1)>>1);

	Y0=q.y-((l.offsetHeight+1)>>1);
	Y1=q.y+((l.offsetHeight+1)>>1);

	X$=q.x-c.x;
	Y$=q.y-c.y;

	X_=1+((X1-X0)>>Q)+1;
	Y_=1+((Y1-Y0)>>Q)+1;

	X0=((X0-0)>>Q)+0;
	X1=((X1-1)>>Q)+1;

	Y0=((Y0-0)>>Q)+0;
	Y1=((Y1-1)>>Q)+1;

	if ((z!=this.Z)+(X$!=this.X$)+(Y$!=this.Y$)+(X_!=this.X_)+(Y_!=this.Y_))
	{
		l=this.L;
		q=this.Q;

		if (l)
		{
			for (i=l.childNodes.length-1;i+1;i--)
			{
				o=l.childNodes[i];

				l.removeChild(o);
			}

			u.overlayLayer.removeChild(l);

			l=null;
		}

		l=document.createElement("DIV");

		if (l)
		{
			u.overlayLayer.appendChild(l);

			l.style.position="absolute";

			for (i=0;X_*Y_-i;i++)
			{
				o=document.createElement("DIV");

				l.appendChild(o);

				o.style.position="absolute";
			}
		}

		this.X$=X$;
		this.Y$=Y$;

		this.X_=X_;
		this.Y_=Y_;

		this.X0=0;
		this.Y0=0;

		this.X1=0;
		this.Y1=0;

		this.L=l;
		this.Z=z;
	}

	if ((z!=this.Z)+(X0<this.X0)+(Y0<this.Y0)+(X1>this.X1)+(Y1>this.Y1))
	{
		l=this.L;
		q=this.Q;

		i=(0%X_)*Y_+(0%Y_);

		for (X=X0;X0+X_-X;X++)
		for (Y=Y0;Y0+Y_-Y;Y++)
		{
			x=X^X>>z<<z;
			y=Y^Y>>z<<z;

			a=this.setUrl({x:x,y:y,X:1<<Q,Y:1<<Q,x0:this.XToL((x+0)<<Q<<Z),y0:this.YToL((y+0)<<Q<<Z),x1:this.XToL((x+1)<<Q<<Z),y1:this.YToL((y+1)<<Q<<Z)},z);

			if (typeof(a)!="object") a=[a];

			i=((X+X_)%X_)*Y_+((Y+Y_)%Y_);

			o=l.childNodes[i];

			o.style.position="absolute";

			o.style.left  =((X<<Q)-X$)+"px";
			o.style.top   =((Y<<Q)-Y$)+"px";

			o.style.width =(1<<Q)+"px";
			o.style.height=(1<<Q)+"px";

			if (a.length!=o.childNodes.length) for (i=o.childNodes.length-1;i+1;i--)
			{
				e=o.childNodes[i];

				o.removeChild(e);
			}

			if (a.length!=o.childNodes.length) for (i=0;a[i]!=null;i++)
			{
				e=document.createElement("DIV");

				o.appendChild(e);

				e.style.position="absolute";

				e.style.left  =0+"px";
				e.style.top   =0+"px";

				e.style.width ="100%";
				e.style.height="100%";

				if (q.alpha[i]==null) q.alpha[i]=1;
				if (q.level[i]==null) q.level[i]=0;

				e.style.display=q.alpha[i] ? "" : "none";
				e.style.zIndex =q.level[i];
			}

			for (i=0;a[i]!=null;i++)
			{
				e=o.childNodes[i];

				if ((!window.XMLHttpRequest)*(!/[\&\?]format\=image\/(gif|jpeg)[\&\?]/gi.test(a[i]+"?")))
				{
					e.style.backgroundImage="";

					if (a[i]=="") e.style.filter="";

					else if (a[i]!=e.src) e.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+a[i]+"',sizingMethod='scale')";
				}

				else
				{
					e.style.filter="";

					if (a[i]=="") e.style.backgroundImage="";

					else if (a[i]!=e.src) e.style.backgroundImage="url"+"("+a[i]+")";
				}

				e.src=a[i];
			}
		}

		this.X0=X0;
		this.Y0=Y0;

		this.X1=X1;
		this.Y1=Y1;

		this.L=l;
		this.Z=z;
	}
}
