var osEnabled = false;

function getMapAdverts(method)
{
	if (enableAdverts)
	{
		// Get map extents.
		var bounds = map.GetMapView();
		var category = $('#routeSearchTabPanel').find('.advertSearchCategory').val();
		var name = $('#routeSearchTabPanel').find('.advertSearchName').val();

		method = method == "search" ? "searchAdverts" : "getMapAdverts";

		// Build URL to request advert JSON feed from.
		var url = "/AdvertProvider.ashx"; //?method=" + method;
		var json = json = { method: method, topLeft: bounds.TopLeftLatLong.Latitude + "," + bounds.TopLeftLatLong.Longitude, bottomRight: bounds.BottomRightLatLong.Latitude + "," + bounds.BottomRightLatLong.Longitude, categories: getSelectedSections() };

		if (method == "searchAdverts") {
		    json = { method: method, topLeft: bounds.TopLeftLatLong.Latitude + "," + bounds.TopLeftLatLong.Longitude, bottomRight: bounds.BottomRightLatLong.Latitude + "," + bounds.BottomRightLatLong.Longitude, categories: getSelectedSections(), category: encodeURI(category), name: encodeURI(name) };
		}

		// Send JSON request for new adverts and pass to updateMapAdverts
		$.post(url, json, updateMapAdverts, "json");
	}
}

function getMapAdvert(id) 
{
    if (enableAdverts) {
        method = "getMapAdvert";

        // Build URL to request advert JSON feed from.
        var url = "/AdvertProvider.ashx"; //?method=" + method;
        var json = json = { method: method, id: id };

        enableAdverts = false;
        // Send JSON request for new adverts and pass to updateMapAdverts
        $.post(url, json, updateMapAdverts, "json");
    }
}

function getWaypoints(rideID)
{
	// Build URL to request advert JSON feed from.
	var url = "/AdvertProvider.ashx?method=getWaypoints";
	url += "&routeId=" + rideID;

	// Send JSON request for ride waypoints and pass to updateWaypoints
	jQuery.getJSON(url, updateWaypoints);
}
var inSearch = false;
function searchAdverts(element) {
    
    var location = $(element).find('.advertSearchLocation').val();
    inSearch = true;
    resetSelectedAdverts();
    if (location != null && location != "") {
	    locationSearch(location);
	}
	else {
	    searchType = "search";
	    enableAdverts = true;
	    getMapAdverts(searchType);
	}
}

function searchRoutes(element)
{
	var name = $(element).find('.routeSearchName').val();
	var location = $(element).find('.routeSearchLocation').val();

	doSearch(name, location);
}

function routeSearchResults(routeIds)
{
	if (routeIds.length > 0) {
		var url = "/AdvertProvider.ashx?method=registerSearchResults&routes=" + routeIds.join(',');
		jQuery.ajax({ async: false, cache: false, type: 'GET', url: url });
	}
}

function trackAdvert(id)
{
	var url = "/AdvertProvider.ashx?method=trackImpression&id=" + id;
	jQuery.get(url);
}

function changeRoute(routeId)
{
	window.location.href = routeUrl.replace('$route', routeId);
}

function getRideDetails(id)
{
}

function changeZoom(level)
{
	if (level == "street")
	{
		toggleOSMaps(false);
		map.SetMapStyle(VEMapStyle.Road);
	}
	else if (level == "aerial")
	{
		toggleOSMaps(false);
		map.SetMapStyle(VEMapStyle.Aerial);
	}
	else
	{
		var z = map.GetZoomLevel();
		if (z != 14)
			map.SetZoomLevel(14);

		toggleOSMaps(true);
	}

	return true;
}

function enableOSTiles(bool)
{
	if (bool)
	{
		osEnabled = true;
	}
	else
	{
		osEnabled = false;
	}
}

function toggleOSMaps(value)
{
	var z = map.GetZoomLevel();
	if (value && (z >= 14 && z <= 16))
	{
		map.ShowTileLayer("os25k");
	} else
	{
		map.HideTileLayer("os25k");
	}
}

function staticMap(bool)
{
	if (bool)
	{
		map.AttachEvent("ondoubleclick", ondoubleclickEvent);
		map.AttachEvent("onmousedown", onmousedownEvent);
		map.AttachEvent("onmousewheel", onmousewheelEvent);
		map.HideDashboard();
	}
	else
	{
		map.DetachEvent("ondoubleclick", ondoubleclickEvent);
		map.DetachEvent("onmousedown", onmousedownEvent);
		map.DetachEvent("onmousewheel", onmousewheelEvent);
		map.ShowDashboard();
	}
}

