

function lochandler(loc_id) {
	var _this = this;
	this.oMap = "";
	this.oBounds = "";
	this.oLoc = "";
	this.show_iloc = false
	this.loc_id = loc_id;

	if (loc_id > 0) {
		_this.show_iloc = true;
	} 
	

	this.init = function () {
		if (GBrowserIsCompatible()) {
			_this.oMap = new GMap2(document.getElementById("inet_googlemap"));
			
			if (_this.loc_id > 0) {
				//Zoom in on selected loc
				_this.oLoc = _this.getLocObj(_this.loc_id);
				_this.oMap.setCenter(new GLatLng(_this.oLoc.lat, _this.oLoc.lng), 15);
			}
			else {
				//Extent bounds to encompass all locs
				_this.oBounds = new GLatLngBounds();
				_this.setBounds();
				_this.oMap.setCenter(_this.oBounds.getCenter(), _this.oMap.getBoundsZoomLevel(_this.oBounds));
			}
			
			//Add controls
			
			_this.oMap.enableScrollWheelZoom();
			_this.oMap.addControl(new GMapTypeControl());
			_this.oMap.addControl(new GSmallMapControl());
			
			// Add id property to GMarker class
			GMarker.prototype.id = 0;

			//Add marker for each loc
			_this.addMarkers();
		}
	}
	
	this.getLocObj = function(loc_id) {
		if (loc_id > 0) {
			var aLocs = _this.locs;
			for (var i = 0; i < aLocs.length; i++) {
				if (aLocs[i].id == loc_id) {
					return aLocs[i];
				}
			}
		}
	}

	this.locs = Array();
	this.addLoc = function(id, lat, lng, title, title2, txt, img, street, housenum, zip, city, country, loctype) {
		var oLoc = new loc();
		oLoc.id = id;
		oLoc.lat = lat;
		oLoc.lng = lng;
		oLoc.title = title;
		oLoc.title2 = title2;
		oLoc.txt = txt;
		oLoc.img = img;
		oLoc.street = street;
		oLoc.housenum = housenum;
		oLoc.zip = zip;
		oLoc.city = city;
		oLoc.country = country;
		oLoc.loctype = loctype;
		
		if (oLoc.loctype == 0) {
			oLoc.oIcon = new GIcon(); 
			oLoc.oIcon.image = "/custom/map/images/ik_stop.png";
			oLoc.oIcon.iconSize = new GSize(25, 25);
			oLoc.oIcon.iconAnchor = new GPoint(12, 12);
			oLoc.oIcon.infoWindowAnchor = new GPoint(12, 12);
		}
		else {
			oLoc.oIcon = new GIcon(G_DEFAULT_ICON);
		}

		_this.locs[_this.locs.length] = oLoc;
		//return _this.locs.length - 1;
	}
	
	this.setBounds = function() {
		var aLocs = _this.locs;
		if (aLocs.length > 0) {
			for (var i = 0; i < aLocs.length; i++) {
				if (_this.show_iloc || aLocs[i].loctype == 0) {
					_this.oBounds.extend(new GLatLng(aLocs[i].lat, aLocs[i].lng));
				}
			}
		}
	}

	this.addMarkers = function() {
		var aLocs = _this.locs;
		if (aLocs.length > 0) {
			for (var i = 0; i < aLocs.length; i++) {
				if (_this.show_iloc || aLocs[i].loctype == 0) {
					var oMarker = new GMarker(new GLatLng(aLocs[i].lat, aLocs[i].lng), {icon : aLocs[i].oIcon});
					oMarker.id = aLocs[i].id;
					_this.oMap.addOverlay(oMarker);
					GEvent.addListener(oMarker, "click", function() {
						_this.showMarkerInfo(this);
					});
				}
			}
		}
	}

	this.showMarkerInfo = function(oMarker) {
		var oLoc = _this.getLocObj(oMarker.id);
		var html = "";
		var img_html = "";
		var win_class = "loc_win";
		var title = "";
		var str_dir = "";

		if (oLoc.title2.length > 0) {
			title = oLoc.title2;
		}
		else {
			title = oLoc.title;
		}
		
		if (oLoc.img.length > 0) {
			img_html = "<div class=\"loc_img\">"+ oLoc.img +"</div>"
			win_class += "_hasimg";
		}

		html += "<div class=\""+ win_class +"\">";

		html += img_html;
		
		html += "<div class=\"loc_txt\">";
		
		if (title.length > 0) {
			html += "<div class=\"loc_title\">"+ title +"</div>"
		}

		if (oLoc.street.length > 0) {
			html += oLoc.street +" "+ oLoc.housenum +"<br />";
		}
		
		if (oLoc.zip.length > 0 && oLoc.city.length > 0) {
			html += oLoc.zip +" "+ oLoc.city +"<br />";
		}
		
		if (oLoc.country.length > 0) {
			html += oLoc.country +"<br />";
		}
		
		if (oLoc.loctype == 1 && oLoc.txt.length > 0) {
			html += oLoc.txt;
		}
		
		if (oLoc.street.length > 0 && oLoc.housenum.length > 0 && oLoc.zip.length > 0) {
			str_dir = "http://maps.google.com/maps?&saddr=&daddr="+ oLoc.street.replace(" ", "+") +"+"+ oLoc.housenum +"+"+ oLoc.zip +"&hl=da";
			html += "<div class=\"loc_dir\"><a href=\""+ str_dir +"\" target=\"_blank\">Rutevejledning</a></div>";
		}

		html += "</div>";
		
		html += "<div class=\"reset\"></div>";
		html += "</div>";

		_this.oMap.openInfoWindow(new GLatLng(oLoc.lat, oLoc.lng), html);
	}
}

function loc() {
	
	this.id = 0;
	this.lat = "";
	this.lng = "";
	this.title = "";
	this.title2 = "";
	this.txt = "";
	this.img = "";
	this.street = "";
	this.housenum = "";
	this.zip = "";
	this.city = "";
	this.country = "";
	this.loctype = 0;
	this.oIcon = "";

}


