function MapInstance(iId, options){
	this.construct(iId, options);
}

MapInstance.prototype.instanceId = null;
MapInstance.prototype.mapObj = null;
MapInstance.prototype.location = null;
MapInstance.prototype.visible = false;
MapInstance.prototype.wrapper = null;
MapInstance.prototype.marker = null;

MapInstance.prototype.options = {
	mapTypeId: google.maps.MapTypeId.ROADMAP,
	zoom: 13,
	locale: "en_GB",
	position: {left: -50, top: -200},
	animate: true,
	animationSpeed: "fast"
}

MapInstance.prototype.construct = function(iId, options){
	this.instanceId = iId;
	this.wrapper = document.getElementById("mapWrapper_" + iId);
	
	if(typeof(options) == 'object'){
		$.extend(this.options, options)
	}
}

MapInstance.prototype.init = function(latLng){
	this.location = latLng;
	
	var iId = this.instanceId;
	var that = this;
	
	var mapOptions = {
		center: latLng,
		mapTypeId: that.options.mapTypeId,
		zoom: that.options.zoom
	};
	
	this.mapObj = new google.maps.Map(document.getElementById("mapCanvas_" + iId), mapOptions);
}

MapInstance.prototype.showRelativeTo = function(emt){
	this.updateRelativePosition(emt);
	this.show();
}

MapInstance.prototype.updateRelativePosition = function(emt){
	var pos = $(emt).position();
	var rel = this.options.position;
	
	$(this.wrapper).css({
		"left": pos.left + rel.left + "px",
		"top": pos.top + rel.top + "px"
	});
}

MapInstance.prototype.show = function(){
	if(this.options.animate){
		$(this.wrapper).fadeIn(this.options.animationSpeed);
	}
	else{
		$(this.wrapper).show();
	}
	
	google.maps.event.trigger(this.mapObj, 'resize');
	this.mapObj.setCenter(this.location);
	
	this.visible = true;
}

MapInstance.prototype.hide = function(){
	if(this.options.animate){
		$(this.wrapper).fadeOut(this.options.animationSpeed);
	}
	else{
		$(this.wrapper).hide();
	}
	
	this.visible = false;
}

MapInstance.prototype.addMarker = function(latLng){
	var map = this.mapObj;
	var markerImage = new google.maps.MarkerImage(
		"http://www.restel.fi/stc/restel/images/bigmap/holidayinn.png",
		new google.maps.Size(18, 18),
		new google.maps.Point(0, 0),
		new google.maps.Point(9, 9)
	);
	var markerOpts = {
		clickable: false,
		flat: true,
		icon: markerImage,
		position: latLng,
		shape: {
			type: "rectangle",
			coord: [0,0,18,18]
		},
		map: map,
		visible: true
	};
	
	this.marker = new google.maps.Marker(markerOpts);
}




var HIMaps = {
	currentGeocodeId: "",
	
	geocoder: new google.maps.Geocoder(),
	diFieldDefault: "",
	
	geocodeSlot: function(addrId, geocodeId){
		HIMaps.currentGeocodeId = geocodeId;
		var addr = document.getElementById(addrId).getElementsByTagName("div")[0].innerHTML;
		
		HIMaps.geocoder.geocode({address: addr}, HIMaps.handleGeocoding);
	},
	
	handleGeocoding: function(results, status){
		if(status == google.maps.GeocoderStatus.OK){
			var point = results[0].geometry.location;
			document.getElementById(HIMaps.currentGeocodeId).getElementsByTagName("div")[0].innerHTML = "lat: " + point.lat() + ", lng: " + point.lng();
			if(!window.Save || !Save.u4_disabled){
				top.main.postData();
			}
		}
		else{
			alert("Geocoding failed.");
		}
	},
	
	attachHandlers: function(iId){
		// Handle DI field focus
		$("#mapWrapper_" + iId + " .fromAddr:first").focus(function(){
			if($(this).val() == HIMaps.diFieldDefault || $(this).val().replace(/\s/g, "") == ""){
				$(this).val("");
			}
		});
		
		// Handle DI field blur
		$("#mapWrapper_" + iId + " .fromAddr:first").blur(function(){
			if($(this).val().replace(/\s/g, "") == ""){
				$(this).val(HIMaps.diFieldDefault);
			}
		});
	},
	
	createMapInstance: function(iId){
		var map = new MapInstance(iId);
		var pos = eval("({" + document.getElementById("geocode_" + iId).innerHTML + "})");
		var latLng = new google.maps.LatLng(pos.lat, pos.lng);
		
		map.init(latLng);
		map.addMarker(latLng);
		
		this.attachHandlers(iId);
		
		return map;
	} 
}
