Index: main/trunk/greenstone3/web/interfaces/default/js/map-scripts-editor-history.js
===================================================================
--- main/trunk/greenstone3/web/interfaces/default/js/map-scripts-editor-history.js	(revision 32798)
+++ main/trunk/greenstone3/web/interfaces/default/js/map-scripts-editor-history.js	(revision 32799)
@@ -1,53 +1,49 @@
-//var prevOverlays = [];
-//var presentOverlays = [];
-//var undoneOverlays = [];
-//var cycleComplete = true;
-
-function MapEditorHistory()
-{
+
+function MapEditorHistory(mapEditor) {
 	this.prevOverlays = [];
 	this.presentOverlays = [];
 	this.undoneOverlays = [];
-	this.cycleComplete = true;
-}
-
-MapEditorHistory.prototype.undo = function() {
-	for (let i = 0; i < overlays.length; i++) {
-		overlays[i].setMap(null);
+	this.cycleCompvare = true;
+	this.mapEditor = mapEditor;
+}
+
+MapEditorHistory.prototype.undo = function () {
+	
+	for (var i = 0; i < this.mapEditor.overlays.length; i++) {
+		this.mapEditor.overlays[i].setMap(null);
 	}
 	if (this.prevOverlays.length != 0) {
-		selectedShapes = [];
-		overlays = [];
+		this.mapEditor.selectedShapes = [];
+		this.mapEditor.overlays = [];
 		this.undoneOverlaysPush();
-		let prev_overlay = this.prevOverlays.pop();
-
+		var prev_overlay = this.prevOverlays.pop();
+		var draggableCB = document.querySelector("input[name=draggableCB]").checked = false;
+		
+		
 		if (prev_overlay.length > 0) {
 			for (var i = 0; i < prev_overlay.length; i++) {
-				overlays[i] = prev_overlay[i];
-				overlays[i].setMap(map);
-				overlays[i].draggable = draggableState;
+				this.mapEditor.overlays[i] = prev_overlay[i];
+				this.mapEditor.overlays[i].setMap(this.mapEditor.map);
+				this.mapEditor.overlays[i].draggable = true;
 			}
 		}
 	}
 };
-	
-
-	
-
-
-MapEditorHistory.prototype.redo = function() {
+
+MapEditorHistory.prototype.redo = function () {
 
 	if (this.undoneOverlays.length != 0) {
-		selectedShapes = [];
-		for (let i = 0; i < overlays.length; i++) {
-			overlays[i].setMap(null);
-		}
-
-		overlays = [];
+		var draggableCB = document.querySelector("input[name=draggableCB]").checked = false;
+		this.mapEditor.selectedShapes = [];
+		for (var i = 0; i < this.mapEditor.overlays.length; i++) {
+			this.mapEditor.overlays[i].setMap(null);
+		}
+
+		this.mapEditor.overlays = [];
 		var lastEntry = this.undoneOverlays[this.undoneOverlays.length - 1];
-		for (let i = 0; i < lastEntry.length; i++) {
-			overlays[i] = lastEntry[i];
-			overlays[i].setMap(map);
-			overlays[i].draggable = draggableState;
+		for (var i = 0; i < lastEntry.length; i++) {
+			this.mapEditor.overlays[i] = lastEntry[i];
+			this.mapEditor.overlays[i].setMap(this.mapEditor.map);
+			this.mapEditor.overlays[i].draggable = true;
 		}
 
@@ -58,10 +54,10 @@
 			} else {
 				var overlays_copy = [];
-				for (let i = 0; i < this.presentOverlays[0].length; i++) {
-					var clone_shape = this.cloneShape(this.presentOverlays[0][i]);
-					if (this.overlayItemIsShape(clone_shape)) {
-						addShapeListeners(clone_shape, null);
+				for (var i = 0; i < this.presentOverlays[0].length; i++) {
+					var clone_shape = ShapesUtil.cloneShape(this.presentOverlays[0][i]);
+					if (ShapesUtil.overlayItemIsShape(clone_shape)) {
+						this.mapEditor.addShapeListeners(clone_shape, null);
 					} else {
-						addMarkerListeners(clone_shape, null);
+						this.mapEditor.addMarkerListeners(clone_shape, null);
 					}
 					overlays_copy[i] = clone_shape;
@@ -76,8 +72,6 @@
 }
 
-
-
-
-MapEditorHistory.prototype.clonePath = function(path){
+/*
+MapEditorHistory.prototype.clonePath = function (path) {
 	var clone_path = [];
 
@@ -95,14 +89,23 @@
 }
 
-
-MapEditorHistory.prototype.clonePolyline = function(polyline){
-	var path = polyline.getPath();
+MapEditorHistory.prototype.clonePolyline = function (polyline) {
 	var geodesic = polyline.geodesic;
 	var strokeColor = polyline.strokeColor;
 	var strokeOpacity = polyline.strokeOpacity;
 	var strokeWeight = polyline.strokeWeight;
-	var clone_path = clonePath(path);
-	//var editable      = rect.editable;
-
+	//var clone_path = clonePath(path);
+	
+	var clone_path = null;
+	
+	var JSONSave = typeof polyline.getPath !== "function";
+	//console.log(JSONSave)
+	if(JSONSave){
+		var path = polyline.latLngs.j[0].j;
+		clone_path = path;
+		
+	} else{
+		var path = polyline.getPath();
+		clone_path = this.clonePath(path);	
+	}
 	var clone_polyline = new google.maps.Polyline({
 			geodesic: geodesic,
@@ -111,5 +114,5 @@
 			strokeWeight: strokeWeight,
 			path: clone_path,
-			draggable: draggableState,
+			draggable: true,
 			editable: false
 		});
@@ -120,7 +123,5 @@
 }
 
-
-MapEditorHistory.prototype.clonePolygon = function(polygon){
-	var path = polygon.getPath();
+MapEditorHistory.prototype.clonePolygon = function (polygon) {
 	var geodesic = polygon.geodesic;
 	var strokeColor = polygon.strokeColor;
@@ -129,7 +130,17 @@
 	var fillColor = polygon.fillColor;
 	var fillOpacity = polygon.fillOpacity;
-	var clone_path = clonePath(path);
-	//var editable      = rect.editable;
-
+
+	var clone_path = null;
+	var JSONSave = typeof polygon.getPath !== "function";
+	//console.log(JSONSave)
+	if(JSONSave){
+		var path = polygon.latLngs.j[0].j;
+		clone_path = path;
+		
+	} else{
+		var path = polygon.getPath();
+		clone_path = this.clonePath(path);
+		console.log(path);	
+	}	
 	var clone_polygon = new google.maps.Polygon({
 			geodesic: geodesic,
@@ -140,14 +151,12 @@
 			fillOpacity: fillOpacity,
 			path: clone_path,
-			draggable: draggableState,
+			draggable: true,
 			editable: false
 		});
-
 	clone_polygon.type = google.maps.drawing.OverlayType.POLYGON;
-
 	return clone_polygon;
 }
 
-MapEditorHistory.prototype.cloneRectangle = function(rect){
+MapEditorHistory.prototype.cloneRectangle = function (rect) {
 	var strokeColor = rect.strokeColor;
 	var strokeOpacity = rect.strokeOpacity;
@@ -155,8 +164,24 @@
 	var fillColor = rect.fillColor;
 	var fillOpacity = rect.fillOpacity;
-	var bounds = rect.getBounds();
+	//var bounds = rect.getBounds();
 	var type = rect.type;
-	//var editable      = rect.editable;
-
+	
+	var bounds = null;
+	
+	var JSONSave = typeof rect.getBounds !== "function";
+	
+	if(JSONSave){
+		var north = rect.bounds.north;
+		var south = rect.bounds.south;
+		var east = rect.bounds.east;
+		var west = rect.bounds.west;
+		if (!isNaN(north) && !isNaN(south) && !isNaN(west) && !isNaN(east)) {
+			var NE = new google.maps.LatLng(north, east);
+			var SW = new google.maps.LatLng(south, west);
+			bounds = new google.maps.LatLngBounds(SW, NE);
+		}		
+	} else {
+		bounds = rect.getBounds();
+	}
 	var clone_rect = new google.maps.Rectangle({
 			strokeColor: strokeColor,
@@ -166,5 +191,5 @@
 			fillOpacity: fillOpacity,
 			bounds: bounds,
-			draggable: draggableState,
+			draggable: true,
 			editable: false,
 			type: type
@@ -172,9 +197,8 @@
 
 	clone_rect.type = google.maps.drawing.OverlayType.RECTANGLE;
-
 	return clone_rect;
 }
 
-MapEditorHistory.prototype.cloneCircle = function(circ){
+MapEditorHistory.prototype.cloneCircle = function (circ) {
 	var strokeColor = circ.strokeColor;
 	var strokeOpacity = circ.strokeOpacity;
@@ -184,6 +208,5 @@
 	var center = circ.center;
 	var radius = circ.radius;
-	//var editable      = rect.editable;
-
+	
 	var clone_circ = new google.maps.Circle({
 			strokeColor: strokeColor,
@@ -194,5 +217,5 @@
 			center: center,
 			radius: radius,
-			draggable: draggableState,
+			draggable: true,
 			editable: false
 		});
@@ -203,12 +226,17 @@
 }
 
-MapEditorHistory.prototype.cloneMarker = function(marker){
-	var position = marker.getPosition();
+MapEditorHistory.prototype.cloneMarker = function (marker) {
 	var anchorPoint = marker.anchorPoint;
+	var JSONSave = typeof marker.getPosition !== "function";
+	if(JSONSave){
+		var position = marker.position;
+	} else{
+		var position = marker.getPosition();
+	}
 	var clone_marker = new google.maps.Marker({
 			anchorPoint: anchorPoint,
 			position: position,
 			clickable: true,
-			draggable: draggableState,
+			draggable: true,
 			editable: false
 		})
@@ -218,26 +246,26 @@
 }
 
-MapEditorHistory.prototype.cloneShape = function(shape){
+MapEditorHistory.prototype.cloneShape = function (shape) {
 	if (shape.type === google.maps.drawing.OverlayType.POLYLINE) {
-		let clone_polyline = this.clonePolyline(shape);
+		var clone_polyline = this.clonePolyline(shape);
 		return clone_polyline;
 	} else if (shape.type === google.maps.drawing.OverlayType.POLYGON) {
-		let clone_polygon = this.clonePolygon(shape);
+		var clone_polygon = this.clonePolygon(shape);
 		return clone_polygon;
 	} else if (shape.type === google.maps.drawing.OverlayType.RECTANGLE) {
-		let clone_rect = this.cloneRectangle(shape);
+		var clone_rect = this.cloneRectangle(shape);
 		return clone_rect;
 
 	} else if (shape.type === google.maps.drawing.OverlayType.CIRCLE) {
-		let clone_circ = this.cloneCircle(shape);
+		var clone_circ = this.cloneCircle(shape);
 		return clone_circ;
 
 	} else {
-		let clone_marker = this.cloneMarker(shape);
+		var clone_marker = this.cloneMarker(shape);
 		return clone_marker;
 	}
 }
 
-MapEditorHistory.prototype.overlayItemIsShape = function(overlay_item){
+MapEditorHistory.prototype.overlayItemIsShape = function (overlay_item) {
 	var type = overlay_item.type;
 
@@ -249,90 +277,61 @@
 	return is_shape;
 }
-MapEditorHistory.prototype.historyOverlayPush = function(){
-	if (this.cycleComplete) {
-		var overlays_copy = [];
-		for (var i = 0; i < overlays.length; i++) {
-			let clone_shape = this.cloneShape(overlays[i]);
-
-			if (this.overlayItemIsShape(clone_shape)) {
-				addShapeListeners(clone_shape, null); // don't have an overlay event!
-			} else {
-				addMarkerListeners(clone_shape, null); // don't have an overlay event!
-			}
-			overlays_copy[i] = clone_shape;
-		}
-		undoneOverlays = [];
+
+*/
+
+MapEditorHistory.prototype.historyOverlayPush = function () {
+	
+	
+	if (this.cycleCompvare) {
+		
+		var overlays_copy = [];
+		for (var i = 0; i < this.mapEditor.overlays.length; i++) {
+			var clone_shape = ShapesUtil.cloneShape(this.mapEditor.overlays[i]);
+			if (ShapesUtil.overlayItemIsShape(clone_shape)) {
+				this.mapEditor.addShapeListeners(clone_shape, null); // don't have an overlay event!
+			} else {
+				this.mapEditor.addMarkerListeners(clone_shape, null); // don't have an overlay event!
+			}
+			overlays_copy[i] = clone_shape;
+		}
+		this.undoneOverlays = [];
 		this.prevOverlays.push(overlays_copy);
 	}
 
-	cycleComplete = false;
-}
-
-MapEditorHistory.prototype.presentOverlayPush = function(){
+	this.cycleCompvare = false;
+}
+
+MapEditorHistory.prototype.presentOverlayPush = function () {
+//	console.log("presentOverlayPush");
+	
 	this.presentOverlays = [];
 	var overlays_copy = [];
-	for (var i = 0; i < overlays.length; i++) {
-		let clone_shape = this.cloneShape(overlays[i]);
-		if (this.overlayItemIsShape(clone_shape)) {
-			addShapeListeners(clone_shape, null); // don't have an overlay event!
+	for (var i = 0; i < this.mapEditor.overlays.length; i++) {
+		var clone_shape = ShapesUtil.cloneShape(this.mapEditor.overlays[i]);
+		if (ShapesUtil.overlayItemIsShape(clone_shape)) {
+			this.mapEditor.addShapeListeners(clone_shape, null); // don't have an overlay event!
 		} else {
-			this.addMarkerListeners(clone_shape, null); // don't have an overlay event!
+			this.mapEditor.addMarkerListeners(clone_shape, null); // don't have an overlay event!
 		}
 		overlays_copy[i] = clone_shape;
 	}
 	this.presentOverlays.push(overlays_copy);
-	cycleComplete = true;
-
-}
-
-MapEditorHistory.prototype.undoneOverlaysPush = function(){
-presentOverlayPush
-	var conditionUndone = presentOverlays[presentOverlays.length - 1] !== undefined;
+	this.cycleCompvare = true;
+
+	
+}
+
+MapEditorHistory.prototype.undoneOverlaysPush = function () {
+	
+	var conditionUndone = this.presentOverlays[this.presentOverlays.length - 1] !== undefined;
 
 	if (conditionUndone) {
-		let overlays_copy = [];
-		for (var i = 0; i < presentOverlays[0].length; i++) {
-			var clone_shape = cloneShape(presentOverlays[0][i]);
-			if (overlayItemIsShape(clone_shape)) {
-				addShapeListeners(clone_shape, null); // don't have an overlay event!
-			} else {
-				addMarkerListeners(clone_shape, null); // don't have an overlay event!
-			}
-			overlays_copy[i] = clone_shape;
-		}
-		undoneOverlays.push(overlays_copy);
-	}
-
-	var conditionPresent = prevOverlays[prevOverlays.length - 1] !== undefined;
-
-	if (conditionPresent) {
-		presentOverlays = [];
-		var overlays_copy = [];
-		for (var i = 0; i < prevOverlays[prevOverlays.length - 1].length; i++) {
-			let clone_shape = cloneShape(prevOverlays[prevOverlays.length - 1][i]);
-			if (overlayItemIsShape(clone_shape)) {
-				addShapeListeners(clone_shape, null); // don't have an overlay event!
-			} else {
-				addMarkerListeners(clone_shape, null); // don't have an overlay event!
-			}
-			overlays_copy[i] = clone_shape;
-		}
-		presentOverlays.push(overlays_copy);
-	}
-}
-
-
-MapEditorHistory.prototype.undoneOverlaysPush = function() {
-
-	let conditionUndone = this.presentOverlays[this.presentOverlays.length - 1] !== undefined;
-
-	if (conditionUndone) {
 		var overlays_copy = [];
 		for (var i = 0; i < this.presentOverlays[0].length; i++) {
-			let clone_shape = this.cloneShape(this.presentOverlays[0][i]);
-			if (this.overlayItemIsShape(clone_shape)) {
-				addShapeListeners(clone_shape, null); // don't have an overlay event!
-			} else {
-				addMarkerListeners(clone_shape, null); // don't have an overlay event!
+			var clone_shape = ShapesUtil.cloneShape(this.presentOverlays[0][i]);
+			if (ShapesUtil.overlayItemIsShape(clone_shape)) {
+				this.mapEditor.addShapeListeners(clone_shape, null); // don't have an overlay event!
+			} else {
+				this.mapEditor.addMarkerListeners(clone_shape, null); // don't have an overlay event!
 			}
 			overlays_copy[i] = clone_shape;
@@ -341,15 +340,15 @@
 	}
 
-	let conditionPresent = this.prevOverlays[this.prevOverlays.length - 1] !== undefined;
+	var conditionPresent = this.prevOverlays[this.prevOverlays.length - 1] !== undefined;
 
 	if (conditionPresent) {
 		this.presentOverlays = [];
 		var overlays_copy = [];
-		for (let i = 0; i < this.prevOverlays[this.prevOverlays.length - 1].length; i++) {
-			var clone_shape = this.cloneShape(this.prevOverlays[this.prevOverlays.length - 1][i]);
-			if (this.overlayItemIsShape(clone_shape)) {
-				addShapeListeners(clone_shape, null); // don't have an overlay event!
-			} else {
-				addMarkerListeners(clone_shape, null); // don't have an overlay event!
+		for (var i = 0; i < this.prevOverlays[this.prevOverlays.length - 1].length; i++) {
+			var clone_shape = cloneShape(this.prevOverlays[this.prevOverlays.length - 1][i]);
+			if (ShapesUtil.overlayItemIsShape(clone_shape)) {
+				this.mapEditor.addShapeListeners(clone_shape, null); // don't have an overlay event!
+			} else {
+				this.mapEditor.addMarkerListeners(clone_shape, null); // don't have an overlay event!
 			}
 			overlays_copy[i] = clone_shape;
@@ -359,2 +358,37 @@
 }
 
+MapEditorHistory.prototype.undoneOverlaysPush = function () {
+
+	var conditionUndone = this.presentOverlays[this.presentOverlays.length - 1] !== undefined;
+
+	if (conditionUndone) {
+		var overlays_copy = [];
+		for (var i = 0; i < this.presentOverlays[0].length; i++) {
+			var clone_shape = ShapesUtil.cloneShape(this.presentOverlays[0][i]);
+			if (ShapesUtil.overlayItemIsShape(clone_shape)) {
+				this.mapEditor.addShapeListeners(clone_shape, null); // don't have an overlay event!
+			} else {
+				this.mapEditor.addMarkerListeners(clone_shape, null); // don't have an overlay event!
+			}
+			overlays_copy[i] = clone_shape;
+		}
+		this.undoneOverlays.push(overlays_copy);
+	}
+
+	var conditionPresent = this.prevOverlays[this.prevOverlays.length - 1] !== undefined;
+
+	if (conditionPresent) {
+		this.presentOverlays = [];
+		var overlays_copy = [];
+		for (var i = 0; i < this.prevOverlays[this.prevOverlays.length - 1].length; i++) {
+			var clone_shape = ShapesUtil.cloneShape(this.prevOverlays[this.prevOverlays.length - 1][i]);
+			if (ShapesUtil.overlayItemIsShape(clone_shape)) {
+				this.mapEditor.addShapeListeners(clone_shape, null); // don't have an overlay event!
+			} else {
+				this.mapEditor.addMarkerListeners(clone_shape, null); // don't have an overlay event!
+			}
+			overlays_copy[i] = clone_shape;
+		}
+		this.presentOverlays.push(overlays_copy);
+	}
+}
