var _maplib_icons=[];

var _maplib_info=[];

var _maplib_legend=0;

var map;



function mapLib_Start(path,container,iz,mz,lat,lng,level,mapid,host,maptype)

{

			 if (maptype==0)

				 map = new GMap(container);

			 else

				 map = new GMap(container,{mapTypes:[G_NORMAL_MAP,G_SATELLITE_MAP,G_HYBRID_MAP,G_PHYSICAL_MAP]});



			map.overlays = []; 

			map.enableScrollWheelZoom();

			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 = []; 

			}); 



			  if (maptype==0)

			  {

				  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 "tiles/"+path+"/"+a.x+"_"+a.y+"_"+(17-b)+".gif";

				  }

				  

				  var custommap = new GMapType(tilelayers, new GMercatorProjection(15), "MapLib");

				  map.addMapType(custommap);

				  map.setMapType(custommap);

			  } else

				{

					map.addControl(new GMapTypeControl());

				}



			  if (maptype==0)

			  map.setCenter(new GLatLng(lat,lng),level,custommap);

			  else

				  if (maptype==2)

				  map.setCenter(new GLatLng(lat,lng),level,G_NORMAL_MAP);

			  else

				  if (maptype==3)

				  map.setCenter(new GLatLng(lat,lng),level,G_HYBRID_MAP);

			  else

				  if (maptype==4)

				  map.setCenter(new GLatLng(lat,lng),level,G_PHYSICAL_MAP);

			  else

				  map.setCenter(new GLatLng(lat,lng),level,G_SATELLITE_MAP);







			  var m_doc=document.getElementsByTagName('body').item(0);

			  var js = document.createElement('div');

			  js.setAttribute('id', 'powered_by_maplib');

			 

			  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());



			  GEvent.addListener(map, "moveend", function() {

				  var center = map.getCenter();

				  document.getElementById("url_link").value=host+'map.php?mapid='+mapid+"&lat="+center.lat()+"&lng="+center.lng()+"&z="+map.getZoom();

				  var fulllink=host+'fullmap.php?mapid='+mapid+"&lat="+center.lat()+"&lng="+center.lng()+"&z="+map.getZoom();

				  document.getElementById("code_link").value='<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="'+fulllink+'"></iframe>';

				  });



			  createBasicIcon();



			  getMarkers(mapid,'',1);

}



function getMarkers(mapid,kw,page)

{

	  map.clearOverlays();

	  GEvent.clearListeners(map,'click');

	  GEvent.clearListeners(map,'mousemove');



	  if (kw==null)

	  {

		  kw='';

	  }



	var url="action.php?action=get_markers_list&kw="+kw+"&page="+page+"&mapid="+mapid+rand();

	var pars;



	new Ajax.Updater('panel', url, {method: 'get', parameters: pars,onFailure: failure,onLoading:loading,onComplete:done});



	var url="action.php?action=get_markers_xml&kw="+kw+"&page="+page+"&mapid="+mapid+rand();

	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"],tag.attributes["iw"],tag.attributes["ih"],tag.attributes["user"],tag.attributes["sdate"]);

	}

	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,iw,ih,user,sdate) {

	

	if (!_maplib_icons[iconid])

	{

		createIcon(iconid,iw,ih);

	}



	var marker = new GMarker(point,{icon:_maplib_icons[iconid],title:tooltip});



	marker.id=markerid;

	marker.group=iconid;

	

	if (!_maplib_info[markerid])

	{

		_maplib_info[markerid]=generateInfo(html,tooltip,user,sdate);

	}

	

  GEvent.addListener(marker, "click", function() {

	

	marker.openInfoWindowHtml(_maplib_info[markerid]);

  });





  map.addOverlay(marker);

}



function failure()

{

	alert("request failure");

}



function loading()

{

}



function done()

{

}



function createBasicIcon(){

	_maplib_icons.basic = new GIcon();

	_maplib_icons.basic.image = "icons/default.png";

	_maplib_icons.basic.iconSize = new GSize(12,20);

	_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,iw,ih)

{

	_maplib_icons[iconid]=new GIcon(_maplib_icons.basic,iconid);

	_maplib_icons[iconid].iconSize = new GSize(iw,ih);

	_maplib_icons[iconid].iconAnchor = new GPoint(6,20);

}



function generateInfo(html,tooltip,user,sdate)

{

	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: 240px;'><b>"+tooltip+"</b><br><hr noshade color='#3399FF' size='1' width='180' align='left'>"+html;

	html2=html2+"<br><p style='color: #C0C0C0'>by "+user+"&nbsp; "+sdate+"</p></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 filtMarkers(gid)

{

	map.closeInfoWindow();





	for (var x=0;x<map.overlays.length;x++) 

	{

		if (map.overlays[x].group==gid)

		{

			if (_maplib_legend>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 (_maplib_legend>0)

					 {

						 tdlist[j].style.display="block";

					 } else

					 {

						 tdlist[j].style.display="none";

					 }

				 }

			}

	}



	_maplib_legend=1-_maplib_legend;

}



function invoke_frm_addPoint(mapid)

{

  var url="action.php?action=frm_addpoint&mapid="+mapid+rand();

  var pars="";



   GEvent.addListener(map, "click", function(marker, point) {

	  document.getElementById('longitude').value=point.lng();

	  document.getElementById('latitude').value=point.lat();

	  document.getElementById('maplib_info').innerHTML='';

	  map.addOverlay(new GMarker(point));

  });



  var myajax=new Ajax.Updater('panel', url, {method: 'get', asynchronous: false, parameters: pars,onFailure: failure,onLoading:loading,onComplete:done});

  generate_wysiwyg('introduction');

}



function rand()

{

	return "&rand="+(Math.random()*Math.random());

}



function rand2()

{

	return "?rand="+(Math.random()*Math.random());

}



function toggleMarker(param,mapid)

{

  updateTextArea("introduction");

  var url="action.php"+rand2();

  document.getElementById('param').value=param;

  document.getElementById('zoom').value=map.getZoom();

  document.getElementById('maplib_info').innerHTML='';

  

  if (param!=3)

  {

	  if (document.getElementById('title').value=='')

	  {

		document.getElementById('maplib_info').innerHTML='title required';

	  } else

	  if (document.getElementById('longitude').value!='0')

	  {

		var myAjax = new Ajax.Updater('panel', url, {method: 'post',asynchronous: false, parameters: Form.serialize("frm_toggle_marker"),onFailure: failure,onLoading:loading,onComplete:done});

		if (param==2)

		{

			getMarkers(mapid,'',1);

		} else

		  {

			generate_wysiwyg('introduction');

		  }

	  }

	  else

	  document.getElementById('maplib_info').innerHTML='Please click a place on the map';

  } else

	{

	  var myAjax = new Ajax.Updater('panel', url, {method: 'post',asynchronous: false, parameters: Form.serialize("frm_toggle_marker"),onFailure: failure,onLoading:loading,onComplete:done});

	  generate_wysiwyg('introduction');

	}

}



function dialog() {

  Dialog.alert({url: "icons.php"+rand2(), options: {method: 'get'}}, 

               {windowParameters: {className: "alphacube", width:320, height:320}, okLabel: "close", 

                ok:function(win) {Dialog.closeInfo();return true;}, cancel:function(win) { Dialog.closeInfo();return true;}});

}



function switchIcon(img)

{

	document.getElementById('layer_icon').innerHTML='<img src="'+img+'" border="0">';

	document.getElementById('icon_id').value=img;

	Dialog.closeInfo();

}



var panelToggled=null;

function togglePanel()

{

	if (panelToggled)

	{

	  document.getElementById('tdPanel').style.display="block";

	  document.getElementById('togglePanelImg').innerHTML="<img border='0' src='images/hide-arrow.png'";

	  panelToggled=null;

	} else

	{

		document.getElementById('tdPanel').style.display="none";

		document.getElementById('togglePanelImg').innerHTML="<img border='0' src='images/show-arrow.png'";

		panelToggled=true;

	}

}



function pop_dialog(durl,dwidth,dheight) {

  

  durl=durl+rand();

  Dialog.alert({url: durl, options: {method: 'get'}}, 

               {windowParameters: {className: "alphacube", width:dwidth, height:dheight},okLabel: "Close"});

}



function countDown(secs,surl){ 

 if(--secs>0){

     setTimeout("countDown("+secs+",'"+surl+"')",1000); 

     }

 else{

   

     location.href=surl;

     }  

 }



 function switch_markers()

{

	map.closeInfoWindow();



	if (document.getElementById('markers_status').value=="1")

	{

		var on=1;

		document.getElementById('markers_status').value="0";

	} else

	{

		var on=0;

		document.getElementById('markers_status').value="1";

	}



	for (var x=0;x<map.overlays.length;x++) 

	{

			if (on>0)

			{

				map.overlays[x].hide();

			} else

			{

				map.overlays[x].show();

			}

	}

}
