function count_marker_type(map,markerArr,loadType)
{
	var loadTypeCounter = 0;
	for(var i = 0; i<markerArr.length;i++)
	{
		if(markerArr[i]['type'] == loadType){
			loadTypeCounter++;
		}
	}
	return loadTypeCounter;
}

function remove_markers(map,removeArr,markerType) {
	for(var i=0; i< removeArr.length; i++) {
		if(markerType == removeArr[i]['type'] || markerType == ''){
			removeArr[i]['loaded'] = 'false';
			map.removeOverlay(removeArr[i]['marker']);
		}
	}

	return removeArr;
}

function getIcon(type)
{
	// Create our "tiny" marker icon
	var iconProperties = new GIcon(G_DEFAULT_ICON);
	switch(type)
	{
		default:
		case "featured":
			iconProperties.image = "/img/frontend/gmap/big_marker.png";
			iconProperties.shadow = "/img/frontend/gmap/big_shadow.png";
			iconProperties.iconSize = new GSize(24, 36);
			iconProperties.shadowSize = new GSize(37, 34);
			iconProperties.iconAnchor = new GPoint(6, 34);
			iconProperties.title = 'click me.';
			
		break;
		case "more":
			iconProperties.image = "/img/frontend/gmap/small_marker.png";
			iconProperties.shadow = "/img/frontend/gmap/small_shadow.png";
			iconProperties.iconSize = new GSize(18, 27);
			iconProperties.shadowSize = new GSize(22, 20);
			iconProperties.iconAnchor = new GPoint(6, 20);
		break;
		case "province":
		case "article":
			iconProperties.image = "/img/frontend/gmap/article_marker.png";
			iconProperties.shadow = "/img/frontend/gmap/small_shadow.png";
			iconProperties.iconSize = new GSize(18, 27);
			iconProperties.shadowSize = new GSize(22, 20);
			iconProperties.iconAnchor = new GPoint(6, 20);
		break;
	}
	iconProperties.infoWindowAnchor = new GPoint(5, 1);
	
	return iconProperties;
}

function addMarker(map,arr,iconProperties)
{
	// Set up our GMarkerOptions object
	markerOptions = { icon:iconProperties };
	markerOptions.title = 'click here for more information.';

	var point = new GLatLng(arr['lat'],arr['lon']);
	var marker = new GMarker(point, markerOptions);
	
	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(arr['text'],{maxWidth:300});
	});

	//only if in map bounds
	//map.addOverlay(marker);

	return marker;
}

function loadMapLocation(lat,lng,zoom)
{
	lat = Number(lat);
	lng = Number(lng);
	zoom = Number(zoom);

	map.setCenter(new GLatLng(lat,lng), zoom);
}

function onMapMove(map,markerArr,zoom)
{
	if(!zoom){
		var zoom = map.getZoom();
	}
  bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var lngSpan		= northEast.lng() - southWest.lng();
	var latSpan		= northEast.lat() - southWest.lat();
	var enabled		= 0;
	for(var i=0;i<markerArr.length;i++)
	{
		if(markerArr[i]['lat'] > southWest.lat()
			&& markerArr[i]['lat'] < northEast.lat()
			&& markerArr[i]['lon'] > southWest.lng()
			&& markerArr[i]['lon'] < northEast.lng()
			&& markerArr[i]['loaded'] == 'false'
			&& (
				(markerArr[i]['type'] == 'province'	&& markerArr[i]['zoom'] == zoom)
				||
				//(markerArr[i]['zoom'] <= zoom && markerArr[i]['type'] == 'more' && $('#moreMarkers').hasClass('listed'))
				(markerArr[i]['type'] == 'more' && $('#moreMarkers').hasClass('listed'))
				||
				//(markerArr[i]['zoom'] <= zoom && markerArr[i]['type'] == 'article' && $('#articleMarkers').hasClass('listed'))
				(markerArr[i]['type'] == 'article' && $('#articleMarkers').hasClass('listed'))
				||
				(markerArr[i]['type'] == 'featured' && $('#featuredMarkers').hasClass('listed'))
			)
		)
		{
			map.addOverlay(markerArr[i]['marker']);
			markerArr[i]['loaded'] = 'true';
			enabled++;
			var response = loadImpressions(markerArr[i]);
			if(response == 'true'){
				markerArr[i]['impression'] = 'true';
			}
		}

		if((markerArr[i]['zoom'] > zoom && markerArr[i]['type'] != 'featured' && markerArr[i]['type'] != 'more' && markerArr[i]['type'] != 'article')||(map.getZoom() != 5 && markerArr[i]['type'] == 'province' )){
			map.removeOverlay(markerArr[i]['marker']);
			markerArr[i]['loaded'] = 'false';
		}
	}

	return enabled;
}

function loadImpressions(marker)
{
	//var response = 'false';
	if(marker['impression'] == 'false' && marker['id'] > 0)
	{
		$.ajax({
			type: 'GET',
			url: '/impressions/addimpression/id:'+marker['id']+'/type:map_'+marker['type'],
			dataType: 'html',
			//async: false,
			//error: function() {alert('error');},
			success: function(html) {
				//response = html;
			}
		});
	}
	
	//return response;
	return 'true';
}

function randomMapPlotings(map,randomMarkerArr)
{
	bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var lngSpan = northEast.lng() - southWest.lng();
	var latSpan = northEast.lat() - southWest.lat();

	var iconProperties = new GIcon(G_DEFAULT_ICON);
	iconProperties.image = "/img/frontend/gmap/small_marker.png";
	iconProperties.shadow = "/img/frontend/gmap/small_shadow.png";
	iconProperties.iconSize = new GSize(12, 20);
	iconProperties.shadowSize = new GSize(22, 20);
	iconProperties.iconAnchor = new GPoint(6, 20);
	markerOptions = { icon:iconProperties };

	for (var i = 0; i < 8000; i++)
	{
		var lat = southWest.lat() + latSpan * Math.random();
		var lng = southWest.lng() + lngSpan * Math.random();
		var point = new GLatLng(lat,lng);
		var marker = new GMarker(point, markerOptions);
		//map.addOverlay(marker);
		randomMarkerArr[i] = new Array();
		randomMarkerArr[i]['lat'] = lat;
		randomMarkerArr[i]['lon'] = lng;
		randomMarkerArr[i]['point'] = point;
		randomMarkerArr[i]['marker'] = marker;
		randomMarkerArr[i]['loaded'] = 'false';
	}
}