//<![CDATA[


// uses latest version 2.0 of google maps API

var map = null;				// the Google map GMap object
var mapDIV = null;			// the map container
var point_id = 0;			// for generating point ids
var my_points = new Array();
var my_iconArray = new Array(3);	
var my_icon = null;			// an icon for the markers

// Click Handling
function clickHandler(overlay, point) {
	if (overlay) {
		var markerStr = overlay.myhtml;
		if (markerStr.length > 0){
		overlay.openInfoWindowHtml(markerStr);
		}
	}
}


function initMap(container) { 

	map = new GMap(container);
	my_icon = makeIcon();
	
	my_iconArray[1] = makeIcon(1);
	my_iconArray[2] = makeIcon(2);
	my_iconArray[3] = makeIcon(3);
	
	map.addControl(new GLargeMapControl()); // GLargeMapControl | GSmallMapControl
	// map.addControl(new GMapTypeControl()); // turned off as no decent hybrid maps
	map.setCenter(new GLatLng(56.56033,-2.59843), 13);
	map.setMapType(G_NORMAL_MAP); // G_NORMAL_MAP | G_SATELLITE_MAP | G_HYBRID_MAP
	

	// disable click handler or now
	GEvent.addListener(map, 'click', clickHandler);
	
    // debugging options:
    /*
     GLog.write('enter initMap()');
    GEvent.addListener(map,'moveend',function(){GLog.write('moveend: '+ map.getCenter().toString())});
    GEvent.addListener(map,'zoom',function(){GLog.write('zoom: ' + map.getZoom().toString())});
    GEvent.addListener(map,'maptypechanged',function(){GLog.write('maptypechanged: ' + map.getCurrentMapType().getName())});
    */	
    // end debugs	
    
	loadXML()
}

function initPage() {
	var mapDiv = document.getElementById("mapDiv");
	if (!GBrowserIsCompatible()) {
		mapDiv.innerHTML = '<img src="images/bd-map.jpg">';
	} else {
		initMap(mapDiv);
	}
}

// Create custom icons based on values in the XML
function makeIcon(_iconType) {
	var icon = new GIcon();
	switch (_iconType){
		case 1: // hotels
			icon.image = "/images/gmap_hotel.gif";
			icon.iconSize = new GSize(16, 20);
			icon.iconAnchor = new GPoint(8, 20);
			icon.infoWindowAnchor = new GPoint(8, 3);
			break;
		
		case 2: // venues - eventually :O)
			icon.image = "/images/gmap_camping.gif";
			icon.iconSize = new GSize(16, 20);
			icon.iconAnchor = new GPoint(8, 20);
			icon.infoWindowAnchor = new GPoint(8, 3);
			break;
			
		case 3: // caravan & camping
			icon.image = "/images/gmap_camping.gif";
			icon.iconSize = new GSize(16, 20);
			icon.iconAnchor = new GPoint(8, 20);
			icon.infoWindowAnchor = new GPoint(8, 3);
			break;
	}
	
	
	return icon;
}

// Make a marker with our extra attributes
function makeMarker(point, _title, _iconType, _content, _id) {
	var marker = new GMarker(point, my_iconArray[_iconType]);
	marker.my_id = _id;
	// add custom HTML content property
	marker.myhtml = _content
	my_points.push(marker);
	return marker;
}

// not the best wau to do this, have to loop through the my_points array
// until we find the marker with the correct id
// no other way to do it without knowing the correct array index
function showOverlay(_id) {
	for (var m = 1; m < my_points.length; m++) {
	    if (my_points[m].my_id == _id){
	    my_points[m].openInfoWindowHtml(my_points[m].myhtml);
	    }
	    //GLog.write('myid: ' + my_points[m].my_id) 
	}

}


// load an XML document containing markers
// **** be careful - xml vars are case sensitive ****
function loadXML() {

	var request = GXmlHttp.create();
	var url = "/travel/data.aspx";
	request.open("GET", url, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			var xmlDoc = request.responseXML;
			
			//markers contains array of accom nodes
			var markers = xmlDoc.documentElement.getElementsByTagName("accom");

            // based very loosly on something from 
			// http://www.quirksmode.org/dom/importxml.html
			// (I got the childNodes[j].nodeName bit from there - was driving me crazy)
			var thisEval = '';
			for (var m = 0; m < markers.length; m++) {
			
			    // load xml vars
			    for (j=0;j<markers[m].childNodes.length;j++){
			        if (markers[m].childNodes[j].nodeType != 1) continue;

                    // check if value exists for nodename			    
                    if( markers[m].childNodes[j].firstChild){
                        thisEval = 'var ' + markers[m].childNodes[j].nodeName + ' = "' + markers[m].childNodes[j].firstChild.nodeValue + '";';
                    }
                    else{
                        thisEval = 'var ' + markers[m].childNodes[j].nodeName + ' = "";'
                    }
                    
                    eval(thisEval);
  
               } // load vars
               
               var content = itemName;
               if (address1.length > 0) {content += '<br>' + address1}
               if (address2.length > 0) {content += '<br>' + address2}
               if (tel.length > 0)      {content += '<br><strong>tel:</strong> ' + tel}
               if (fax.length > 0)      {content += '<br><strong>fax:</strong> ' + fax}
               if (website.length > 0)  {content += '<br><strong>w:</strong> <a href="' + website + '" target="_blank">website</a>'}
               if (email.length > 0)    {content += '<br><strong>e:</strong> <a href="mailto:' + email + '">' + email + '</a>'}
               
               // GLog.write('adding: ' + itemName);
               
               var point = new GLatLng(parseFloat(Lat), parseFloat(Lng) );
										
                var marker		= makeMarker(point, itemName, TypeID, content, id);
                map.addOverlay(marker);
				
            } // loop i
			
		}
	}
	request.send(null);
}

//]]>
