var allElements = new Array;
var sliderSpacing = 365/googlePeriod;
for(var i=1; i<=googlePeriod; i++) {
	allElements[i*2.5*sliderSpacing+1]=i+1;
}
	
function setInitialSliderPosition(position1, position2) {
	elSlider1 = findObject('sliderDragcell1');
	elSlider2 = findObject('sliderDragcell2');
	var slider1Offset = parseInt(910-((googlePeriod-position1)/googlePeriod)*902);
	elSlider1.style.left=slider1Offset+'px';
	var slider2Offset = parseInt(902-((googlePeriod-position2)/googlePeriod)*902);
	elSlider2.style.left=slider2Offset+'px';
}
	
var globalLoading = true;
alterLoading(globalLoading);
function alterLoading(isLoading) { // Show loading element when getting new data from server after doFilter call.
	loadingElement = document.getElementById('loadElement');
	mapContainer = document.getElementById('mapContainer');
	globalLoading = isLoading;
	try {
		if (loadingElement.style.display == 'none') {
			loadingElement.style.display = '';
			mapContainer.className = 'partialVis';
		} else {
			loadingElement.style.display = 'none';
			mapContainer.className = 'totalVis';
		}
	} catch (e) { }
}
	
function closeWindow() {
	map.closeExtInfoWindow();
}
	
function doFilter(category, item) {
	if (!globalLoading) {
		alterLoading(true);
		
		// Either show or hide the clicked item
		if (item) {
			showItems[item] = !showItems[item];
			// Populate the URL & change the button of the category pressed
			if (showItems[item]) {
				setButton(item+'_cat', '/ownnew/google_maps/box_f2.png');
				showUrl += '&show'+item+'=1';
			} else {
				setButton(item+'_cat', '/ownnew/google_maps/box.png');
				showUrl = showUrl.replace('&show'+item+'=1', '');
			}
		}

		// Either show or hide the clicked category
		showCats[category] = !showCats[category];
	
		if (category) {
			// Populate the URL & change the button of the category pressed
			if (showCats[category]) {
				setButton(category+'_cat', '/ownnew/google_maps/cat/google_maps_'+category+'_f2.gif');
				showUrl += '&'+category+'=1';
			} else {
				setButton(category+'_cat', '/ownnew/google_maps/cat/google_maps_'+category+'.gif');
				showUrl = showUrl.replace('&'+category+'=1', '');
			}
		}

		//alert(showUrl);
		// Reload the map
		//alert(clusterer.markers.length);
		readMap(showUrl);
	}
}
	
// Change the URL to the correct date range.
function doFilterDate(startDate, endDate) {
	if (!globalLoading) {
		alterLoading(true);

		showUrl = showUrl.replace(/\&start=([0-9]*)/, '');
		showUrl = showUrl.replace(/\&end=([0-9]*)/, '');
		showUrl += '&start='+startDate;
		showUrl += '&end='+endDate;
		//alert(showUrl);
		readMap(showUrl);
	}
}
	
function openfullscreen() {
	window.open(wwwroot+'/index.php?page=_37', '', 'fullscreen=yes, scrollbars=auto');
}
	
function openfullscreen2(target) {
	window.open(target, '', 'fullscreen=no, location=yes, menubar=yes, resizable=yes, status=yes, titlebar=yes, toolbar=yes, scrollbars=yes');
}
	
// Get the positions of the sliders.
function getPosition() {
	el1 = findObject('sliderDraghandle1');
	elOffsetLeft1 = el1.parentNode.offsetLeft-(/MSIE/.test(navigator.userAgent)?5:0);
	el2 = findObject('sliderDraghandle2');
	elOffsetLeft2 = el2.parentNode.offsetLeft-(/MSIE/.test(navigator.userAgent)?5:0);
	if (elOffsetLeft2 < elOffsetLeft1) {
		elOffsetOld = elOffsetLeft2;
		elOffsetLeft2 = elOffsetLeft1;
		elOffsetLeft1 = elOffsetOld;
	}
	var previousOffset1 = 1
		var previousOffset2 = 1
		for (var nextOffset in allElements) {
			if (elOffsetLeft1 > nextOffset) {
				previousOffset1 = nextOffset;
			}
			if (elOffsetLeft2 > nextOffset) {
				previousOffset2 = nextOffset;
			}
		}
	doFilterDate(allElements[previousOffset1], allElements[previousOffset2]);
}

// A function to create the marker and set up the event window
function createMarker(point,label,html,category) {
	var marker = new GMarker(point, icon);
	var opt_opts=new Object;
	if (category) {
		opt_opts.className='google_maps_'+category;
	}
	GEvent.addListener(marker, 'click', function() {
								 marker.openExtInfoWindow(
									 map,
									 'custom_window',
									 html,
									 opt_opts
									 );
							 })
		/*
		  GEvent.addListener(marker, marker+'i', function(){ 
		  marker.openExtInfoWindow(
		  map,
		  marker+'i',
		  'I'm some HTML content that will go in the simple example's ExtInfoWindow.',
		  {beakOffset: 3}); 
		  });*/
		gmarkers[ii] = marker;
	htmls[ii] = html;
	ii++;
	return marker;
}

function readMap(url, zoomLevel0) {
	for (var rmMarker in clusterer.markers) {
		clusterer.RemoveMarker( clusterer.markers[rmMarker] );
	}
		
	//map.removeOverlay();
	// This Javascript is based on code provided by the
	// Blackpool Community Church Javascript Team
	// http://www.commchurch.freeserve.co.uk/   
	// http://econym.googlepages.com/index.htm
	clusterer = null;
	clusterer = new Clusterer(map);
	clusterer.SetMaxVisibleMarkers(5);
	clusterer.SetMinMarkersPerCluster(3);
	clusterer.SetMaxLinesPerInfoBox(4);
	var bounds = new GLatLngBounds();
	//map.setCenter(new GLatLng(0,0),0);
	var request = GXmlHttp.create();

	request.open('GET', url, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {

			//Internet Explorer
			try {
				var xmlDoc=new ActiveXObject('Microsoft.XMLDOM');
				xmlDoc.async='false';
				xmlDoc.loadXML(request.responseText);
				//alert(request.responseText);
			} catch(e) {
				//Firefox, Mozilla, Opera, etc.
				try {
					parser=new DOMParser();
					var xmlDoc=parser.parseFromString(request.responseText,'text/xml');
				} catch(e) {
					alert(e.message);
					return;
				}
			}

			//var xmlDoc = GXml.parse(request.responseText);
			// obtain the array of markers and loop through it
			//var markers = xmlDoc.documentElement.getElementsByTagName('marker');
			//var markers = xmlDoc.getElementsByTagName('marker');
			var markers = xmlDoc.getElementsByTagName('marker');
			//alert(markers.length);
            
			// hide the info window, otherwise it still stays open where the removed marker used to be
			map.getInfoWindow().hide();
			/*
			  var markersLength = markers.length;
			  for (var i = 0; i < markersLength; i++) {
			  clusterer.RemoveMarker( markers[i] );
			  }
			  map.clearOverlays();
			*/
           
			// empty the arrays
			gmarkers = [];
			htmls = [];
			ii = 0;

			// reset the side_bar
			side_bar_html='';
            
			var markersLength = markers.length;
			for (var i = 0; i < markersLength; i++) {
				// obtain the attribues of each marker
				var lat = parseFloat(markers[i].getAttribute('lat'));
				var lng = parseFloat(markers[i].getAttribute('lng'));
				var point = new GLatLng(lat,lng);
				var iconimage = markers[i].getAttribute('icon');
				iconimge='actueel';
				bounds.extend(point);
		      icon.image = wwwroot+'/images/ownnew/google_maps/markers/'+iconimage+'_marker.png';
				//alert(icon.image);
				// create the marker

				var date = GXml.value(markers[i].getElementsByTagName('date')[0]);
				var title = GXml.value(markers[i].getElementsByTagName('title')[0]);
				var shorttitle = GXml.value(markers[i].getElementsByTagName('shorttitle')[0]);
				var contents = GXml.value(markers[i].getElementsByTagName('contents')[0]);
				var html = '<div style="width:240px;">'+date+title+contents+'</div>';
				var marker = createMarker(point,'marker'+i,html,iconimage);
				//map.addOverlay(marker);
				clusterer.AddMarker(marker, shorttitle);
			}

			var zoomLevel = (zoomLevel0>0?zoomLevel0:(map.getBoundsZoomLevel(bounds) > 5 ? 5 : (map.getBoundsZoomLevel(bounds)>(page=='_37'?2:2)?map.getBoundsZoomLevel(bounds):(page=='_37'?2:2))));

			map.setZoom(zoomLevel);
			map.enableScrollWheelZoom();
			map.setCenter(bounds.getCenter());
			// put the assembled side_bar_html contents into the side_bar div
			//document.getElementById('side_bar').innerHTML = side_bar_html;
			alterLoading(false);
		}
	}
	request.send(null);

}

// This function picks up the click and opens the corresponding info window
function myclick(i) {
	gmarkers[i].openInfoWindowHtml(htmls[i]);
}

function noteMapClick(href) {
   _uacct = 'UA-1234625-1';
	href=href.replace(/https?:\/\/[^\/]*\//, '/');
	urchinTracker(href);
}
