/* bm_showmap.js

   javascript functions used to create/show maps.

   Use bm_editmaps for editing maps functions

   (C) blomap 2007

*/



var mmap; // the main map

var geocoder; // the geocoder

var markerlist = new Array();

var tempmarkerlist = new Array();

var iconlist = new Array();

var iconfiles = new Array();

var markercount; // the number of markers

var tempmarkercount; // the number of temp markers

var centerpoint;

var searchMarker = null;

var searchMarkerIcon;

var textSearch;

var timeOutId=null;

var curIdSearch = 0;
var curIdSearchStat=0;
var prevIdSearch = 0;
var curAddrSearch = "";



var catlist = new Array();;

var catCount = 0;

var catEdit = 0;

var curMarker = 0;

var isShow = 0;

var startLat = 0;

var startLon = 0;

var searchTextVAL="";

var locViewInfoUrl="";

var locViewInfoUrlPrv="";
var countMarkerTO = 0;
var markerTimeout;

function initializeMap(elid)

/* Initialize map element elid, with width wd and height hg dependent on the width and height of the element.

*/

{
 
	mmap = new GMap2(document.getElementById(elid));

	mmap.addControl(new GLargeMapControl());

	

	mmap.addControl(new GMapTypeControl());

	

	geocoder = new GClientGeocoder();

	markercount = 0;

}



function initializeUserMap()

{  //alert("initializeUserMap");

	GEvent.addListener(mmap, "dragend", function () {

				ll = mmap.getCenter();
     //alert("initializeUserMap=getCenter="+ll);
				mmap.closeInfoWindow();

				

				if (searchMarker!=null)

				{	searchMarker.popupText = "<div class='mapPopup'><div class='olpopuptext'> "+textNavTitle+"</div>"+textSearch+"</div>";

					searchMarker.setPoint(ll);

				}

				waitToSearch();

			});

	GEvent.addListener(mmap, "zoomend", function (o, n) {

				if (searchMarker!=null)

				{	searchMarker.popupText = "<div class='mapPopup'><div class='olpopuptext'> "+textNavTitle+"</div>"+textSearch+"</div>";

					mmap.closeInfoWindow();

				}

				

				waitToSearch();

			});
	

}



function createIcon(img, sh_img, i_size, s_size, i_anc, i_inf)

{  

	var gi = new GIcon

	gi.image = img;

	if (sh_img!='')

		{	gi.shadow = sh_img; }

	gi.iconSize = i_size;

	if (s_size!=null)

	{	gi.shadowSize = s_size;

	}

	if (i_anc!=null)

	{	gi.iconAnchor = i_anc;}

	if (i_inf!=null)

	{	gi.infoWindowAnchor = i_inf;

	}

	return gi

}

 

function showMap(hd, sh, txt, lat, lon, zm)

{

	//alert("showMap");

	h(hd);

	s(sh);

	isShow = 1;

   searchTextVAL=txt;

	if (lat!=null&&lon!=null)

	{	

		if (zm==null)

		{

			zm = 2;

		}

		mmap.checkResize();
     

		var pnt = new GLatLng(lat, lon);
//alert("showMap=pnt="+pnt+"zm"+zm+"");
		addSearchMarker(pnt, txt, true, zm);

		mmap.setCenter(pnt, zm);

	}

	return false;

}



function searchAddress(str)

{ 

	h("searchdiv");

	xajax_searchMarker(xajax.getFormValues("searchform"))

}



function addSearchMarker (point, addr, drag, zm)

{ //alert("addSearchMarker");

	var nzoom=mmap.getZoom();

	if (drag==null)

	{

		drag = false;

	}

	if (zm==null)

	{

		if (nzoom<11)

		{

			nzoom = 11;

		}

		

	}

	else

	{

		nzoom = zm;

	}

	

	

	if (searchMarker!=null)

	{

		searchMarker.setPoint(point);

		

	}

	else

	{

		searchMarker = new GMarker(point, {icon: searchMarkerIcon, draggable: drag});

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

				searchMarker.openInfoWindowHtml(searchMarker.popupText);

//alert(searchMarker.popupText);

		});

		if (drag)

		{

			GEvent.addListener(searchMarker, "dragend", function () {

				searchMarker.popupText = "<div class='mapPopup'><div class='olpopuptext'> "+textNavTitle+"</div>"+textSearch+"</div>";
//alert(searchMarker.popupText);
				ll = searchMarker.getPoint();

				mmap.panTo(ll);

				mmap.closeInfoWindow();

				waitToSearch();

			});

		}

		mmap.addOverlay(searchMarker);

	}
  
	mmap.setCenter(point, nzoom);

   var urlvinfo = getLocInfoUrl();
   //alert (textSearch);
   if(locViewInfoUrlPrv != "")
    {
	  textSearch = textSearch.replace(locViewInfoUrlPrv,urlvinfo); 
	 
	}else {
      textSearch = textSearch.replace('LOCINFOURL',urlvinfo); 
	}
	textSearch = textSearch.replace('locid='+prevIdSearch, 'locid='+curIdSearch); 

   //alert (textSearch);
	searchMarker.popupText = "<div class='mapPopup'><div class='olpopuptext'> "+textNavTitle+"</div><b>"+addr+"</b><br/>"+textSearch+"</div>";
//alert(searchMarker.popupText);
	searchMarker.textSearch = textSearch;

	searchMarker.openInfoWindowHtml(searchMarker.popupText);

	return false;

}



function waitToSearch()

{
 
	if (timeOutId!=null)

	{	clearTimeout(timeOutId);

	}

	timeOutId=setTimeout("timeOutSearch()", 1500);

}



function timeOutSearch()

{ 

	timeOutId = null;

	showSearchMarker();

}



function loadIcon()

{
  
}



function addLocationMarker(iconid, idloc, lt, ln, isdrag)

{ //alert("addLocationMarker");
  
	var mp;

	if (isdrag==null)

	{

		isdrag = false;

	}

	

	// check if marker already exist

	var i = 0;

	var found = false;

	while (!found&&i<markercount)

	{

		found = markerlist[i].loc_id==idloc;

		if (!found)

		{

			i++;

		}

	}

	

	// if marker already exist, use that (change the icon?)

	if (found)

	{

		curEdit = i;

		markerlist[i].category[markerlist[i].ccount] = iconid;

		markerlist[i].ccount++;

		mmap.removeOverlay(markerlist[i]);

		// change the icon

		mmap.addOverlay(markerlist[i]);

	}

	else

	{

		if (lt==null||ln==null)

		{	mp= mmap.getCenter();

		}

		else

		{

			mp=new GLatLng(lt,  ln);

		}

		if (iconlist[iconid]!=null)

		{

			marker = new GMarker(mp, {draggable: isdrag, icon: iconlist[iconid]});

		}

		else

		{

			marker = new GMarker(mp, {draggable: isdrag});

		}

		marker.index = markercount;

		marker.loc_id = idloc;

		marker.category = new Array();

		marker.category[0] = iconid;

		marker.ccount = 1;

		marker.userown = 0;

		marker.loaded = false;

	

		mmap.addOverlay(marker);

		markerlist[markercount] = marker;

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

			openPopup(this.index);

		});

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

			//closePopup(this.index);

		});

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

			openPopup(this.index);

		});

		

		if (isdrag)

		{

			/*GEvent.addListener(marker, "dragstart", function (){			

			if (curEdit!=this.index)

			{	markerClick(this.index);

				}}

			);*/

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

				saveUserMarkerLocation(this.index); 

			});

		}

		/*GEvent.addListener(marker, "infowindowclose", function () {					

			mmap.panTo(centerpoint);

		});*/

		

		

		curEdit = markercount;

		markercount++;

	}

}



function openMarker(id)

{ //alert("openMarker");
 
	centerpoint = mmap.getCenter();

	if (!markerlist[id].loaded)

		

	{	//mmap.panTo(markerlist[id].getPoint()); 

		showLocationData(id);

	}

	else

	{
     
		markerlist[id].openInfoWindowHtml(markerlist[id].popuptext);

	}

	curMarker = id;

}



function openPopup(id)
 
{    //alert("openPopup");

	centerpoint = mmap.getCenter();

	if (!markerlist[id].loaded)

		{

			openMarker(id);	

		}

	else

		{
           //alert("dddf="+markerlist[id].popuptext);
			markerlist[id].openInfoWindowHtml(markerlist[id].popuptext);

		}

}



function closePopup(id)

{ 

	centerpoint = mmap.getCenter();

	markerlist[id].closeInfoWindow(); 

}





function clearTempMarkers()

{
 
	//alert ("Clearing Temp Markers");

	for (var i=0;i<tempmarkercount;i++)

	{

		mmap.removeOverlay(tempmarkerlist[i]);

		tempmarkerlist[i] = null;

	}

	tempmarkercount = 0;

}



// click on show all marker

function showSearchMarker(idcat)

{  
	if (el('valltext')!=null)

	{

		var ncat = idcat;

		if (idcat==null)

		{

			ncat = el('maincat').value;

		}

		if (ncat==-1)

		{	el('valltext').style.display='none';

   			if ('function' == typeof window.iconAllOn) {

   				iconAllOn(0);

			}



		}

		else

		{	el('valltext').style.display='inline';	}

			

	}

	if (!isShow)

	{

		showMap('staticmap', 'dynamicmap', ' ', startLat, startLon, 14);

	}



	// delete all markers if change category

	for (var i=0;i<markercount;i++)

	{

		if (markerlist[i]!=null)

		{	

			tempmarkerlist[i] = markerlist[i]

			tempmarkercount++;

			markerlist[i] = null;

		}

	}

	markercount = 0;

	

	if (idcat!=null)

	{

		el('maincat').value=idcat;

	}

	

	var mp;

	if (searchMarker!=null) 

	{

		mp = searchMarker.getPoint();

	}

	else

	{

		mp = mmap.getCenter();

	}

	//alert(xajax.getFormValues('formshowcat'));

	var border = mmap.getBounds();
  
/*var svall= document.getElementById("searchtextrp").innerHTML;
 alert("vv="+searchText);
 if(svall=="The location  "+searchTextVAL+"  has not been found<br><br>Maybe you are searching for:<br><br>") 
  {*/

  	countMarkerTO = 0;
	xajax_loadMarker(border.getSouthWest().lat(), border.getSouthWest().lng(), border.getNorthEast().lat(), border.getNorthEast().lng(), xajax.getFormValues('formshowcat'));
	
  //}

}

function openSearchMarkerText(addr)
{
	
	searchMarker.popupText = "<div class='mapPopup'><div class='olpopuptext'> "+textNavTitle+"</div><b>"+addr+"</b><br/>"+textSearch+"</div>";
//alert(searchMarker.popupText);
	searchMarker.textSearch = textSearch;

	searchMarker.openInfoWindowHtml(searchMarker.popupText);

}


function createSearchText(id, nm, desc)

{

	markerlist[id].popuptext = "";

}



function showLocationData(id)

{  //alert("showLocationData");

	//s('searchdiv');

	//var cat = markerlist[id].category[0];

	el('searchcontenttext').innerHTML = "<h2>"+catlist[0].catName+"</h2>";

	

	if (el('mainppage'))

	{

			xajax_loadLocInfo(markerlist[id].loc_id, id, el('userprof').value);

	}

	else

	{

			xajax_loadLocInfo(markerlist[id].loc_id, id, 0);

	}

	

	var cll = textLinksLoc.replace(/%id%/gi,markerlist[id].loc_id);

	cll = cll.replace(/%mid%/gi,id);

	var adl = getMapReturn();

	cll = cll.replace(/%addlink%/gi, adl);

	el('linkcontent').innerHTML = cll;

	

	return false;

}


function getMapReturn()

{

	var mp = searchMarker.getPoint();

	var adl = '&lat=' +mp.lat()+ '&lon=' +mp.lng()+ '&z=' +mmap.getZoom()+ '&c=' + el('maincat').value;

	return adl;



}

function addCategory(id, nm)

{  //alert("addCategory");
  
 var svall= document.getElementById("searchtextrp").innerHTML;
 //alert("nm="+searchTextVAL+"id="+svall);
 //if(svall=="The location  "+searchTextVAL+"  has not been found<br><br>Maybe you are searching for:<br><br>") 
 //{
	catlist[0] = new objCategory(0, nm);

	catEdit = 0;
  //}
}



function printCat(id)

{

	catlist[0].outputCategory();

}



function openLocFrame(idloc, type)

{

	el('transframe').src='index.php?page=locblog&handler=locblog&locid='+idloc+'&type='+type;

	s('translayer');
	

}



function closeLocFrame()

{

	h('translayer');

}



function changeMapType(type)

{

	

	el('usermap').value = type;

	showSearchMarker();

	

}



function openId(idl)

{

	var i = 0;

	var found = false;

	while (!found&&i<markercount)

	{

		found = markerlist[i].loc_id==idl;

		if (!found)

		{

			i++;

		}

	}

	if (found)

		{	openMarker(i);}

}



function updateUserLoc()

{

	//alert ("TESTING "+curMarker);

	//alert (markerlist[curMarker].loc_id);

	// two empty values should be phone and fax

	xajax_updateUserLocation(el('mrk_locid').value, '', '', el('mrk_desc').value);

}



function changeMapCenterLoc (txt, lt, ln, lz)

{ //alert("changeMapCenterLoc");

	showMap('staticmap', 'dynamicmap', txt, lt, ln, lz)

	showSearchMarker();
	centerpoint = mmap.getCenter();
   addSearchMarker(centerpoint, txt, true, 14);

}



function changeSearch(str,pmemid)

{  //alert("changeSearch"+pmemid);
     
	el("searchtext").value=str;

	hideSearchSuggestion();
// alert(el("searchtext").value);
	xajax_centerLocation(el("searchtext").value, pmemid,1);	
   
}



function saveUserMarkerLocation(idx)

{ //alert("saveUserMarkerLocation");

	var ctr = markerlist[idx].getPoint();

	xajax_saveUserMarkerLocation(markerlist[idx].loc_id, ctr.lat(), ctr.lng());

}



function setSearchSuggestion(txt)

{ //alert("setSearchSuggestion"+txt);

    //initializeUserMap();
	
	var defSS = 'searchtextrp';

	if (el('searchsuggestionrp')!=null)

	{

		s('searchsuggestioncont');

		defSS = 'searchsuggestionrp';

	}

	else

	{

		el('searchcategorytitle').innerHTML = '';

	}

	el(defSS).innerHTML = txt;
  mapHomeSugest();
}



function hideSearchSuggestion()

{  //alert("hideSearchSuggestion");
  
	if (el('searchsuggestionrp')!=null)

	{

		h('searchsuggestioncont');

	}

}

function mapHomeSugest()
 {
	//alert("mapHomeSugest");
	showMap("dynamicmap", "staticmap"); return false;
 }
 
 function setLocInfoUrl(infpageurl)
  {
	  locViewInfoUrlPrv = locViewInfoUrl;
	  locViewInfoUrl = "";	 
	  locViewInfoUrl = infpageurl;
	 
  }
  
  function getLocInfoUrl()
  {  
	 return locViewInfoUrl;
  }
  
  function setLocSearchId(id, addr, stat)
  {
  	  prevIdSearch = curIdSearch;
  	  curIdSearch = id;
  	  curAddrSearch = addr;
  	  curIdSearchStat = stat;
  }
  
  function waitToSearchMarker(idl)
  {
  	  if (markercount==0)
  	  {
  	  	  if (markerTimeout!=null)
  	  	  {	clearTimeout(markerTimeout);
  	  	  }

  	  	  if (countMarkerTO<5)
  	  	  {
  	  	  	  markerTimeout=setTimeout("waitToSearchMarker("+idl+")", 1000);
  	  	  	  countMarkerTO++; 
  	  	  }
  	  	  else
  	  	  {
  	  	  	  countMarkerTO = 0;
  	  	  }
  	  }
  	  else
  	  {
  	  	  openId(idl);
  	  }
  }
  
