if(typeof(Map24)!="undefined")
{
	mapObj = {
		mouseClick		: {
			lat				: 0,
			long			: 0
		},
		parent			: {
			coord			: {
				topLeft			: {
					lat				: 0,
					long			: 0
				},
				bottomRight		: {
					lat				: 0,
					long			: 0
				},
				center			: {
					lat				: 0,
					long			: 0
				}
			},
			size				: {
				height				: 0,
				width				: 0
			}
		},
		events			: {
			onMouseClick	: null,
			onPan			: null,
			onZoom			: null,
			onViewChange	: null,
			onFirstPointLoaded	: null,
			onEndPointLoaded	: null
		},
		marker				: {
			createMarkerHtml	: function(html){
				var htmlStr = "";
				htmlStr += "<div>";
				htmlStr += "	<div>";
				htmlStr += "		<div class='" + this.classBaseName + "-tl'></div>";
				htmlStr += "		<div class='" + this.classBaseName + "-tc'></div>";
				htmlStr += "		<div class='" + this.classBaseName + "-tr'></div>";
				htmlStr += "	</div>";
				
				htmlStr += "	<div>";
				htmlStr += "		<div class='" + this.classBaseName + "-cl'></div>";
				htmlStr += "		<div id='htmlMarkerObj'  class='" + this.classBaseName + "-cc'>" + html + "</div>";
				htmlStr += "		<div class='" + this.classBaseName + "_cr'></div>";
				htmlStr += "	</div>";
				
				htmlStr += "	<div>";
				htmlStr += "		<div class='" + this.classBaseName + "-bl'></div>";
				htmlStr += "		<div class='" + this.classBaseName + "-bc'></div>";
				htmlStr += "		<div class='" + this.classBaseName + "-br'></div>";
				htmlStr += "	</div>";
				htmlStr += "</div>";
				return htmlStr;
			},
			insertMarkerHtml	: function(html){
				var htmlParent = document.getElementById("htmlMarkerObj");
				if(htmlParent!=null){
					htmlParent.innertHTML = html;
				}
			},
			imgSource			: "",
			classBaseName		: "",
			markerObj			: null,
			showMarker			: function(html, lat, long){
				if(this.markerObj==null){
					this.markerObj = new Map24.HtmlObject({
						Longitude: long, 
						Latitude: lat,
						Content: this.createMarkerHtml(html)
					});
					this.markerObj.commit();
					this.markerObj.show();
				}
				else{
					this.markerObj.setLatitude(lat);
					this.markerObj.setLongitude(long);
					
					this.insertMarkerHtml(html);
					
					this.markerObj.commit();
					this.markerObj.show();
				}
			},
			hideMarker			: function(){
				if(this.markerObj!=null){
					this.markerObj.hide();
				}
			}
		},
		html				: {			
			size				: {
				height				: 0,
				width				: 0
			},
			descriptionParent		: null,
			htmlObjects			: new Array(),
			widthLimit			: 1000,
			classBaseName		: "html-point-marker",
			addHtmlObject		: function(objectString, name, lat, long){
				if(typeof(this.htmlObjects[name]) == "undefined"){
					if(this.widthLimit > this.size.width){
						var objId = "htmlPointObj_" + this.htmlObjects.length;
						
						htmlObj = new Map24.HtmlObject({
							Longitude	: long, 
							Latitude	: lat,
							Content		: "<div class='" + this.classBaseName + "-n' id='" + objId + "' name='" + objId + "' click='Map24.dispatchEvent(\'a\')'>" + objectString + "</div>",
							Layout		: Map24.MapObject.LAYOUT_NONE,
							Size		: Map24.MapObject.SIZE_S
						});
						htmlObj.commit();
						this.htmlObjects[name] = {
							objectM		: htmlObj,
							name		: objId
						};
						this.htmlObjects.length++;
						var obj = this;
						setTimeout(function(){
							var objTeml = document.getElementById(objId);
							if(objTeml!=null){
								function chNormalClass(){
									util.changeClass(objTeml, obj.classBaseName + "-n")
								}
								function chActiveClass(){
									util.changeClass(objTeml, obj.classBaseName + "-a")
								}
								
								util.attachEvent(objTeml, "mouseout", chNormalClass);
								util.attachEvent(objTeml, "mouseover", chActiveClass);
							}
						}, 300);
					}
					else{
						this.resetHtmlObjects();
					}
				}
			},
			normalizeLayouts	: function(){
				for(var index in this.htmlObjects){
					if(this.htmlObjects[index].objectM.getLayout()==Map24.MapObject.LAYOUT_BALLOON){
						this.htmlObjects[index].objectM.setLayout(Map24.MapObject.LAYOUT_NONE);
						this.htmlObjects[index].objectM.commit();
					}
				}
			},
			resetHtmlObjects	: function(){
				for(var index in this.htmlObjects){
					this.htmlObjects[index].objectM.hide();
					this.htmlObjects[index].objectM.remove();
					this.htmlObjects[index] = null;
				}
				this.htmlObjects.splice(0, this.htmlObjects.length);
			},
			showHtmlObjects		: function(){
				for(var index in this.htmlObjects){
					this.htmlObjects[index].objectM.show();
				}
			},
			hideHtmlObjects		: function(){
				for(var index in this.htmlObjects){
					this.htmlObjects[index].objectM.hide();
				}
			}
		},
		route				: {
			start				: {
				countryCode			: 'RO',
				searchText			: null,
				country				: null,
				county				: null,
				city				: null,
				location			: null
			},
			end				: {
				countryCode			: 'RO',
				searchText			: null,
				country				: null,
				county				: null,
				city				: null,
				location			: null
			},
			setStartByAdd		: function(address, setPoint, parent){
				this.setByAdd(address, this.start, startSetPointToMap);
				var obj = this;
				function startSetPointToMap(targetPoint){
					if(setPoint){
						obj.setPointToMap(targetPoint);
					}
					if(typeof(parent.events.onFirstPointLoaded)=="function"){
						parent.events.onFirstPointLoaded();
					}
				}
			},
			setStartByGeo		: function(lat, long, setPoint, parent){
				this.setByGeo(lat, long, this.start, startSetPointToMap);
				var obj = this;
				function startSetPointToMap(targetPoint){
					if(setPoint){
						obj.setPointToMap(targetPoint);
					}
					if(typeof(parent.events.onFirstPointLoaded)=="function"){
						parent.events.onFirstPointLoaded();
					}
				}
			},
			setEndByAdd			: function(address, setPoint, parent, callback){
				this.setByAdd(address, this.end, startCalculateRoute);
				var obj = this;
				function startCalculateRoute(){
					if(setPoint){
						obj.calculateRoute(parent, callback);
					}
					if(typeof(parent.events.onEndPointLoaded)=="function"){
						parent.events.onEndPointLoaded();
					}
				}
			},
			setEndByGeo			: function(lat, long, setPoint, parent, callback){
				this.setByGeo(lat, long, this.end, startCalculateRoute);
				var obj = this;
				function startCalculateRoute(){
					if(setPoint){
						obj.calculateRoute(parent, callback);
					}
					if(typeof(parent.events.onEndPointLoaded)=="function"){
						parent.events.onEndPointLoaded();
					}
				}
			},
			setByAdd			: function(address, targetPoint, callback){
				var addrSplit = address.split(",");
				
				targetPoint.searchText = Map24.trim(addrSplit[0]).toLowerCase();
				targetPoint.county = Map24.trim(addrSplit[1]).toLowerCase();
				this.setPoint(targetPoint, callback);
			},
			setByGeo			: function(lat, long, targetPoint, callback){
				targetPoint.lat = lat;
				targetPoint.long = long;
				this.setPoint(targetPoint, callback);
			},
			setPoint			: function(targetPoint, callback){
				var obj = this;
				var geocoder = new Map24.GeocoderServiceStub();
				if(targetPoint.searchText != null){
					geocoder.geocode({
						SearchText			: targetPoint.searchText,
						Country				: targetPoint.countryCode,
						CallbackFunction	: callbackFct
					});
				}
				else{
					geocoder.reverseGeocode({
						Longitude			: targetPoint.long,
						Latitude			: targetPoint.lat,
						CallbackFunction		: callbackFct
					});			
				}
				function callbackFct(location){
					if(location.length > 0){
						var assigned=false;
						for(i=0; i<location.length; i++){
							if(location[i].getState()!=null){
								if(targetPoint.county == location[i].getState().toLowerCase()){
									targetPoint.lat = location[i].getLatitude();
									targetPoint.long = location[i].getLongitude();
									targetPoint.country = location[i].getCountry();
									targetPoint.county = location[i].getCounty();
									targetPoint.city = location[i].getCity();
									targetPoint.location = location[i];
									assigned = true;
									break;
								}
							}
						}
						if(assigned==false){
							targetPoint.lat = location[0].getLatitude();
							targetPoint.long = location[0].getLongitude();
							targetPoint.country = location[0].getCountry();
							targetPoint.county = location[0].getCounty();
							targetPoint.city = location[0].getCity();
							targetPoint.location = location[0];
						}
					}
					else if(location!=null){
						targetPoint.lat = location.getLatitude();
						targetPoint.long = location.getLongitude();
						targetPoint.country = location.getCountry();
						targetPoint.county = location.getCounty();
						targetPoint.city = location.getCity();
						targetPoint.location = location;
					}
					if(typeof(callback)=="function"){
						callback(targetPoint);
					}
				}
			},
			calculateRoute		: function(obj, callback){
				if(this.start.location!=null && this.end.location!=null){
					router = new Map24.RoutingServiceStub();
					var me=this;
					router.calculateRoute({
						Start			: this.start.location,
						Destination		: this.end.location,
						ShowRoute: false,
						//CalculationMode	: 'Fastest',
						DescriptionLanguage : 'ro',
						//MinQualityOfGeocodeResults	:	'City',
						//IgnoreWaypoints		: true,
						CallbackFunction: function(route){
							router.showRoute({
								RouteId			: route.RouteID,
								Color			: ['#00F', 150]
							});
							
							startLoc = new Map24.Location({
						       Longitude: me.start.location.getLongitude(),
						       Latitude: me.start.location.getLatitude(),
						       SymbolId: 20950
						      }); 
						     startLoc.commit();

						     //Add a location at the position of the route's destination point.
						     destLoc = new Map24.Location({
						      Longitude: me.end.location.getLongitude(),
						      Latitude: me.end.location.getLatitude(),
						      SymbolId: 20958
						     });
						    destLoc.commit();
							
							if(obj.html.descriptionParent!=null){
							
								divContent="";
								for(var i = 0; i < route.Segments.length; i++){
							      		for(var j = 0; j < route.Segments[i].Descriptions.length; j++){
							      			divContent += (i+1) + ". " + route.Segments[i].Descriptions[j].Text.replace(/(\[|\[\/)[0-9A-Z_]+\]/g, '' ) + '<br>';
							      		}
							    	}
							    obj.html.descriptionParent.innerHTML = divContent;
							}
							
							if(typeof(callback)=="function"){
								callback();
							}
						}
					});
				}
			},
			setPointToMap		: function(targetPoint){
				Map24.MapApplication.center({
					Longitude: targetPoint.location.getLongitude(),
					Latitude: targetPoint.location.getLatitude(),
					MinimumWidth: 5000
				});
			}
		},
		loadMap				: function(parent, callback){
			if(typeof(parent)!="undefined" && parent!=null){
				var obj = this;
				Map24.loadApi( ["core_api", "wrapper_api"] , function(){
					Map24.MapApplication.init({ 
						NodeName	: parent
					}); 
					
					Map24.MapApplication.Map.addListener('Map24.Event.MapClick', function(e) {
						obj.mouseClick.long = e.Coordinate.Longitude;
						obj.mouseClick.lat = e.Coordinate.Latitude;
						if(typeof(obj.events.onMouseClick) == "function"){
							obj.events.onMouseClick(obj.events.mouseClick.lat, obj.mouseClick.long);
						}
						e.stop();
					});
					
					Map24.MapApplication.Map.addListener('Map24.Event.MapPanStop', function(e) {
						obj.setParentCoordMap24(e.ClipRect);
						if(typeof(obj.events.onPan) == "function"){
							obj.events.onPan(obj.parent.topLeft, obj.parent.bottomRight, obj.parent.center);
						}
					});
					
					Map24.MapApplication.Map.addListener('Map24.Event.MapZoomRectStop', function(e) {
						obj.setParentCoordMap24(e.ClipRect);
						if(typeof(obj.events.onZoom) == "function"){
							obj.events.onZoom(obj.parent.topLeft, obj.parent.bottomRight, obj.parent.center);
						}
					});
					
					Map24.MapApplication.Map.addListener('Map24.Event.MapViewChanged', function(e) {
						if(e.ClipRect.getWidth() > obj.html.widthLimit){
							obj.html.resetHtmlObjects();
						}
						obj.setParentCoordMap24(e.ClipRect);
					});
					
					if(callback!=null){
						callback();
					}
				});
			}
		},
		setParentCoordMap24		: function(rect){
			if(typeof(rect.ClassId) != "undefined" &&  rect.ClassId == "Map24.MapView"){
				this.parent.coord.topLeft.lat = rect.TopLeft.Latitude;
				this.parent.coord.topLeft.long = rect.TopLeft.Longitude;
				this.parent.coord.bottomRight.lat = rect.LowerRight.Latitude;
				this.parent.coord.bottomRight.long = rect.LowerRight.Longitude;
				this.parent.coord.center.lat = rect.Center.Latitude;
				this.parent.coord.center.long = rect.Center.Longitude;
				
				this.parent.size.width = rect.getWidth();
				this.parent.size.height = rect.getHeight();
				this.html.size.width = rect.getWidth();
				this.html.size.height = rect.getHeight();
				
				if(typeof(this.events.onViewChange)=="function"){
					this.events.onViewChange(this.parent.topLeft, this.parent.bottomRight, this.parent.center);
				}
			}
		},
		setRoute		:function(startAddress, destinationAddress, showRoute, descPar, callback){
			this.html.descriptionParent = descPar;
			var obj = this;
			this.attachEvents('onFirstPointLoaded', function(){
				obj.route.setEndByAdd(destinationAddress, showRoute, obj, callback);
			});
			this.route.setStartByAdd(startAddress, null, obj);
		},
		setRouteDestGeo		:function(startAddress, destLat, destLong, showRoute, descPar, callback){
			this.html.descriptionParent = descPar;
			var obj = this;
			this.attachEvents('onFirstPointLoaded', function(){
				obj.route.setEndByGeo(destLat, destLong, showRoute, obj, callback);
			});
			this.route.setStartByAdd(startAddress, null, obj);
		},
		attachEvents	:function(event, fct){
			if(typeof(this.events[event])=="function"){
				var oldFct = this.events[event];
				this.events[event] = function(p1,p2,p3,p4,p5,p6,p7,p7){
					oldFct(p1,p2,p3,p4,p5,p6,p7,p7);
					fct(p1,p2,p3,p4,p5,p6,p7,p7);
				}
			}
			else{
				this.events[event] = fct;
			}
		}
	}
}
else{
	mapObj = null;
}

util = {
	changeClass			: function(obj, className){
		if(obj!=null){
			obj.className = className;
		}
	},
	mouseOver			: function(){
		return "mouseover";
	},
	mouseLeave			: function(){
		return "mouseout";
	},
	attachEvent			: function(obj, evType , fn , useCapture){
		var ret=false;
		if(obj!=null){
			if(obj.addEventListener){
				obj.addEventListener(evType , fn , useCapture);
				ret=true;
			}
			else if(obj.attachEvent){
				obj.attachEvent("on" + evType,fn);
				ret=true;
			}
		}
		return ret;
	}
}
