var GroupID;
var map = null;
var startLat = 51.1334;
var startLng = -3.63782;
var zoomlevel = 10;
var maptype = 0;
var TypeID = 0;
var GroupID = 0;
var ActivityID = 0;
var TownLat = 0;
var TownLng = 0;
var radius = 0;
var smallicon = "/graphics/map_marker.png";
var largeicon = "/graphics/map_marker_large.png";

function initialize(MyActivityID)
{
	ActivityID = MyActivityID;
	setActivity();
}

function setActivity()
{
	GroupID = $(".GroupDD").val();
	$(".ActivityDD > option").remove();
     $(".ActivityDD").append(
     	$('<option></option>').val(0).html("Filter results by activity")
     );
	 $(".ActivityDD").append(
     	$('<option></option>').val(0).html("Show all")
     );
	 for (var i = 0; i < activitydata.activity.length; i++) {
	 	if (GroupID > 0) {
			if ((GroupID == activitydata.activity[i].GroupID) || (activitydata.activity[i].GroupID == 0))
			{
				$(".ActivityDD").append(
	                 $('<option></option>').val(activitydata.activity[i].ActivityID).html(activitydata.activity[i].ActivityName)
	             );
			}
		} else
		{
			$(".ActivityDD").append(
                 $('<option></option>').val(activitydata.activity[i].ActivityID).html(activitydata.activity[i].ActivityName)
             );
		}
	 }
	 $(".ActivityDD").val(ActivityID);
	 $(".HidActivity").val($(".ActivityDD").val());
}

function addmarkers(MyTypeID, MyGroupID, MyActivityID, MyRadius, MyTownLat, MyTownLng) {
	TypeID = MyTypeID;
	GroupID = MyGroupID;
	ActivityID = MyActivityID;
	TownLat = MyTownLat;
	TownLng = MyTownLng;
	radius = MyRadius;
	
	var markers = [];
	for (var i = 0; i < data.properties.length; i++) {
		var mymarker;
		mymarker = addmarkertolist(data.properties[i].latitude, data.properties[i].longitude, data.properties[i].AdvertID);
		markers.push(mymarker);
		zoomlevel = 10;
	}
	
	var mapTypeControlvar = true;
	var streetViewControlvar = true;
	if (maptype == 1)
	{
		mapTypeControlvar = false;
		streetViewControlvar = false;
		
	}
	var latlng = new google.maps.LatLng(startLat, startLng);
	var myOptions = {
		zoom: zoomlevel-maptype,
		center: latlng,
		mapTypeControl: mapTypeControlvar,
		streetViewControl: streetViewControlvar,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		scrollwheel: false
	};
	map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
	
	var styles = [[{
       url: largeicon,
       height: 24,
       width: 24,
       opt_anchor: [0, 0],
       textColor: '#FFFFFF'
   }]];

   var mcOptions = { gridSize: 24, maxZoom: 16, styles: styles[0] };
   var markerCluster = new MarkerClusterer(map, markers, mcOptions);
	
}

function addmarkertolist(Lat, Lng, AdvertID) {
    var myLatlng = new google.maps.LatLng(Lat, Lng);
    var marker = new google.maps.Marker({
        position: myLatlng,
        title: "",
		icon: smallicon
    });
    var AdID = AdvertID;
    var func = openWindow(AdID, marker)
    google.maps.event.addListener(marker, 'click', func);
    return marker;
}

function openWindow(markerID, marker)
{
    return function () {
        var contentString;
        var request = null;
		$(".infoBox").hide();
        request = jQuery.ajax({
            type: "GET",
            url: "/advertinfo.aspx?AdvertID=" + markerID,
            cache: false,
            success: function (html) {

                var myOptions = {
                    content: html
                    , alignBottom: true
                    , closeBoxMargin: "5px 8px 2px 2px"
                    , pixelOffset: new google.maps.Size(0, -20)
                };

                var ib = new InfoBox(myOptions);

                var infowindow = new google.maps.InfoWindow({ content: html });
                ib.open(map, marker);
            }
        });
    }
}

function singlepointmap(AdLat, AdLng)
{
	zoomlevel = 12;
	var mapTypeControlvar = true;
	var streetViewControlvar = true;
	if (maptype == 1)
	{
		mapTypeControlvar = false;
		streetViewControlvar = false;
		
	}
	var latlng = new google.maps.LatLng(AdLat, AdLng);
	var myOptions = {
		zoom: zoomlevel-maptype,
		center: latlng,
		mapTypeControl: mapTypeControlvar,
		streetViewControl: streetViewControlvar,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		scrollwheel: false
	};
	map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
	
	var marker = new google.maps.Marker({
	      position: latlng, 
	      map: map,
		  icon: smallicon
	  }); 
}

function ddchanger()
{
	setActivity();
}

function ddchangerA()
{
	$(".HidActivity").val($(".ActivityDD").val());
}

