// Copyright (c) 2008 Regenesys. All rights reserved. 

var map;
var localSearch = new GlocalSearch();
var centerPoint = new GLatLng(50.724286,-1.878662);
var zoomPoint = 15;
var labelContainer;

var baseicon = new GIcon(G_DEFAULT_ICON, "../gmap/icons/m2.png");
baseicon.iconSize = new GSize(30, 30);
baseicon.iconAnchor = new GPoint(30, 30);

var j = 0;
var gmarkers = [];
var htmls = [];
var strURL = "../queries/public_xml_mapquery.cfm?Type=ObjectMap&ComponentList=News,Event,Blog,Poll,Organisation,Document";

function getSearch() {
	var MetaSel = "";
	var strKeyword = document.getElementById("MAPSEARCHSTRING"); 
	var strMetaKeyID = document.getElementById("SelectMapSearch").MYGROUPID.length;
	
	for (i = 0; i < strMetaKeyID; i++)
      if (document.getElementById("SelectMapSearch").MYGROUPID[i].checked){
         MetaSel = MetaSel + document.getElementById("SelectMapSearch").MYGROUPID[i].value + "|"
    }

	if(strKeyword.value!="")
		{ strURL = "../queries/public_xml_mapquery.cfm?Type=ObjectMap&ComponentList=News,Event,Blog,Poll,Organisation,Document&searchstring=" + strKeyword.value + "&groupid=" + MetaSel; }
	else { strURL = "../queries/public_xml_mapquery.cfm?Type=ObjectMap&ComponentList=News,Event,Blog,Poll,Organisation,Document&groupid=" + MetaSel; }; 
	j = 0;
	gmarkers = [];
	htmls = [];
	mapLoad();
	}

function reQuerySearch() {
	if (GBrowserIsCompatible()) {
		document.getElementById("prpCount").innerHTML ="LOADING...";
		GDownloadUrl(strURL, function(data) { var xml = GXml.parse(data);  
		var markers = xml.documentElement.getElementsByTagName("marker");  
		document.getElementById("prpCount").innerHTML = markers.length + " items(s) found:";
		var select_html = "Search Items on Map ("+ markers.length +" found):<br /><select style='font-size:80%; width:230px;' onChange='myclick(this)'><option>Select an Item...</option>";
		
		for ( var i = 0; i < markers.length; i++) { 
			var title = markers[i].getAttribute('OBJECT_TITLE');
			var summary = markers[i].getAttribute('OBJECT_SUMMARY');
			var objecttype = markers[i].getAttribute('OBJECT_TYPE');
			var postcode = markers[i].getAttribute('GEO_POSTCODE');
			var id = markers[i].getAttribute('OBJECTID');
			var containerid = markers[i].getAttribute('CONTAINERID');
			var containertitle = markers[i].getAttribute('CONTAINER_TITLE');
			var html = '<div style="width:200px"><strong>' + title + '</strong><br /><em>(' + containertitle + ')</em><br /><br />' + summary + '<br /><br /><em>(' + objecttype + ')</em><br /><a href=00,organisation,' + id + ',' + containerid + ',00.htm><strong>More Information &raquo;</strong></a></div>';
			htmls[i] = html;
			usePointFromPostcode(postcode,i,html); 
			if (title.length > 30) {
			select_html += '<option value="' + i + '">' + title.substr(0,25) + '... (' + objecttype +')<\/option>'; }
			else { select_html += '<option value="' + i + '">' + title + ' (' + objecttype +')<\/option>'; };
			//select_html += '<a href="javascript:myclick(' + i + ')">' + title + '<\/a><br />';
		}	//labelContainer.innerHTML = select_html;
			document.getElementById("side_bar").innerHTML = select_html + "</select>";
		});
	  }
	else { alert("Sorry, the Google Maps API is not compatible with this browser"); }
}

function usePointFromPostcode(postcode,id,html) {
	localSearch.setSearchCompleteCallback(null, function() { 
		if (localSearch.results[0])
		{	
			var resultLat = localSearch.results[0].lat; 
			var resultLng = localSearch.results[0].lng; 
			var point = new GLatLng(resultLat,resultLng); 
			var marker = createMarker(point,j);
			map.addOverlay(marker); 
			gmarkers[j] = marker;
			j++;
			if (j==1){map.setCenter(point, zoomPoint);} 
		}   else{ j++;  /* alert("Postcode not found!"); */	}
	});	
	localSearch.execute(postcode + ", UK");
}

function createMarker(point,id) {
	var marker = new GMarker(point,{icon:baseicon}); 
	GEvent.addListener(marker, "click", function() { 
	marker.openInfoWindowHtml(htmls[id]);
	}); 
	return marker;
}

function mapLoad(point) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map")); 
		//map.addControl(new LabelControl());
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(centerPoint, zoomPoint);
		reQuerySearch();
	}
}

function myclick(thisClick) { 
	var i = thisClick.selectedIndex - 1; 
	if (i > -1) { 
		gmarkers[i].openInfoWindowHtml(htmls[i]);
	} 
	else { 
		map.closeInfoWindow(); 
	} 
}

function LabelControl() {  }
	LabelControl.prototype = new GControl();
	
	LabelControl.prototype.initialize = function(map) {
	labelContainer = document.createElement("div");
	labelContainer.style.overflow="auto";
	labelContainer.style.backgroundColor = "#ffffff";
	labelContainer.style.border = "1px solid black";
	labelContainer.style.height="100px";
	labelContainer.style.width="200px";
	labelContainer.style.paddingLeft="5px";
	
	map.getContainer().appendChild(labelContainer);
	return labelContainer;
	}
	
	LabelControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 33));
	}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addUnLoadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
	  window.onunload = func;
	} else {
	  window.onunload = function() {
	    oldonunload();
	    func();
	  }
	}
}
addLoadEvent(mapLoad);
addUnLoadEvent(GUnload);
