var map=null;
var bounds; 
var baseIcons;
var marker=[];
var html=[];
var to_html=[];
var flag=0;
var geoCoder;
var gdir;
function loadMap()
{
	if(map==null)
	{
      	if (GBrowserIsCompatible())
  		{
			document.getElementById('light1').style.display='block';
			map = new GMap2(document.getElementById("map"));
			map.addControl(new GMapTypeControl());
			map.addControl(new GOverviewMapControl()); 
			map.setCenter(new GLatLng(34,-105), 5);
			geoCoder = new GClientGeocoder();
			bounds = new GLatLngBounds(); 
			//Create a base icon for all of our markers that specifies the
			// shadow, icon dimensions, etc.
			baseIcons = new GIcon();
			baseIcons.shadow = "http://www.google.com/mapfiles/shadow50.png";
			baseIcons.iconSize = new GSize(20, 34);
			baseIcons.shadowSize = new GSize(37, 34);
			baseIcons.iconAnchor = new GPoint(9, 34);
			baseIcons.infoWindowAnchor = new GPoint(9, 2);
			baseIcons.infoShadowAnchor = new GPoint(18, 25);
			GEvent.addListener(map, "moveend", function()
			{
        		var center = map.getCenter();
      		});
   			map.addControl(new GLargeMapControl());
   			
   			gdir = new GDirections(map, document.getElementById("directions"));
      		GEvent.addListener(gdir, "load", onGDirectionsLoad);
      		GEvent.addListener(gdir, "error", handleErrors); 
   			
   		}
     	else
    	{
    	alert("Browser doesnot support google maps");
		}
	}
}

function tohere(i)
{

document.getElementById('light1').style.width="950px";
document.getElementById('light1').style.left="10px";
document.getElementById('directions').style.display="block";
marker[i].openInfoWindowHtml(to_html[i]);
}
function block()
{
document.getElementById('light1').style.width="720px";
document.getElementById('light1').style.left="10px";
document.getElementById('directions').style.display="none";
gdir.clear();
showAllMarkers();
}
//to display directions
function setDirections(){
var fromAddress=document.getElementById('saddr1').value;
var toAddress=document.getElementById('daddr1').value;
//var toAddress="41.78,-74.2309";
var locale="en";
try{
    map.closeInfoWindow();
    //alert(fromAddress+"  "+toAddress); 
    gdir.load("from: " + fromAddress + " to: " + toAddress,
           { "locale": locale });
}
catch(err){alert("error occured \n"+err.message);}    
}
function onGDirectionsLoad(){ 
try{  
      document.getElementById("directions").innerHTML="";      
}
catch(err){alert("error occured \n"+err.message);}
}


//handles errors
function handleErrors(){
try{
document.getElementById("directions").innerHTML="";
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS){
     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
     document.getElementById("directions").innerHTML="No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code;
     }
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR){
     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
     document.getElementById("directions").innerHTML="A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code;
     }
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY){
     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
     document.getElementById("directions").innerHTML="The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code;
     }

    
   else if (gdir.getStatus().code == G_GEO_BAD_KEY){
     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
     document.getElementById("directions").innerHTML="The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code;
     }
   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST){
     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
     document.getElementById("directions").innerHTML="A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code;
     }
   else {
   document.getElementById("directions").innerHTML="We could not calculate driving directions between the given cities";
   alert("We could not calculate driving directions between the given cities");
   }
 }
catch(err){alert("error occured \n"+err.message);}
}
function add_us_Markers(url,geo,cntr,rank,cname,street,city,state,zip)
{

	
		var latlong= new Array(2);
		latlong=geo.split(",");
		var point=new GLatLng(latlong[0],latlong[1]);
		rank=cntr;
		html[rank]='<p><a href="'+url+'"  >'+cname+'</a><br></p><p>'+street +'<br>'+city+','+state+'<br>'+zip+'</p>';
		to_html[rank] ='<div class="map_info">'+html[rank]+ 
								'<p>Directions:<strong>To&nbsp;'+cname+'</strong><br>Enter Start Address:<br><input type="text" size=40 maxlength=40 id="saddr1" name="from" value="San Francisco" title="Start" /><br>' +
	          		            '<input type="submit" value="Get Directions" onclick="setDirections()" />' +
	           		            '<input type="hidden" id="daddr1" name="daddr1" value="'+point.lat()+','+point.lng()+ '" /></p></div>';
		html[rank] = html[rank] + '<p><a href='+"'"+'javascript:tohere('+rank+')'+"'"+'>'+'Directions</a></p>';
		html[rank]='<div class="map_info">'+html[rank]+'</div>';
		var rankIcon = new GIcon(baseIcons);
		rankIcon.image ="http://img.timeinc.net/golf/static/img/course/icons/marker"+cntr+".gif";
		
		markerOptions = { icon:rankIcon };
		marker[rank] = new GMarker(point,markerOptions);
		marker[rank].bindInfoWindowHtml(html[rank]);
		map.addOverlay(marker[rank]);
		bounds.extend(point);
		
}
function add_top_Markers(url,geo,rank,cname,city,state)
{
		
		if(url=='null' || url=='')
		 url="#";
		else
		 url=url;
		var latlong= new Array(2);
		latlong=geo.split(",");
		var point=new GLatLng(latlong[0],latlong[1]);
		html[rank]='<em>Top 100 U.S. Rank: '+rank+'</em><p><a href="'+url+'" target="_blank">'+cname.unescapeHTML()+'</a><br>'+city+', '+state+'</p>';
		to_html[rank] ='<div class="map_info">'+html[rank]+ 
								'<p>Directions:<strong>To&nbsp;'+cname+'</strong><br>Enter Start Address:<br><input type="text" size=40 maxlength=40 id="saddr1" name="from" value="" title="Start" /><br>' +
	          		            '<input type="submit" value="Get Directions" onclick="setDirections()" />' +
	           		            '<input type="hidden" id="daddr1" name="daddr1" value="'+point.lat()+','+point.lng()+ '" /></p></div>';
		html[rank] = html[rank] + '<p><a href='+"'"+'javascript:tohere('+rank+')'+"'"+'>'+'Directions</a></p>';
		html[rank]='<div class="map_info">'+html[rank]+'</div>';
		var rankIcon = new GIcon(baseIcons);
		rankIcon.image ="http://img.timeinc.net/golf/static/img/course/icons/top100_markers/marker"+rank+".png";
		markerOptions = { icon:rankIcon };
		marker[rank] = new GMarker(point, markerOptions);
		marker[rank].bindInfoWindowHtml(html[rank]);
		map.addOverlay(marker[rank]);
		bounds.extend(point);
}
function add_world_Markers(url,geo,rank,cname,city,country)
{
		if(url=='null' || url=='')
		 url="#";
		else
		 url=url;
		var latlong= new Array(2);
		latlong=geo.split(",");
		var point=new GLatLng(latlong[0],latlong[1]);
		html[rank]='<em>Top 100 World Rank: '+rank+'</em><br><p> <a href="'+url+'" target="_blank">'+cname.unescapeHTML()+'</a><br>'+city+','+country+'</p>';
		to_html[rank] ='<div class="map_info">'+html[rank]+ 
								'<p>Directions:<strong>To&nbsp;'+cname+'</strong><br>Enter Start Address:<br><input type="text" size=40 maxlength=40 id="saddr1" name="from" value="" title="Start" /><br>' +
	          		            '<input type="submit" value="Get Directions" onclick="setDirections()" />' +
	           		            '<input type="hidden" id="daddr1" name="daddr1" value="'+point.lat()+','+point.lng()+ '" /></p></div>';
		html[rank] = html[rank] + '<p><a href='+"'"+'javascript:tohere('+rank+')'+"'"+'>'+'Directions</a></p>';
		html[rank]='<div class="map_info">'+html[rank]+'</div>';
		var rankIcon = new GIcon(baseIcons);
		rankIcon.image ="http://img.timeinc.net/golf/static/img/course/icons/top100_markers/marker"+rank+".png";
		markerOptions = { icon:rankIcon };
		marker[rank] = new GMarker(point, markerOptions);
		marker[rank].bindInfoWindowHtml(html[rank]);
		map.addOverlay(marker[rank]);
		bounds.extend(point);
		
}

function show_city(city,state,rank,stateCode)
{
var myNewString = city.replace(" ", "-");
var address = city+","+state;
var url = '<a  href="'+stateCode+'/'+myNewString+'.html">'+city+'</a>';
	if (geoCoder)
	{
        geoCoder.getLatLng(address,function(point)
		{
            if (!point)
			{
             // alert(address + " not found( overlaymap )");
            }
			else
			{
			  map.setCenter(point, 13);
			  var rankIcon = new GIcon(baseIcon);
		rankIcon.image ="http://img.timeinc.net/golf/static/img/course/icons/marker"+rank+".gif";
		markerOptions = { icon:rankIcon };
		
              var marker = new GMarker(point,markerOptions);
              map.addOverlay(marker);
              marker.bindInfoWindowHtml('<div style="text-align:left;">'+url+'</div>');
              bounds.extend(point);
			  showAllMarkers()
            }
         });
      }
}
function showAllMarkers()
{
	try
	{
	    var center = bounds.getCenter();
	    map.closeInfoWindow();
	    map.setZoom(map.getBoundsZoomLevel(bounds));
	    map.setCenter(center);
	    map.panDirection(0,0.1);
	    return false;
	}     
	catch(err)
	{
	    alert("error occured \n"+err.message);
	}    
}
function opentopOverlay()
{
loadMap();
add_markers();
document.getElementById('light1').style.display='block'
if (navigator.appName == "Microsoft Internet Explorer")
{
	document.getElementById('light1').style.top=document.documentElement.scrollTop+200+'px';
}	
else
{
	document.getElementById('light1').style.top=window.pageYOffset+200+'px';
}
}
