function infoWindow(latlng, html) {
	this.latlng_ = latlng;
	this.html_ = html;
	this.prototype = new GOverlay();

	// create infoWindow div
	this.initialize = function(map) {
		var div = $('<div />');
		div.css({
			position: 'absolute',
			width: '200px'
		}).appendTo(map.getPane(G_MAP_FLOAT_PANE))

		this.map_ = map;
		this.div_ = div;

		this.update(html);
	}

	this.update = function(html) {
		this.html_ = html;

		this.div_.empty();

		var content = $('<div />').addClass('info-window').html(html).appendTo(this.div_);

		this.redraw(true);
	}

	// remove div
	this.remove = function() {
	  this.div_.remove();
	}

	// copy data to new instance
	this.copy = function() {
	  return new infoWindow(this.latlng_, this.html_);
	}

	// redraw based on the current projection and zoom level
	this.redraw = function(force) {
		if (!force) return;
		var point = this.map_.fromLatLngToDivPixel(this.latlng_);

		// position
		this.div_.css({
			top: point.y - 20 + 'px',
			left: point.x + 16 + 'px'
		});
	}
}

function ResultMap() {
	this.map;
	this.blueIcon;
	this.markerOptions;
	this.markers = [];
}

ResultMap.prototype.initialize = function(container, markers) {
	if(GBrowserIsCompatible()) {
		this.map = new GMap2(document.getElementById(container));
		this.map.setCenter(new GLatLng(51.540997,-0.103683), 13);
		this.map.addControl(new GSmallMapControl());
		
		this.greenIcon = new GIcon();
		this.greenIcon.image = "/images/maps/green-icon.png";
		this.greenIcon.shadow = "";
		this.greenIcon.iconSize = new GSize(22, 22);
		this.greenIcon.shadowSize = new GSize(0, 0);
		this.greenIcon.iconAnchor = new GPoint(6, 20);
		this.greenIcon.infoWindowAnchor = new GPoint(5, 1);
		
		this.pinkIcon = new GIcon();
		this.pinkIcon.image = "/images/maps/pink-icon.png";
		this.pinkIcon.shadow = "";
		this.pinkIcon.iconSize = new GSize(22, 22);
		this.pinkIcon.shadowSize = new GSize(0, 0);
		this.pinkIcon.iconAnchor = new GPoint(6, 20);
		this.pinkIcon.infoWindowAnchor = new GPoint(5, 1);

		this.redIcon = new GIcon();
		this.redIcon.image = "/images/maps/red-icon.png";
		this.redIcon.shadow = "";
		this.redIcon.iconSize = new GSize(22, 22);
		this.redIcon.shadowSize = new GSize(0, 0);
		this.redIcon.iconAnchor = new GPoint(6, 20);
		this.redIcon.infoWindowAnchor = new GPoint(5, 1);
		
		this.orangeIcon = new GIcon();
		this.orangeIcon.image = "/images/maps/orange-icon.png";
		this.orangeIcon.shadow = "";
		this.orangeIcon.iconSize = new GSize(22, 22);
		this.orangeIcon.shadowSize = new GSize(0, 0);
		this.orangeIcon.iconAnchor = new GPoint(6, 20);
		this.orangeIcon.infoWindowAnchor = new GPoint(5, 1);
		
		this.brownIcon = new GIcon();
		this.brownIcon.image = "/images/maps/orange-icon.png";
		this.brownIcon.shadow = "";
		this.brownIcon.iconSize = new GSize(22, 22);
		this.brownIcon.shadowSize = new GSize(0, 0);
		this.brownIcon.iconAnchor = new GPoint(6, 20);
		this.brownIcon.infoWindowAnchor = new GPoint(5, 1);

		// default marker icon
		this.markerOptions = { icon: this.blueIcon };
		
		var length = markers.length;
		for(var count = 0; count < length; count++) {
			var marker = this.create_marker(markers[count].title, markers[count].lat, markers[count].lng, markers[count].type, markers[count].url);
			marker.type = markers[count].type;
			this.map.addOverlay(marker);
			this.markers.push(marker);
		}
	}
}

ResultMap.prototype.create_marker = function(title, lat, lng, type, url) {
	var point = new GLatLng(lat, lng);
	var color;
	var marker;
	var html = "<p>" + title + "</p>";
	var overlay = new infoWindow(point, html);
	var overlayWidth;
	var speed = 200;
	var animating = false;
	
	if(type == "story") {
		color = "pink";
		this.markerOptions = { icon: this.pinkIcon }
	}
	if(type == "event") {
		color = "green";
		this.markerOptions = { icon: this.greenIcon }
	}
	if(type == "company") {
		color = "red";
		this.markerOptions = { icon: this.redIcon }
	}
	if(type == "job") {
		color = "orange";
		this.markerOptions = { icon: this.orangeIcon }
	}
	if(type == "opportunity") {
		color = "brown";
		this.markerOptions = { icon: this.brownIcon }
	}
	
	marker = new GMarker(point, this.markerOptions);
	
	GEvent.bind(marker, "click", this, function() {
		window.location = url;
	});
	
	GEvent.bind(marker, "mouseover", this, function() {
		$(".info-window").remove();
		for(i = 0; i < markers.length; i++) {
		    this.markers[i].setImage(this.markers[i].getIcon().image);
		}
		marker.setImage("/images/maps/" + color + "-icon-hover.png");
		this.map.addOverlay(overlay);
		overlayWidth = $("div#map div.info-window").width();
		if(!animating) {
			$("div#map div.info-window").css({
				width: 0
			});					
			animating = true;
			$("div#map div.info-window").css({
				display: "block"
			}).animate({
				width: overlayWidth + 'px'
			}, speed, function() {
				animating = false;
			});
		}
	});
	
	GEvent.bind(marker, "mouseout", this, function() {
		if(!animating) {
			animating = true;
			$("div#map div.info-window").animate({
				width: 0
			}, speed, function() {
				marker.setImage("/images/maps/" + color + "-icon.png");
				$(this).remove();
				animating = false;
			});
		}
	});
	
	return marker;
}
