var _maplib_icons=[];
var _overlays=[];
var _maplib_info=[];
var map;

createBasicIcon();

function mapLib_Start(container,iz,mz,lat,lng,level)
{
			 map = new GMap(container,{mapTypes:[]});

			map.overlays = []; 
			GEvent.addListener(map,'addoverlay',function(o){ 
					this.overlays.push(o); 
			});

			GEvent.addListener(map,'removeoverlay',function(o){ 
					for(var i = 0; i < this.overlays.length; i++) 
							if(this.overlays[i] === o) 
									return this.overlays.splice(i,1); 
			}); 

			GEvent.addListener(map,'clearoverlays',function(){ 
					this.overlays = []; 
			}); 

			  var copyright = new GCopyright(1,new GLatLngBounds(new GLatLng(lat, lng),new GLatLng(lat, lng) ),14, "MapLib.net");
			  var copyrightCollection = new GCopyrightCollection('Map Data:');
			  copyrightCollection.addCopyright(copyright);

			  var tilelayers = [new GTileLayer(copyrightCollection,7,17-iz)];
			  tilelayers[0].getTileUrl = function(a,b){
				  return "/mapdata/b82c83a624bb32016a287ef14977e43c/"+a.x+"_"+a.y+"_"+(17-b)+".gif";
			  }
			  
			  var custommap = new GMapType(tilelayers, new GMercatorProjection(15), "MapLib");
			  map.addMapType(custommap);
			  map.setMapType(custommap);
			  map.setCenter(new GLatLng(lat,lng),level,custommap);
			  omap=new GOverviewMapControl(new GSize(200,200));
			  map.addControl(omap);
			  omap.hide(true);

			  var m_doc=document.getElementsByTagName('body').item(0);
			  var js = document.createElement('div');
			  js.setAttribute('id', 'powered_by_maplib');
			  js.innerHTML="<a href='http://www.maplib.net/' target='_blank' title='MapLib.net - Enabling New Ways of Viewing Pictures'><img src='http://www.maplib.net/images/poweredby.png' border='0' onload='pngfix(this)'></a>";
			  m_doc.appendChild(js);

			  var pos2= new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(64,0));
			  pos2.apply(document.getElementById("powered_by_maplib"));
			  map.getContainer().appendChild(document.getElementById("powered_by_maplib"));
			  map.addControl(new GLargeMapControl());

			  var url="data/markers.xml";
			  var pars;
			  new Ajax.Request(url,{method: 'get', parameters: pars,onFailure: failure,onLoading:loading, onComplete: createMarkers});
}

function createMarkers(o)
{
	var tag=new Array();
	tag=Xparse(o.responseText);
	process_tag(tag);
}

function process_tag(tag)
{
	if (tag.attributes["longitude"]!=null)
	{
		createMarker(tag.attributes["id"],new GLatLng(tag.attributes["latitude"], tag.attributes["longitude"]),tag.attributes["title"],tag.attributes["introduction"],tag.attributes["iconid"]);
	}
	for(var i=0;i < tag.contents.length;i++)
	{
		if(tag.contents[i].type=="element")
			process_tag(tag.contents[i]);
	}
}

function createMarker(markerid,point,tooltip,html,iconid) {
	
	if (iconid==0)
	{
		var marker = new GMarker(point,{icon:_maplib_icons.basic,title:tooltip});
	}
	else
	{
		if (!_maplib_icons[iconid])
		{
			 createIcon(iconid);
		}
	  var marker = new GMarker(point,{icon:_maplib_icons[iconid],title:tooltip});
	}

	marker.id=markerid;
	marker.group=iconid;
	
	_maplib_info[markerid]=generateInfo(html,tooltip);

  GEvent.addListener(marker, "click", function() {
	
	marker.openInfoWindowHtml(_maplib_info[markerid]);
  });


  map.addOverlay(marker);
}

function failure()
{
	alert("request failure");
}

function loading()
{
}

function createBasicIcon(){
	_maplib_icons.basic = new GIcon();
	_maplib_icons.basic.image = "icons/basic.png";
	_maplib_icons.basic.iconSize = new GSize(20,34);
	_maplib_icons.basic.iconAnchor = new GPoint(6,20);
	_maplib_icons.basic.infoWindowAnchor = new GPoint(5,1);
	_maplib_icons.basic.imageMap = [5,0, 1,4, 1,8, 3,12, 5,20, 7,20, 8,12, 11,8, 11,4, 7,0];
}

function createIcon(iconid)
{
	imgfile="icons/"+iconid;
	_maplib_icons[iconid]=new GIcon(_maplib_icons.basic,imgfile);
	_maplib_icons[iconid].iconSize = new GSize(20,34);
	_maplib_icons[iconid].iconAnchor = new GPoint(6,20);
}

function generateInfo(html,tooltip)
{
	html=html.replace(/\[b\](.*?)\[\/b\]/i,"<strong>$1</strong>");
	html=html.replace(/\[i\](.*?)\[\/i\]/i,"<em>$1</em>");
	html=html.replace(/\[u\](.*?)\[\/u\]/i,"<u>$1</u>");
	html=html.replace(/\[img\](.*?)\[\/img\]/i,"<img src='$1' />");
	html=html.replace(/\[url\](.*?)\[\/url\]/i,"<a href='$1' target='_blank'>$1</a>");
	html=html.replace(/\[url\=(.*?)\](.*?)\[\/url\]/i,"<a href='$1' target='_blank'>$2</a>");

	html2="<div style='width: 200px;'><b>"+tooltip+"</b><br><hr noshade color='#3399FF' size='1' width='180' align='left'>"+html;
	html2=html2+"<br></div>";
	return html2;
}


function locate(x,y,z,markerid)
{
	map.closeInfoWindow();
	map.panTo(new GLatLng(x, y));
	map.setZoom(z);
	for (var x=0;x<map.overlays.length;x++) 
	{
		if (map.overlays[x].id==markerid)
		{
			map.overlays[x].openInfoWindowHtml(_maplib_info[markerid]);
		}
	}
}

function filter_markers(gid,flag)
{
	map.closeInfoWindow();

	for (var x=0;x<map.overlays.length;x++) 
	{
		if (map.overlays[x].group==gid)
		{
			if (flag>0)
			{
				map.overlays[x].show();
			} else
			{
				map.overlays[x].hide();
			}
		}
	}

	if (document.getElementById('tbl_marker_list').rows.length>2)
	{
		var tdlist=document.getElementById('tbl_marker_list').getElementsByTagName('tr');
			 for (j=0; j<tdlist.length;j++)
			{
				 if (tdlist[j].id.indexOf('-'+gid)>0)
				 {
	
					 if (flag>0)
					 {
						 tdlist[j].style.display="block";
					 } else
					 {
						 tdlist[j].style.display="none";
					 }
				 }
			}
	}
}