/* Copyright (c) 2006-2007 MetaCarta, Inc., published under the BSD license.
 * See http://svn.openlayers.org/trunk/openlayers/release-license.txt 
 * for the full text of the license. */


/**
 * @requires OpenLayers/Layer.js
 * 
 * Class: OpenLayers.Layer.Markers
 * 
 * Inherits from:
 *  - <OpenLayers.Layer> 
 */
OpenLayers.Layer.LookupLayer = OpenLayers.Class.create();
OpenLayers.Layer.LookupLayer.prototype = 
  OpenLayers.Class.inherit( OpenLayers.Layer.Markers, {

    /** store url of text file - this should be specified in the 
     *   "options" hashtable
    * @type str */
    location:null,

    /** @type Array(OpenLayers.Feature) */
    features: null,

    /** @type OpenLayers.Feature */
    selectedFeature: null,

    /**
    * @constructor
    *
    * @param {String} name
    * @param {String} location
    * @param {Object} options Hashtable of extra options to tag onto the layer
    */
    //initialize: function(name, options) {

      //  OpenLayers.Layer.Markers.prototype.initialize.apply(this, arguments);
        //this.features = new Array();
        //if (this.location != null) {
			
          //  OpenLayers.loadURL(this.location, null, this, this.parseData);
        //}
    //},
    initialize: function(name, lookup_results, options) 
	{

        OpenLayers.Layer.Markers.prototype.initialize.apply(this, arguments);

        this.features = new Array();
      	this.loadObjects(lookup_results);
		this.displayInLayerSwitcher = false;

    },

   /**
     * 
     */
    destroy: function() {
        this.clearFeatures();
        this.features = null;
        OpenLayers.Layer.Markers.prototype.destroy.apply(this, arguments);
    },
    
    loadObjects: function(lookupResults)
	{

		//Initalise local variables
		var lookupResultsXML;
		var data = {};
		
		//Load the results into an XML dom
		lookupResultsXML = OpenLayers.parseXMLString(lookupResults);

		for (var iNode = 0; iNode < lookupResultsXML.childNodes.length; iNode++) 
		{
		
    		var node = lookupResultsXML.childNodes.item(iNode);
		
			for (var iNode2 = 0; iNode2 < node.childNodes.length; iNode2++) 
			{
		
    			var node2 = node.childNodes.item(iNode2);
				switch(node2.nodeName)
				{
					case 'electoral_ward':

						//Load ward data
						this.loadWardData(node2);

						break;
						
					case 'recycling_location':
					
						//Load recycling location data
						this.loadRecLocData(node2);

						break;
						
					case 'website_polling_district':
					
						//Load polling district data
						this.loadPollingDistrictData(node2);

						break;
						
					case 'website_polling_place':
					
						//Load polling place data
						this.loadPollingPlaceData(node2);

						break;
						
					case 'parish':
					
						//Load polling place data
						this.loadParishData(node2);

						break;

					case 'aberdeenshire_area':
					
						//Display Aberdeenshire Area popup
						this.loadAbshireAreaData(node2);

						break;

					case 'community_council':
					
						//Display community council popup
						this.loadComCouncilData(node2);

						break;
						
					case 'locality':
					
						//Display locality popup
						this.loadLocalityData(node2);

						break;
						
					case 'website_school_building':
					case 'website_school_catchment':
					
						//Display school popup
						this.loadSchoolData(node2);

						break;
						
					case 'cag_property':
					
						//Display school popup
						this.loadPropertyData(node2);

						break;
						
					case 'planning_app':
					
						//Display planning application popup
						this.loadPlanAppData(node2);

						break;

					case 'his_location':
					
						//Display HIS location popup
						this.loadHISLocationData(node2);

						break;

					case 'confirm_lighting_unit':
					
						//Display confirm lighting unit popup
						this.loadConfirmLightData(node2);

						break;

					case 'website_road':
					
						//Display confirm lighting unit popup
						this.loadRoadData(node2);

						break;

					default:
						break;
				}
	
			}

		}
		
		
	},

	showObjects: function()
	{

		for(var i=0; i < this.features.length; i++) 
		{
			this.map.addPopup(this.features[i].popup);
        }
		
	},
	
	loadWardData: function(ward_node)
	{
		var ward_name; 
		var ward_no; 
		var easting; 
		var northing; 
		var location; 
		var feature; 	
		var marker;
		var contentHTML = "";
		var data = {};
	
		//Get ward name
		ward_name = getElementValue(ward_node, 'ward_name');

		//Get ward number
		ward_no = getElementValue(ward_node, 'ward_number');
	
		//Get the centre point of the ward 
		easting = parseFloat(getElementValue(ward_node, 'centre_x'));
		northing = parseFloat(getElementValue(ward_node, 'centre_y'));
		location = new OpenLayers.LonLat(easting, northing);
	
		data.icon = OpenLayers.Marker.defaultIcon();		
		data.popupSize = new OpenLayers.Size(150, 150);
		//data['popupContentHTML'] = "<p>" + ward_name + "</p>"
		
		//build up the html to display in the popup
		//contentHTML += '<div class="olLayerGeoRSSTitle">Electoral Ward</div>';
		contentHTML += '<div class="LookupPanelTitle">Electoral Ward</div>';
		contentHTML += '<div class="LookupPanelText">';
		contentHTML += '<p><a class="link" href="http://www.aberdeenshire.gov.uk/councillors/contact/ward_' + ward_no + '.asp">' + ward_name + '</a></p>';
		contentHTML += '</div>';
		
		data['popupContentHTML'] = contentHTML; 
		
		var feature = new OpenLayers.Feature(this, location, data);

		this.features.push(feature);
		
		var marker = feature.createMarker();
		marker.events.register('click', feature, this.markerClick);
		this.addMarker(marker);
		
		var popup = feature.createPopup(true);

	},
	
	loadRecLocData: function(rec_loc_node)
	{
		var rec_loc_type; 
		var rec_loc_name; 
		var description;
		var easting; 
		var northing; 
		var location; 
		var feature; 	
		var marker;
		var contentHTML = "";
		var data = {};
	
		//Is this a recycling location or point
		rec_loc_type = getElementValue(rec_loc_node, 'type');

		//Get the recycling location/point name
		rec_loc_name = getElementValue(rec_loc_node, 'name');

		//Get the recycling location/point descriptive text
		description = getElementValue(rec_loc_node, 'description');

		//Get the centre point of the ward 
		easting = parseFloat(getElementValue(rec_loc_node, 'centre_x'));
		northing = parseFloat(getElementValue(rec_loc_node, 'centre_y'));
		location = new OpenLayers.LonLat(easting, northing);
	
		//data.icon = OpenLayers.Marker.defaultIcon();		
		//data.popupSize = new OpenLayers.Size(30, 30);
		
		//Build up the html to display in the popup
		contentHTML += '<div><h2>' + rec_loc_type + '</h2></div>';
		contentHTML += '<div>';
		contentHTML += '<p>' + rec_loc_name + '<br />' + description + '</p><br/>';
		contentHTML += '</div>';
		
		//data['popupContentHTML'] = contentHTML; 
		
		//Create the popup bubble to use for this feature
		var feature = new OpenLayers.Feature(this, location);
		feature.closeBox = true;
		feature.popupClass = OpenLayers.Class(OpenLayers.Popup.FramedCloud);
		feature.data.popupContentHTML = contentHTML;
		feature.data.overflow = "auto";
		feature.data.autosize = true;

		//Store this current feature
		this.features.push(feature);
		
		//Create the marker for this feature
		var marker = feature.createMarker();
		
		//Create the call-back function to be called when the user 
		//clicks on the feature's marker
		var markerClick = function(evt){
			if(this.popup == null)
			{
				this.popup = this.createPopup(true);
				map.addPopup(this.popup);
				this.popup.show();
			}
			else
			{
				this.popup.toggle();
			}
			currentPopup = this.popup;
			OpenLayers.Event.stop(evt);
		};
		
		marker.events.register('mousedown', feature, markerClick);
		
		//Add the marker to the map
		this.addMarker(marker);
		
		feature.popup = feature.createPopup(true);		
	},
	loadPollingDistrictData: function(polling_district_node)
	{
		var district_code; 
		var district_name; 
		var polling_place;
		var easting; 
		var northing; 
		var location; 
		var feature; 	
		var marker;
		var contentHTML = "";
		var data = {};
	

		//Get the polling district code
		district_code = getElementValue(polling_district_node, 'code');

		//Get the polling district name
		district_name = getElementValue(polling_district_node, 'name');

		//Get the name of the polling place
		polling_place = getElementValue(polling_district_node, 'polling_place');

		//Get the centre point of the polling place
		easting = parseFloat(getElementValue(polling_district_node, 'centre_x'));
		northing = parseFloat(getElementValue(polling_district_node, 'centre_y'));
		location = new OpenLayers.LonLat(easting, northing);
	
		data.icon = OpenLayers.Marker.defaultIcon();		
		data.popupSize = new OpenLayers.Size(150, 150);
		
		//Build up the html to display in the popup
		contentHTML += '<div class="LookupPanelTitle">Polling District</div>';
		contentHTML += '<div class="LookupPanelText">';
		contentHTML += '<p>' + district_code + '</p>';
		contentHTML += '<p>' + district_name + '</p>';
		var link_text = '<a href="./info_lookup.asp?type=polling_place&id=' + district_code + '">' + polling_place + '</a>';
		contentHTML += '<p><a href="./info_lookup.asp?type=polling_place&id=' + district_code + '">' + polling_place + '</a></p>';
		contentHTML += '</div>';
		
		data['popupContentHTML'] = contentHTML; 
		
		var feature = new OpenLayers.Feature(this, location, data);
		this.features.push(feature);
		
		var marker = feature.createMarker();
		marker.events.register('click', feature, this.markerClick);
		this.addMarker(marker);
		
		var popup = feature.createPopup(true);		
	},
	loadPollingPlaceData: function(polling_place_node)
	{
		var polling_place;
		var easting; 
		var northing; 
		var location; 
		var feature; 	
		var marker;
		var contentHTML = "";
		var data = {};
	
		//Get the name of the polling place
		polling_place = getElementValue(polling_place_node, 'name');
		
		//Get the centre point of the polling place
		easting = parseFloat(getElementValue(polling_place_node, 'centre_x'));
		northing = parseFloat(getElementValue(polling_place_node, 'centre_y'));
		location = new OpenLayers.LonLat(easting, northing);
	
		data.icon = OpenLayers.Marker.defaultIcon();		
		data.popupSize = new OpenLayers.Size(150, 150);
		
		//Build up the html to display in the popup
		contentHTML += '<div class="LookupPanelTitle">Polling Place</div>';
		contentHTML += '<div class="LookupPanelText">';
		contentHTML += '<p>' + polling_place + '</p>';
		contentHTML += '</div>';

		data['popupContentHTML'] = contentHTML; 
		
		var feature = new OpenLayers.Feature(this, location, data);
		this.features.push(feature);
		
		var marker = feature.createMarker();
		marker.events.register('click', feature, this.markerClick);
		this.addMarker(marker);
		
		var popup = feature.createPopup(true);		
	},
	loadParishData: function(parish_node)
	{
		var parish;
		var easting; 
		var northing; 
		var location; 
		var feature; 	
		var marker;
		var contentHTML = "";
		var data = {};
	
		//Get the name of the parish
		parish = getElementValue(parish_node, 'name');
		
		//Get the centre point of the parish
		easting = parseFloat(getElementValue(parish_node, 'centre_x'));
		northing = parseFloat(getElementValue(parish_node, 'centre_y'));
		location = new OpenLayers.LonLat(easting, northing);
	
		data.icon = OpenLayers.Marker.defaultIcon();		
		data.popupSize = new OpenLayers.Size(150, 150);
		
		//Build up the html to display in the popup
		contentHTML += '<div class="LookupPanelTitle">Parish</div>';
		contentHTML += '<div class="LookupPanelText">';
		contentHTML += '<p>' + parish + '</p>';
		contentHTML += '</div>';

		data['popupContentHTML'] = contentHTML; 
		
		var feature = new OpenLayers.Feature(this, location, data);
		this.features.push(feature);
		
		var marker = feature.createMarker();
		marker.events.register('click', feature, this.markerClick);
		this.addMarker(marker);
		
		var popup = feature.createPopup(true);		
	},
	loadAbshireAreaData: function(area_node)
	{
		var area_name;
		var easting; 
		var northing; 
		var location; 
		var feature; 	
		var marker;
		var contentHTML = "";
		var data = {};
	
		//Get the name of the aberdeenshire area
		area_name = getElementValue(area_node, 'name');
		
		//Get the centre point of the aberdeenshire area
		easting = parseFloat(getElementValue(area_node, 'centre_x'));
		northing = parseFloat(getElementValue(area_node, 'centre_y'));
		location = new OpenLayers.LonLat(easting, northing);
	
		data.icon = OpenLayers.Marker.defaultIcon();		
		data.popupSize = new OpenLayers.Size(200, 200);
		
		//Build up the html to display in the popup
		contentHTML += '<div class="LookupPanelTitle">Aberdeenshire Area</div>';
		contentHTML += '<div class="LookupPanelText">';
		contentHTML += '<p>' + area_name + '</p>';
		contentHTML += '</div>';

		data['popupContentHTML'] = contentHTML; 
		
		var feature = new OpenLayers.Feature(this, location, data);
		this.features.push(feature);
		
		var marker = feature.createMarker();
		marker.events.register('click', feature, this.markerClick);
		this.addMarker(marker);
		
		var popup = feature.createPopup(true);		
	},
	loadComCouncilData: function(community_council_node)
	{
		var community_council_name;
		var easting; 
		var northing; 
		var location; 
		var feature; 	
		var marker;
		var contentHTML = "";
		var data = {};
	
		//Get the name of the community council
		community_council_name = getElementValue(community_council_node, 'name');
		
		//Get the centre point of the community council
		easting = parseFloat(getElementValue(community_council_node, 'centre_x'));
		northing = parseFloat(getElementValue(community_council_node, 'centre_y'));
		location = new OpenLayers.LonLat(easting, northing);
	
		data.icon = OpenLayers.Marker.defaultIcon();		
		data.popupSize = new OpenLayers.Size(160, 150);
		
		//Build up the html to display in the popup
		contentHTML += '<div class="LookupPanelTitle">Community Council</div>';
		contentHTML += '<div class="LookupPanelText">';
		contentHTML += '<p>' + community_council_name + '</p>';
		contentHTML += '</div>';

		data['popupContentHTML'] = contentHTML; 
		
		var feature = new OpenLayers.Feature(this, location, data);
		this.features.push(feature);
		
		var marker = feature.createMarker();
		marker.events.register('click', feature, this.markerClick);
		this.addMarker(marker);
		
		var popup = feature.createPopup(true);		
	},
	loadPlanAppData: function(plan_app_node)
	{
		var plan_app_refno;
		var easting; 
		var northing; 
		var location; 
		var feature; 	
		var marker;
		var contentHTML = "";
		var data = {};
	
		//Get the reference no of the plannning app
		plan_app_refno = getElementValue(plan_app_node, 'refno');
		
		//Get the centre point of the locality
		easting = parseFloat(getElementValue(plan_app_node, 'centre_x'));
		northing = parseFloat(getElementValue(plan_app_node, 'centre_y'));
		location = new OpenLayers.LonLat(easting, northing);
	
		data.icon = OpenLayers.Marker.defaultIcon();		
		data.popupSize = new OpenLayers.Size(150, 150);
		
		//Build up the html to display in the popup
		contentHTML += '<div class="LookupPanelTitle">Planning Application</div>';
		contentHTML += '<div class="LookupPanelText">';
		contentHTML += '<p>' + plan_app_refno + '</p>';
		contentHTML += '</div>';

		data['popupContentHTML'] = contentHTML; 
		
		var feature = new OpenLayers.Feature(this, location, data);
		this.features.push(feature);
		
		var marker = feature.createMarker();
		marker.events.register('click', feature, this.markerClick);
		this.addMarker(marker);
		
		var popup = feature.createPopup(true);		
	},

	loadHISLocationData: function(his_location_node)
	{
		var his_location_refno;
		var easting; 
		var northing; 
		var location; 
		var feature; 	
		var marker;
		var contentHTML = "";
		var data = {};
	
		//Get the reference no of the his location
		his_location_refno = getElementValue(his_location_node, 'siteref');
		
		//Get the centre point of the location
		easting = parseFloat(getElementValue(his_location_node, 'centre_x'));
		northing = parseFloat(getElementValue(his_location_node, 'centre_y'));
		location = new OpenLayers.LonLat(easting, northing);
	
		data.icon = OpenLayers.Marker.defaultIcon();		
		data.popupSize = new OpenLayers.Size(150, 150);
		
		//Build up the html to display in the popup
		contentHTML += '<div class="LookupPanelTitle">Land Audit</div>';
		contentHTML += '<div class="LookupPanelText">';
		contentHTML += '<p>' + his_location_refno + '</p>';
		contentHTML += '</div>';

		data['popupContentHTML'] = contentHTML; 
		
		//var feature = new OpenLayers.Feature(this, location, data);
		//this.features.push(feature);
		
		//var marker = feature.createMarker();
		//marker.events.register('click', feature, this.markerClick);
		//this.addMarker(marker);
		
		//var popup = feature.createPopup(true);		
	},


	loadLocalityData: function(locality_node)
	{
		var locality_name;
		var easting; 
		var northing; 
		var location; 
		var feature; 	
		var marker;
		var contentHTML = "";
		var data = {};
	
		//Get the name of the locality
		locality_name = getElementValue(locality_node, 'name');
		
		//Get the centre point of the locality
		easting = parseFloat(getElementValue(locality_node, 'centre_x'));
		northing = parseFloat(getElementValue(locality_node, 'centre_y'));
		location = new OpenLayers.LonLat(easting, northing);
	
		data.icon = OpenLayers.Marker.defaultIcon();		
		data.popupSize = new OpenLayers.Size(100, 100);
		
		//Build up the html to display in the popup
		contentHTML += '<div><h2>Locality</h2></div>';
		contentHTML += '<div>';
		contentHTML += '<p>' + locality_name + '</p>';
		contentHTML += '</div>';

		//data['popupContentHTML'] = contentHTML; 
		//Create the popup bubble to use for this feature
		OpenLayers.Popup.FramedCloud.prototype.autoSize = true;
		var feature = new OpenLayers.Feature(this, location);
		feature.closeBox = true;
		feature.popupClass = OpenLayers.Class(OpenLayers.Popup.FramedCloud, {
            'autoSize': false, 'overflow' : 'hidden'
        });
		feature.data.popupContentHTML = contentHTML;
		feature.data.overflow = "auto";
		feature.data.autosize = true;
		feature.data.icon = OpenLayers.Marker.defaultIcon();		
		feature.data.popupSize = new OpenLayers.Size(100, 100);
		
		//Store this current feature
		this.features.push(feature);
		
		//Create the marker for this feature
		var marker = feature.createMarker();
		
		//Create the call-back function to be called when the user 
		
		//clicks on the feature's marker
		var markerClick = function(evt){
			if(this.popup == null)
			{
				this.popup = this.createPopup(true);
				map.addPopup(this.popup);
				this.popup.show();
			}
			else
			{
				this.popup.toggle();
			}
			currentPopup = this.popup;
			OpenLayers.Event.stop(evt);
		};
		
		marker.events.register('mousedown', feature, markerClick);
		
		//Add the marker to the map
		this.addMarker(marker);
				        	
		//Store the pop with the feature
		feature.popup = feature.createPopup(true);		
		
	
	},
	loadSchoolData: function(school_node)
	{
		var school_name;
		var school_type;
		var easting; 
		var northing; 
		var location; 
		var feature; 	
		var marker;
		var contentHTML = "";
		var data = {};
	
		//Determine the type of school 
		school_type = getElementValue(school_node, 'type');
		
		//Get the name of the school
		school_name = getElementValue(school_node, 'name');
		
		//Get the centre point of the secondary school
		easting = parseFloat(getElementValue(school_node, 'centre_x'));
		northing = parseFloat(getElementValue(school_node, 'centre_y'));
		location = new OpenLayers.LonLat(easting, northing);

			
		data.icon = OpenLayers.Marker.defaultIcon();		
		data.popupSize = new OpenLayers.Size(150, 150);
		
		//Build up the html to display in the popup
		contentHTML += '<div class="LookupPanelTitle">' + school_type + '</div>';
		contentHTML += '<div class="LookupPanelText">';
		contentHTML += '<p>' + school_name + '</p>';
		contentHTML += '</div>';

		data['popupContentHTML'] = contentHTML; 
		
		var feature = new OpenLayers.Feature(this, location, data);
		this.features.push(feature);
		
		var marker = feature.createMarker();
		marker.events.register('click', feature, this.markerClick);
		this.addMarker(marker);
		
		var popup = feature.createPopup(true);		
	},
	
	loadPropertyData: function(cag_address_node)
	{

		var address;

		var easting; 
		var northing; 
		var location; 
		var feature; 	
		var marker;
		var contentHTML = "";
		var data = {};
	
		//Extract the address details
		address = ""
		address += this.getAddressLine(cag_address_node, 'address_line1');
		address += this.getAddressLine(cag_address_node, 'address_line2');
		address += this.getAddressLine(cag_address_node, 'address_line3');
		address += this.getAddressLine(cag_address_node, 'address_line4');
		address += this.getAddressLine(cag_address_node, 'address_line5');
		address += this.getAddressLine(cag_address_node, 'address_line6');
		address += this.getAddressLine(cag_address_node, 'address_line7');


		//Get the centre point of the secondary school
		easting = parseFloat(getElementValue(cag_address_node, 'centre_x'));
		northing = parseFloat(getElementValue(cag_address_node, 'centre_y'));
		location = new OpenLayers.LonLat(easting, northing);
			
		//data.icon = OpenLayers.Marker.defaultIcon();		
		//data.popupSize = new OpenLayers.Size(150, 170);
		
		//Build up the html to display in the popup
		contentHTML += '<div><h2>Address</h2></div>';
		contentHTML += '<div">';
		contentHTML += '<p>' + address + '</p>';
		contentHTML += '</div>';

		//data['popupContentHTML'] = contentHTML; 
		
		//Create the popup bubble to use for this feature
		OpenLayers.Popup.FramedCloud.prototype.autoSize = true;
		var feature = new OpenLayers.Feature(this, location);
		feature.closeBox = true;
		feature.popupClass = OpenLayers.Class(OpenLayers.Popup.FramedCloud, {
            'autoSize': false, 'overflow' : 'hidden'
        });
		feature.data.popupContentHTML = contentHTML;
		feature.data.icon = OpenLayers.Marker.defaultIcon();		
		feature.data.popupSize = new OpenLayers.Size(100, 150);
		feature.data.overflow = "auto";
		feature.data.autosize = true;

		//Store this current feature
		this.features.push(feature);
		
		//Create the marker for this feature
		var marker = feature.createMarker();
		
		//Create the call-back function to be called when the user 
		//clicks on the feature's marker
		var markerClick = function(evt){
			if(this.popup == null)
			{
				this.popup = this.createPopup(true);
				map.addPopup(this.popup);
				this.popup.show();
			}
			else
			{
				this.popup.toggle();
			}
			currentPopup = this.popup;
			OpenLayers.Event.stop(evt);
		};
		
		marker.events.register('mousedown', feature, markerClick);
		
		//Add the marker to the map
		this.addMarker(marker);
				        	
		//Store the pop with the feature
		feature.popup = feature.createPopup(true);		
		
	},
	getAddressLine: function(cag_address_node, address_line_name)
	{
		var address_line = "";

		address_line = getElementValue(cag_address_node, address_line_name);
		if(address_line != "") 
		{
			address_line += '<br />';
		}

		return address_line;
	},	
	loadConfirmLightData: function(confirm_light_node)
	{

		var easting; 
		var northing; 
		var location; 
		var street;
		var street_location;
		var street_id;
		var status;
		var id; 
		var report_link;
		var feature; 	
		var marker;
		var contentHTML = "";
		var data = {};

		//Get the location of the confirm light
		easting = parseFloat(getElementValue(confirm_light_node, 'centre_x'));
		northing = parseFloat(getElementValue(confirm_light_node, 'centre_y'));
		location = new OpenLayers.LonLat(easting, northing);
		
		//Get the location description 
		street_location = getElementValue(confirm_light_node, 'feature_location');
		street = getElementValue(confirm_light_node, 'street');
		
		//Get the status of the light 
		status = getElementValue(confirm_light_node, 'status');

		//Get the id of the street
		street_id = getElementValue(confirm_light_node, 'nsg_id');

		//Get the id of the lighting unit
		id = getElementValue(confirm_light_node, 'central_asset_id');
		
		//data.icon = OpenLayers.Marker.defaultIcon();		
		//data.popupSize = new OpenLayers.Size(150, 170);
		
		//Build up the html to display in the popup
		contentHTML += '<div><h2>Light Unit</h2></div>';
		contentHTML += '<div">';
				
		//If there is no known fault add link to report fault 
		if(status == 'No known fault') 
		{
			//Build up the link to report the fault
			report_link = "/roadfaults/LogFault.aspx?site_ref=" + street_id + "&asset_id=" + id;
			contentHTML += '<p><a href="' + report_link + '">Report Fault</a></p>';				
		}
		else
		{
			contentHTML += '<p>' + status + '</p>';
		}

		//Add location details
		contentHTML += '<p>' + street_location + '<br />';
		contentHTML += street + '</p>';		

		contentHTML += '</div>';

		//data['popupContentHTML'] = contentHTML; 
		
		//Create the popup bubble to use for this feature
		OpenLayers.Popup.FramedCloud.prototype.autoSize = true;
		var feature = new OpenLayers.Feature(this, location);
		feature.closeBox = true;
		var AutoSizeFramedCloud = OpenLayers.Class(OpenLayers.Popup.FramedCloud, 
												   		{
													   	'autoSize': true,
														'minSize': new OpenLayers.Size(200,200)																											
														}
												   );

		feature.popupClass = AutoSizeFramedCloud;
		feature.data.popupContentHTML = contentHTML;
		feature.data.icon = OpenLayers.Marker.defaultIcon();		


		//Store this current feature
		this.features.push(feature);
		
		//Create the marker for this feature
		var marker = feature.createMarker();
		
		//Create the call-back function to be called when the user 
		//clicks on the feature's marker
		var markerClick = function(evt){
			if(this.popup == null)
			{
				this.popup = this.createPopup(true);
				map.addPopup(this.popup);
				this.popup.show();
			}
			else
			{
				this.popup.toggle();
			}
			currentPopup = this.popup;
			OpenLayers.Event.stop(evt);
		};
		
		marker.events.register('mousedown', feature, markerClick);
		
		//Add the marker to the map
		this.addMarker(marker);
				        	
		//Store the pop with the feature
		feature.popup = feature.createPopup(true);		
		
	},

	loadRoadData: function(road_node)
	{

		var road_location;
		var closure_date;
		//var status;
		//var id; 
		var report_link;
		var feature; 	
		var marker;
		var contentHTML = "";
		var data = {};

		try
		{


			//Get the location of the road's centre point
			easting = parseFloat(getElementValue(road_node, 'centre_x'));
			northing = parseFloat(getElementValue(road_node, 'centre_y'));
			road_location = new OpenLayers.LonLat(easting, northing);
		
			//Get the html to display the road details 
			contentHTML = get_road_lookup_html(road_node); 
			
			//Create the popup bubble to use for this feature
			OpenLayers.Popup.FramedCloud.prototype.autoSize = true;
			var feature = new OpenLayers.Feature(this, road_location);
			feature.closeBox = true;
			var AutoSizeFramedCloud = OpenLayers.Class(OpenLayers.Popup.FramedCloud, 
															{
															'autoSize': true,
															'minSize': new OpenLayers.Size(200,200)																											
															}
													   );
	
			feature.popupClass = AutoSizeFramedCloud;
			feature.data.popupContentHTML = contentHTML;
			feature.data.icon = OpenLayers.Marker.defaultIcon();		
	
			//Store this current feature
			this.features.push(feature);
			
			//Create the marker for this feature
			var marker = feature.createMarker();
			
			//Create the call-back function to be called when the user 
			//clicks on the feature's marker
			var markerClick = function(evt){
				if(this.popup == null)
				{
					this.popup = this.createPopup(true);
					map.addPopup(this.popup);
					this.popup.show();
				}
				else
				{
					this.popup.toggle();
				}
				currentPopup = this.popup;
				OpenLayers.Event.stop(evt);
			};
			
			marker.events.register('mousedown', feature, markerClick);
			
			//Add the marker to the map
			this.addMarker(marker);
								
			//Store the pop with the feature
			feature.popup = feature.createPopup(true);		
		}
		catch(e)
		{
			alert("Problem with loadRoadData() - " + e.description);
			
		}		
	

		
	},	
		
		
	/**
     * @param {Event} evt
     */
    markerClick: function(evt) {
		
        sameMarkerClicked = (this == this.layer.selectedFeature);
        this.layer.selectedFeature = (!sameMarkerClicked) ? this : null;
		
        for(var i=0; i < this.layer.map.popups.length; i++) {
		
            this.layer.map.removePopup(this.layer.map.popups[i]);
        }
        if (!sameMarkerClicked) {
		
            //this.layer.map.addPopup(this.createPopup()); 
			this.layer.map.addPopup(this.createPopup(true)); 
        }
        OpenLayers.Event.stop(evt);
    },
    /**
     * 
     */
    clearFeatures: function() {
        if (this.features != null) {
            while(this.features.length > 0) {
                var feature = this.features[0];
                OpenLayers.Util.removeItem(this.features, feature);
                feature.destroy();
            }
        }        
    },
    
    /** @final @type String */
    CLASS_NAME: "OpenLayers.Layer.LookupLayer"
});

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function
