//<![CDATA[

// Copyright (c) 2006, Laura J Pilates

var map;

var gmarkers = [];
var htmls = [];
var ivenues = 0;

var nelat, nelng, swlat, swlng;

var czoom = 11;
var centre = new GLatLng(51.673407, -1.562119);
var bounds;

var baseIcon = new GIcon();

var c = 0;
var w = 0;

function getData( xmlEl, alttxt )
{
  try
  {
    return xmlEl.firstChild.data;
  }
  catch (er)
  {
    return alttxt;
  }
}

// This function picks up the click and opens the corresponding info window
function myclick(i) {
  gmarkers[i].openInfoWindowHtml(htmls[i]);
}
      
// This function is invoked when the mouse goes over an entry in the sidebar
// It deletes the cold Icon marker and replaces it with the hot Icon marker      
function classover(i) {
  gmarkers[i].openInfoWindowHtml(htmls[i]);
}
// This function is invoked when the mouse leaves an entry in the sidebar
// It deletes the hot Icon marker and replaces it with the cold Icon marker      
function classout(i) {
}

function venueinfo( venue, lat, lng ) {
    var nbsp = '';
    var addr = venue.getAttribute( "addr" );
    var url = venue.getAttribute( "url" );
    
    xt = venue.getElementsByTagName("town");
    xv = venue.getElementsByTagName("venue");
    nbsp = getData( xt[0], '' ) + '&nbsp;'+ getData( xv[0], '' );
    nbsp = nbsp.replace(/ /g, '&nbsp;');

    var times = '';

    dx = venue.getElementsByTagName("date");
    for (j=0;j<dx.length;j++)
    {
      cltype = dx[j].getAttribute( "cltype" );
      if( cltype == "c" )
	c = 1;	  
      else if( cltype == "w" )
	w = 1;	  

      if( j > 0 )
      {
	times += '</td></tr><tr><td>';
      }
      try
      {
	var cltimes = dx[j].firstChild.data;
	cltimes = cltimes.replace(/, /g, '</td><td>').replace(/: /g, '</td><td>');
	times += cltimes;
      }
      catch (er)
      {
      }
    }

    if( ivenues == 0 ) {
	bounds = new GLatLngBounds( new GLatLng( lat, lng ), new GLatLng( lat, lng ) );
    }
    else {
        bounds.extend( new GLatLng( lat, lng ) );
    }
    
    var html = '<div class="mapinfo">';
    html += 'Venue: ';
    if( url != "" )
	html += '<a href="http://'+url+'">';
    html += '<b>' + nbsp + '</b>';
    if( url != "" )
        html += '</a>';
    html += '<br/>';

    html += '<table style="width: 26em; text-align: left; margin-left: auto; margin-right: auto;" border="0" cellpadding="0" cellspacing="2">';
    html += '<tbody>';
    html += '<tr>';
    html += '<td rowspan="3">Times:</td>';

    html += '<td>' + times + '</td></tr>';
    
    html += '</tbody></table>';

    if( addr != "" )
	html += 'Address: <a href="javascript:map.setCenter(new GLatLng('+lat+','+lng+'),15)">'+addr+'</a><br/>';

    html += '<div class="return"><a href="javascript:setScale()">Reset map</a></div>';

    html += '</div>';

    return html;
}

// A function to create the marker and set up the event window
function createMarker( point,venuename,html ) { // also included name in arg list
    var icon = new GIcon(baseIcon);
    if( w == 1 ) {
      icon.shadow = "../images/WorkshopGIs.png";
      icon.iconAnchor = new GPoint(0, 29);
      icon.image = "../images/WorkshopGI.png";
    }
    else if( c == 1 ){
      icon.iconAnchor = new GPoint(24, 29);
      icon.image = "../images/CourseGI.png";
      icon.shadow = "../images/CourseGIs.png";
    }

    var marker = new GMarker(point, {icon:icon, title:venuename});

    GEvent.addListener(marker, "click", function() {
	marker.openInfoWindowHtml(html);
    });

    // save the info we need to use later for the sidebar
    gmarkers[ivenues] = marker;
    htmls[ivenues] = html;
    ivenues++;
    
    return marker;
}

function setScale() {
    map.closeInfoWindow();

    var sw = bounds.getSouthWest();
    var ne = bounds.getNorthEast();
    centre = new GLatLng( ( ne.lat() + sw.lat() )/2, ( ne.lng() + sw.lng() )/2 );

    czoom = map.getBoundsZoomLevel( bounds );
    if( czoom > 11 )
	var czoom = 11;
    map.setCenter( centre, czoom );
}

function load() {
    if (GBrowserIsCompatible()) {
        mapObj = document.getElementById("map");

	mapObj.style.height = 32 + "em";

	map = new GMap2(mapObj);
	map.addControl(new GSmallMapControl());
	map.addControl(new GScaleControl());
//	map.addControl(new GMapTypeControl());
	map.setCenter(centre, czoom);

	map.enableDoubleClickZoom();
	map.enableContinuousZoom();

//	GMap2.prototype.wheelZoom = function(event)
//	   { if((event.detail || -event.wheelDelta) < 0)
//	      { map.zoomIn();}
//	     else
//	      { map.zoomOut();}
//	     return false;
//	   }
//	 GEvent.addDomListener(mapObj, "DOMMouseScroll", map.wheelZoom);
//	 GEvent.addDomListener(mapObj, "mousewheel", map.wheelZoom);

	// Create a base icon for all of our markers that specifies the
	// shadow, icon dimensions, etc.
	baseIcon.iconSize = new GSize(24, 36);
//	baseIcon.shadowSize = new GSize(48, 36);
	baseIcon.infoWindowAnchor = new GPoint(12, 4);

	// Read the data from ljvenues.xml
	var request = GXmlHttp.create();

	request.open("GET", "ljvenues.xml", true);
	request.onreadystatechange = function() {
	    if (request.readyState == 4) {
		var xmlDoc = request.responseXML;
		// obtain the array of markers and loop through it
		var markers = xmlDoc.documentElement.getElementsByTagName("marker");
          
		for (var i = 0; i < markers.length; i++) {
		    c = w = 0;
		    // obtain the attribues of each marker
		    var lat = parseFloat(markers[i].getAttribute("lat"));
		    var lng = parseFloat(markers[i].getAttribute("lng"));
		    var point = new GLatLng(lat,lng);
		    var html = venueinfo( markers[i], lat, lng );
		    xt = markers[i].getElementsByTagName("town");
		    xv = markers[i].getElementsByTagName("venue");
		    var venuename = getData( xt[0], '' ) + ' '+ getData( xv[0], '' );

		    // create the marker
		    var marker = createMarker(point, venuename, html ); // Also passed label in arg list
		    map.addOverlay(marker);
		}
		setScale();
	    }
	}
	// put the assembled sidebar_html contents into the sidebar div
	request.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
	request.send(null);
    }
    else {
	alert("Sorry, the Google Maps API is not compatible with this browser");
    }
}

//]]>
