
/* override some functions of the GeoMashup object to work with multi blog maps */

function listProperties(obj) {
   var propList = "";
   for(var propName in obj) {
      if(typeof(obj[propName]) != "undefined") {
         propList += (propName + ", ");
      }
   }
 
}

//mostly the same as the original, but with blog_ids worked in
GeoMashup.addPosts = function(response_data, add_category_info) {

  //ignore categories, regardless of the arg value them being 'on'
  add_category_info = false

  if (add_category_info) {
    for (category_id in this.categories) {
      this.categories[category_id].points.length = 0;
      if (this.categories[category_id].line) {
        this.map.removeOverlay( this.categories[category_id].line );
      }
    }
  }
  for (var i = 0; i < response_data.length; i++) {
    // Make a marker for each new post location
    var post_id = response_data[i].post_id;
    var cross_blog_identifier = post_id+'_'+response_data[i].blog_id
    var point = new GLatLng(
      parseFloat(response_data[i].lat),
      parseFloat(response_data[i].lng));    
    // Update categories
    for (var j = 0; j < response_data[i].categories.length; j++) {
    	if(response_data[i].categories[j] === "") response_data[i].categories[j] = "1";
      var category_id = response_data[i].categories[j];
      this.extendCategory(point, category_id, post_id); 
    }
    if (this.opts.max_posts && this.post_count >= this.opts.max_posts) break;
    if (!this.posts[cross_blog_identifier]) {
      // This post has not yet been loaded
      this.post_count++;
      if (!this.locations[point]) {
        // There are no other posts yet at this point, create a marker
        this.locations[point] = new Object();
        this.locations[point].posts = new Array();
        this.locations[point].posts.push(post_id);
        this.locations[point].loaded = false;
        this.posts[cross_blog_identifier] = response_data[i];
        var marker = this.createMarker(point, response_data[i]);
        this.posts[cross_blog_identifier].marker = marker;
        this.locations[point].marker = marker;
        this.map.addOverlay(marker);
        if ( this.map.getZoom() < this.opts.marker_min_zoom ) {
          marker.hide();
        }
      } else {
        // There is already a marker at this point, add the new post to it
        this.locations[point].posts.push(post_id);
        var plus_image;
        var marker = this.locations[point].marker;
        if (typeof(customGeoMashupMultiplePostImage) == 'function') {
          plus_image = customGeoMashupMultiplePostImage(this.opts, marker.getIcon().image);
        }
        if (!plus_image) {
          plus_image = this.opts.url_path + '/images/mm_20_plus.png';
        }
        marker.setImage(plus_image);
        this.posts[cross_blog_identifier] = response_data[i];
        this.addObjectIcon( this.posts[cross_blog_identifier] );
        this.posts[cross_blog_identifier].marker = this.locations[point].marker;
      }
      this.posts[cross_blog_identifier].title = response_data[i].title;
    }
  } // end for each marker
  // Add category lines
  if (add_category_info) this.showCategoryInfo();

  if (this.firstLoad) {
    this.firstLoad = false;
    if (this.opts.auto_info_open && this.opts.open_post_id) {
      this.clickMarker(this.opts.open_post_id);
    }
    this.updateVisibleList();
  }
}//end addPosts (override)

//largely the same as the original, except it uses urls from extra post data provided by the fma plugin
GeoMashup.createMarker = function(point,post) {
  var marker_opts = {title:post.title};
  if ( !post.icon ) {
    this.addObjectIcon( post );
  }
  marker_opts.icon = post.icon;

  var marker = new GMarker(point,marker_opts);

  // Show this markers index in the info window when it is clicked
  GEvent.addListener(marker, "click", function() {
    var post_ids, i, url, info_window_request, post_element, post_request;
    GeoMashup.map.closeInfoWindow();
    if (GeoMashup.locations[point].loaded) {
      marker.openInfoWindow(GeoMashup.locations[point].info_node);
      if (GeoMashup.opts.show_post && GeoMashup.getShowPostElement()) {
        GeoMashup.getShowPostElement().innerHTML = GeoMashup.locations[point].post_html;
      }
    } else {
      marker.openInfoWindowHtml('<div align="center"><img src="' +
        GeoMashup.opts.url_path + 
        '/images/busy_icon.gif" alt="Loading..." /></div>');
      post_ids = '';
      for(i=0; i<GeoMashup.locations[point].posts.length; i++) {
        if (i>0) post_ids += ',';
        post_ids += GeoMashup.locations[point].posts[i];
      }
      //~ url = GeoMashup.opts.url_path + '/geo-query.php?post_ids=' + post_ids; //original single blog way
      url = post.geo_url + '/geo-query.php?post_ids=' + post_ids;
      info_window_request = new GXmlHttp.create();
      info_window_request.open('GET', url, true);
      info_window_request.onreadystatechange = function() {
        var node, links, i;
        if (info_window_request.readyState == 4 && info_window_request.status == 200) {
          node = document.createElement('div');
          node.innerHTML = info_window_request.responseText;
          links = node.getElementsByTagName('a');
          for (i=0; i<links.length; ++i) {
            links[i].onclick = function () { return GeoMashup.directLink(this); }
          }
          GeoMashup.locations[point].info_node = node;
          GeoMashup.locations[point].loaded = true;
          marker.openInfoWindow(node);
          if (GeoMashup.opts.show_post && 'full-post' !== GeoMashup.opts.template && GeoMashup.getShowPostElement()) {
            url += '&template=full-post';
            post_request = new GXmlHttp.create();
            post_request.open('GET', url, true);
            post_request.onreadystatechange = function() {
              if (post_request.readyState == 4 && post_request.status == 200) {
                GeoMashup.getShowPostElement().innerHTML = post_request.responseText;
                GeoMashup.locations[point].post_html = post_request.responseText;
              } // end readystate == 4
            }; // end onreadystatechange function
            post_request.send(null);
          }
        } // end readystate == 4
      }; // end onreadystatechange function
      info_window_request.send(null);
    } // end post not loaded yet 
  }); // end marker click

  GEvent.addListener(marker, 'infowindowclose', function() {
    var post_element = GeoMashup.getShowPostElement();
    if (post_element) {
      post_element.innerHTML = '';
    }
  });

  return marker;
  
}//end createMarker (override)

/* ----------------------------------------------------------------------------------------------------------------------------
*/


GeoMashup.createMap = function(container, opts) {

		this.container = container;

		this.checkDependencies();

		this.base_color_icon = new GIcon();

		this.base_color_icon.image = opts.url_path + '/images/mm_20_black.png';

		this.base_color_icon.shadow = opts.url_path + '/images/mm_20_shadow.png';

		this.base_color_icon.iconSize = new GSize(12, 20);

		this.base_color_icon.shadowSize = new GSize(22, 20);

		this.base_color_icon.iconAnchor = new GPoint(6, 20);

		var size;
		if(opts.height && opts.width)
			size = new GSize(opts.width,opts.height);

		this.base_color_icon.infoWindowAnchor = new GPoint(5, 1);

		this.multiple_category_icon = new GIcon(this.base_color_icon);

		this.multiple_category_icon.image = opts.url_path + '/images/mm_20_mixed.png';
		
		var gmap_opts = {backgroundColor : '#' + opts.background_color};
		if(size)
			gmap_opts['size'] = size;

		this.map = new GMap2(this.container, gmap_opts);

		this.map.setCenter(new GLatLng(0,0), 1);

		this.map.addMapType(G_PHYSICAL_MAP);
		




		if (window.location.search == this.getCookie('back_search'))

		{
// this cookie setting business was overridding my settings for map type and zoom why this only started acting up now, I will never know!
//			this.loadSettings(opts, this.getCookie('back_settings'));
		}



		// Falsify options to make tests simpler

		for ( opt in opts ) {

			if ( opts.hasOwnProperty( opt ) && typeof opt !== 'function' ) {

				if ( 'false' === opts[opt] || 'FALSE' === opts[opt] ) {

					opts[opt] = false;

				}

			}

		}

		this.opts = opts;



		if (typeof(opts.zoom) == 'string') {

			opts.zoom = parseInt(opts.zoom);

		} else if (typeof(opts.zoom) == 'undefined') {

			opts.zoom = 5;

		}



		if (typeof(opts.map_type) == 'string') {

			var typeNum = parseInt(opts.map_type);



			if (isNaN(typeNum)) {

				opts.map_type = eval(opts.map_type);

			} else {

				opts.map_type = this.map.getMapTypes()[typeNum];

			}

		} else if (typeof(opts.map_type) == 'undefined') {

			opts.map_type = G_NORMAL_MAP;

		}



		if (opts.load_kml)

		{

			this.kml = new GGeoXml(opts.load_kml);

			this.map.addOverlay(this.kml);

		}



		this.buildCategoryHierarchy();



		if (opts.center_lat && opts.center_lng) {

			// Use the center form options
/* ---------------------------------------------------------------------------------------------------------------------------- */
/* 21-04-2011 LG -> the folloing 5 lines move the contextual map center to the right - which results in the group of markers moving to the left... The single maps remain the same which is cool.
/* ---------------------------------------------------------------------------------------------------------------------------- */ 
			if (opts.map_content == 'single') {
			
				this.map.setCenter(new GLatLng(opts.center_lat, opts.center_lng), opts.zoom, opts.map_type);
				
			} else {
				
				this.map.setCenter(new GLatLng(opts.center_lat, (opts.center_lng+6)), opts.zoom, opts.map_type);
				
			}

		} else if (this.kml) {

			this.map.setCenter(this.kml.getDefaultCenter, opts.zoom, opts.map_type);

		} else if (opts.post_data && opts.post_data.posts[0]) {

			var center_latlng = new GLatLng(opts.post_data.posts[0].lat, opts.post_data.posts[0].lng);

			this.map.setCenter(center_latlng, opts.zoom, opts.map_type);

			if (this.opts.auto_info_open && !this.opts.open_post_id) {

				this.opts.open_post_id = opts.post_data.posts[0].post_id;

			}

		} else {

			// Center on the most recent located post

			var request = GXmlHttp.create();

			var url = this.opts.url_path + '/geo-query.php?limit=1';

			if (opts.map_cat) {

				url += '&cat='+opts.map_cat;

			}

			request.open("GET", url, false);

			request.send(null);

			var posts = eval(request.responseText);

			if (posts.length>0) {

				var point = new GLatLng(posts[0].lat,posts[0].lng);

				this.map.setCenter(point,opts.zoom,opts.map_type);

				if (this.opts.auto_info_open) {

					this.opts.open_post_id = posts[0].post_id;

				}

			} else {

				this.map.setCenter(new GLatLng(0,0),opts.zoom,opts.map_type);

			}

		}



		GEvent.bind(this.map, "zoomend", this, this.adjustZoom);

		GEvent.bind(this.map, "moveend", this, this.adjustViewport);



		if (opts.map_content == 'single')

		{

			if (opts.center_lat && opts.center_lng && !this.kml)

			{

				var marker_opts = new Object();

				if (typeof(customGeoMashupSinglePostIcon) == 'function') {

					marker_opts.icon = customGeoMashupSinglePostIcon(this.opts);

				}

				this.map.addOverlay(new GMarker(new GLatLng(this.opts.center_lat,this.opts.center_lng), marker_opts));

			}

		}

		else if (opts.post_data)

		{

			this.addPosts(opts.post_data.posts,true);

		}

		else

		{

			// Request posts near visible range first

			this.requestPosts(true);



			// Request all posts

			this.requestPosts(false);

		}



		if ('GSmallZoomControl' == opts.map_control) {

			this.map.addControl(new GSmallZoomControl());

		} else if ('GSmallZoomControl3D' == opts.map_control) {

			this.map.addControl(new GSmallZoomControl3D());

		} else if ('GSmallMapControl' == opts.map_control) {

			this.map.addControl(new GSmallMapControl());

		} else if ('GLargeMapControl' == opts.map_control) {

			this.map.addControl(new GLargeMapControl());

		} else if ('GLargeMapControl3D' == opts.map_control) {

			this.map.addControl(new GLargeMapControl3D());

		}



		if (opts.add_map_type_control) {

			this.map.addControl(new GMapTypeControl());

		}



		if (opts.add_overview_control) {

			this.overview_control = new GOverviewMapControl();

			this.overview_control.setMapType( opts.map_type );

			this.map.addControl( this.overview_control );

			var ov = document.getElementById('gm-overview');

			if (ov) {

				ov.style.position = 'absolute';

				this.container.appendChild(ov);

			}

		}



		if (this.addCategoryControl) {

			this.map.addControl(new GeoMashupCategoryControl());

		}



		if (typeof(customizeGeoMashupMap) == 'function') {

			customizeGeoMashupMap(this.opts, this.map);

		}



		if (typeof(customizeGeoMashup) == 'function') {

			customizeGeoMashup(this);

		}


	}
	

