var gmarkers = [];
var htmls = [];
var to_htmls = [];
var from_htmls = [];
var i=0;
function initializefromAdsearch(newlat, newlng, radius, userArray) {
    if (GBrowserIsCompatible()) {
		newlat=parseFloat(newlat)
		newlng=parseFloat(newlng)
		radius=parseFloat(radius)
	    clickedPixel; 

		map = new GMap2(document.getElementById("map_canvas"));
		
		map.setCenter(new GLatLng(newlat, newlng), zoom);
		


		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
		map.enableContinuousZoom();
		map.enableScrollWheelZoom();

		
		// === create the context menu div ===
		contextmenu = document.createElement("div");
		contextmenu.style.visibility = "hidden";
		contextmenu.style.background = "#ffffff";
		contextmenu.style.border="1px solid #8888FF";

		contextmenu.innerHTML = '<a href="javascript:zoomIn()" ><div class="context" >&nbsp;&nbsp;Zoom in&nbsp;&nbsp;</div></a>'
						+ '<a href="javascript:zoomOut()"><div class="context">&nbsp;&nbsp;Zoom out&nbsp;&nbsp;</div></a>'
						 + '<a href="javascript:setLocation()"><div class="context">&nbsp;&nbsp;Set location&nbsp;&nbsp;</div></a>';
					   
		map.getContainer().appendChild(contextmenu);
		
		// === listen for singlerightclick ===
		GEvent.addListener(map,"singlerightclick",function(pixel,tile) {
		// store the "pixel" info in case we need it later
		// adjust the context menu location if near an egde
		// create a GControlPosition
		// apply it to the context menu, and make the context menu visible
		clickedPixel = pixel;
		mapClickedPixel=pixel;
		
		var x = pixel.x;
		var y = pixel.y;
		if (x > map.getSize().width - 120) { x = map.getSize().width - 120 }
		if (y > map.getSize().height - 100) { y = map.getSize().height - 100 }
		var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));  
		pos.apply(contextmenu);
		contextmenu.style.visibility = "visible";
		});
		// === If the user clicks on the map, close the context menu ===
		GEvent.addListener(map, "click", function() {
		contextmenu.style.visibility = "hidden";
		});

		geocoder     = new GClientGeocoder();
	
		for(i=0 ; i< userArray.length; i++)
		{
			point        = new GLatLng(userArray[i][6],userArray[i][7]);
			marker[i]       = createInfoMarkerAdSearch(point, userArray[i][0],i,userArray[i][9],userArray[i][1]);
			map.addOverlay(marker[i]);	
		}
		
		point        = new GLatLng(newlat,newlng);

		marker       = createInfoMarkerAdSearch(point, userid);
		//map.addOverlay(marker);	
		
		var point    = marker.getLatLng();
		var center   = point;
		radius       = selectradius(radius);
		createCircle = drawCricleSearch(radius, center);
		
		}
    }


function createInfoMarkerAdSearch(point, otheruserid,itm_no,vrf_status,u_name) {

	
	baseIcon = new GIcon(G_DEFAULT_ICON);
		baseIcon.image = webURL+'/location/iconimageinsearch/'+otheruserid+'/'+vrf_status;

		baseIcon.iconSize         = new GSize(44, 54);

		baseIcon.iconAnchor       = new GPoint(20, 54);
		baseIcon.infoWindowAnchor = new GPoint(20, 13);
	
		baseIcon.imageMap=[0,0, 44,0, 44,44, 0,44];

		markerOptions = { icon:baseIcon,draggable: false};
	    marker[itm_no]        = new GMarker(point,markerOptions);
		
		 

		
		GEvent.addListener(marker[itm_no], 'click', function(){
			
			var minContent = "<html><body><div style='text-align:left; height:200px;overflow:auto;'>";
			minContent+=document.getElementById("userprofiledata"+itm_no).innerHTML+document.getElementById("userlocationdata"+itm_no).innerHTML;
			minContent+="</div></body></html>";
			var maxContent = "<div style='padding:7px;text-align:left;'>"+document.getElementById("userinfo"+itm_no).innerHTML+"</div>";
			var maxTitle ="<div style='text-align:center;font-size:18px;'><b>"+u_name+"</b></div>";
			var tag_search_result="<div style='text-align:left;padding:7px;'>"+document.getElementById("usersearchpostdata"+itm_no).innerHTML+"</div>";
			var user_blog_post_result="<div style='text-align:left;padding:7px;'>"+document.getElementById("userblogpostdata"+itm_no).innerHTML+"</div>";
			var tabs = [
			new MaxContentTab('SEARCH RESULT',tag_search_result),
			new MaxContentTab('MORE BLOGS AND POSTS',user_blog_post_result)];
			map.openMaxContentTabsHtml(point, minContent, maxContent, tabs, {maxTitle: maxTitle});
		});
		return marker[itm_no];
    }

	function openmapwindow(itm_no,lat,lng,u_name) {  
		point        = new GLatLng(lat,lng);
		var minContent = "<html><body><div style='text-align:left; height:200px;overflow:auto;'>";
		minContent+=document.getElementById("userprofiledata"+itm_no).innerHTML+document.getElementById("userlocationdata"+itm_no).innerHTML;
		minContent+="</div></body></html>";
		var maxContent = "<div style='padding:7px;text-align:left;'>"+document.getElementById("userinfo"+itm_no).innerHTML+"</div>";
		var maxTitle ="<div style='text-align:center;font-size:18px;'><b>"+u_name+"</b></div>";
		var tag_search_result="<div style='text-align:left;padding:7px;'>"+document.getElementById("usersearchpostdata"+itm_no).innerHTML+"</div>";
		var user_blog_post_result="<div style='text-align:left;padding:7px;'>"+document.getElementById("userblogpostdata"+itm_no).innerHTML+"</div>";
		var tabs = [
		new MaxContentTab('SEARCH RESULT',tag_search_result),
		new MaxContentTab('MORE BLOGS AND POSTS',user_blog_post_result)];
		map.openMaxContentTabsHtml(point, minContent, maxContent, tabs, {maxTitle: maxTitle});
	}
	
	
	function openmaxmapwindow(itm_no,lat,lng,u_name) {  
		openmapwindow(itm_no,lat,lng,u_name);
		setTimeout("map.getInfoWindow().maximize(true)",1000);
	}
	
	
	

function initializefromsearch(newlat, newlng, radius, userArray) {
    if (GBrowserIsCompatible()) {
		newlat=parseFloat(newlat)
		newlng=parseFloat(newlng)
		radius=parseFloat(radius)
	    clickedPixel; 

		map = new GMap2(document.getElementById("map_canvas"));
		
		map.setCenter(new GLatLng(newlat, newlng), zoom);

		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
		map.enableContinuousZoom();
		map.enableScrollWheelZoom();
		
		
		// === create the context menu div ===
		contextmenu = document.createElement("div");
		contextmenu.style.visibility = "hidden";
		contextmenu.style.background = "#ffffff";
		contextmenu.style.border="1px solid #8888FF";

		contextmenu.innerHTML = '<a href="javascript:zoomIn()" ><div class="context" >&nbsp;&nbsp;Zoom in&nbsp;&nbsp;</div></a>'
						+ '<a href="javascript:zoomOut()"><div class="context">&nbsp;&nbsp;Zoom out&nbsp;&nbsp;</div></a>'
						 + '<a href="javascript:setLocation()"><div class="context">&nbsp;&nbsp;Set location&nbsp;&nbsp;</div></a>';
					   
		map.getContainer().appendChild(contextmenu);
		
		// === listen for singlerightclick ===
		GEvent.addListener(map,"singlerightclick",function(pixel,tile) {
		// store the "pixel" info in case we need it later
		// adjust the context menu location if near an egde
		// create a GControlPosition
		// apply it to the context menu, and make the context menu visible
		clickedPixel = pixel;
		mapClickedPixel=pixel;
		
		var x = pixel.x;
		var y = pixel.y;
		if (x > map.getSize().width - 120) { x = map.getSize().width - 120 }
		if (y > map.getSize().height - 100) { y = map.getSize().height - 100 }
		var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));  
		pos.apply(contextmenu);
		contextmenu.style.visibility = "visible";
		});
		// === If the user clicks on the map, close the context menu ===
		GEvent.addListener(map, "click", function() {
		contextmenu.style.visibility = "hidden";
		});

		geocoder     = new GClientGeocoder();
	
		for(i=0 ; i< userArray.length; i++)
		{

			point        = new GLatLng(userArray[i][6],userArray[i][7]);
			marker       = createInfoMarkerSearch(point, userArray[i][0]);
			map.addOverlay(marker);	
		}
		
		point        = new GLatLng(newlat,newlng);

		marker       = createInfoMarkerSearch(point, userid);
		//map.addOverlay(marker);	
		
		var point    = marker.getLatLng();
		var center   = point;
		radius       = selectradius(radius);
		createCircle = drawCricleSearch(radius, center);
		
		}
    }
	
	function drawCricleSearch(radius,center) // draw a circle
	{
		var latOffset = 0.01;
		var lonOffset = 0.01;
		var latConv   = center.distanceFrom(new GLatLng(center.lat()+0.1,center.lng()))/100;
		var lngConv   = center.distanceFrom(new GLatLng(center.lat(),center.lng()+0.1))/100;

		// nodes = number of points to create circle polygon
		var nodes = 40;
		//Loop
		var points = [];
		var step = parseInt(360/nodes)||10;
		for(var i=0; i<=360; i+=step)
		{
			var pint = new GLatLng(center.lat() + (radius/latConv * Math.cos(i* Math.PI/180)), center.lng() + (radius/lngConv * Math.sin(i *Math.PI/180)));
			// push pints into points array
			points.push(pint);
		}
		var polygon = new GPolygon(points, "#f33f00", 1, 1, "#ff0000",0.1);
		map.addOverlay(polygon);
		return polygon;
	}
	
	function createInfoMarkerSearch(point, otheruserid) {

		baseIcon = new GIcon(G_DEFAULT_ICON);
		baseIcon.image = webURL+'/location/iconimageinsearch/'+otheruserid;

		baseIcon.iconSize         = new GSize(44, 54);

		baseIcon.iconAnchor       = new GPoint(20, 54);
		baseIcon.infoWindowAnchor = new GPoint(20, 13);
	
		baseIcon.imageMap=[0,0, 44,0, 44,44, 0,44];

		markerOptions = { icon:baseIcon,draggable: false};
	    marker        = new GMarker(point,markerOptions);
		
	/*	GEvent.addListener(marker, "click",
		function() {
			 marker.openInfoWindowHtml();
		});
       */
		return marker;
    }


    function initialize() {
    if (GBrowserIsCompatible()) {
	    clickedPixel; 
		map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(lat, lon), zoom);
		//map.setCenter(new GLatLng(30.333570,  78.047760),10);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
		map.enableContinuousZoom();
		map.enableScrollWheelZoom();
		
		
		// === create the context menu div ===
		contextmenu = document.createElement("div");
		contextmenu.style.visibility = "hidden";
		contextmenu.style.background = "#ffffff";
		contextmenu.style.border="1px solid #8888FF";

		contextmenu.innerHTML = '<a href="javascript:zoomIn()" ><div class="context" >&nbsp;&nbsp;Zoom in&nbsp;&nbsp;</div></a>'
						+ '<a href="javascript:zoomOut()"><div class="context">&nbsp;&nbsp;Zoom out&nbsp;&nbsp;</div></a>'
						 + '<a href="javascript:setLocation()"><div class="context">&nbsp;&nbsp;Set location&nbsp;&nbsp;</div></a>';
					   
		map.getContainer().appendChild(contextmenu);
		
		// === listen for singlerightclick ===
		GEvent.addListener(map,"singlerightclick",function(pixel,tile) {
		// store the "pixel" info in case we need it later
		// adjust the context menu location if near an egde
		// create a GControlPosition
		// apply it to the context menu, and make the context menu visible
		clickedPixel = pixel;
		mapClickedPixel=pixel;
		
		var x = pixel.x;
		var y = pixel.y;
		if (x > map.getSize().width - 120) { x = map.getSize().width - 120 }
		if (y > map.getSize().height - 100) { y = map.getSize().height - 100 }
		var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));  
		pos.apply(contextmenu);
		contextmenu.style.visibility = "visible";
		});
		// === If the user clicks on the map, close the context menu ===
		GEvent.addListener(map, "click", function() {
		contextmenu.style.visibility = "hidden";
		});

		geocoder     = new GClientGeocoder();
		point        = new GLatLng(lat,lon);
		
		marker       = createInfoMarker(point);
		map.addOverlay(marker);	
		
		var point    = marker.getLatLng();
		var center   = point;
		var radius   = outerRadius;
		createCircle = drawCricle(radius, center);
		geocoder.getLocations(point, setAddress);	
		}
    }
	

	function setAddress(response) {
	
	  if (!response || response.Status.code != 200) {
		alert("Status Code:" + response.Status.code);
	  } else {
		place = response.Placemark[0];
		if(document.getElementById("setaddress").value=='')
		{	
			document.getElementById("setaddress").value=place.address+' -- '+document.getElementById('selectRadius').value+' Miles';			        }else{
				address=getAddress(document.getElementById("setaddress").value);
			document.getElementById("setaddress").value=address+' -- '+document.getElementById('selectRadius').value+' Miles';	
		}
	  }
	}
   	
	function createInfoMarker(point, address) {
	 
		//var webURL   = document.getElementById("webURL").value;
		baseIcon = new GIcon(G_DEFAULT_ICON);
		
		if(userid && userimage!='noimage')
		{
			baseIcon.image = webURL+'/location/iconImage/';
			//baseIcon.image = mapURL+'/location/iconImage';
			
		}
		else 
		{
			baseIcon.image = mapURL+'/map_icon.png';
			
		}
		//baseIcon.image = mapURL+'/map_icon.png';
		baseIcon.iconSize         = new GSize(44, 54);
		//baseIcon.image = mapURL+'/map_icon.png';
		//alert(mapURL+'/map_iconshadow3.gif');
		//baseIcon.shadow           = mapURL+'/map_iconshadow3.png';
       //baseIcon.shadowSize       = new GSize(65, 54);
		baseIcon.iconAnchor       = new GPoint(20, 54);
		baseIcon.infoWindowAnchor = new GPoint(20, 13);
	
		baseIcon.imageMap=[0,0, 44,0, 44,44, 0,44];

		markerOptions = { icon:baseIcon,draggable: true};
	    marker        = new GMarker(point,markerOptions);
       
		GEvent.addListener(marker, "dragstart", function() {
        map.closeInfoWindow();
		map.removeOverlay(createCircle);
        });

        GEvent.addListener(marker, "dragend", function() {
		point        = marker.getPoint();
		var center   = point;
		var radius   = outerRadius;
		createCircle = drawCricle(radius, center);
	    }); 
		
		GEvent.addListener(marker, "click",
		function(point) {
			
			if(userid)
			{
				openusermapwindow(point,userid,loggedInUserName);
			}
			else 
			{
				geocoder.getLocations(point, showAddress1);
			}
		});
	    return marker;
    }

	
	function createInfoMarkerUser(point,userid1,username) {
	  
	   
		//var webURL   = document.getElementById("webURL").value;
		baseIcon = new GIcon(G_DEFAULT_ICON);
		
		if(userid && userimage!='')
		{
			baseIcon.image = webURL+'/location/iconimageuser/'+userid;
			//baseIcon.image = mapURL+'/location/iconImage';
			
		}
		else 
		{
			baseIcon.image = mapURL+'/map_icon.png';
			
		}
		//baseIcon.image = mapURL+'/map_icon.png';
		baseIcon.iconSize         = new GSize(44, 54);
		//baseIcon.image = mapURL+'/map_icon.png';
		//alert(mapURL+'/map_iconshadow3.gif');
		//baseIcon.shadow           = mapURL+'/map_iconshadow3.png';
       //baseIcon.shadowSize       = new GSize(65, 54);
		baseIcon.iconAnchor       = new GPoint(20, 54);
		baseIcon.infoWindowAnchor = new GPoint(20, 13);
	
		baseIcon.imageMap=[0,0, 44,0, 44,44, 0,44];

		markerOptions = { icon:baseIcon,draggable: true};
	    marker        = new GMarker(point,markerOptions);
       
		GEvent.addListener(marker, "dragstart", function() {
        map.closeInfoWindow();
		map.removeOverlay(createCircle);
        });

        GEvent.addListener(marker, "dragend", function() {
		point        = marker.getPoint();
		var center   = point;
		var radius   = outerRadius;
		createCircle = drawCricle(radius, center);
	    }); 
		
		GEvent.addListener(marker, "click",
		function(point) {
			 //marker.openInfoWindowHtml(address);
			 //geocoder.getLocations(point, showAddress1);
			 openusermapwindow(point,userid1,username);
		});
	    return marker;
    }
	
	function userInfoOnMapMaxWindow(lat,lon,userid1,userimage1,username) {  
		userInfoOnMap(lat,lon,userid1,userimage1,username);
		setTimeout("map.getInfoWindow().maximize(true)",3000);
	}
	
	function userInfoOnMap(lat,lon,userid1,userimage1,username) {
    
	userid=userid1;
	userimage=userimage1;
	
	if (GBrowserIsCompatible()) {
	    clickedPixel; 
		map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(lat, lon), zoom);
		//map.setCenter(new GLatLng(30.333570,  78.047760),10);
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
		map.enableContinuousZoom();
		map.enableScrollWheelZoom();
		
		
		// === create the context menu div ===
		contextmenu = document.createElement("div");
		contextmenu.style.visibility = "hidden";
		contextmenu.style.background = "#ffffff";
		contextmenu.style.border="1px solid #8888FF";

		contextmenu.innerHTML = '<a href="javascript:zoomIn()" ><div class="context" >&nbsp;&nbsp;Zoom in&nbsp;&nbsp;</div></a>'
						+ '<a href="javascript:zoomOut()"><div class="context">&nbsp;&nbsp;Zoom out&nbsp;&nbsp;</div></a>'
						 + '<a href="javascript:setLocation()"><div class="context">&nbsp;&nbsp;Set location&nbsp;&nbsp;</div></a>';
					   
		map.getContainer().appendChild(contextmenu);
		
		// === listen for singlerightclick ===
		GEvent.addListener(map,"singlerightclick",function(pixel,tile) {
		// store the "pixel" info in case we need it later
		// adjust the context menu location if near an egde
		// create a GControlPosition
		// apply it to the context menu, and make the context menu visible
		clickedPixel = pixel;
		mapClickedPixel=pixel;
		
		var x = pixel.x;
		var y = pixel.y;
		if (x > map.getSize().width - 120) { x = map.getSize().width - 120 }
		if (y > map.getSize().height - 100) { y = map.getSize().height - 100 }
		var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x,y));  
		pos.apply(contextmenu);
		contextmenu.style.visibility = "visible";
		});
		// === If the user clicks on the map, close the context menu ===
		GEvent.addListener(map, "click", function() {
		contextmenu.style.visibility = "hidden";
		});

		geocoder     = new GClientGeocoder();
		point        = new GLatLng(lat,lon);
		
		marker       = createInfoMarkerUser(point,userid1,username);
		map.addOverlay(marker);	
		
		var point    = marker.getLatLng();
		var center   = point;
		var radius   = outerRadius;
		createCircle = drawCricle(radius, center);
		//geocoder.getLocations(point, showAddress1);
		openusermapwindow(point,userid1,username);
		
		}
    }
	
	
	function openusermapwindow(point,userid1,username) { 
	
		 $.ajax(
	 	{
		type: "POST",
		url: webURL+"/search/usermapinfo/"+userid1,
		data:'',
		success: function(response)
			{
				var addressArray=response.split("<!-- userinfo-->");
				var error_msg='<b>'+profile_errors.vars.reg_user_info+'</b>';
				var point=marker.getLatLng();
				lat=point.lat();
				lng=point.lng();
				
				
				if(addressArray[1]=="0")
				{
					var minContent = "<html><body><div style='text-align:left;padding-top:5px'>";
					minContent+=error_msg;
					
   
					
					minContent+="</div></body></html>";
					var maxContent = "<div style='padding:7px;text-align:left;'>"+error_msg+"</div>";
					var maxTitle ="<div style='text-align:center;font-size:18px;'><b>"+username+"</b></div>";
					var user_blog_post_result="<div style='text-align:left;padding:7px;'>"+error_msg+"</div>";
					var tabs = [
				new MaxContentTab('MORE BLOGS AND POSTS',user_blog_post_result)];
				map.openMaxContentTabsHtml(point, minContent, maxContent, tabs, {maxTitle: maxTitle});
				}else{	
					
					
					var minContent = "<html><body><div style='text-align:left;'>";
					minContent+=addressArray[1];
					
					to_htmls[i] = minContent+'<br>Directions: <b>To here<\/b> - <a href="javascript:fromhere(' + i + ')">From here<\/a>' +
   '<br>Start address:<form action="javascript:getDirections()">' +
   '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
   '<INPUT value="Get Directions" TYPE="SUBMIT"><br>' +
   '<input type="hidden" id="daddr" value="'+''+"@"+ lat + ',' + lng + '"/>';
   					from_htmls[i] =  minContent+'<br>Directions: <a href="javascript:tohere(' + i + ')">To here<\/a> - <b>From here<\/b>' +
   '<br>End address:<form action="javascript:getDirections()">' +
   '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
   '<INPUT value="Get Directions" TYPE="SUBMIT"><br>' +
   '<input type="hidden" id="saddr" value="'+''+"@"+ lat + ',' + lng +'"/>';
   
					minContent+="<a href='javascript:tohere("+i+");'>Get Directions</a></div></body></html>";

					var maxContent = "<div style='padding:7px;text-align:left;'>"+addressArray[1]+"</div>";
					var maxTitle ="<div style='text-align:center;font-size:18px;'><b>"+username+"</b></div>";
					var user_blog_post_result="<div style='text-align:left;padding:7px;'>"+addressArray[3]+"</div>";
					var tabs = [
				new MaxContentTab('MORE BLOGS AND POSTS',user_blog_post_result)];
				map.openMaxContentTabsHtml(point, minContent, maxContent, tabs, {maxTitle: maxTitle});
				GEvent.addListener(marker, "click", function() {
				  
				 map.openMaxContentTabsHtml(point, minContent, maxContent, tabs, {maxTitle: maxTitle});
				});
			
				gmarkers[i] = marker;
				htmls[i] = minContent;
				i++;
				return marker;
				}
				
			
			}
	  	});
		
	}
	
	
   
   function showAddress1(response) {
  
  if (!response || response.Status.code != 200) {
    alert("Status Code:" + response.Status.code);
  } else {
      place = response.Placemark[0];
    //point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
	html1 = '<div style="text-align:left;">' + 
		'<b>You are here </b><br>' +
        '<b>latlng:</b> ' + place.Point.coordinates[1] + "," + place.Point.coordinates[0] + '<br>' +
        '<b>Address:</b> ' + place.address + '<br>' +
        '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode +' ';
		
		to_htmls[i] = html1+'<br>Directions: <b>To here<\/b> - <a href="javascript:fromhere(' + i + ')">From here<\/a>' +
   '<br>Start address:<form action="javascript:getDirections()">' +
   '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
   '<INPUT value="Get Directions" TYPE="SUBMIT"><br>' +
   '<input type="hidden" id="daddr" value="'+place.address+"@"+ place.Point.coordinates[1] + ',' + place.Point.coordinates[0] + '"/>';

	from_htmls[i] =  html1+'<br>Directions: <a href="javascript:tohere(' + i + ')">To here<\/a> - <b>From here<\/b>' +
   '<br>End address:<form action="javascript:getDirections()">' +
   '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
   '<INPUT value="Get Directions" TYPE="SUBMIT"><br>' +
   '<input type="hidden" id="saddr" value="'+place.address+"@"+ place.Point.coordinates[1] + ',' + place.Point.coordinates[0] +'"/>';
		
	html = html1 +'<br/><a href="javascript:tohere('+i+');">Get Directions</a></div>';	
		
		
    marker.openInfoWindowHtml(html);
	

	GEvent.addListener(marker, "click", function() {
	  marker.openInfoWindowHtml(html);
	});

	gmarkers[i] = marker;
	htmls[i] = html;
	i++;
	return marker;
  }
}
   
   
   function showAddress(address,selectRadius) {
	   address=getAddress(address);
	   document.getElementById("setaddress").value=address+' -- '+selectRadius+' Miles';	 
		if(address)
		{
		  if(selectRadius){
			 outerRadius=selectradius(selectRadius);
		  }
		  if (geocoder) {
		    //geocoder.getLocations(address, addAddressToMap);
			geocoder.getLatLng(
			  address,
			  function(point) {
				if (!point) {
				  alert(address + " not found");
				} else {
					latnew=point.lat();
					longnew=point.lng();
					
					
					if(!document.getElementById("realrender")){
					map.clearOverlays();
					map.setCenter(point,zoom);
					marker = createInfoMarker(point, address);
					map.addOverlay(marker);
					var point=marker.getLatLng();
					var center = point;
					var radius =selectRadius;
					var radius = outerRadius;
					createCircle = drawCricle(radius, center);
					}else{
							$.ajax(
								{
								type: "POST",
								url: webURL+'/rtv/tempmapinfo',
								data:'latnew='+latnew+'&longnew='+longnew+'&selectRadius='+selectRadius,
								success: function(msg)
									{
									document.getElementById("realrender").innerHTML	=msg;
									}
								});
									map.clearOverlays();
									map.setCenter(point,zoom);
									marker = createInfoMarker(point, address);
									map.addOverlay(marker);
									var point=marker.getLatLng();
									var center = point;
									var radius =selectRadius;
									var radius = outerRadius;
									createCircle = drawCricle(radius, center);
						
						}
				}
			  }
			);
		  }
		}
    }


	function getAddress(address)
	{
		var newaddress='';
		var addressArray=address.split("--");
		var addressString=addressArray[0];
		
		if(addressString.substring((addressString.length-1))==' ')
		{
			newaddress=addressString.substring(0,(addressString.length-1));
		}else{
			newaddress=addressString;
		}
		return newaddress;
		
	}
	
	function drawCricle(radius,center) // draw a circle
	{
	 
	    var radius    = radius;
		var latOffset = 0.01;
		var lonOffset = 0.01;
		var latConv   = center.distanceFrom(new GLatLng(center.lat()+0.1,center.lng()))/100;
		var lngConv   = center.distanceFrom(new GLatLng(center.lat(),center.lng()+0.1))/100;

		// nodes = number of points to create circle polygon
		var nodes = 40;
		//Loop
		var points = [];
		var step = parseInt(360/nodes)||10;
		for(var i=0; i<=360; i+=step)
		{
			var pint = new GLatLng(center.lat() + (radius/latConv * Math.cos(i* Math.PI/180)), center.lng() + (radius/lngConv * Math.sin(i *Math.PI/180)));
			// push pints into points array
			points.push(pint);
		}
		var polygon = new GPolygon(points, "#f33f00", 1, 1, "#ff0000",0.1);
		map.addOverlay(polygon);
		return polygon;
	}
	
    
    function selectradius(selectradius)  //convert kilometers to miles-diameter	
    {
		var radiusinmeter;
		if(selectradius)
		{
		  radiusinmeter = 1.609344*selectradius;
		}
		return radiusinmeter;
    }	

	
	function addAddressToMap(response) {
      map.clearOverlays();
      if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
      } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
      }
    }

	function clearShapes(){
		map.clearOverlays();
	}


  // === functions that perform the context menu options ===
	function zoomIn() {
		map.zoomIn();
		contextmenu.style.visibility = "hidden";
	}

	function zoomOut() {
		map.zoomOut();
		contextmenu.style.visibility = "hidden";
	} 
	  
	function setLocation() {
	  if(userid)
	  {
		  var bool = confirm("Do you want to move your address,blogs and posts to new location?");
		  if(bool==1)
		  {
			var point = map.fromContainerPixelToLatLng(mapClickedPixel);
			geocoder.getLocations(point, showNewAddress);
			contextmenu.style.visibility = "hidden";
		  }
		  else
		  {

			var point     = map.fromContainerPixelToLatLng(mapClickedPixel);
			contextmenu.style.visibility = "hidden";
			map.clearOverlays();
			map.setCenter(point,zoom);
			marker        = createInfoMarker(point);
			map.addOverlay(marker);
			var point     = marker.getLatLng();
			var center    = point;
			var radius    = outerRadius;
			createCircle  = drawCricle(radius, center);
		  }
	  }
	  else
	  {
		    var point     = map.fromContainerPixelToLatLng(mapClickedPixel);
			contextmenu.style.visibility = "hidden";
			map.clearOverlays();
			map.setCenter(point,zoom);
			marker        = createInfoMarker(point);
			map.addOverlay(marker);
			var point     = marker.getLatLng();
			var center    = point;
			var radius    = outerRadius;
			createCircle  = drawCricle(radius, center);
	  }
	}
	  
	function showNewAddress(response) {
		//var webURL   = document.getElementById("webURL").value;
		if (!response || response.Status.code != 200) {
		//alert("Status Code:" + response.Status.code);
		alert("Address not found");
		} else {
		place          = response.Placemark[0];
		point          = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]);
		map.clearOverlays();
		map.setCenter(point,zoom);
		marker         = createInfoMarker(point, place.address);
		map.addOverlay(marker);
		var point      = marker.getLatLng();
		var center     = point;
		var radius     = outerRadius;
		createCircle   = drawCricle(radius, center);
		
		map.panTo(point);
		latnew=point.lat();
		longnew=point.lng();
		var mapzoom     = map.getZoom();
		country         = place.AddressDetails.Country.CountryNameCode;
		accuracy        = place. AddressDetails.Accuracy;
		addressbygoogle = place.address;
		//alert(accuracy);alert(addressbygoogle);
		
		$.post(webURL+"/location/savelocation&latnew="+latnew+"&longnew="+longnew+"&userid="+userid+"&mapzoom="+mapzoom+"&country="+country+"&accuracy="+accuracy+"&addressbygoogle="+addressbygoogle+"", { },
		function(data){ 
		//alert("Data Loaded: " + data);
		/*if(data=="1")
		{
			alert('Your Point Save');
		}*/
		});
		}
	}
	
	function setRadius(address,radius)
	{
	  showAddress(address,radius);
	}
	
	function getUsers(searchForm)
	{
		//var webURL   = document.getElementById("webURL").value;
		
		var address      = document.getElementById('setaddress').value;
		var selectRadius = document.getElementById('selectRadius').value;
		var searchText   = document.getElementById('searchText').value;
		address=getAddress(address);
		
		if(address)
		{
		  if(selectRadius){
			 outerRadius=selectradius(selectRadius);
		  }
		  if (geocoder) {
			geocoder.getLatLng(
			  address,
			  function(point) {
				if (!point) {
					var limit=5;
				  alert(address + " not found");
				} else {
					map.setCenter(point,zoom);
					marker = createInfoMarker(point, address);
					var point=marker.getLatLng();
					lat=point.lat();
					lng=point.lng();
					var limit=5;
					
				document.getElementById('searchAdhoLat').value=lat;
				document.getElementById('searchAdhoLng').value=lng;
				document.getElementById('topSearchAddress').value=address;
				searchForm.action=webURL+'/search/advancedsearchresult/';
				searchForm.submit();
				//window.location=webURL+"/search/index/"+escape(address)+"/"+lat+"/"+lng+"/"+selectRadius+"/"+limit+"/"+0+"/"+escape(searchText);
				}
			  }
			);
		  }
		}
	}
	function gettwitterUsers(searchForm)
	{
		//var webURL   = document.getElementById("webURL").value;
		
		var address      = searchForm;
		var selectRadius = 5;
		//var searchText   = document.getElementById('searchText').value;
		address=getAddress(address);
		 
		if(address) 
		{
		  if(selectRadius){
			 outerRadius=selectradius(selectRadius);
		  }
		  if (geocoder) {
			geocoder.getLatLng(
			  address,
			  function(point) {
				if (!point) {
					var limit=5;
				  alert(address + " not found");
				} else {
					contextmenu.style.visibility = "hidden";
					map.clearOverlays();
					map.setCenter(point,zoom);
					marker = createInfoMarkertwitter(point, address);
					map.addOverlay(marker);
					 marker.openInfoWindowHtml(
        				'<div style="text-align:left;">' + 
		 			'<b>'+ address +'</b> ' + '')+'</div>';
					var point=marker.getLatLng();
					lat=point.lat();
					lng=point.lng();
					var center   = point;
					var radius   = outerRadius;
					createCircle = drawCricle(radius, center);
					
				
				//window.location=webURL+"/search/index/"+escape(address)+"/"+lat+"/"+lng+"/"+selectRadius+"/"+limit+"/"+0+"/"+escape(searchText);
				}
			  }
			);
		  }
		}
		
	}
	
	function createInfoMarkertwitter(point, address) {
	 
		

		baseIcon = new GIcon(G_DEFAULT_ICON);
		
		
			baseIcon.image = mapURL+'/map_icon.png';
		
		baseIcon.iconSize         = new GSize(44, 54);
		baseIcon.iconAnchor       = new GPoint(20, 54);
		baseIcon.infoWindowAnchor = new GPoint(20, 13);
	
		baseIcon.imageMap=[0,0, 44,0, 44,44, 0,44];

		markerOptions = { icon:baseIcon,draggable: true};
	    marker        = new GMarker(point,markerOptions);
		GEvent.addListener(marker, "click",
		function(point) {
			 //marker.openInfoWindowHtml(address);
			 //geocoder.getLocations(point, showAddress1);
			 //openusermapwindow(point,'','naveen');
		});
	    return marker;
    }
	function getChannelsUsers(address,selectRadius)
	{	address=getAddress(address);
		var searchText   = document.getElementById('searchText').value;

		if(address)
		{
		  if(selectRadius){
			 outerRadius=selectradius(selectRadius);
		  }
		  if (geocoder) {
			geocoder.getLatLng(
			  address,
			  function(point) {
				if (!point) {
				  alert(address + " not found");
				} else {
					map.setCenter(point,zoom);
					marker = createInfoMarker(point, address);
					var point=marker.getLatLng();
					lat=point.lat();
					lng=point.lng();
					var limit=5;
				window.location=webURL+"/channel/index/"+escape(address)+"/"+lat+"/"+lng+"/"+selectRadius+"/"+escape(searchText);
				}
			  }
			);
		  }
		}
  }
    function getDirections() 
{
	var reasons=[];
	reasons[G_GEO_SUCCESS]            = "Success.";
	reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
	reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
	reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
	reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
	reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
	reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
	reasons[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
	reasons[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";
	reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";
	
	
	var saddr = document.getElementById("saddr").value;
	var daddr = document.getElementById("daddr").value;
	document.getElementById("directions").innerHTML="";
	document.getElementById("gdirections").style.display='block';
	document.getElementById("nodirection").style.display='none';
	
	var gdir=new GDirections(map, document.getElementById("directions"));
	gdir.load("from: "+saddr+" to: "+daddr, {getPolyline:true});
		
	GEvent.addListener(gdir, "error", function() 
	{
		var code = gdir.getStatus().code;
		var reason="Code "+code;
		if (reasons[code]) 
		{
			reason = reasons[code]
		} 
		 alert("Failed to obtain directions, "+reason);
	});

	//map.clearOverlays();
}
 
function tohere(i) 
{
	gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}

function fromhere(i) 
{
	gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}
function swapDIV(){
	document.getElementById("gdirections").style.display='none';
	document.getElementById("nodirection").style.display='block';
	
}