var map;
var poly;
var visiblePoints;
var runClickEvent;
var kmlLayers;
var geoZoom;
var locMarker = null;
var firstModuleLoaded = false;
var circles = new Array();

Array.prototype.remove = function(from, to) {
	var rest = this.slice((to || from) + 1 || this.length);
	return this.push.apply(this, rest);
};

Array.prototype.move_element = function(index, delta) {
  var index2, temp_item;

  if (index < 0 || index >= this.length) {
    return false;
  }

  index2 = index + delta;
  if (index2 < 0 || index2 >= this.length || index2 == index) {
    return false;
  }

  temp_item = this[index2];
  this[index2] = this[index];
  this[index] = temp_item;

  return true;
}

function geo_success(p)
{
	if(gis_version!='mobile')
	{
		var btn = Ext.getCmp('locateBtn');
		var btnEl = btn.getEl().child(btn.buttonSelector);
		btnEl.setStyle('background-image', 'url(rs/icons/silk/world_add.png)');
		
		btn.setDisabled(false);
	}

    var cp = new GLatLng(p.coords.latitude, p.coords.longitude);
    if(locMarker!=null)
    {
        map.removeOverlay(locMarker);
        map.setCenter(cp, 15);
    }

    var gIcon = new GIcon();
    gIcon.image = "rs/icons/gmaps/regroup.png";
	gIcon.shadow = "";
	gIcon.iconSize = new GSize(32, 32);
	gIcon.shadowSize = new GSize(32, 32);
	gIcon.iconAnchor = new GPoint(16, 16);
	gIcon.infoWindowAnchor = new GPoint(0, 32);
	gIcon.infoShadowAnchor = new GPoint(32, 0);
    markerOptions = { icon: gIcon };
    
    locMarker = new GMarker(cp, markerOptions);
    map.addOverlay(locMarker);
}

function geo_error()
{
	if(gis_version!='mobile')
	{
		var btn = Ext.getCmp('locateBtn');
		var btnEl = btn.getEl().child(btn.buttonSelector);
		btnEl.setStyle('background-image', 'url(rs/icons/silk/world_add.png)');
		btn.setDisabled(false);
	}
}

function findLocation()
{
	
    if (geo_position_js.init())
    {
    	if(gis_version!='mobile')
    		Ext.getCmp('locateBtn').setDisabled(false);
    }
    else
    {
    	if(gis_version!='mobile')
    		Ext.getCmp('locateBtn').setDisabled(true);
    	return;
    }
    
    if(gis_version!='mobile')
    {
		var btn = Ext.getCmp('locateBtn');
		var btnEl = btn.getEl().child(btn.buttonSelector);
		btnEl.setStyle('background-image', 'url(rs/icons/loading/snake.gif)');
		
		btn.setDisabled(true);
	}
	
    geo_position_js.getCurrentPosition(geo_success, geo_error);
}

Ext.onReady(function(){	
	Ext.QuickTips.init();
	Map.init();
	
	divScrollButtons = document.getElementById('scrollButtons');
	divToScroll = null;
    
    if (register_status == 'ok')
    {
        Ext.MessageBox.alert(LOCALE_register, LOCALE_register_ok);
    }
});

	function enableScroll(divId)
	{
		divToScroll = divId;
		divScrollButtons.style.visibility = '';
	}
	function disableScroll()
	{
		divToScroll = null;
		divScrollButtons.style.visibility = 'hidden';
	}
	function scrollUp(px)
	{
		px = px || 100;
		divToScroll.scrollTop=(divToScroll.scrollTop>0?divToScroll.scrollTop:0)-px;
		
		return false;
	}
	function scrollDown(px)
	{
		px = px || 100;
		divToScroll.scrollTop=(divToScroll.scrollTop>0?divToScroll.scrollTop:0)+px;
		
		return false;
	}
	
var Map = function(){
	return {
		init : function(){
			
			kmlLayers = [];
			runClickEvent = true;
			visiblePoints = {};
			
			this.glayersRoot = new Ext.tree.AsyncTreeNode({
				id: 0,
				children: [
					{
						leaf: true,
						id: 'wikipedia-layer',
						text: 'Ηλεκτρονική εγκυκλ. wikipedia',
						url: 'org.wikipedia.en',
						iconCls: ''
					},
					{
						leaf: true,
						id: 'panoramio-layer',
						text: 'Φωτογραφίες Panoramio',
						url: 'com.panoramio.all',
						iconCls: ''
					}
				]
			});
			
			this.glayersTree = new Ext.tree.TreePanel({
			//	el:'poi-glayers',
				title: LOCALE_header_resources,
				collapsible: true,
				//autoHeight: true,
				useArrows: true,
				border: false,
				iconCls: 'icon_layers',
				trackMouseOver: true,
				animate: true,
				loader: new Ext.tree.TreeLoader({
					baseAttrs: { uiProvider: Ext.tree.TreeCheckNodeUI }
				}),
				rootVisible: false,
				root: this.glayersRoot
			});
			
			this.glayersTree.on("check",function(node,checked){
				if (checked)
				{
					this.showGLayer(node.attributes.id, node.attributes.url);
				}
				else
				{
					this.hideGLayer(node.attributes.id);
				}
			}, this);
			
			this.filesTree = new Ext.tree.TreePanel({
				title: 'Αρχεία',
				collapsible: true,
				//autoHeight: true,
				useArrows: true,
				border: false,
				trackMouseOver: true,
				iconCls: 'icon_files',
				hidden: (count_file==0) ? true : false,
				animate: true,
				rootVisible: false,
				loader: new Ext.tree.TreeLoader({
					dataUrl:'./filesNodes'
				}),
				root: new Ext.tree.AsyncTreeNode({ id:'0' })
			});

			this.kmlTree = new Ext.tree.TreePanel({
				title: LOCALE_header_kml,
				collapsible: true,
                autoScroll: true,
				iconCls: 'icon_kml',
				checkModel: 'cascade',
				useArrows: true,
				//autoHeight: true,
				hidden: (count_kml==0) ? true : false,
				border: false,
				trackMouseOver: true,
				onlyLeafCheckable: true,
				animate: true,
				rootVisible: false,
				loader: new Ext.tree.TreeLoader({
					dataUrl:'./kmlNodes',
					baseAttrs: { uiProvider: Ext.tree.TreeCheckNodeUI }
				}),
				root: new Ext.tree.AsyncTreeNode({ id:'0' })
			});
			
			this.kmlTree.on("check",function(node,checked){
				if (checked)
				{
					if(node.attributes.s)
					{
						this.showImageLayer(node.attributes.id, node.attributes.url, node.attributes.s, node.attributes.w, node.attributes.n, node.attributes.e);
					}
					else
					{
						this.showKml(node.attributes.id, node.attributes.url);
					}
				}
				else
				{
					if(node.attributes.s)
					{			
						this.hideImageLayer(node.attributes.id);
					}
					else
					{
						this.hideKml(node.attributes.id);
					}
				}
			}, this);

			this.localeMenu = new Ext.menu.Menu({});
			Ext.each(localesArray, function(l){
				
				this.localeMenu.add(
					new Ext.Action({
						text: l.title,
						handler: function(){
							window.location = './' + l.locale + '/gis';
						},
						icon: 'rs/icons/flags/' + l.locale + '.png'
					})
				);
			}, this);
		
			vp_center_map = new Ext.Panel({
				//title: (gis_version=='mobile') ? null : LOCALE_header_map,
				title: LOCALE_header_map,
				iconCls: 'tabs',
				id: 'gmap',
				html: 'map',
				closable:false,
				region:"center",
				deferredRender:false,
				cls: 'mobileMap'
			});
			
			if(gis_version=='mobile')
			{
				vp_center = vp_center_map;
				/*
				vp_center = new Ext.TabPanel({
					id: 'center-tabs',
					region: 'center',
					resizeTabs:true,
					activeTab: 0,
					//renderTo: 'm',
					
					//width: 240,
					//height: 320,
					
					
					deferredRender:false,
					enableTabScroll:true,
					items:[
						vp_center_map
					],
					defaults: {autoScroll: true},
					region:"center",
					xtype:"panel",
					listeners: {
						'beforeadd': {
							fn: function(){ PdMarkerCloseAll(); },
							delay: 5
						}
					}
				});
				*/
			}
			else
			{
				vp_center = new Ext.TabPanel({
					id: 'center-tabs',
					resizeTabs:true,
					activeTab: 0,
					
					deferredRender:false,
					enableTabScroll:true,
					items:[
						vp_center_map
					],
					defaults: {autoScroll: true},
					region:"center",
					xtype:"panel",
					listeners: {
						'beforeadd': {
							fn: function(){ PdMarkerCloseAll(); },
							delay: 5
						}
					}
				});
			}
			
			vp_south = new Ext.Panel({
				region:"south",
				cls: 'south-footer',
				height:45,
				split:true,
				resizable:false,
				bodyStyle: { 
					'padding': '3px'
				},
				autoLoad: './pages/south'
			});
			
			vp_north = new Ext.Panel({
				region:"north",
				xtype:"panel",
				autoLoad: './pages/header',
				cls: 'north-header',
				id: 'north-panel',
				html: '',
				height:120,
				split:true,
				border: false,
				resizable:false,
				bbar: [
					new Ext.Action({
						text: LOCALE_header_home,
						hidden: (showGisHomeButton == 1) ? false : true,
						handler: function(){
							window.location = '.';
						},
						icon: 'rs/icons/silk/house.png'
					}),
					new Ext.Action({
						text: LOCALE_header_about,
						handler: function(){
							Ext.getCmp('center-tabs').add({
								title: LOCALE_header_about,
                                cls: 'contentpage',
								iconCls: 'tabs',
								autoLoad: './pages/about',
								closable:true,
                                bodyStyle: { 'padding': '10px' }
							}).show();
						},
						icon: 'rs/icons/silk/information.png'
					}),
					new Ext.Action({
						text: LOCALE_header_help,
						handler: function(){
							Ext.getCmp('center-tabs').add({
								title: LOCALE_header_help,
                                cls: 'contentpage',
								iconCls: 'tabs',
								autoLoad: './pages/help',
								closable:true,
                                bodyStyle: { 'padding': '10px' }
							}).show();
						},
						icon: 'rs/icons/silk/help.png'
					}),
					'->',
                    new Ext.Action({
						text: LOCALE_header_submit_poi,
						hidden: (showGisSubmitPoi == 1) ? false : true,
                        url : '#',
        				handler : function() {
        				    new Ext.Window({
                                width:800,
                                height:600,
                                resizable: true,
								closable:true,
                                autoScroll:false,
                                title: LOCALE_header_submit_poi,
                                modal:true,
                                html: '<iframe src="./'+LOCALE_name+'/gisSubmit/poi" width="100%" height="100%" scrolling="auto" frameborder="0"></iframe>'
        				    }).show();
                        },
						icon: 'rs/icons/silk/add.png'
					}),
                    '-',
					new Ext.Action({
						text: LOCALE_locate,
						id: 'locateBtn',
						handler: function(){
							findLocation();
						},
						icon: 'rs/icons/silk/world.png'
					}),
					'-',
					{
						text:LOCALE_change_locale,
						icon: 'rs/icons/flags/europeanunion.png',
						menu: this.localeMenu
					},
					'-',
					{
						xtype: 'tbtext',
						text: loginInfo,
						id: 'loginInfo',
						hidden: (USER_IN>0) ? false : true
					},
					' ',
					new Ext.Action({
						text: LOCALE_logout,
						id: 'logoutBtn',
						hidden: (USER_IN>0) ? false : true,
						handler: function(){
							window.location = './&login_logout=true&redirect=gis';
						},
						icon: 'rs/icons/silk/lock.png'
					}),
					new Ext.Action({
						text: LOCALE_register,
						id: 'registerBtn',
						hidden: (USER_IN>0) ? true : false,
						handler: function(){
							window.location = './account/register';
						},
						icon: 'rs/icons/silk/user_go.png'
					})
				]
			});
			
			if(gis_version=='mobile')
			{
				vp_west_items = [];
			
				vp_west = new Ext.Panel({
					id: 'west-panel',
					autoScroll: true,
					defaults: {
						bodyStyle: {
							'padding-top': '5px',
							'padding-bottom': '5px',
							'border-bottom': '1px solid #99BBE8'
						}
					},
					items: vp_west_items
				});
			}
			else
			{
				vp_west_items = [
					this.filesTree,
					this.kmlTree,
					this.glayersTree
				];
			
				vp_west = new Ext.Panel({
					region:"west",
					title: LOCALE_header_poiand,
					width:300,
					minWidth:250,
					mask: true,
					split:true,
					layout: 'accordion',
					collapsible:true,
					id: 'west-panel',
					defaults: {
						bodyStyle: {
							'padding-top': '5px',
							'padding-bottom': '5px',
							'border-bottom': '1px solid #99BBE8'
						}
					},
					items: vp_west_items
				});
			}
						
			vp_east = new Ext.Panel({
				id:'east-panel',
				region:"east",
				title: LOCALE_header_routing,
				//header: (gis_version=='mobile') ? false : true,
				width:300,
				collapsible:(gis_version=='mobile') ? false : true,
				autoScroll:true,
				items: [
					(USER_IN>0) ? {} : login,
					new Ext.Panel({
						title: LOCALE_routing_findaddress_title,
						html: '<input type="text" size="27" name="findAddress_address" id="findAddress_address" value="" /> <input type="button" class="btn" onclick="Map.findAddress();" value="' + LOCALE_routing_findaddress_go +'" />',
						height: 70,
						border: false,
						autoHeight: true,
						bodyStyle: {
							padding: '7px'
						}
					}),
					new routePanel({
						id: 'routePanel',
						title: LOCALE_routing_myroutes,
						autoHeight: true,
						height: 250,
						cmp: {
							name: 'routes'
						}
					}),
					
					new Ext.Panel({
						title: LOCALE_routing_directions,
						autoHeight: true,
						layout: 'fit',
						bodyStyle: { 'padding-left': '7px', 'padding-right': '7px' },
						autoScroll: true,
						id: 'custom_path'
					})
					
				]
			});
			
			if(gis_version=='mobile')
			{
				
				/*
				vp = new Ext.Panel({
					applyTo: 'm',
//					width: s_w,
//					height: s_h,
					layout: 'fit',
					items: [ vp_center ]
				}).show();
				*/
				
				vp = new Ext.Viewport({
					layout:"border",
					items:[
						vp_center
					]
				});
				
				
				win_west = new Ext.Window({
//					width: s_w,
//					height: s_h,
					
					width: 230,
					height: 310,
					y: 5,
					x: 5,
					maximized: false,
					closable: false,
			border: false,
					layout: 'fit',
					tbar: [
						new Ext.Action({
							text: 'Close',
							handler: function(){
								win_west.hide();
								disableScroll();
							},
							icon: 'rs/icons/silk/delete.png'
						})
					],
					items: [
						vp_west
					]
				}).show();
				win_west.hide();
				
				win_kml = new Ext.Window({
//					width: s_w,
//					height: s_h,
					
					width: 230,
					height: 310,
					y: 5,
					x: 5,
					maximized: false,
			border: false,
					closable: false,
					layout: 'fit',
					tbar: [
						new Ext.Action({
							text: 'Close',
							handler: function(){
								win_kml.hide();
								disableScroll();
							},
							icon: 'rs/icons/silk/delete.png'
						})
					],
					items: [
						this.kmlTree
					]
				}).show();
				win_kml.hide();
				
				win_east = new Ext.Window({
//					width: s_w,
//					height: s_h,
					
					width: 230,
					height: 310,
					y: 5,
					x: 5,
					maximized: false,
			border: false,
					
					closable: false,
					layout: 'fit',
					tbar: [
						new Ext.Action({
							text: 'Close',
							handler: function(){
								win_east.hide();
								disableScroll();
							},
							icon: 'rs/icons/silk/delete.png'
						})
					],
					items: [
						vp_east
					]
				}).show();
				win_east.hide();
				
			}
			else
			{
				var vp = new Ext.Viewport({
					layout:"border",
					items:[
						vp_center,
						vp_south,
						vp_north,
						vp_west,
						vp_east
					]
				});
				
			}
			
			Ext.each(gisModules, function(i)
			{
				var tree = new poiPanel(i);
				Ext.getCmp('west-panel').add(tree);
				if(firstModuleLoaded==false)
				{
					firstModuleLoaded = tree;
				}
			});
			
			var gmap = Ext.getCmp('gmap');
			map = new GMap2(gmap.body.dom);

            if (GBrowserIsCompatible()) {
                map.addMapType(G_SATELLITE_3D_MAP);
            }
            
			if(gis_version=='mobile')
			{
				//map.disableDragging();
				map.addControl(new MobileMapControls());
			}
			else
			{
				map.enableScrollWheelZoom();
				map.enableContinuousZoom();
			
				map.setUIToDefault();
				map.addControl(new GOverviewMapControl());
			}
			
			map.disableDoubleClickZoom();
			map.setCenter(new GLatLng(MAP_def_lat, MAP_def_lon), MAP_def_zoom);
			
			//findLocation();
			
			bounds = new GLatLngBounds();
			
			gdir = new GDirections(map, Ext.getCmp("custom_path").body.dom);
						
			GEvent.addListener(map, "mouseover", function() {
				map.checkResize();
			});
			
			GEvent.addListener(gdir, "load", function() {
				if (poly)
					map.removeOverlay(poly);
				poly = gdir.getPolyline();
				map.addOverlay(poly);
			});
			
			GEvent.addListener(map, "move", function() {
				var marker = map.getFirstMarker();
				while (marker != null)
				{
					if (marker.win)
					{
						if (marker.win.isVisible())
						{
							marker.relocateWindow();
						}
					}
					marker = map.getNextMarker();
				}
			});
			
			function handleErrors(){
				if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
				{}	//alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
				else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
				{}	//alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
				else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
				{}	//alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
				else if (gdir.getStatus().code == G_GEO_BAD_KEY)
				{
					alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
				}
				else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
				{}	//alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
				else
				{
					//TODO: remove the last point but why?
					//Map.removeLastDirectionPoint();
					alert(LOCALE_map_gdir_unknown);
				}
			}
		
			GEvent.addListener(gdir, "error", handleErrors);

			GEvent.addListener(map, "click", function(marker, point) {
				
				if (!runClickEvent)
				{
					if (!marker)
					{
						PdMarkerCloseAll();
					}
					return true;
				}
			});
			
			GEvent.addListener(map, "dblclick", function(marker, point) {
				if((gis_version=='mobile') && (USER_IN==0))
				{
					return;
				}
				
				PdMarkerCloseAll();	
				if (!marker)
				{
					var newPoint = Ext.getCmp('routePanel').newPoint({
						lat: point.lat(),
						lng: point.lng()
					});
					
					var marker = Map.createMarker('user', '0', point, newPoint.id, newPoint.text, '', 'user');
					map.addOverlay(marker);
				}
			});
		},

		findAddress: function()
		{
			geocoder = new GClientGeocoder();
			var address = Ext.get('findAddress_address').dom.value;
            
			if (MAP_def_city && address.search(",")<0)
			{
				address = address + ', ' + MAP_def_city;
			}
			
			if (geocoder)
			{
				geocoder.getLatLng(
					address,
					function(point)
					{
						if (!point)
						{
							alert(address + " not found");
						} else {
							map.setCenter(point, 13);
							
							var newPoint = Ext.getCmp('routePanel').newPoint({
								lat: point.lat(),
								lng: point.lng(),
								text: Ext.get('findAddress_address').dom.value
							});
							
							var marker = Map.createMarker('user', '0', point, newPoint.id, newPoint.text, '', 'user');
							map.addOverlay(marker);
						}
					}
				);
			}
		},
		
		clearPois: function(cmpName)
		{
			var marker = map.getFirstMarker();
			
			while (marker != null)
			{
				if (marker.xxc.cmpName == cmpName)
				{
					this.removeSpecificMarker(marker);
					marker = map.getFirstMarker();
				}
				else
				{
					marker = map.getNextMarker();
				}
			}			
			return true;
			
		},
		
		showKml: function(id, url)
		{
			var geoXml = new GGeoXml(url);
			GEvent.addListener(geoXml, 'load', function() {
				if (geoXml.loadedCorrectly()) {
					geoXml.gotoDefaultViewport(map);
					kmlLayers[id] = geoXml;
				}
			});
			kmlLayers[id] = geoXml;
			map.addOverlay(kmlLayers[id]);
			//alert('Loaded kml on id '+id);
		},
		
		hideKml: function(id)
		{
			map.removeOverlay(kmlLayers[id]);
		},
		
		showImageLayer: function(id, url, s, w, n, e)
		{
			var id = id;
			var boundaries = new GLatLngBounds(new GLatLng(s,w), new GLatLng(n,e));
			var oldmap = new GGroundOverlay(url, boundaries);
			kmlLayers[id] = oldmap;
			map.addOverlay(oldmap);
		},
		
		hideImageLayer: function(id)
		{
			map.removeOverlay(kmlLayers[id]);
		},
		
		showGLayer: function(id, url)
		{
			kmlLayers[id] = new GLayer(url);
			map.addOverlay(kmlLayers[id]);
		},
		
		hideGLayer: function(id)
		{
			map.removeOverlay(kmlLayers[id]);
		},
		
		hideAllTooltips: function()
		{
			var marker = map.getFirstMarker();
			while (marker != null)
			{
				marker.hideTooltip();
				marker = map.getNextMarker();
			}
		},
		
		getMarker: function(cmpName, xxcId)
		{
			var marker = map.getFirstMarker();
			while (marker != null)
			{
				if ( (marker.xxc.id == xxcId) && (marker.xxc.cmpName == cmpName) )
				{
					return marker;
				}
				marker = map.getNextMarker();
			}
		},
		
		openInfo: function(cmpName, xxcId)
		{
			var m = this.getMarker(cmpName, xxcId);
			Ext.getCmp('center-tabs').add({
				title: m.xxc.name,
				iconCls: 'tabs',
				autoLoad: './'+ LOCALE_name + '/tabs/'+ cmpName + '/' + m.xxc.id,
				closable:true
			}).show();
			
		},
		
		zoomToMarker: function(cmpName, xxcId)
		{
			PdMarkerCloseAll();
			var marker = map.getFirstMarker();
			while (marker != null)
			{
				if ( (marker.xxc.id == xxcId) && (marker.xxc.cmpName == cmpName) )
				{
					map.setCenter(marker.xxc.point, 15);
					return true;
				}
				else
				{
					marker = map.getNextMarker();
				}
			}
		},
		
		addDirectionPoint: function(cmpName, xxcId)
		{
			var m = this.getMarker(cmpName, xxcId);

			var newPoint = Ext.getCmp('routePanel').newPoint({
				lat: m.xxc.point.lat(),
				lng: m.xxc.point.lng(),
				text: m.xxc.name
			});

		},
		
		removeDirectionPoint: function(directionId)
		{
		},
		
		showCategory: function(cmpName, categoryId)
		{
			GDownloadUrl(BASEURL+"/poiPoints/" + categoryId + "/" + cmpName, this.loadPoints);
		},
		
		hideCategory: function(cmpName, categoryId)
		{
			var marker = map.getFirstMarker();
			
			while (marker != null)
			{
				if ( (marker.xxc.categoryId == categoryId) && (marker.xxc.cmpName == cmpName) )
				{
					this.removeMarker(cmpName, marker.xxc.id);
                    
                    if (circles != null && circles[marker.xxc.id] != null)
                    {
                        map.removeOverlay(circles[marker.xxc.id]);
                    }
            
					this.hideCategory(cmpName, categoryId);
				}
				marker = map.getNextMarker();
			}
			
			return true;
		},
		
		removeMarker: function(cmpName, xxcId)
		{
			PdMarkerCloseAll();
			
			var marker = this.getMarker(cmpName, xxcId);
			
			marker.remove();
		},
		
		removeSpecificMarker: function(marker)
		{
			PdMarkerCloseAll();
			
			marker.remove();
		},
		
		createMarker: function(cmpName, categoryId, point, id, name, html, icon)
		{
			if (categoryId=='0') {
				userDefined = true;
				cmpName = 'user';
				id = 'userpoi' + id;
				icon = BASEURL + '/rs/p192/images/user.png';
			}
			else {
				userDefined = false;
			}	        
			
			var gIcon = new GIcon();
			gIcon.shadow = "";
			if((gis_version!='mobile') || ((gis_version=='mobile') && (USER_IN>0)))
			{
				gIcon.iconSize = new GSize(32, 32);
				gIcon.shadowSize = new GSize(32, 32);
			}
			else
			{
				gIcon.iconSize = new GSize(16, 16);
				gIcon.shadowSize = new GSize(16, 16);
			}
			gIcon.iconAnchor = new GPoint(16, 16);
			gIcon.infoWindowAnchor = new GPoint(0, 32);
			gIcon.infoShadowAnchor = new GPoint(32, 0);
			gIcon.image = icon;
			
			marker = new PdMarker(point, { icon: gIcon });
			marker.setTooltip(name);
			
        	if (!userDefined)
        	{
				if(gis_version!='mobile')
                {
                    html = '<p>' + html + '</p><br />' +
    				'<a href="#" onclick="Map.openInfo(\'' + cmpName + 
    					'\', \'' + id + '\');return false;"><img src="rs/icons/silk/information.png" align="top" /> ' + 
    					LOCALE_routing_more + '</a>';
				}
                else
                {
                    html = '<p>' + html + '</p><br />';
                }
                	
				if((gis_version!='mobile') || ((gis_version=='mobile') && (USER_IN>0)))
				{
					html = html +'<br />' +
						'<a href="#" onclick="Map.addDirectionPoint(\'' + cmpName + '\', \'' + id + 
						'\');return false;"> <img src="rs/icons/silk/add.png" align="top" /> ' + LOCALE_routing_add + '</a>';
				}
			}
			
			marker.setDetailWinHTML(html);
		
			marker.xxc = {
				userDefined: userDefined
				, name: name
				, id: id
				, icon: icon
				, categoryId: categoryId
				, point: point
				, cmpName: cmpName
			};
			
			return marker;
		},
		
		showRoute: function(catNode)
		{
			var routeString = '';
			var from = true;
			
			Ext.each(catNode.childNodes, function(i){
				var poi = i.attributes;
				routeString += ( from ? 'from: ' : ' to: ' ) + poi.lat + ', ' + poi.lng;
				from = false;
			});

			gdir.load(routeString, {getPolyline:true, getSteps:true});
		},
		
		showRoutePoints: function(catNode)
		{
			Ext.each(catNode.childNodes, function(i){
				var poi = i.attributes;
				var point = new GLatLng(poi.lat, poi.lng);
				var marker = Map.createMarker('user', '0', point, point.id, poi.text, '', 'user');
				map.addOverlay(marker);
			});
		},
		
		hideRoutePoints: function(catNode)
		{
			Map.hideCategory('user', '0');
		},
		
		hideRoute: function()
		{
			gdir.load('', {getPolyline:true, getSteps:true});
		},
		
		loadPoints: function(doc) {
			// parse the json doc
			var jsonData = eval('(' + doc + ')');
            
			// create and set the markers on the map
			if (jsonData)
			{
				for (var i=0; i<jsonData.markers.length; i++)
				{
					jsonData.markers[i].lat = parseFloat(jsonData.markers[i].lat.replace(',','.'));
					jsonData.markers[i].lng = parseFloat(jsonData.markers[i].lng.replace(',','.'));
					if (jsonData.markers[i].lat==0 || jsonData.markers[i].lng==0 || jsonData.markers[i].text=='')
					{
						//console.info('test');
					}
					else
					{
						//console.info(jsonData.markers[i]);
						var point = new GLatLng(jsonData.markers[i].lat, jsonData.markers[i].lng);
						var marker = Map.createMarker(jsonData.markers[i].cmpName, jsonData.markers[i].categoryId, point, jsonData.markers[i].id, jsonData.markers[i].text, jsonData.markers[i].html, jsonData.markers[i].mapIcon);
						map.addOverlay(marker);
                        
                        if (marker.xxc.cmpName == 'poi_wireless')
                        {
                            //Create and add the circle
                            //0.125 miles = 200 meters
                            if (jsonData.markers[i].radius)
                            {
                                var radius = jsonData.markers[i].radius; 
                                var degrees = jsonData.markers[i].degrees ? jsonData.markers[i].degrees : 360; 
                                var angle = jsonData.markers[i].angle ? jsonData.markers[i].angle : 0; 
                                circles[marker.xxc.id] = new CircleOverlay(point, radius, degrees, angle, "#999999", 1, 1, '#999999', 0.25);
                                map.addOverlay(circles[marker.xxc.id]);
                            }
                        }
					}
				}	
			}
			
			vp_west.body.unmask()
			
		}
    };
}();
