// Include Advantage API Toolkit
document.write('<script src="http://btilelog.access.mapquest.com/tilelog/transaction?transaction=script&key=mjtd%7Clu6t2901nu%2Crx%3Do5-laawg&ipr=true&itk=true&itk=true&v=b2btk" type="text/javascript"></script>');

// Object with default values.  Blah = new mapAttributeSet(); Blah.mapDiv = "MyMap"; etc.
function mapAttributeSet() {
	// Set defaults.
	this.mapDiv = false;					// REQUIRED
	this.mapPointsLat = false;				// REQUIRED
	this.mapPointsLon = false;				// REQUIRED
	this.mapLabelTitle = false;
	this.mapLabelText = false;
	this.mapSmallLabels = false;
	this.mapLegend = false;
	this.mapType = "map";					// Legitimate values: "map" - 2D normal map; "sat" - satellite imagery; "hyb" - hybrid map
	this.mapDefaultZoomLevel = 11;
	this.mapInfoDelimiter = "||";
	this.zoomPlacement = new MQMapCornerPlacement(MQMapCorner.TOP_LEFT, new MQSize(2,18)); 
	this.zoomLargePlacement = false;
	this.viewPlacement = false;
	this.panPlacement = new MQMapCornerPlacement(MQMapCorner.TOP_LEFT, new MQSize(2,60));
	this.useLegendIcons = true;				// Used if mapLegend is defined
        this.mapLegendIconPath = "/images/maps/";		// Required if useLegendIcons = true
        this.mapLegendIconFilename = "markers_iconbg_";		// Required if useLegendIcons = true
        this.mapLegendIconExt = "png";				// Required if useLegendIcons = true
        this.mapLegendIconWidth = 36;
        this.mapLegendIconHeight = 36;
	this.mapLegendIconIsPNG = true;
	this.mapLegendIconShadow = "off";
	this.mapLegendRedrawOffset = false;
	this.OffsetX = 0;
	this.OffsetY = -30;
}

// Test to see if div exists and contains usable info.
function validityTest(mapAttribute) {
	if(mapAttribute && document.getElementById(mapAttribute)) {
		if(document.getElementById(mapAttribute).innerHTML != "") {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

// Function for turning info inside divs into usable arrays.
function splitInnerHTML(whichDiv,stringDelimiter) {
	var ArrayOfStuff = new Array();
	var Blob = document.getElementById(whichDiv).innerHTML;
	ArrayOfStuff = Blob.split(stringDelimiter);
	return ArrayOfStuff;
}

// Compare length of two arrays.
function compareArrLen(Arr1,Arr2) {
	if(Arr1.length == Arr2.length) {
		return true;
	} else {
		return false;
	}
}

// Called in body onLoad.  Create the map ( createMap(mapDiv); )and fill it out with points of interest if conditions are met.
function showMap(mapAttributeObj) {
	try {
		// Stuff required to draw the map: browser is supported, Latitude and Longitude coordinates, and the map div.
		if(isSupportedBrowser() && document.getElementById(mapAttributeObj.mapDiv) && validityTest(mapAttributeObj.mapPointsLat) && validityTest(mapAttributeObj.mapPointsLon)) {
			// Declare all possible arrays of stuff.
			var Latitudes = new Array();
			var Longitudes = new Array();
			var LabelTitles = new Array();
			var LabelText = new Array();
			var SmallLabels = new Array();
			var Legend = new Array();

			Latitudes = splitInnerHTML(mapAttributeObj.mapPointsLat,mapAttributeObj.mapInfoDelimiter);
			Longitudes = splitInnerHTML(mapAttributeObj.mapPointsLon,mapAttributeObj.mapInfoDelimiter);
			// Don't proceed unless these arrays are the same length.  Something is wrong if they're not.
			if(compareArrLen(Latitudes,Longitudes)) {
				// Optional attributes.
				// Titles for small popup bubble.
				if(validityTest(mapAttributeObj.mapLabelTitle)) {
					LabelTitles = splitInnerHTML(mapAttributeObj.mapLabelTitle,mapAttributeObj.mapInfoDelimiter);
				}
				// Body text for small popup bubble.
				if(validityTest(mapAttributeObj.mapLabelText)) {
					LabelText = splitInnerHTML(mapAttributeObj.mapLabelText,mapAttributeObj.mapInfoDelimiter);
				}
				// "Legend" key for multiple points -- used for custom icons.
				if(validityTest(mapAttributeObj.mapLegend)) {
					Legend = splitInnerHTML(mapAttributeObj.mapLegend,mapAttributeObj.mapInfoDelimiter); 
				}
				// Small label underneath point on the map.
				if(validityTest(mapAttributeObj.mapSmallLabels)) {
					SmallLabels = splitInnerHTML(mapAttributeObj.mapSmallLabels,mapAttributeObj.mapInfoDelimiter);
				}

				// Variables all set.  Now create map.
				theMap = new MQTileMap(document.getElementById(mapAttributeObj.mapDiv));
				theMap.setMapType(mapAttributeObj.mapType);

				// Zoom control?
				if(mapAttributeObj.zoomPlacement) {
					zoomControl = new MQZoomControl(theMap);
					theMap.addControl(zoomControl, mapAttributeObj.zoomPlacement);
				}

				// Large zoom control?
				if(mapAttributeObj.zoomLargePlacement) {
					zoomLargeControl = new MQLargeZoomControl(theMap);
					theMap.addControl(zoomLargeControl, mapAttributeObj.zoomLargePlacement);
				}

				// View control?
				if(mapAttributeObj.viewPlacement) {
					viewControl = new MQViewControl(theMap);
					theMap.addControl(viewControl, mapAttributeObj.viewPlacement);
				}

				// Pan control?
				if(mapAttributeObj.panPlacement) {
					panControl = new MQPanControl(theMap);
					theMap.addControl(panControl, mapAttributeObj.panPlacement);
				}

				// Variables & behavior all set.  Now plot the points.
				for (i=0; i < Latitudes.length; i++) {
					if(!isNaN(Latitudes[i]) && !isNaN(Longitudes[i])) {
						thisPoint = new MQLatLng(Latitudes[i],Longitudes[i]);
						if(thisPoint.valid) {
							newPOI = new MQPoi(thisPoint);
							if(LabelTitles[i] && compareArrLen(LabelTitles,Latitudes)) newPOI.setInfoTitleHTML(LabelTitles[i]);
							if(LabelText[i] && compareArrLen(LabelText,Latitudes)) newPOI.setInfoContentHTML(LabelText[i]);
							if(SmallLabels[i] && compareArrLen(SmallLabels,Latitudes)) newPOI.setLabel(SmallLabels[i]);
							if(Legend[i] && compareArrLen(Legend,Latitudes) && mapAttributeObj.useLegendIcons) {
//								poi.setKey(Legend[i]);
								var iconURL = mapAttributeObj.mapLegendIconPath + mapAttributeObj.mapLegendIconFilename + Legend[i] + "." + mapAttributeObj.mapLegendIconExt;
								thisLegendIcon = new MQMapIcon();
								anchorPoint = new MQPoint(mapAttributeObj.OffsetX,mapAttributeObj.OffsetY);
								thisLegendIcon.setAnchorOffset(anchorPoint);
								thisLegendIcon.setImage(iconURL,mapAttributeObj.mapLegendIconWidth,mapAttributeObj.mapLegendIconHeight,mapAttributeObj.mapLegendRedrawOffset,mapAttributeObj.mapLegendIconIsPNG);
								if(mapAttributeObj.mapLegendIconShadow == "on") {
									thisLegendIcon.setShadow('http://img.mqcdn.com/mqtoolkit/shadow.png', -2, 20, 23, 7, true);
								} else {
									thisLegendIcon.setShadow('', 0, 0, 0, 0, false);
								}
								newPOI.setIcon(thisLegendIcon);
							}
							theMap.addPoi(newPOI);
						} // else: point not valid
					} // else: lat or lon wasn't numeric
				} // End loop.
				// Center.
				if(Latitudes.length > 1) {
					theMap.bestFit();
				} else {
					theMap.setCenter(thisPoint,mapAttributeObj.mapDefaultZoomLevel);
				}
			}
		}
	} catch(e) {
		// Don't do anything!
	}
}

