var batch = new Array();
var tab1HTML = "Error getting trail info.";
var tab2HTML = "No uses found at this time";
var curMarker;
var calledByPage;
var llat;
var llong;
var prevCenter;


function setupParkingMarkers() {
	mgr.addMarkers(batch, parkingvisibleZoom);
    	mgr.refresh();
	
	}

function GetAllParkingMarkers() {
	if (document.location.href.indexOf('singletrail.aspx') >= 0 )
    	{ calledByPage = 'single'; }
   	 else
    	{ calledByPage = 'main'; }
    	var params = GetEnvelope();
    	TrailService.TrailService.GetParkingWithinEnvelope(params, GetParkingSuccess, GetParkingFailure);

	}

function GetParkingByTRAIL(trailid) {
    	calledByPage = 'single';

    	TrailService.TrailService.GetParkingNearTrails(trailid, GetParkingSuccess, GetParkingFailure);
	}

function GetParkingFailure()
	{ alert(result.get_message()); }

function GetParkingSuccess(result) {
    	var myParkingLocations = result;
    	for (var itemno in myParkingLocations) {
        
        llat = myParkingLocations[itemno].latitude;
        llong = myParkingLocations[itemno].longitude;
        var latlng = new GLatLng(llat, llong);

        
        //marker has to be created in a separate function
	//alert(llat + " : " + llong + " -- " + myParkingLocations[itemno].trailid + " : " + myParkingLocations[itemno].NAME01);
        marker = createMarker(latlng, myParkingLocations[itemno].trailid, myParkingLocations[itemno].NAME01);

	//temporary fix for the single trail page, where certain parking icons do not show.
	if (calledByPage == 'single' && myParkingLocations[itemno].trailid == 233)
		{map.addOverlay(marker);}

        batch.push(marker);

    	}


    	setupParkingMarkers();
    	return batch;

}


function createMarker(point, trailid, trailname) {
    	//var baseIcon = new GIcon(G_DEFAULT_ICON);
    	var parkIcon = new GIcon();
    	parkIcon.image = "images/parking.png";
    	parkIcon.shadow = "";
    	parkIcon.iconSize = new GSize(16, 16);

    	parkIcon.iconAnchor = new GPoint(16, 16);
    	parkIcon.infoWindowAnchor = new GPoint(16, 0);
    	parkIcon.shadow = "";
    	parkIcon.shadowSize = new GSize(0, 0)
    

	//alert(point.lat() + ":" + point.lng());

    	var marker = new GMarker(point, { icon: parkIcon, maxwidth:250 });
    
    	//GEvent.addListener(marker, "click", function()
    	//{ marker.openInfoWindowHtml(html); });
    	if (calledByPage != 'single') {
        	GEvent.addListener(marker, "click", function()
        	{ openFullInfoWindow(marker, trailid, point); });
	
    	}
    	else {

        	GEvent.addListener(marker, "click", function()
        	{ openLimitedInfoWindow(marker, point, trailname); });
    
    	}
    	//add the event listener to reset the map after the window closes
    	GEvent.addListener(marker, "infowindowclose", function(){closeinfoWindow()});
    
    	return marker;
	}


function closeinfoWindow()
	{
		//called to reset the map after closing an info window
		map.panTo(prevCenter);
	}

	//called on the full map page to show all trail info in the popup on the map
function openFullInfoWindow(marker, trailid, point) {

    	//query to get this trails information
    	//we don't load all the trail info into each parking marker, it would be too slow

    		llat = point.lat();
    		llong = point.lng();

    	//capture the current map bounds and zoom level, so when info box is closed we can revert back
    		prevCenter = map.getCenter();

    		parkingiconclicked = true;

    		curMarker = marker;
    		GetTrailByTRAILID(trailid);
	           
	}


	//called on on the single trail report page to show only a link to driving directions
function openLimitedInfoWindow(marker, point, trailname) {

	parkingiconclicked = true;

    	var googURL = 'http://maps.google.com/maps?f=d&source=s_d&saddr=&daddr=' + point.lat() + ',+' + point.lng() + '(' + trailname + ')&hl=en';
    	window.open(googURL, "directions"); 
    
}



function GetTrailByTRAILID(trailid) {
	var params = trailid;
	TrailService.TrailService.GetTrailByID(params, GetTrailByIDSuccess, GetTrailByIDFailure);
	}

function GetTrailByIDFailure()
	{ alert(result.get_message()); }


function GetTrailByIDSuccess(result) {

    //tab1HTML -- holds the html for the general info tab
    //trailUses -- holds the html for the uses tab
    
    var myTrailInfo = result;

    for (var itemno in myTrailInfo) {

        if (myTrailInfo[itemno].NAME01)
        { toLoc = myTrailInfo[itemno].NAME01; }
        else { toLoc = " selected trail "; }

        //TAB 1 Content
        tab1HTML = "<table style='width: 250px;'><tr><td>";
        tab1HTML += toLoc;
        tab1HTML += "<BR /> (" + myTrailInfo[itemno].COUNTY;
        if (myTrailInfo[itemno].COUNTY == "Multiple" || myTrailInfo[itemno].COUNTY.indexOf(',') > -1)
        { tab1HTML += " Counties)"; }
        else
        { tab1HTML += " County)"; }
               
        tab1HTML += "<br/><br/>";
        tab1HTML += "<textarea id=\"TextArea1\" cols=\"25\" name=\"TextArea1\" rows=\"1\">" + myTrailInfo[itemno].DESC + "</textarea>";
        tab1HTML += "<div style='font-family:Tahoma; font-size:10pt'>";
        tab1HTML += "<form action=\"http://maps.google.com/maps\" method=\"get\" target=\"_blank\">";
        tab1HTML += "Enter your starting address:<br />";
        tab1HTML += "<input type=\"text\" name=\"saddr\" style='width:225px;' />";
        tab1HTML += "<input type=\"hidden\" name=\"daddr\" value=\"" + llat + "," + llong + "(" + myTrailInfo[itemno].NAME01 + ")\" /><br/>";
        tab1HTML += "<input type=\"submit\" value=\"get directions\" /></form>";
        tab1HTML += "</div>";
        tab1HTML += "</td></tr></table>";

        //TAB 2 Content
        var tab2HTML = "<table style='width: 250px;'>";
        tab2HTML += "<tr><td class='style18'><img title='Hiking' alt='Hiking' src='images/rec_images/hiking_small.png'";
        tab2HTML += " style='width: 25px; height: 25px' /></td>";
        tab2HTML += "<td padding-left: 3px; padding-right: 3px;>";
        tab2HTML += "<img style='width: 24px; height: 25px' ";
        if (myTrailInfo[itemno].HIKING == "Yes")
        { tab2HTML += "src='images/rec_images/check_green.png'"; }
        else
        { tab2HTML += "src='images/rec_images/x.png'"; }
        tab2HTML += "/></td>";


        tab2HTML += "<td class='style18'>";
        tab2HTML += "<img title='Biking' alt='Biking' src='images/rec_images/biking_small.png'";
        tab2HTML += " style='width:25px; height: 25px' /></td>";
        tab2HTML += "<td>";
        tab2HTML += "<img style='width: 24px; height: 25px' ";
        if (myTrailInfo[itemno].BIKING == "Yes")
        { tab2HTML += "src='images/rec_images/check_green.png'"; }
        else
        { tab2HTML += "src='images/rec_images/x.png'"; }

        tab2HTML += " /></td></tr>";

        tab2HTML += "<tr>";
        tab2HTML += "<td class='style18'>";
        tab2HTML += "<img title='Equestrian' alt='Equestrian' src='images/rec_images/horseback_small.png'";
        tab2HTML += " style='width: 25px; height: 25px' /></td>";
        tab2HTML += "<td>";
        tab2HTML += "<img style='width: 24px; height: 25px' ";
        if (myTrailInfo[itemno].EQUESTRIAN == "Yes")
        { tab2HTML += "src='images/rec_images/check_green.png'"; }
        else
        { tab2HTML += "src='images/rec_images/x.png'"; }
        tab2HTML += " /></td>";


        tab2HTML += "<td class='style18'>";
        tab2HTML += "<img title='Cross Country Skiing' alt='Skiing' src='images/rec_images/ski_small.png'";
        tab2HTML += " style='width: 25px; height: 25px' /></td>";
        tab2HTML += "<td>";
        tab2HTML += "<img style='width: 24px; height: 25px' ";
        if (myTrailInfo[itemno].CROSS_COUN == "Yes")
        { tab2HTML += "src='images/rec_images/check_green.png'"; }
        else
        { tab2HTML += "src='images/rec_images/x.png'"; }
        tab2HTML += " /></td></tr>";


        tab2HTML += "<tr>";
        tab2HTML += "<td class='style18'>";
        tab2HTML += "<img title='Interpretive' src='images/rec_images/interpretive_small.png'";
        tab2HTML += " style='width: 25px; height: 25px' /></td>";
        tab2HTML += "<td>";
        tab2HTML += "<img style='width: 24px; height: 25px' ";
        if (myTrailInfo[itemno].INTERPRETI == "Yes")
        { tab2HTML += "src='images/rec_images/check_green.png'"; }
        else
        { tab2HTML += "src='images/rec_images/x.png'"; }
        tab2HTML += " /></td>";

        tab2HTML += "<td class='style18'>";
        tab2HTML += "<img title='ATV' alt='ATV' src='images/rec_images/atv_small.png'";
        tab2HTML += " style='width: 25px; height: 25px' /></td>";
        tab2HTML += "<td>";
        tab2HTML += "<img style='width: 24px; height: 25px' ";
        if (myTrailInfo[itemno].ATV == "Yes")
        { tab2HTML += "src='images/rec_images/check_green.png'"; }
        else
        { tab2HTML += "src='images/rec_images/x.png'"; }
        tab2HTML += " /></td></tr>";


        tab2HTML += "<tr>";
        tab2HTML += "<td class='style18'>";
        tab2HTML += "<img title='Snowmobile' alt='Snowmobile' src='images/rec_images/snowmobile_small.png'";
        tab2HTML += " style='width: 25px; height: 25px' /></td>";
        tab2HTML += "<td>";
        tab2HTML += "<img style='width: 24px; height: 25px' ";
        if (myTrailInfo[itemno].SNOWMOBILE == "Yes")
        { tab2HTML += "src='images/rec_images/check_green.png'"; }
        else
        { tab2HTML += "src='images/rec_images/x.png'"; }
        tab2HTML += " /></td>";

        tab2HTML += "<td class='style18'>";
        tab2HTML += "<img title='ADA Access' alt='ADA Access' src='images/rec_images/ada_small.png'";
        tab2HTML += " style='width: 25px; height: 25px' /></td>";
        tab2HTML += "<td>";
        tab2HTML += "<img style='width: 24px; height: 25px' ";
        if (myTrailInfo[itemno].ADA_ACCESS == "Yes")
        { tab2HTML += "src='images/rec_images/check_green.png'"; }
        else
        { tab2HTML += "src='images/rec_images/x.png'"; }
        tab2HTML += " /></td></tr>";

        tab2HTML += "<tr>";
        tab2HTML += "<td class='style18'>";
        tab2HTML += "<img title='4 Wheeler' alt='4 Wheeler' src='images/rec_images/4wheeler_small.png'";
        tab2HTML += " style='width: 25px; height: 25px' /></td>";
        tab2HTML += "<td>";
        tab2HTML += "<img style='width: 24px; height: 25px' ";
        if (myTrailInfo[itemno].FOUR_WHEEL == "Yes")
        { tab2HTML += "src='images/rec_images/check_green.png'"; }
        else
        { tab2HTML += "src='images/rec_images/x.png'"; }
        tab2HTML += " /></td>";

        tab2HTML += "<td class='style18'>";
        tab2HTML += "<img title='Off Road Motorcycle' alt='Off Road Motorcycle' src='images/rec_images/motorbike_small.png'";
        tab2HTML += " style='width: 25px; height: 25px' /></td>";
        tab2HTML += "<td>";
        tab2HTML += "<img style='width: 24px; height: 25px' ";
        if (myTrailInfo[itemno].OFF_ROAD_M == "Yes")
        { tab2HTML += "src='images/rec_images/check_green.png'"; }
        else
        { tab2HTML += "src='images/rec_images/x.png'"; }
        tab2HTML += " /></td></tr>";
        tab2HTML += "</table>";
        
        var infoTabs = [
        new GInfoWindowTab("General Info", tab1HTML),
        new GInfoWindowTab("Trail Uses", tab2HTML)
        ];
        curMarker.openInfoWindowTabsHtml(infoTabs);

    }
        
}

 function sleep(naptime){
         naptime = naptime * 1000;
         var sleeping = true;
         var now = new Date();
         var alarm;
         var startingMSeconds = now.getTime();
         //alert("starting nap at timestamp: " + startingMSeconds + "\nWill sleep for: " + naptime + " ms");
         while(sleeping){
             alarm = new Date();
             alarmMSeconds = alarm.getTime();
             if(alarmMSeconds - startingMSeconds > naptime){ sleeping = false; }
         }        
         //alert("Wakeup!");
     }






