/*******************************************************************************
 * New Class: SpiderMarker
 * Original from: http://www.williams.edu/Economics/ArtsEcon/mappages/expandmarkersdemo.htm
 ******************************************************************************/
function SpiderMarker() {
	this.markers = [];
	var thisObject = this;

	this.markerOrder = function(marker, b) {
		return GOverlay.getZIndex(marker.getPoint().lat()) + marker.importance
				* 1000000;
	}

	this.setOuterBounds = function(num) {
		var n_outer = thisObject.markers[num].startcoord.lat()
				+ .5
				* ((thisObject.markers[num].n_outer - thisObject.markers[num].s_outer + .00075) / Math
						.pow(2, -(17 - map.getZoom())));
		var s_outer = thisObject.markers[num].startcoord.lat()
				- .5
				* ((thisObject.markers[num].n_outer - thisObject.markers[num].s_outer) / Math.pow(2,
						-(17 - map.getZoom())));
		var e_outer = thisObject.markers[num].startcoord.lng()
				+ .5
				* ((thisObject.markers[num].e_outer - thisObject.markers[num].w_outer + .00025) / Math
						.pow(2, -(17 - map.getZoom())));
		var w_outer = thisObject.markers[num].startcoord.lng()
				- .5
				* ((thisObject.markers[num].e_outer - thisObject.markers[num].w_outer + .00025) / Math
						.pow(2, -(17 - map.getZoom())));
		var newOuterBounds = new GLatLngBounds(new GLatLng(s_outer, w_outer),
				new GLatLng(n_outer, e_outer));
		return newOuterBounds;
	}

	this.newSpideredMarker = function(temp_marker) {
		temp_marker.startimport = 1;
		temp_marker.importance = 1;
		temp_marker.targeted = 0;
		temp_marker.startcoord = temp_marker.getLatLng();
		temp_marker.openwindow = false;
		temp_marker.line = null;
		temp_marker.shifted = 0;

		var t_error = this.getExactnessTolerance();
		var shift = .0005;
		var n_coord = temp_marker.getLatLng().lat() + t_error;
		var s_coord = temp_marker.getLatLng().lat() - t_error;
		var e_coord = temp_marker.getLatLng().lng() + t_error;
		var w_coord = temp_marker.getLatLng().lng() - t_error;
		temp_marker.inBounds = new GLatLngBounds(new GLatLng(s_coord, w_coord),
				new GLatLng(n_coord, e_coord));

		temp_marker.n_outer = temp_marker.startcoord.lat() + shift;
		temp_marker.s_outer = temp_marker.startcoord.lat() - shift;
		temp_marker.e_outer = temp_marker.startcoord.lng() + shift;
		temp_marker.w_outer = temp_marker.startcoord.lng() - shift;
		temp_marker.outBounds = null;

		GEvent
				.addListener(
						temp_marker,
						"mouseover",
						function() {
							if (map.getZoom() >= 9) {
								var step = 0;
								for (x = 0; x < thisObject.markers.length; x++) {
									var target = thisObject.markers[x]
											.getLatLng();
									if (temp_marker.inBounds
											.containsLatLng(target) == true
											&& thisObject.markers[x].isHidden() == false) {
										step++;
										thisObject.markers[x].targeted = 1;
										thisObject.markers[x].outBounds = thisObject
												.setOuterBounds(x);
									}
									;
								}
								;
								if (step == 1) {
									temp_marker.targeted = 0;
									temp_marker.outBounds = null;
								}
								;
								if (step > 1) {
									if (!map.getInfoWindow().isHidden()) {
										map.closeInfoWindow();
									}
									var tick = 1;
									var deg = (360 / (step));
									for (x = 0; x < thisObject.markers.length; x++) {
										if (thisObject.markers[x].targeted == 1
												&& thisObject.markers[x].shifted == 0) {
											map
													.removeOverlay(thisObject.markers[x]);
											var delta_lat = (shift + shift
													* (Math.pow(2, (17 - map
															.getZoom())) - 1))
													* Math
															.cos(((deg * tick) * Math.PI) / 180);
											var delta_lng = (shift + shift
													* (Math.pow(2, (17 - map
															.getZoom())) - 1))
													* Math
															.sin(((deg * tick) * Math.PI) / 180);
											var point_latlng = new GLatLng(
													(thisObject.markers[x].startcoord
															.lat() + (delta_lat * .15)),
													(thisObject.markers[x].startcoord
															.lng() + (delta_lng * .15)));
											var new_lat = thisObject.markers[x]
													.getLatLng().lat()
													+ delta_lat;
											var new_lng = thisObject.markers[x]
													.getLatLng().lng()
													+ delta_lng;
											thisObject.markers[x]
													.setLatLng(new GLatLng(
															new_lat, new_lng));
											thisObject.markers[x].importance = 10;
											map.addOverlay(thisObject.markers[x]);

											thisObject.markers[x].line = new GPolyline(
													[
															thisObject.markers[x].startcoord,
															thisObject.markers[x]
																	.getLatLng() ],
													'#000000', 4, 1);
											map
													.addOverlay(thisObject.markers[x].line);

											var linepointcolor = '#9BE61A';
											if (thisObject.markers[x].iconsizeurl == 'small') {
												linepointcolor = '#62D962';
											}
											thisObject.markers[x].point = new GPolyline(
													[
															thisObject.markers[x].startcoord,
															point_latlng ],
													linepointcolor, 2, 1);
											map
													.addOverlay(thisObject.markers[x].point);

											thisObject.markers[x].shifted = 1;
											tick++;
										}
										;
									}
									;
								}
								;
								GEvent
										.addListener(
												map,
												"mousemove",
												function(latlng) {
													for (x = 0; x < thisObject.markers.length; x++) {
														if (thisObject.markers[x].targeted == 1
																&& thisObject.markers[x].outBounds
																		.containsLatLng(latlng) == false) {
															if (!map
																	.getInfoWindow()
																	.isHidden()) {
																map
																		.closeInfoWindow();
															}
															map
																	.removeOverlay(thisObject.markers[x].line);
															map
																	.removeOverlay(thisObject.markers[x].point);
															map
																	.removeOverlay(thisObject.markers[x]);
															thisObject.markers[x]
																	.setLatLng(thisObject.markers[x].startcoord);
															thisObject.markers[x].targeted = 0;
															thisObject.markers[x].shifted = 0;
															thisObject.markers[x].importance = thisObject.markers[x].startimport;
															map
																	.addOverlay(thisObject.markers[x]);
															if (thisObject.markers[x].openwindow == true) {
																GEvent
																		.trigger(
																				thisObject.markers[x],
																				"click")
															}
															;
															thisObject.markers[x].openwindow = false;
														}
														;
													}
													;
												});
							}
							;
						});

		GEvent.addListener(temp_marker, "click", function() {
			for (i = 0; i < thisObject.markers.length; i++) {
				thisObject.markers[i].openwindow = false;
			}
			temp_marker.openwindow = true;
		});
		return temp_marker;
	}

	this.addSpiderMarker = function(marker) {
		thisObject.markers.push(marker);
	}
	
	this.removeMarkers = function() {
		thisObject.markers = new Array();
	}
	
	this.getExactnessTolerance = function() {
		return 0.0001 * Math.pow(2, 16 - map.getZoom()); //.0003;
	}
}
