﻿/// <reference path="AjaxHelper.js" />

var icon = new GIcon();
icon.image = "http://labs.google.com/ridefinder/images/mm_20_green.png";
icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
icon.iconSize = new GSize(12, 20);
icon.shadowSize = new GSize(22, 20);
icon.iconAnchor = new GPoint(6, 20);
icon.infoWindowAnchor = new GPoint(5, 1);

var items;

function load(){
		var map = new GMap2(document.getElementById("map"));
		var bounds = new GLatLngBounds();
		//map.addControl(new GSmallMapControl());
		//map.addControl(new GMapTypeControl());
		map.addControl(new TextualZoomControl());
		map.setCenter(new GLatLng(55.22589, -4.092407), 1);
		// This function runs through the Map Names xml file
		GDownloadUrl("/MapData.xml", function(data, responseCode) {
				var xml = GXml.parse(data);
				var markers = xml.documentElement.getElementsByTagName("marker");
				for (var i = 0; i < markers.length; i++) {
					var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")));
					//Assign the variable ICON image file
					var icon2 = new GIcon();
		            icon2.image = "http://www.futuremuseum.co.uk/images/place_names/"+(markers[i].getAttribute("image"));
                    icon2.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
                    icon2.iconSize = new GSize((markers[i].getAttribute("width")),(markers[i].getAttribute("height")));
                    icon2.shadowSize = new GSize(22, 20);
                    icon2.iconAnchor = new GPoint(6, 20);
                    icon2.infoWindowAnchor = new GPoint(5, 1);
                    var thisMarker = CreateMarker2(point,icon2, ("<strong>" + (markers[i].getAttribute("name")) + "</strong>"));
                    
				    //map.addOverlay(thisMarker);
			}
			
			var defZoom = map.getBoundsZoomLevel(bounds);
            if (defZoom > 14) {defZoom=14}; // limit how far we zoom in

            map.setZoom(defZoom);
                
            var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2;
            var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2;
            
            map.setCenter(new GLatLng(clat,clng));
		});
		
		//Map museum points.
		GDownloadUrl("/MapData.xml", function(data, responseCode) {
				var xml = GXml.parse(data);
				var markers = xml.documentElement.getElementsByTagName("marker");
				 items = new Array(markers.length);
				for (var i = 0; i < markers.length; i++) {
						var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")));
						
						var thisMarker = CreateMarker(point, (//"<img scr=\"http://www.futuremuseum.co.uk/fmmain/images/museum_thumbs/" + (markers[i].getAttribute("image")) + "\" alt=\"http://www.futuremuseum.co.uk/images/museum_thumbs/"+(markers[i].getAttribute("image"))+"\" /><br/>" +
								"<a href=\"/Default.aspx?Id="+ (markers[i].getAttribute("link")) +"\" ><strong>" + (markers[i].getAttribute("name")) + "</strong></a>") +
								"<br/>" + (markers[i].getAttribute("address")), markers[i].getAttribute("link"));
                        
						map.addOverlay(thisMarker);
						bounds.extend(point);
			}
			var defZoom = map.getBoundsZoomLevel(bounds);
            if (defZoom > 14) {defZoom=14}; // limit how far we zoom in

            map.setZoom(defZoom);
                
            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 loadExtendedDetails(link)
{
    $('#partner-details').find('div').hide();
    
    if ( link != null && link.length )
    {        
        if ( $('#'+link).length )
            $('#'+link).show();
        else
            callAjax(retrievePartnerDetailsUrl + link, { }, function(data) {showDetails(data)}, errorHandler);

        $('#partner-details').show()
    }
}

function showDetails(response)
{
    $('#partner-details').append(response);
}

// Create a marker whose info window displays the given number.
function CreateMarker(point, message, link) {
		var marker = new GMarker(point, icon);

		// Show this marker's index in the info window when it is clicked.
		GEvent.addListener(marker, 'click', function() {
		    marker.openInfoWindowHtml(message);
		    loadExtendedDetails(link);
		});

return marker;
}

// Create a marker whose info window displays the given number.
function CreateMarker2(point, icon3, message) {
		var marker = new GMarker(point, icon3);

		// Show this marker's index in the info window when it is clicked.
		//GEvent.addListener(marker, 'click', function() {
		//marker.openInfoWindowHtml(message);
		//marker.showMapBlowup(3);
		//});

return marker;
}

// A TextualZoomControl is a GControl that displays textual "Zoom In"
// and "Zoom Out" buttons (as opposed to the iconic buttons used in
// Google Maps).
function TextualZoomControl() {
}
TextualZoomControl.prototype = new GControl();

// Creates a one DIV for each of the buttons and places them in a container
// DIV which is returned as our control element. We add the control to
// to the map container and return the element for the map class to
// position properly.
TextualZoomControl.prototype.initialize = function(map) {
  var container = document.createElement("div");

  var zoomInDiv = document.createElement("div");
  this.setButtonStyle_(zoomInDiv);
  container.appendChild(zoomInDiv);
  zoomInDiv.appendChild(document.createTextNode("Zoom In"));
  GEvent.addDomListener(zoomInDiv, "click", function() {
    map.zoomIn();
  });

  var zoomOutDiv = document.createElement("div");
  this.setButtonStyle_(zoomOutDiv);
  container.appendChild(zoomOutDiv);
  zoomOutDiv.appendChild(document.createTextNode("Zoom Out"));
  GEvent.addDomListener(zoomOutDiv, "click", function() {
    map.zoomOut();
  });

  map.getContainer().appendChild(container);
  return container;
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
TextualZoomControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
}

// Sets the proper CSS for the given button element.
TextualZoomControl.prototype.setButtonStyle_ = function(button) {
  button.style.textDecoration = "underline";
  button.style.color = "#0000cc";
  button.style.backgroundColor = "white";
  button.style.font = "small Arial";
  button.style.border = "1px solid black";
  button.style.padding = "2px";
  button.style.marginBottom = "3px";
  button.style.textAlign = "center";
  button.style.width = "6em";
  button.style.cursor = "pointer";
}


	

  

