var RGServerNum = Math.floor(Math.random() * 4);

function RGNextTileX(addr, forward) {
	if (addr == 't') return addr;		
	var parent = addr.substr(0, addr.length-1);
	var last = addr.substr(addr.length-1);
	
	if (last == 'q') { last = 'r'; if (!forward) parent = RGNextTileX(parent, forward) } 
	else if (last == 'r') { last = 'q'; if (forward) parent = RGNextTileX(parent, forward) } 
	else if (last == 's') { last = 't'; if (forward) parent = RGNextTileX(parent, forward) } 
	else if (last == 't') { last = 's'; if (!forward) parent = RGNextTileX(parent, forward) }
	return parent + last;
}

function RGNextTileY(addr, forward) {
	if (addr == 't') return addr;		
	var parent = addr.substr(0, addr.length-1);
	var last = addr.substr(addr.length-1);
	
	if (last == 'q') { last = 't'; if (!forward) parent = RGNextTileY(parent, forward) } 
	else if (last == 'r')	{ last = 's'; if (!forward) parent = RGNextTileY(parent, forward) } 
	else if (last == 's') { last = 'r'; if (forward) parent = RGNextTileY(parent, forward) }
	else if (last == 't')	{ last = 'q'; if (forward) parent = RGNextTileY(parent, forward) }
	return parent + last;
}

function RGMapPreviewImg(mark) { 
	var url = '/groups/rg/images/forum_map_preview' + (mark ? '_cross' : '') + '.png';

	return navigator.userAgent.match(/MSIE 6/) ?
			'<img style="border: none; position: absolute; z-index: 9999; width: 130px; height: 90px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + url + '\')" src="/groups/rg/images/transp.gif"/>'
		:
			'<img style="border: none; position: absolute; z-index: 9999" src="' + url + '"/>';
}

function RGMapPreview(lng, lat, z, t, w, h, url, mark) {
	z -= 2;
	if (z < 1) z = 1;
	var PI = 3.1415926535897;
	var digits = z;
	var x = (180.0 + parseFloat(lng)) / 360.0;
	var y = -parseFloat(lat) * PI / 180;
	y = 0.5 * Math.log((1+Math.sin(y)) / (1 - Math.sin(y)));
	
	y *= 1.0/(2 * PI);
	y += 0.5;

	var px = x * Math.pow(2, z);
	var py = y * Math.pow(2, z);

	var ax = px - Math.floor(px);
	var ay = py - Math.floor(py);	

	var dx = Math.floor(x * Math.pow(2, z + 1)) % 2;
	var dy = Math.floor(y * Math.pow(2, z + 1)) % 2;

	if (t == 1) {

		var rx = Math.floor(px);
		var ry = Math.floor(py);
		var zoom = 17 - z;
	
	
		if (dx < 0.5) { rx = (rx - 1 + Math.pow(2, z)) % Math.pow(2, z); ax++ }
		if (dy < 0.5) { ry = (ry - 1 + Math.pow(2, z)) % Math.pow(2, z); ay++ }
	
		return '<a href="' + url + '">' + RGMapPreviewImg(mark) + '</a><div style="border: solid 1px gray; overflow: hidden; width: ' + w + 'px; height: ' + h + 'px"><div style="white-space: nowrap; margin-left: ' + (ax * -256 + w/2) + 'px; margin-top: ' + (ay * -256 + h/2) + 'px">' +
			'<img alt="" style="width: 256px; height: 256px" src="http://mt' + RGServerNum + '.google.com/mt?n=404&v=ap.80&x=' + rx + '&y=' + ry + '&zoom=' + zoom + '" />' + 
			'<img alt="" style="width: 256px; height: 256px" src="http://mt' + RGServerNum + '.google.com/mt?n=404&v=ap.80&x=' + ((rx + 1) % Math.pow(2, z)) + '&y=' + ry + '&zoom=' + zoom + '" /><br/>' +
			'<img alt="" style="width: 256px; height: 256px" src="http://mt' + RGServerNum + '.google.com/mt?n=404&v=ap.80&x=' + rx + '&y=' + ((ry + 1) % Math.pow(2, z)) + '&zoom=' + zoom + '" />' + 
			'<img alt="" style="width: 256px; height: 256px" src="http://mt' + RGServerNum + '.google.com/mt?n=404&v=ap.80&x=' + ((rx + 1) % Math.pow(2, z)) + '&y=' + ((ry + 1) % Math.pow(2, z)) + '&zoom=' + zoom + '" /></div></div>';

	} else {

		var quad = "t";
		var lookup = "qrts";
		
		while (digits--) {
			x -= Math.floor(x);
			y -= Math.floor(y);
			
			quad = quad + lookup.substr((x>=0.5?1:0) + (y>=0.5?2:0), 1);
	
			x *= 2;
			y *= 2;
		}
	
		if (dx < 0.5) { quad = RGNextTileX(quad, false); ax++ }
		if (dy < 0.5) { quad = RGNextTileY(quad, false); ay++ }
		var quad2 = RGNextTileY(quad, true);
	
		return '<a href="' + url + '">' + RGMapPreviewImg(mark) + '</a><div style="border: solid 1px gray; overflow: hidden; width: ' + w + 'px; height: ' + h + 'px"><div style="white-space: nowrap; margin-left: ' + (ax * -256 + w/2) + 'px; margin-top: ' + (ay * -256 + h/2) + 'px">' +
			'<img alt="" style="width: 256px; height: 256px" src="http://kh' + RGServerNum + '.google.com/kh?n=404&v=ap.80&t=' + quad + '" />' +
			'<img alt="" style="width: 256px; height: 256px" src="http://kh' + RGServerNum + '.google.com/kh?n=404&v=ap.80&t=' + RGNextTileX(quad, true) + '" /><br/>' +
			'<img alt="" style="width: 256px; height: 256px" src="http://kh' + RGServerNum + '.google.com/kh?n=404&v=ap.80&t=' + quad2 + '" />' +
			'<img alt="" style="width: 256px; height: 256px" src="http://kh' + RGServerNum + '.google.com/kh?n=404&v=ap.80&t=' + RGNextTileX(quad2, true) + '" /></div></div>';
	
	}
}

