var AJAX_REQUEST = "RideOxfordAJAX.aspx";
var GMapObj = null;
var PlaceMarkerIcons = null;
var GallerySlideViewer = {Viewer:null, LastIndex: null};
var map = null;
var list = null;

function Start_GMap()
{		
	if (GBrowserIsCompatible()) 
	{				
		GMapObj = new GMap2(document.getElementById("GMap"));
		GMapObj.addControl(new GLargeMapControl());		
		GMapObj.addControl(new GMapTypeControl());
		
		GMapObj.setCenter(new GLatLng(43.109235666,-80.768820842), 10);
		
		// Enable wheel scrolling + disable whole page mouse wheel scrolling except within the map
		GMapObj.enableScrollWheelZoom();
		GMapObj.enableContinuousZoom();

		if (GMapObj.addEventListener)
			GMapObj.addEventListener("DOMMouseScroll", function(e){e.preventDefault();}, false);
		else if (GMapObj.attachEvent)
			GMapObj.attachEvent("onmousewheel", function(){event.returnValue = false;});	
			
		GMapObj.addOverlay(new GGeoXml("http://www.rideoxford.ca/kml/routes_limits.kml"));				
	}			
}

// Folder Clicked
function FI_OnClick(e)
{
	var obj = e.srcElement || e.target;
	
	if (obj.nodeName == "A")
	{		
		//Check for Motorcycling and Cycling click.
		if(obj.getAttribute("findex") == 692)
		{
			$("GMapCell").style.display="none";		
			$("rideOxfordHome").style.display="none";
			$("rideOxfordListView").style.display="none";
			$("rideOxfordListDetails").style.display="none";				
			$("rideOxfordContactPage").style.display="block";
		}
		else
		{
			// Change display and AJAX for the placemarkers
			HandleAjaxProgress("SHOWPROGRESS");
			
			var ajaxOptions = {
				method: 		'get',
				onComplete: 	HandleAjaxResponse, 
				onFailure: 		HandleAjaxResponse
			};
						
			//var folderHeirarchy = [];
					
			// Cycle thru the findexes to compose the heirarchy
			//while(obj.getAttribute("findex") != null)
			//{
			//	folderHeirarchy.unshift("Folder[position()=" + obj.getAttribute("findex") + "]");
			//	obj = obj.parentNode;
			//}		
				
			var url = AJAX_REQUEST + "?cat=" + obj.getAttribute("findex");		
			new Ajax(url, ajaxOptions).request();
			
			//If both the map and list view are hidden when the user clicks on the menu, show the map view.
			if (document.getElementById)
			{
				map = document.getElementById("GMapCell");
				list = document.getElementById("rideOxfordListView");
				if (map.style.display == "none" && list.style.display == "none")
				{		
					//Show/hide the appropriate divs.			
					$("GMapCell").style.display="block";
					GMapObj.checkResize();
					GMapObj.setCenter(new GLatLng(43.131847,-80.760955), 10);
					$("rideOxfordHome").style.display="none";
					$("rideOxfordListView").style.display="none";
					$("rideOxfordListDetails").style.display="none";				
					$("rideOxfordContactPage").style.display="none";			
				}
			}
		}
	}
	StopEvent(e);
}

function PI_OnClick(e)
{
	var obj = e.srcElement || e.target;
	
	if (obj.nodeName == "A")
	{	
		GEvent.trigger(ResponseData.PlaceMarkers[obj.getAttribute("index")].Marker,"click") 
	}
	StopEvent(e);
}

function Back_OnClick(e)
{
	HandleAjaxProgress("BACK");
}

function ShowMarkers(e,skipCenterZoom)
{
	GMapObj.clearOverlays();
	GMapObj.addOverlay(new GGeoXml("http://www.rideoxford.ca/kml/routes_limits.kml"));		

	if (ResponseData.PlaceMarkers.length > 0)
	{
		var myBounds = new GLatLngBounds();
		//var pmCount = PlaceMarkerIcons.length;		
		var pmCount = 0;		
		var markerArray = [];
		
		for (var i = 0, cnt = ResponseData.PlaceMarkers.length; i < cnt; i++)
		{
			var cnt2= 0, iconFile = "";
			var pmObj = ResponseData.PlaceMarkers[i];
					
			// Get the marker icon
			if (pmCount > 0 && i < pmCount)
				iconFile = PlaceMarkerIcons[i];
			else
			{
				cnt2 = i % 47;
				iconFile = "img/iconb" + (cnt2 < 26 ? String.fromCharCode(cnt2+97) : (cnt2 - 25)) + ".png";
			}				

			// Create the marker			
			var icon = new GIcon(G_DEFAULT_ICON, iconFile);
			//var icon = new GIcon(G_DEFAULT_ICON);
			var markerLatLng = new GLatLng(parseFloat(pmObj.Lat),parseFloat(pmObj.Lng));
			var marker = new GMarker(markerLatLng, {icon:icon, title:pmObj.Name});

			// Store the marker
			pmObj.Marker = marker;
			
			// Store the latlng of the marker to compose the bounds
			myBounds.extend(markerLatLng);
			
			// Create the tabs
			marker.index = i;
			marker.bindInfoWindowTabs(BuildTabs(pmObj), {selectedTab:0, maxWidth:350});
			
			GEvent.addListener(marker, "infowindowopen", 
			function(marker, point)
			{ 
				if (GallerySlideViewer.LastIndex != marker.index && ResponseData.PlaceMarkers[marker.index].Gallery.length > 0)
				{
					GallerySlideViewer.Viewer = null;
					GallerySlideViewer.Viewer = new PhotoViewer();
					
					with (GallerySlideViewer)
					{
						var go = ResponseData.PlaceMarkers[marker.index].Gallery;
						for (var i = 0, cnt = go.length; i < cnt; i++)
							Viewer.add(go[i].FullImage);
					}
					
					GallerySlideViewer.LastIndex = marker.index;
				}
				
				if (ResponseData.PlaceMarkers[marker.index].Description.length > 0)
				{
					$("rideOxfordListDetails").innerHTML = ResponseData.PlaceMarkers[marker.index].Description;
				}
			}); 			

			// Store the marker for later adding to the manager
			GMapObj.addOverlay(marker);			
		}

		//if (!skipCenterZoom)
		//	centerAndZoomOnBounds(GMapObj, myBounds);		
			
	}
}

function HandleAjaxProgress(type)
{
	// Hide the standard SidePanel and show the AjaxPanel
	if (type == "SHOWPROGRESS")
	{
		$("SidePanel").style.display="none";
		$("AjaxSidePanel").style.display="block";
		ProgressHtml = $("AjaxProgress").innerHTML;
	}
	else if (type == "HIDEPROGRESS")
	{
		$("AjaxProgress").style.display = "none";		
	}	
	else if (type == "BACK")
	{
		//Clear the overlays and selections.
		GMapObj.closeInfoWindow();	
		GMapObj.clearOverlays();
		GMapObj.addOverlay(new GGeoXml("http://www.rideoxford.ca/kml/routes_limits.kml"));
		GMapObj.setCenter(new GLatLng(43.131847,-80.760955), 10);			
		$("AjaxSidePanel").style.display="none";
		$("SidePanel").style.display="block";
		$("PlaceMarkerContent").innerHTML = "";	
		$("rideOxfordListDetails").innerHTML = "";
		$("rideOxfordListView").innerHTML = "";
		$("AjaxProgress").innerHTML = ProgressHtml;	
		$("AjaxProgress").style.display = "block";
		$("SidePanel").focus();
	}	
	else if (type == "AJAXERROR")
	{
		$("AjaxProgress").innerHTML = "There was a problem retrieving the information.<br>Please try again later.";
	}	
}

function HandleAjaxResponse(respData)
{
	if (this.transport.status == 200)	
	{
		HandleAjaxProgress("HIDEPROGRESS");
		
		if (/^{/.test(this.response.text))
		{
			eval("ResponseData = " + this.response.text);
		
			if (typeof(ResponseData) == "object")
			{
				$("PlaceMarkerContent").innerHTML = ResponseData.Display;
				//Testing
				$("rideOxfordListView").innerHTML = ResponseData.ListDisplay;
				setTimeout(ShowMarkers,200);
			}
		}
		else
		{
			$("PlaceMarkerContent").innerHTML = "<div class='NoMatch'>There were no matching items for this catgeory.</div>";
		}	
	}
	else
		HandleAjaxProgress("AJAXERROR");	
}

function BuildTabs(orgItem)
{
	var tabs = [], data = "";

	// Build the tabs for the display

	// Details
	//var GetDirectionsStr = "<div class='DirectionsText' id='d_"+orgItem.Marker.index+"'>Get Directions: <a href='javascript:void(0)' onclick='GetDirections(1,1,"+orgItem.Marker.index+")'>To Here</a> &bull; <a href='javascript:void(0)' onclick='GetDirections(1,2,"+orgItem.Marker.index+")'>From Here</a></div>";	
	//var tab = new GInfoWindowTab("Details", orgItem.Description + GetDirectionsStr);
	var tab = new GInfoWindowTab("Details", orgItem.Description);
	tabs.push(tab);

	// Photos
	//if (orgItem.Gallery.length > 0)
	//{
	//	var images = [];
	
	//	for (var i = 0, cnt = orgItem.Gallery.length; i < cnt; i++)
	//	{
	//		var gallery = orgItem.Gallery[i]
	//		images.push("<a href='#' index="+i+"><img src='"+gallery.Thumbnail+"'></a> ");
	//	}
			
	//	tabs.push(new GInfoWindowTab("Photos", "<table cellspacing=\"0\" cellpadding=\"0\" class=\"GMapTable_Photos\"><tbody><tr><td class=\"Name\">"+orgItem.Name+"</td></tr><tr><td class=\"MediaInfo\">"+images.length+" Photo"+(images.length > 2 ? "s" : "")+"</td></tr><tr><td><div class='Gallery' onclick='GI_OnClick(event)'>"+images.join("")+"</div></td></tr></tbody></table>"));
	//}
		
	return tabs;
}

function StopEvent(e)
{
	e.cancelBubble = true;
	if (e.preventDefault) e.preventDefault();
}

function centerAndZoomOnBounds(map, bounds)
{
	map.setZoom(map.getBoundsZoomLevel(bounds));

	var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
	var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
	map.setCenter(new GLatLng(clat,clng));
}

function list_OnClick(e)
{
	var obj = e.srcElement || e.target;
	
	if (obj.nodeName == "A")
	{		
		//Show/hide the appropriate divs.
		$("GMapCell").style.display="none";
		$("rideOxfordHome").style.display="none";
		$("rideOxfordListView").style.display="block";
		$("rideOxfordListDetails").style.display="block";				
		$("rideOxfordContactPage").style.display="none";
	}
	StopEvent(e);
}

function map_OnClick(e)
{
	var obj = e.srcElement || e.target;
	
	if (obj.nodeName == "A")
	{		
		//Show/hide the appropriate divs.			
		$("GMapCell").style.display="block";
		GMapObj.checkResize();
		GMapObj.setCenter(new GLatLng(43.131847,-80.760955), 10);
		$("rideOxfordHome").style.display="none";
		$("rideOxfordListView").style.display="none";
		$("rideOxfordListDetails").style.display="none";				
		$("rideOxfordContactPage").style.display="none";
	}
	StopEvent(e);
}

function home_OnClick(e)
{
	var obj = e.srcElement || e.target;
	
	if (obj.nodeName == "A")
	{		
		//Show/hide the appropriate divs.			
		$("GMapCell").style.display="none";		
		$("rideOxfordHome").style.display="block";
		$("rideOxfordListView").style.display="none";
		$("rideOxfordListDetails").style.display="none";
		$("rideOxfordContactPage").style.display="none";				
	}
	StopEvent(e);
}

function contact_OnClick(e)
{
	var obj = e.srcElement || e.target;
	
	if (obj.nodeName == "A")
	{		
		//Show/hide the appropriate divs.			
		$("GMapCell").style.display="none";		
		$("rideOxfordHome").style.display="none";
		$("rideOxfordListView").style.display="none";
		$("rideOxfordListDetails").style.display="none";				
		$("rideOxfordContactPage").style.display="block";
	}
	StopEvent(e);
}