var map = null;
var geocoder = null;
var points = [];
var markers = [];
var lats = [];
var lngs = [];
var entries = [];
var nanIcon = null;
var myLocationMarker = null;

function initialize() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map_canvas"));

// CENTERS ON USERS LOCATION		
/*		if ($('#user-lat').length > 0){
			var center = new GLatLng($('#user-lat').text(),$('#user-lng').text());
		} else {
			var center = new GLatLng("43.731331","-79.398396");
		}
*/

		var center = new GLatLng("43.731331","-79.398396");


    map.addControl(new GLargeMapControl3D());
    map.setCenter(center, 11);
    geocoder = new GClientGeocoder();

    nanIcon = new GIcon(G_DEFAULT_ICON);
		nanIcon.image = '/sites/all/themes/nan/images/nan-marker.png';
		nanIcon.shadow = '/sites/all/themes/nan/images/nan-marker-shadow.png';
		nanIcon.highlight = '/sites/all/themes/nan/images/nan-marker-hover.png';
		nanIcon.iconSize = new GSize(28, 38);
		nanIcon.shadowSize = new GSize(44, 39);
		nanIcon.iconAnchor = new GPoint(13, 38);
		
		$('#main .views-row').each(function(i,n){
			lats.push(parseFloat($(this).find('.views-field-field-latitude-value span').text()));
			lngs.push(parseFloat($(this).find('.views-field-field-longitude-value span').text()));
			entries.push($(this));
		})
		
		var bounds = map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		var maxLng = northEast.lng();
		var maxLat = northEast.lat();
		var minLng = southWest.lng();
		var minLat = southWest.lat();
		for (var i=0; i<lats.length; i++){
			if (lats[i] > maxLat) {
				entries[i].hide();
			} else if (lats[i] < minLat) {
				entries[i].hide();
			} else if (lngs[i] > maxLng) {
				entries[i].hide();
			} else if (lngs[i] < minLng) {
				entries[i].hide();
			} else {
				entries[i].show();
			}
		}

		GEvent.addListener(map,'moveend',function(){
			var bounds = map.getBounds();
			var southWest = bounds.getSouthWest();
			var northEast = bounds.getNorthEast();
			var maxLng = northEast.lng();
			var maxLat = northEast.lat();
			var minLng = southWest.lng();
			var minLat = southWest.lat();
			for (var i=0; i<lats.length; i++){
				if (lats[i] > maxLat) {
					entries[i].hide();
				} else if (lats[i] < minLat) {
					entries[i].hide();
				} else if (lngs[i] > maxLng) {
					entries[i].hide();
				} else if (lngs[i] < minLng) {
					entries[i].hide();
				} else {
					entries[i].show();
				}
			}
		})
	}
	if ($('body').hasClass('view-map-members')){
		$('#main .views-row').each(function(i,n){
			$this = $(this);
			var point = null;
			var title = $this.find('.views-field-name').html();
			var image = $this.find('.views-field-picture-1').html();
			var disciplines = $this.find('.views-field-tid').html();
			var description = $this.find('.views-field-field-member-about-value').html();
			var lat = $this.find('.views-field-field-latitude-value span').text();
			var lng = $this.find('.views-field-field-longitude-value span').text();
			
			if (lat) {
				point = new GLatLng(parseFloat(lat),parseFloat(lng));
				points.push(point);
			}
			
			if (point) {
				var marker = new GMarker(point,{icon:nanIcon});
				$this.data('marker',marker);
				marker.point = point;
				marker.el = $this;
				GEvent.addListener(marker,'click',function(){
					var tabs=[];
					tabs.push(new GInfoWindowTab('Info','<div class="info-window"><div class="image">'+image+'</div><div class="title">'+title+'</div><div class="description">'+description+'</p>'));
					map.openInfoWindowTabsHtml(marker.getLatLng(),tabs,{pixelOffset:new GSize(8,-30),maxWidth:220});
				})
				GEvent.addListener(marker,'mouseover',function(){
					marker.setImage('/sites/all/themes/nan/images/nan-marker-hover.png');
					marker.el.addClass('selected');
				})
				GEvent.addListener(marker,'mouseout',function(){
					marker.setImage('/sites/all/themes/nan/images/nan-marker.png');
					marker.el.removeClass('selected');
				})
				markers.push(marker);
			}		
		})
	} else {
		$('#main .views-row').each(function(i,n){
			$this = $(this);
			var point = null;
			var title = $this.find('.views-field-field-name-value').html();
			var name = $this.find('.views-field-name').html();
			var image = $this.find('.views-field-field-cover-fid-1').html();
			var disciplines = $this.find('.views-field-tid').html();
			var description = $this.find('.views-field-field-description-value').html();
			var lat = $this.find('.views-field-field-latitude-value span').text();
			var lng = $this.find('.views-field-field-longitude-value span').text();
			
			if (lat) {
				point = new GLatLng(parseFloat(lat),parseFloat(lng));
				points.push(point);
			}
			
			if (point) {
				var marker = new GMarker(point,{icon:nanIcon});
				$this.data('marker',marker);
				marker.point = point;
				marker.el = $this;
				GEvent.addListener(marker,'click',function(){
					var tabs=[];
					tabs.push(new GInfoWindowTab('Info','<div class="info-window"><div class="image">'+image+'</div><div class="title">'+title+'</div><div class="name">'+name+'</div><p class="description">'+description+'</p>'));
					map.openInfoWindowTabsHtml(marker.getLatLng(),tabs,{pixelOffset:new GSize(8,-30),maxWidth:220});
				})
				GEvent.addListener(marker,'mouseover',function(){
					marker.setImage('/sites/all/themes/nan/images/nan-marker-hover.png');
					marker.el.addClass('selected');
				})
				GEvent.addListener(marker,'mouseout',function(){
					marker.setImage('/sites/all/themes/nan/images/nan-marker.png');
					marker.el.removeClass('selected');
				})
				markers.push(marker);
			}		
		})
	}	
	$('#main .views-row').click(function(){
		GEvent.trigger($(this).data('marker'),'click');
		return false;
	})
	$('#main .views-row').hover(function(){
		GEvent.trigger($(this).data('marker'),'mouseover');
	},function(){
		GEvent.trigger($(this).data('marker'),'mouseout');
	})
	
	for (var i = 0; i < markers.length; i++){
		map.addOverlay(markers[i]);
	}
		
}

function showAddress(address) {
  if (geocoder) {
    geocoder.getLatLng(
			address+'Toronto, Ontario',
      function(point) {
        if (!point) {
          alert(address + " not found");
        } else {
          map.panTo(point);
        }
      }
    );
  }
}



