function gup( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
var mylat = gup( 'lat' );
var mylong = gup( 'long' );
var myRadius = gup( 'rd' );
var mymenu= gup( 'mn' );

if (!mylat){
	mylat = 38;
	};
	
if (!mylong){
	mylong = -97;
	};
	
if (gup( 'zl' ) == ''){
	var myZoom = 4;
	}
	else{
	var myZoom	= parseFloat(gup( 'zl' ));
	};

function DiscoverMap(){
	if (GBrowserIsCompatible()) {
		G_HYBRID_MAP.getName=function($short){
			if($short){
				return 'Sat';
			} else {
				return 'Satellite';
			}
		};
		G_PHYSICAL_MAP.getName=function($short){
			if($short){
				return 'Phy';
			} else {
				return 'Physical';
			}
		};
		G_NORMAL_MAP.getName=function($short){
			if($short){
				return 'Nor';
			} else {
				return 'Normal';
			}
		};
		//	var _mLastResult = "Reset map position and zoom level"; 
		this.map=new GMap2(document.getElementById('map'), {mapTypes:[G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP, G_PHYSICAL_MAP ]});
		this.map.setCenter(new GLatLng(mylat,mylong), myZoom, G_PHYSICAL_MAP);
		
		this.map.setUIToDefault();
		
		
		
		this.clusterer=new ClusterMarker(this.map);
		var $map=this.map, $clusterer=this.clusterer;	//	req'd for function closure
		
		GEvent.addListener($clusterer, 'refreshed', function($status){
			for (var i=$status.length-1; i>=0; i--){
				if($map._activeMarkerIndex===i){ $status[i]+=' active'; }
				document.getElementById('anchor'+i).className=$status[i];
			}
		});
		
		GEvent.addListener($clusterer, 'clustermouseover', function($markers) {
			var i, $id;
			//for(i=$markers.length-1; i>=0; i--){
				//$id='anchor'+$markers[i].index;
				//document.getElementById($id).style.backgroundColor='#fec658';	//	use multiple classes instead of individual properties
			//}
		});
		GEvent.addListener($clusterer, 'clustermouseout', function($markers) {
			var i, $id;
			//for(i=$markers.length-1; i>=0; i--){
				//$id='anchor'+$markers[i].index;
				//document.getElementById($id).style.backgroundColor='';
			//}
		});
		
		GEvent.addListener(this.map, 'infowindowopen', function(){
			//var $li=document.getElementById('anchor'+$map._activeMarkerIndex);
			//$li.className=$li.className+' active';
		});
		
		GEvent.addListener(this.map, 'infowindowclose', function(){
			//var $li=document.getElementById('anchor'+$map._activeMarkerIndex);
			//$class=$li.className;
			//$class=$class.replace(/\sactive/,'');
			//$li.className=$class;
			$map._activeMarkerIndex=false;
		});
		
		GEvent.addListener(this.map, 'moveend', function() { $map._lastCenter=$map.getCenter(); });
		GEvent.addListener(this.map, 'resize', function() { $map.setCenter($map._lastCenter); });
	}
}

DiscoverMap.prototype.getMarkers=function(){
	function createMarker($args) {
		var $marker=new GMarker($args.location, {title:$args.name, ID:$args.ID, icon:$args.MyIcon, draggable:false});
		var $html='<div class="infowindow">'+$args.html+'</div>';
		$marker.index=$args.index;
		$marker.myID=$args.ID;
		
		
		GEvent.addListener($marker, 'mouseover', function(){
			if($map._activeMarkerIndex!==$marker.index){
				$map.openInfoWindowHtml($marker.getLatLng(), $html, {maxWidth:400});
				$map._activeMarkerIndex=$marker.index;
			} else {
				$map.closeInfoWindow();
			}
		});
		GEvent.addListener($marker, 'click', function() { 
		if($map._activeMarkerIndex!==$marker.index){
				window.location.href='SubdivisionDetails.cfm?SubdivisionID='+$marker.myID+'&lat='+$marker.getLatLng().lat()+'&lon='+$marker.getLatLng().lng()+'&zl=1';
				$map._activeMarkerIndex=$marker.index;
			}
 
});
		
		
		
		GEvent.addListener($marker, 'dragend', function() { document.getElementById('title').innerHTML=$marker.getLatLng(); });
		return $marker;
	}
	var $Houseicon = new GIcon(), $this=this, $map=this.map;
	$Houseicon.image = 'images/marker_icons/house.png';
	$Houseicon.iconSize = new GSize(20, 20);
	$Houseicon.iconAnchor = new GPoint(10, 10);
	$Houseicon.infoWindowAnchor = new GPoint(10, 1);
	
	var $Premiericon = new GIcon(), $this=this, $map=this.map;
	$Premiericon.image = 'images/marker_icons/PremierMap.png';
	$Premiericon.iconSize = new GSize(20, 20);
	$Premiericon.iconAnchor = new GPoint(10, 10);
	$Premiericon.infoWindowAnchor = new GPoint(10, 1);


//GEvent.addListener($this.map, 'zoomend', function() {
//  if( $this.map.getZoom() < 4 ) {
//       alert($this.map.getZoom());
//   }
//});
	
	GDownloadUrl('scripts/query.cfm', function($file, $code) {
		if ($code===200) {
			var $json=[];
			eval($file);
			var i, j=$json.length, $row, $markers=[], $linksHtml='<ul>';
			for (i=0; i<j; i++) {
				$row=$json[i];
				$row.index=i;
				$linksHtml+='<li id="anchor'+i+'" onmouseover="" onclick="window.location='+"'"+'SubdivisionDetails.cfm?SubdivisionID='+$row.ID+"'"+'">';
				if ($row.Premium == '-1'){$linksHtml+='<img src="images/Premier.png" alt="" width="15" height="15" style="float:left; margin-left:-5px; margin-top:1px; margin-right:5px;"/>';}
				if ($row.Premium == '-1'){ $linksHtml+=$row.name+'<br><span class="menu_builder">'+$row.builder+'</span></li>'; }
				else { $linksHtml+=$row.name+'<br><span class="menu_builder"> </span></li>';}
				$markers.push(createMarker($row));
				$linksDetail= $row.ID;
			}
			
			$linksHtml+='</ul>';
			// ****** Create Side menu list ******
			if (mymenu == 1){
			document.getElementById('menu').innerHTML=$linksHtml;
			} else {
			document.getElementById('menu').innerHTML= null;
			}
			// ****** End Create Side menu list ******
			$this.clusterer.addMarkers($markers);
			$this.clusterer.fitMapToMarkers();
			$this.map.savePosition();
		} else {
			GLog.write('DB query failed, error code: '+$code);
		}
	});
};


DiscoverMap.prototype.menuSelect=function($id){
	if(this.map._activeMarkerIndex!==$id){
		this.clusterer.triggerClick($id);
	} else {
		this.map.closeInfoWindow();
	}
};


