function initMap(lat,lng,zoom,controler,type) {

	var map = initGMap(lat,lng,zoom,controler,type);

	// We need to keep track of all the markers we add to the map to ensure we don't add duplicates in the mediaUpdater function

		try {
			var updateTimer = null;

			// Only update media 2 seconds after a map movement has ended and cancel any other media updates that may be pending.
			GEvent.addListener ( map, 'moveend', function () {
				if ( updateTimer != null )
				clearTimeout ( updateTimer );
				setTimeout ( mediaUpdater, 2000 );
			} );

			mediaUpdater(true,true);

		} catch (ex) { console.log('error: '+ex); }

}

function mediaUpdater(users,media) {

	if ($('map').getWidth() < 300) {
		var picSize = 13;
	} else {
		var picSize = 14;
	}
	
	// if no user or media was passed
	if (!users && !media) {
		// if this page has the ability to show and hide users and media
		if ($('cb_users') && $('cb_media')) {
			if ($('cb_users').checked) {
				var users = true;
			} else {
				var users = false;
			}
			if ($('cb_media').checked) {
				var media = true;
			} else {
				var media = false;
			}
		} else {
			var users = true;
			var media = true;
		}
	} else {
		var users = users;
		var media = media;
	}
	
	var showPhotos = true;
	var showVideos = true;	
	
	if ($('cb_photos')) {
		if ($('cb_photos').checked == true) {
			showPhotos = true;
		} else {
			showPhotos = false;
		}
	} else {
		showPhotos = true;
	}
	
	if ($('cb_videos')) {
		if ($('cb_videos').checked == true) {
			showVideos = true;
		} else {
			showVideos = false;
		}
	} else {
		showVideos = true;
	}
	
	//console.log("showPhotos "+showPhotos+", showVideos "+showVideos+", media "+media);
	
	var mapMarkers = new Hash ();

	var params = {
		'vhost' : selectedVHost,
		'northEast' : [map.getBounds().getNorthEast().lat(),map.getBounds().getNorthEast().lng()],
		'southWest' : [map.getBounds().getSouthWest().lat(),map.getBounds().getSouthWest().lng()],
		'moderationStatus': 1               
	}
	
	if(users == false || media == false || showPhotos == false || showVideos == false) {
		map.clearOverlays();
	}
	
	if (media == true) {
	
		jsonRequest('geo.getMediaFromArea',params,function(result) {

			for(var i=0; i<result.length; i++) {
				
				if ((result[i].filetype == 1 && showPhotos) || (result[i].filetype == 2 && showVideos)) {
				
					console.log(result[i]);
				
					var item = result[i];
					if ( mapMarkers.get ( 'u' + item.id ) )
					continue;
	
					var latlng = new GLatLng(item.geo_latitude,item.geo_longitude);
					var marker = new AvatarMarker(latlng,item.thumbUrl+"/11",24);
	
					marker.info = item;
				
					GEvent.addListener(marker,'click',function() {
						var mid = this.info.thumbUrl.substr(this.info.thumbUrl.lastIndexOf("/")+1,this.info.thumbUrl.length);
						//
						var incImg = new Image();
						incImg.src = this.info.thumbUrl+'/'+picSize;
						if (incImg.complete) {
							infoW = incImg.width
							infoH = incImg.height;
						} else {
							infoW = 320;
							infoH = 260;
						}
						
						if ($('map').getWidth() > 300) {
							var short = '<div class="right">'+this.info.hits+' views</div>';
							var infoT = infoW - 80;
						} else {
							var short = "";
							var infoT = infoW;
						}
						
						this.openInfoWindowHtml (
							'<div class="mediainfo mapmarker">'+
								'<div class="title" style="width:'+infoT+'px;">'+
									'<a href="/mediadetail/'+mid+'-'+this.info.title+'">'+
										this.info.title+
									'</a>'+
								'</div>'+short+
								'<div class="clear"></div>'+
								'<a href="/mediadetail/'+mid+'-'+this.info.title+'">'+
									'<img src="'+this.info.thumbUrl+'/'+picSize+'" alt="'+this.info.title+'" style="width:'+infoW+'px; height:'+infoH+'px;"/>'+
								'</a>'+
								'<div class="clear"></div>'+
							'</div>'
						);
					});
	
					map.addOverlay(marker);
					mapMarkers.set ( 'm' + i, marker );
					
				}
			}
			
		},false,true);

	}

	if (users == true) {

		jsonRequest('geo.getUsersFromArea',params,function(result) {

			for(var i=0; i<result.length; i++) {

				var item = result[i];
				if ( mapMarkers.get ( 'u' + item.id ) )
				continue;

				var latlng = new GLatLng(item.geo_latitude,item.geo_longitude);
				var marker = new AvatarMarker(latlng,'http://www.gravatar.com/avatar/' + item.gravatarid + '?s=16&d=identicon',16);

				marker.userInfo = item;
				
				GEvent.addListener(marker,'click',function() {
					this.openInfoWindowHtml(
						'<div class="mapmarker mapuser" style="height:68px;">' +
							'<a href="/profile/' + this.userInfo.id + '" class="avatar left">' + 
								'<img src="http://www.gravatar.com/avatar/' + this.userInfo.gravatarid + '?s=64&d=identicon"/>' + 
							'</a>'+
							'<div class="left">' +
								'<ul>'+
									'<li>'+
										'<a href="/profile/' + this.userInfo.id + '"> ' + this.userInfo.user.escapeHTML() + '</a>' +
									'</li>'+
									'<li>'+this.userInfo.city+'</li>'+
									'<li>'+this.userInfo.country+'</li>'+
								'</ul>'+
								''+ 
								
							'</div>'+
							'<div class="clear"></div>'+
						'</div>'
					);
				});

				map.addOverlay(marker);
				mapMarkers.set ( 'u' + item.id, marker );

			}

		},false,true);

	}

}




function initGMap(lat,lng,zoom,controler,type) {

	var lat = lat;
	var lng = lng;
	var zoom = zoom;

    // Google maps initialization
	window.map = new GMap2(document.getElementById("map"));
    var map = window.map;

	//console.log(lat+","+lng);

    var latlng = new GLatLng(lat, lng);

	if (!zoom) {
		zoom = 4;
	}

    // Zoom level 4 will allow the majority of North America to appear in the map by default
    map.setCenter(latlng, zoom);

	if (controler) {
		if ($('map').getHeight() < 260) {
		    var zoomer = new GSmallMapControl();	
		} else {
    		var zoomer = new GLargeMapControl();
    	}
    	map.addControl(zoomer);
	}
	
	if (type) {
    	var maptype = new GMapTypeControl(false);
    	map.addControl(maptype);
    }

    map.enableScrollWheelZoom();
    map.enableContinuousZoom();
    
    if ($('maploading')) {
    	$('maploading').remove();
    }
   
    return map;

}

function selectLocation(map,latlng, center) {
  if (center) map.setCenter(latlng, 10);
}

function searchAddress(searchString) {

   var geocoder = new GClientGeocoder();
   geocoder.getLatLng(searchString, function(point) { 

        if (point) {
            selectLocation(window.map, point, true);
        } else {
            Messenger.message(1,searchString + ' not found!');
        }

   });
   
}

