(function(){

/**
 * Code for loading the generic search results map
 */


var map;
var widgetContainer;
var mapInited = false;


window.NewMind = window.NewMind || {};
var NewMind = window.NewMind;

NewMind.tourism = NewMind.tourism || {};

NewMind.tourism.settings = NewMind.tourism.settings || {};

NewMind.tourism.settings['mappingDefaultPolyOn'] = true;

/**
 * Load a generic search results map into the element of ID provided.
 * @param {Object} strContainerId ID of element into which the map will be loaded
 */
NewMind.tourism.loadGenericSearchResultsMap = function(strContainerId)
{
	if(!mapInited)
	{
		searchResultsLoadMain(strContainerId);
	}
	else
	{
		map.toggleVisibility();
	}
	mapInited = true;
};


function searchResultsLoadMain(strContainerId)
{
	
	var chk;
	map = NewMind.mapping.addMap(strContainerId, 'multimap');
	map.mapstractionMap.addLargeControls();
	map.mapstractionMap.addMapTypeControls();
	
	widgetContainer = document.getElementById('mapWidgetContainer');
			
	var dynamicMarkers = new NewMind.tourism.ProductMapDataSource();
	var olDynamicProds = new NewMind.mapping.MarkerOverlay(dynamicMarkers);
	
	olDynamicProds.Decluttering = true;
	var grpMkr = new Marker( new LatLonPoint(0.0, 0.0) );
	grpMkr.addData({
		'icon': '/sitebuilder/shared_gfx/mkr-grp.png',
		'iconSize': [27, 30],
		'iconAnchor': [12, 0]		
	});
	olDynamicProds.DeclutteringOptions.marker = grpMkr;
			
	map.addOverlay('dynamicProds', olDynamicProds);
	chk = olDynamicProds.getShowHideCheckbox();
	widgetContainer.appendChild(chk);
	chk.checked = true;
	wrapShowHideCheck(chk, 'show / hide Search Results');
	//addLabel(chk, 'Search Layer');
	
	
	var pmdFilterWgt = new NewMind.tourism.ProductMarkerFilterWidget(olDynamicProds, document.getElementById('mapFilters'));
	
	
	if (NewMind.env.install.itinenabled) {
		// itinerary
		var olItinProds = new NewMind.mapping.MarkerOverlay(new NewMind.tourism.ItineraryMapDataSource());
		
		map.addOverlay('itinProds', olItinProds);
		chk = olItinProds.getShowHideCheckbox();
		widgetContainer.appendChild(chk);
		chk.checked = true;
		wrapShowHideCheck(chk, 'show / hide Itinerary Items');
	}
	
	var bGotPoly, bGotProx, bGotProdEN, llProxPoint;
	bGotPoly = false;
	bGotProx = false;
	bGotProdEN = false;
	
			
	if (NewMind.env.search) {
		
		if (NewMind.env.search.polygon) {
			var polygon = {
				color: '#0000FF',
				width: 1,
				opacity: 0.5,
				closed: true,
				fillColor: '#CCCCFF',
				points: []
			};
			
			var plypts =  NewMind.env.search.polygon.points;
			for(var iPCount = 0; iPCount < plypts.length; iPCount++){
				var osP = new OSRef(plypts[iPCount].x, plypts[iPCount].y);
				var llP = osP.toLatLng();
				polygon.points.push({lat:llP.lat,lon:llP.lng});
			}
			
			var olPolygon = new NewMind.mapping.PolylineOverlay(polygon);
			
			map.addOverlay('polygon', olPolygon);
			
			chk = olPolygon.getShowHideCheckbox();
			widgetContainer.appendChild(chk);
			chk.checked = true;
			wrapShowHideCheck(chk, 'show / hide Area Outlines');
			
			
			if (!NewMind.tourism.settings.mappingDefaultPolyOn) {
				olPolygon.hide();
			}
			
			
			bGotPoly = true;
		}
		
		if (NewMind.env.search.proximity) {
			
			var osP = new OSRef(NewMind.env.search.proximity.eastings, NewMind.env.search.proximity.northings);
			var llProxPoint = osP.toLatLng();
			
			//fixes the dodgy slight inaccuracies
			llProxPoint.OSGB36ToWGS84();
			
			var osSWP = new OSRef(NewMind.env.search.proximity.ESW, NewMind.env.search.proximity.NSW);
			var llSW = osSWP.toLatLng();
			var osNEP = new OSRef(NewMind.env.search.proximity.ENE, NewMind.env.search.proximity.NNE);
			var llNE = osNEP.toLatLng();
			
			
			var staticMarkers = [
				{ 
					lat: llProxPoint.lat, 
					lon: llProxPoint.lng, 
					label: NewMind.env.search.proximity.product_name, 
					infoBubble: NewMind.env.search.proximity.product_name, 
					showProximity: true,
					icon: '/sitebuilder/shared_gfx/mkr-prox.png',
					iconSize: [23, 26],
					iconAnchor: [12, 0]
				}
			];
			var olProxProd = new NewMind.mapping.MarkerOverlay(staticMarkers);
			
			
			
			map.addOverlay('proxProd', olProxProd);
			chk = olProxProd.getShowHideCheckbox();
			widgetContainer.appendChild(chk);
			chk.checked = true;
			wrapShowHideCheck(chk, 'show / hide Proximity Radius');
			
			bGotProx = true;
		}
		
		if (NewMind.env.search.prodsEN) {
			bGotProdEN = true;			
		}
	
	}
	
	var mtc;
	var hashMapDetails = {};
	var reHashMapStuff = /(map[xyz])=([\d\.-]+)/g;
	while((mtc = reHashMapStuff.exec(window.location.hash)) != null){
		hashMapDetails[mtc[1]] = mtc[2];
	}
	
	if('mapx' in hashMapDetails && 'mapy' in hashMapDetails && 'mapz' in hashMapDetails) {
		
		map.mapstractionMap.setCenterAndZoom(new LatLonPoint(hashMapDetails.mapy, hashMapDetails.mapx), hashMapDetails.mapz);
	
	}
	else if(bGotProx) {		// centre on prox
		
		//map.mapstractionMap.setCenterAndZoom(new LatLonPoint(llProxPoint.lat, llProxPoint.lng), 13);
		map.mapstractionMap.setBounds(new BoundingBox(llSW.lat, llSW.lng, llNE.lat, llNE.lng));
	}
	else if(bGotPoly) {
		var osSW = new OSRef(NewMind.env.search.polygon.swx, NewMind.env.search.polygon.swy);
		var osNE = new OSRef(NewMind.env.search.polygon.nex, NewMind.env.search.polygon.ney);
		var llSW = osSW.toLatLng();
		var llNE = osNE.toLatLng();		
		map.mapstractionMap.setBounds(new BoundingBox(llSW.lat, llSW.lng, llNE.lat, llNE.lng));
		$('<span class="polygonName">' + NewMind.env.search.polygon.polygonname + '</span>').appendTo('#' + strContainerId);
		map.mapstractionMap.addEventListener('moveend', function(){
			$('#' + strContainerId + ' .polygonName').hide(2000).queue(function(){$(this).remove();});
		}, null);
	}
	else if(bGotProdEN) {
		var osSW = new OSRef(NewMind.env.search.prodsEN.swx, NewMind.env.search.prodsEN.swy);
		var osNE = new OSRef(NewMind.env.search.prodsEN.nex, NewMind.env.search.prodsEN.ney);
		var llSW = osSW.toLatLng();
		var llNE = osNE.toLatLng();
		map.mapstractionMap.setBounds(new BoundingBox(llSW.lat, llSW.lng, llNE.lat, llNE.lng));
	}
	else {
		// centre on default location		
		map.mapstractionMap.setCenterAndZoom(new LatLonPoint(NewMind.env.install.mapping.home.lat, NewMind.env.install.mapping.home.lon), NewMind.env.install.mapping.home.zoom);
	}
		
}

function wrapShowHideCheck(oInp, sText){
	var checkedState = oInp.checked;
	$(oInp).wrap('<div class=\"chk-overlayshowhide ' + oInp.id + '\"></div>').after('<label for=\"' + oInp.id + '\">' + sText + '</label>');	
	oInp.checked = checkedState;
}

})();


