// CONFIG STUFF
var map=null;
var outputDiv='mapdevs';
var mapDiv='map';
var menuDiv='devinfo';
var inputXML='inputXML';
var iconImage="http://www.profileproperties.com/images/map/marker.png";
var iconShadow="http://www.profileproperties.com/images/map/marker_shadow.png";

// Create our "tiny" marker icon
var icon = new GIcon();
icon.image = iconImage;
icon.shadow = iconShadow;
icon.iconSize = new GSize(61, 73);
icon.shadowSize = new GSize(61, 73);
icon.iconAnchor = new GPoint(18, 67);
icon.infoWindowAnchor = new GPoint(5, 1);

// You shouldn't need to edit anything below here.
var map;
var markers=new Array();

function ImagePoint(x, y, name, menuHtml, img, i){
	this.img=img;
	this.x=x;
	this.y=y;
	this.menuHtml=menuHtml;
	this.name=name;
	this.i=i;
	this.marker=null;
	
	this.generatePoint=function(){
		this.marker= new GMarker(new GLatLng(this.x,this.y), icon);
		GEvent.addListener(this.marker, "click", markers[this.i].click.bind(this));
		return this.marker;
	}
	
	this.generateImageBox=function(){
		var divObj=document.createElement('div');
		divObj.className='mapprop';
		try{
			divObj.addEventListener('click', this.click.bind(this), true);
		}catch(e){
			try{
				divObj.attachEvent('onclick', this.click.bind(this));
			}catch(e){
				window.alert("Can't attach events.");
			}
		}
		var imgObj=document.createElement('img');
		imgObj.setAttribute('src', this.img);
		imgObj.setAttribute('alt', this.name);
		divObj.appendChild(imgObj);
		document.getElementById(outputDiv).appendChild(divObj);
	}
	
	this.click=function(){
		map.panTo(this.marker.getPoint());
		document.getElementById(menuDiv).innerHTML=this.menuHtml;
	}
}

function load(){
	try{
		if (GBrowserIsCompatible()) {
			// create the map
			map = new GMap2(document.getElementById(mapDiv));
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.setCenter(new GLatLng( 53.32021, -6.26976), 10);
			pointsObject=document.getElementById(inputXML).value.toXML();
			points=pointsObject.documentElement.getElementsByTagName('point');
			for(i=0; i<points.length; i++){
				imgPoint=new ImagePoint(
							points[i].getAttribute('x'),
							points[i].getAttribute('y'),
							points[i].getAttribute('name'),
							getXmlNodeValue(points[i].getElementsByTagName('menuText')[0]),
							points[i].getAttribute('img'),
							i
					);
				imgPoint.generateImageBox();
				markers.push(imgPoint);
				map.addOverlay(imgPoint.generatePoint());
			}
			
		}else{
			window.alert("Sorry, the Google Maps API is not compatible with this browser");
		}
	}catch(e){
		window.alert("There was an error generating the script\n"+e);
		throw e;
	}
}

// workarounds for internet explorer
function getXmlNodeValue(xmlNode){
	try{
		if(XMLHttpRequest){
			return xmlNode.textContent;
		}
	}catch(e){
		try{
			return xmlNode.text;
		}catch(e){
		}
	}
}

Function.prototype.bind = function(object) {
  var __method = this;
  return function() {
    __method.apply(object, arguments);
  }
}

String.prototype.toXML=function(){
	try{
		dom= (new DOMParser()).parseFromString(this,'text/xml');
		return dom;
	}catch(e){
		try{
			dom=new ActiveXObject("msxml2.DOMDocument.3.0");
			dom.async = false;
			dom.resolveExternals = false;
			try{
				dom.loadXML(this);
				return dom;
			}catch(e){
				window.alert("failed to turn string into xml");
			}
		}catch(e){
		}
	}
}
