/**
 * Open Space 
 *   Using free maps to show location of accommodation that the club stays in.
 *
 **/
var osMap, screenOverlay, mapOV;

function init() {
	// Creating the Openspace map and the postcode service
	osMap = new OpenSpace.Map('map');
	
	// Adding the Grid Coordinates (mouse over map) box 
	screenOverlay = new OpenSpace.Layer.ScreenOverlay("coords");
	var leftPos = osMap.div.clientWidth - 116;
	screenOverlay.setPosition(new OpenLayers.Pixel(leftPos, 5));
	osMap.addLayer(screenOverlay);
	var gridProjection = new OpenSpace.GridProjection();
	osMap.events.register("mousemove", osMap, function(e) {
	var pt = osMap.getLonLatFromViewPortPx(e.xy);
	var lonlat = gridProjection.getLonLatFromMapPoint(pt);
	screenOverlay.setHTML("<DIV style=\"width: 108px; height=86px; padding-left: 4px; color: white; background-color: #222; font-size: 12px\">" +
	"EAST: " + (pt.lon).toFixed(0) + "<BR>" +
	"NORTH: " + (pt.lat).toFixed(0) + "<BR>" +
	"LON: " + (lonlat.lon).toFixed(8) + "<BR>" +
	"LAT: " + (lonlat.lat).toFixed(8) + " </DIV>");
	});
	
	// Adding the map overview
	mapOV = new OpenSpace.Control.OverviewMap();
	osMap.addControl(mapOV);
	//fix to put copyright on top of overview map: Needs api fix for later version
	var ccControl = osMap.getControlsByClass("OpenSpace.Control.CopyrightCollection")
	osMap.removeControl(ccControl[0]);
	ccControl = new OpenSpace.Control.CopyrightCollection();
	osMap.addControl(ccControl);
	ccControl.activate();
	//end of fix
	// Turning the overview map off
	mapOV.minimizeControl();
	
	// Defining the center of the map and the zoom level
	// osMap.setCenter(new OpenSpace.MapPoint(148710,819640),7);

	// Defining a marker
	// pos = new OpenSpace.MapPoint(148710,819640);
	// size = new OpenLayers.Size(33,45);
	// offset = new OpenLayers.Pixel(-16,-36);
	// infoWindowAnchor = new OpenLayers.Pixel(16,16);
	// icon = new OpenSpace.Icon('http://openspace.ordnancesurvey.co.uk/osmapapi/img_versions/img_1.0.1/OS/images/markers/marker_red.png', size, offset, null, infoWindowAnchor);
	// osMap.createMarker(pos, icon, null, null);
	
	clusterControl = new OpenSpace.Control.ClusterManager();
	osMap.addControl(clusterControl);
	clusterControl.activate();
}

function setGridReference (ngr) {
	init();

	var ne = NGR2NE_wtp(ngr);

	osMap.setCenter(ne,7);
	// Defining a marker
	pos = ne;
	size = new OpenLayers.Size(33,45);
	offset = new OpenLayers.Pixel(-16,-36);
	infoWindowAnchor = new OpenLayers.Pixel(16,16);
	icon = new OpenSpace.Icon('http://openspace.ordnancesurvey.co.uk/osmapapi/img_versions/img_1.0.1/OS/images/markers/marker_red.png', size, offset, null, infoWindowAnchor);
	osMap.createMarker(pos, icon, null, null);
}

//convert northing and easting to letter and number grid system
function NE2NGR_wtp(east,  north) {
	east = parseInt(east,10) ;
	north = parseInt(north,10);
	var eX = east / 500000;
	var nX = north / 500000;
	var tmp = Math.floor(eX) - 5.0 * Math.floor(nX) + 17.0; 
	nX = 5 * (nX - Math.floor(nX));
	eX = 20 - 5.0 * Math.floor(nX) + Math.floor(5.0 * (eX - Math.floor(eX)));
	if (eX > 7.5) eX = eX + 1; // I is not used
	if (tmp > 7.5) tmp = tmp + 1; // I is not used
	 
	var eing = east - (Math.floor(east / 100000)*100000);
	var ning = north - (Math.floor(north / 100000)*100000);
	var estr = eing.toFixed(0);
	var nstr = ning.toFixed(0);
	while(estr.length < 5)
		estr = "0" + estr;
	while(nstr.length < 5)
		nstr = "0" + nstr;
	 
	var ngr = String.fromCharCode(tmp + 65) + 
	          String.fromCharCode(eX + 65) + 
	          " " + estr + " " + nstr;
	return ngr;
}
 
 
//convert northing and easting to letter and number grid system, 4 figures
function NE2NGR_wtp4(east,  north) {
	east = parseInt(east/10,10)*10;
	north = parseInt(north/10,10)*10;
	var eX = east / 500000;
	var nX = north / 500000;
	var tmp = Math.floor(eX) - 5.0 * Math.floor(nX) + 17.0; 
	nX = 5 * (nX - Math.floor(nX));
	eX = 20 - 5.0 * Math.floor(nX) + Math.floor(5.0 * (eX - Math.floor(eX)));
	if (eX > 7.5) eX = eX + 1; // I is not used
	if (tmp > 7.5) tmp = tmp + 1; // I is not used
	 
	var eing = east - (Math.floor(east / 100000)*100000);
	var ning = north - (Math.floor(north / 100000)*100000);
	var estr = (eing/10.0).toFixed(0);
	var nstr = (ning/10.0).toFixed(0);
	while(estr.length < 4)
		estr = "0" + estr;
	while(nstr.length < 4)
		nstr = "0" + nstr;
	 
	var ngr = String.fromCharCode(tmp + 65) + 
	          String.fromCharCode(eX + 65) + 
	          " " + estr + " " + nstr;
	return ngr;
}
 
function NGR2NE_wtp(ngr) {
	var e;
	var n;
	 
	ngr = ngr.toUpperCase(ngr);
	 
	var bits = ngr.split(' ');
	ngr = "";
	for (var i=0;i<bits.length;i++)
	    ngr+=bits[i];
	 
	var c = ngr.charAt(0);
	if (c =='S') {
	    e = 0;
	    n = 0;
	} else if (c == 'T') {
	    e = 500000;
	    n = 0;
	} else if (c == 'N') {
	    n = 500000;
	    e = 0;
	} else if (c == 'O') {
	    n = 500000;
	    e = 500000;
	} else if(c == 'H') {
	    n = 1000000;
	    e = 0;
	} else 
	    return null;
	    
	c = ngr.charAt(1);
	if(c == 'I')
	    return null;
	    
	c = ngr.charCodeAt(1) - 65;
	if(c > 8)
	    c -= 1;
	e += (c % 5) * 100000;
	n += (4 - Math.floor(c/5)) * 100000;
	 
	c = ngr.substr(2);
	if ((c.length%2) == 1) 
	    return null;
	if (c.length > 10) 
	    return null;
	 
	try {
	    var s = c.substr(0,c.length/2);
	    while(s.length < 5)
	        s += '0';
	    e += parseInt(s,10); 
	    if(isNaN(e))
	        return null; 
	    
	    s = c.substr(c.length/2);
	    while(s.length < 5)
	        s += '0';
	    n += parseInt(s,10); 
	    if(isNaN(n))
	        return null;
	        
	    return new OpenSpace.MapPoint(e,n);
	} catch (ex) {
	    return null;
	} 
}
