/**
 * View object to display a location on Google Maps.
 */
function MaceLocation(positionedContent, mapWidget, newGLatLng) {
	
	// Content data (from MACE services) 
	this.positionedContent = positionedContent;

	// back references
	this.mapWidget = mapWidget;
	
	// Position and marker --------------------------------
	
	// TODO Set original if posC.geo, and new if param
	if (newGLatLng) {
		// The original point
		this.originalGLatLng = null;
		// The new point
		this.newGLatLng = newGLatLng;
	}
	else {
		// The original point, i.e. the last saved one
		this.originalGLatLng = new GLatLng(positionedContent.geo.latitude, positionedContent.geo.longitude);
		// The new point, after dragging to a new position
		this.newGLatLng = null;
	}
	
	// Create marker
	var selected = positionedContent.isSelected && positionedContent.isSelected();
	var icon = selected ? mapWidget.iconSelected : mapWidget.icon;
	icon = (positionedContent.isExternal && positionedContent.isExternal()) ? mapWidget.iconExternal : icon;
	if (this.newGLatLng) {
		this.marker = this.createMarker(this.newGLatLng, icon, selected);
	}
	else {
		this.marker = this.createMarker(this.originalGLatLng, icon, selected);
	}


	// Info window with detail map in second tab ----------

	// Create content to show in info window
	this.infoHtml = MaceLocation.createInfoHtml(positionedContent);
	this.infoHtml += MaceLocation.createButtonsHtml(positionedContent);
	
	this.tab1 = new GInfoWindowTab("Info", '<div class="info">' + this.infoHtml + '</div>');
	// NB: Because GMapAPI handles this internal mapDiv by itself this
	//     strange mechanism is used: tab2 is just created onClick each time.
	
	
	// Event handler --------------------------------------
	var t = this;
	
	// Close info window on drag
	GEvent.addListener(this.marker, "dragstart", function() {
		t.mapWidget.map.closeInfoWindow();
	});	
	
	// Add new coordinates to info content to display next time the info is shown
	GEvent.addListener(this.marker, "dragend", function() {
		t.newGLatLng = t.marker.getPoint();
		console.log("newGLatLng to save=" + t.marker.getPoint().toString());
	});	
}

MaceLocation.lomDetailsURL = MACEConstants.rootURL + 'details/';


MaceLocation.prototype.hasNewPosition = function() {
	return this.newGLatLng != null;
}

MaceLocation.prototype.hasOriginalPosition = function() {
	return this.originalGLatLng != null;
}

MaceLocation.prototype.getCurrentPosition = function() {
	return (this.newGLatLng) ? this.newGLatLng : this.originalGLatLng;
}

MaceLocation.prototype.updatePosition = function() {
	if (this.hasNewPosition()) {
		console.log("Move to new pos.");
		this.marker.setLatLng(this.newGLatLng);
	}
	else {
		console.log("Move to original pos.");
		this.marker.setLatLng(this.originalGLatLng);
	}
}

MaceLocation.prototype.makeEditable = function(refine) {
	this.marker.enableDragging();
	this.marker.setImage(this.mapWidget.iconEditable.image);
	
	this.mapWidget.map.closeInfoWindow();
	if (refine) {
		this.mapWidget.map.setCenter(this.marker.getPoint(), 16);
	}
}

MaceLocation.prototype.makeNonEditable = function() {
	this.marker.disableDragging();
	this.marker.setImage(this.mapWidget.iconSelected.image);
	this.mapWidget.map.closeInfoWindow();
}

// TODO Implement methods, remove positionedContent.isSelected, switch completely to mapWidget.currentPC
MaceLocation.prototype.makeSelected = function() {
}
MaceLocation.prototype.makeNonSelected = function() {
}

MaceLocation.prototype.toString = function() {
	return "\"" + this.positionedContent.id + "\"\n"
			+ "\toriginal=" + this.originalGLatLng + "\n" 
			+ "\tnew=" + this.newGLatLng + "\n"
			+ "\tposContent.geo=" + this.positionedContent.geo + "\n"
			+ "\tposContent.cId=" + this.positionedContent.id + "\n"
			+ "\tposContent.pId=" + this.positionedContent.positionId;
}

MaceLocation.prototype.createMarker = function(posLatLng, icon, selected){
	var zIndexProcessFunction = function(){};
	if (selected) {
		zIndexProcessFunction = function(){
			return 1000;
		}
	}
	
	// GMarker (draggable, but disabled)
	var marker = new GMarker(posLatLng, {
		"title": this.positionedContent.title,
		"draggable": true,
		"icon": icon,
		"zIndexProcess": zIndexProcessFunction
	});
	marker.disableDragging();
	
	var t = this;
	GEvent.addListener(marker, "click", function() {
		// Open detail window with positioned content	
		window.open(MaceLocation.lomDetailsURL + t.positionedContent.id);
		
		/*
		// Opens info window and creates detail map
		
		var tab2 = new GInfoWindowTab("Detail", '<div id="detailMap"></div>');
		var infoTabs = [t.tab1, tab2];
		t.marker.openInfoWindowTabs(infoTabs);
		var dMapDiv = document.getElementById("detailMap");
		var detailMap = new GMap2(dMapDiv);
		// Detail maps shows satellite with high zoom at current center
		detailMap.setCenter(t.getCurrentPosition(), 17, G_SATELLITE_MAP);

		// Switch off google copyright text (permitted for multiple map)
		var CopyrightDiv = dMapDiv.firstChild.nextSibling;
		var CopyrightImg = dMapDiv.firstChild.nextSibling.nextSibling;
		CopyrightDiv.style.display = "none"; 
		CopyrightImg.style.display = "none";
		*/
	});
	return marker;
}

MaceLocation.createInfoHtml = function(positionedContent) {
	var infoHtml = "<h3>" + positionedContent.title + "</h3>";
	infoHtml += "<div class=\"infoContent\">";
	if (positionedContent.description) {
		infoHtml += "<p>" + positionedContent.description + "</p>";
	}
	infoHtml += "<p>";
	if (positionedContent.images && positionedContent.images.length > 0) {
		infoHtml += '<img src="' + positionedContent.images[0] + '" /><br/>';
	}
	if (positionedContent.address) {
		if (positionedContent.address.street.length > 0) {
			infoHtml += positionedContent.address.street + ", ";
		}
		if (positionedContent.address.city.length > 0) {
			infoHtml += positionedContent.address.city + ", ";
		}
		if (positionedContent.address.country.length > 0) {
			infoHtml += positionedContent.address.country + "";
		}
	}
	infoHtml += "</p></div>";
	return infoHtml;
}

// Add icon actions
MaceLocation.createButtonsHtml = function(positionedContent) {
	var id = positionedContent.id;
	var linksHtml = '<div class="actions">';
	linksHtml += '<div class="action"><a href="javascript:void(0)" class="position button" onclick="mapWidget.centerPositionById(\'' + id + '\');" title="Center"><span class="icon">Center</span></a></div>';
	linksHtml += '<div class="action"><a href="javascript:void(0)" class="find button" onclick="mapWidget.openInMACE(\'' + id + '\');" title="Open in MACE"><span class="icon">Open</span></a></div>';
	linksHtml += '<div class="action"><a href="javascript:void(0)" class="button" onclick="mapWidget.logInfo(\'' + id + '\');" title="Log info to console"><span>More Info</span></a></div>';
	linksHtml += '</div>';
	return linksHtml;
}