var MetaCartaMap = function(settings){			
	this.entries = null;
	
	//the url for the headline link
	var urlBase="http://geosearch.metacarta.com/";
	var encodedUrl=null;
	//these are sample render functions...you can write your own and pass them in a dict to the constructor
	if(settings.renderTitle) this.renderTitle = settings.renderTitle;
	else this.renderTitle = function(data){
		return data["$PLACENAME"];
	}
	if(settings.renderBefore) this.renderBefore = settings.renderBefore;
	else this.renderBefore = function(data){
		return "";
	}
	if(settings.renderHeadline) this.renderHeadline = settings.renderHeadline;
	else this.renderHeadline = function(data){
	  encodedUrl=data["$URL"];
		return '<p><a href="' + urlBase + '?article_id=' + data["$ARTICLEID"] + '&title=' + data["$HEADLINE"] + '" class="' + settings.headlineClass + '" >' + data["$HEADLINE"] + '<\/a><\/p>'
	}
	if(settings.renderAfter) this.renderAfter = settings.renderAfter;
	else this.renderAfter = function(data){
		return "";
	}

        if(settings.defaultMapView){
            this.defaultMapView = settings.defaultMapView;
        }
        else{
            this.defaultMapView = { minLat : -90, minLon : -180, maxLat : 90, maxLon : 180 };
        }
	
	//the callback from query.js calls a global called processEntries, which, in turn, should call this function
	MetaCartaMap.prototype.processEntries = function(e){
		this.entries = e;				
	}
	
	//this function gets called after the entries have been assigned to this.entries
	//it basically plots all of the points on the map and creates the neat little html bubbles
	MetaCartaMap.prototype.addPins = function(){  
		var points = new Array();
		
		with(this){
			for(var i=0; i < entries.geotags.length; i ++){
				var entry = entries.geotags[i];
				
				var infobox_settings = {
					"$PLACENAME" : entry[2],
					"$COUNT" : entry[3].length,
					"$COUNT_PLURAL" : (entry[3].length > 0 ? "s" : ""),
					"$LATITUDE" : entry[0],
					"$LONGITUDE" : entry[1],
					"$URL" : "",
					"$HEADLINE" : "",
					"$ARTICLEID" : ""
				}
				
				var point = new VEShape(VEShapeType.Pushpin, new VELatLong(entry[0], entry[1]));
				point.SetTitle(renderTitle(infobox_settings));
				
				var html = renderBefore(infobox_settings);
				
				for(var j=0; j < entry[3].length; j++){
					var article = entries.articles[entry[3][j]];
					
					var article_settings = {
						"$PLACENAME" : entry[2],
						"$COUNT" : entry[3].length,
						"$COUNT_PLURAL" : (entry[3].length > 0 ? "s" : ""),
						"$LATITUDE" : entry[0],
						"$LONGITUDE" : entry[1],
						"$URL" : entries.baseUrl + article[1],
						"$HEADLINE" : article[0],
						"$ARTICLEID" : article[1]
					}

					html += renderHeadline(article_settings);
				}
				html += renderAfter(infobox_settings);
				point.SetDescription(html);
                
				point.SetCustomIcon(settings.iconImagePath);
				points.push(point);
			}
			// Add the points all at once...this is much faster
			map.AddShape(points);
		}
	}	
	
	
	//this performs the actual drawing on the map...must be called from the onload method, after the page is rendered
	//other VE whines
	MetaCartaMap.prototype.render = function(e){
		this.map = new VEMap(settings.mapDivId);
    this.map.SetDashboardSize(VEDashboardSize.Tiny);
		this.map.LoadMap();				
                var v = this.defaultMapView;
                this.map.SetMapView(
                        new VELatLongRectangle(
                            new VELatLong(v.minLat,v.minLon),
                            new VELatLong(v.maxLat,v.maxLon)));
		this.addPins();
	}
}


//PNG HACK

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}
window.attachEvent("onload", correctPNG); 
