LANG = 'en';
var map = null;
var id = null;
var mapproject = null;
var data = null;
var categories = [];
var subcategories = [];
var subCatArray = new Array( );
var contextPin = null;
var loadIds = null;
var cstmCount = 0;
var defaults = {
	//xml : 'xml/map.xml',
	xml : '../admin/map-xml.php',
	dashboard : 'Normal',
	sortable : true,
	listView : false,
	hotelOnly: false,
	printNames : true,
	miniMap : false,
	kilometers : false,
	context : false,
	acceptQuery : false,
	simpleMap : false,
	printMap : false
};
var regional = [];
regional[''] = {};
var items = [];
var changedItems = new Array();
var layer;
var showIt = true;
var editmode = false;
var editid;
var orgPoint;
var orgIndex;


$(function() {   	
	$("div.subcategory").live("mouseover", function(){
		if ( !($(this).hasClass('on')) )
			$(this).css('backgroundPosition', '-250px 0');
	    $(this).children('a.question').css('display','block');
	});
	$("div.subcategory").live("mouseout", function(){
		if ( !($(this).hasClass('on')) )
			$(this).css('backgroundPosition', '0px 0');
	    $(this).children('a.question').css('display','none');
	});
	
	var elemPos
	$('a.question').live("mouseover",function() { 
		var subid = $(this).parent().attr('id');
		var subid = 'desc-' + subid;
		elemPos = this.parentNode;
		
		//var elemTop = elemtPos.offsetHeight
		
		$('div#'+subid).show();	
		var elemHeight = document.getElementById(subid);
		var elemTop = elemPos.offsetTop - elemHeight.offsetHeight;
		//alert ( elemPos.offsetTop+ ' ' + elemHeight.offsetHeight);
		$('div#'+subid).css('top', (elemTop + 10)+ 'px');
		$('div#'+subid).css('left', (elemPos.offsetLeft+2)+ 'px');
										  
	});
	$('a.question').live("mouseout",function() { 
		var subid = $(this).parent().attr('id');
		$('div#desc-'+subid).hide();									  
	});

	
	$('div.subcategory').hover(function() {
        var subid = $(this).attr('id');
		alert(subid);
		$('div#desc-'+subid).show();
    },function() {
        var subid = $(this).attr('id');
		$('div#desc-'+subid).hide();
    });

	
	
	//$('#cstm-submit').live("click",function() { findCustom($.trim($('#cstm-address').val()), true); return false; });
	//$('#cstm-submit2').live("click",function() { findCustom($.trim($('#cstm-address').val()), true); return false; });
	
});		
		


	function MSNMap(mapid,project) {
		id = mapid;
		mapproject = project;
		initializeMap();
		
		
		$('input#drawLine').attr('checked',false);
		$('input#drawPoly').attr('checked',false);
		$('input#addPin').attr('checked',false);
	}
	
	function initializeMap() {			
			this.getData();					
			$(window).unload(function() { if(this._map) { this._map.Dispose(); } });
	}
	
	function getData() {
		var obj = this;
		$.get(defaults.xml, function(data) { 
			
			$('item', data).each(function(i) {
				items[i] = {};
				$(this).children().each(function() {
					if (this.tagName == 'subcategory')
						items[i][this.tagName] = $(this).text().split(",");	
					else 
						items[i][this.tagName] = $(this).text();	
				});
			});
	
			// Get String Query
			var q = window.location.search.substring(1);
			if(q.length>0) {
				var query = obj.parseQuery(q);
				if(query.id!==null&&query.id!==undefined) {
					obj.loadIds = query.id.split(',');							
				}
			}
					
			//data = items;
			createMap();
			$('div#road').addClass('on');
			loadPoints('environmental','road');
		});	
	}
	
	function createMap() {
		map = new VEMap(id);

		// initial point to loap map, 
		if ( items[0]['georss:point'] ) {
			var fp = getLatLong(items[0]['georss:point']);
		}
		else if ( items[0]['georss:polygon'] ) {				
			var fp = getLatLong(items[0]['georss:polygon']);
		}
		else if ( items[0]['georss:polyline'] ) 
			var fp = getLatLong(items[0]['georss:polyline']);
		
		switch(mapproject){
			case "a":
				map.LoadMap(new VELatLong(39.1358, -77.1296), 13);
				break;
			case 'b':
				map.LoadMap(new VELatLong(39.0981, -77.0206), 13);
				break;
			case 'c':
				map.LoadMap(new VELatLong(39.0677, -76.9306), 13);
				break;
			case 'd':
				map.LoadMap(new VELatLong(39.0847, -76.8937), 13);
				break;
			case 'e':
				map.LoadMap(new VELatLong(39.0684, -76.8937), 15);
				break;
			default:
				map.LoadMap(new VELatLong(39.0842, -77.0216), 12);
		}	
			
		map.SetMapStyle(VEMapStyle.Road);
		map.SetMouseWheelZoomToCenter(false);
		map.Hide3DNavigationControl();

		//Create Map
		//map = new VEMap('MapDiv');
		map.SetMouseWheelZoomToCenter(false);
							
		lineLayer = new VEShapeLayer();
		map.AddShapeLayer(lineLayer);  
		
		switch(mapproject){
			case "a":
				map.PanToLatLong(new VELatLong(39.1358, -77.1296));
				break;
			case 'b':
				map.PanToLatLong(new VELatLong(39.0981, -77.0206));
				break;
			case 'c':
				map.PanToLatLong(new VELatLong(39.0677, -76.9306));
				break;
			case 'd':
				map.PanToLatLong(new VELatLong(39.0847, -76.8937));
				break;
			case 'e':
				map.PanToLatLong(new VELatLong(39.0684, -76.8937));
				break;
			default:
				map.PanToLatLong(new VELatLong(39.0842, -77.0216));
		}	
		
		
		
		//Gets the categories, but commented out for first phase.  So we'll default to getSubCategories
		//getCategories();
		
		if(loadIds!==null) {
			loadPoints(null);
		} else {
			getSubCategories('environmental');
			//this.loadPoints(this._categories[0]);
		}	
	}
	
	function getLatLong(point) {
		var pts = point.split(" ");
		return pts;
	}
	
	function getCategories() {	
		var obj = this;
		$.each(items, function(i, val) {
			var dup = false; // duplicate set to valse
			var tem = val.category; // get temporary value of category
			$.each(categories, function(j, val2) {
				if(tem == val2) { 
					dup = true; // dupblicate 
				}
			}); 
			if(!dup) { 
				categories.push(tem); // add to categories if not a dupublicate
			}
		});
		printCategories();
	}
	function printCategories() {
		// Print all of the categories
		var div = $('div#categories');
		$.each(categories, function(i, val) {
			$('<div />').addClass('category').html(val)
				.click(function() {
					getSubCategories(val);
				}).appendTo(div);					  
		});
	}
	
	function getSubCategories(cat) {
		$.each(items, function(i, val) {
			if (cat == val.category) {
				var dup = false; // duplicate set to false
				var tempsplit = val.subcategory; // split each subcategory node
				
				subCatArray = val.subcategory;
			
				for (x in subCatArray){
					dup = false; // duplicate set to valse
					$.each(subcategories, function(j, val2) {
						if ( subCatArray[x]	 == val2 ) {
							dup = true;
						}
					});
					if(!dup) {
						subcategories.push(subCatArray[x]);
					}			
				}
			}
		});
		printSubCategories(cat);		
	}
	function printSubCategories(cat) {
		// Print all of the categories
		var div = $('div#subcategories');		
		$.each(subcategories, function(i, val) {
			/*$('<div />').addClass('subcategory').attr('id',val).html('<em class="alt">' + val + '</em><a href="" class="question"><em class="alt">Question</em></a>')
				.click(function() {
					$(this).addClass('on');
					loadPoints(cat,val);							
					return false;
				}).appendTo(div);					  */
		});
	}
	function loadPoints(cat,subcat) {
		var subcats = subcat===null?subcategories:[subcat];
		var notMade = checkLayers(subcat);
		if(notMade) {
			$.each(subcats, function(i, val) {
				//var layer = new VEShapeLayer();
				layer = new VEShapeLayer();
				layer.id = val;
				var shapes = [];
				
				if(loadIds!==null) {
					$.each(loadIds, function(k, val2) {
						$.each(items, function(j) {
							if(items[j].id==val2) {
								subcat = 'allcats';
								editmode = true;
								var d = createPoints(layer, shapes, j, subcat);
							}								
						});						
					});
				} else {
					$.each(items, function(j) {
						if (cat == items[j].category) {	
							var subCatSelectedArray = new Array( );
							var currarray = String(items[j].subcategory);
							subCatSelectedArray = currarray.split(",");
	
							for (x in subCatSelectedArray){
								if(subCatSelectedArray[x]==val) {
										var d = createPoints(layer, shapes, j, subcat);
								}
							}
						}							
					});
				}
				map.AddShapeLayer(layer);
				if (subcat == 'road') {
					switch(mapproject){
						case "a":
							map.PanToLatLong(new VELatLong(39.1358, -77.1296));
							break;
						case 'b':
							map.PanToLatLong(new VELatLong(39.0981, -77.0206));
							break;
						case 'c':
							map.PanToLatLong(new VELatLong(39.0677, -76.9306));
							break;
						case 'd':
							map.PanToLatLong(new VELatLong(39.0847, -76.8937));
							break;
						case 'e':
							map.PanToLatLong(new VELatLong(39.0684, -76.8937));
							break;
						default:
							map.PanToLatLong(new VELatLong(39.0842, -77.0216));
					}	
				}
			});
		}
	}
	
	function checkLayers(subcat) {		
		var mapLayers = map.GetShapeLayerCount();
		for(var h=0; h<mapLayers; h++) {
			var l = map.GetShapeLayerByIndex(h);
			// this will hide the layer, since it is preloaded		
			if(l.id!=undefined && l.id==subcat) {
				if ( l.IsVisible() ) {
					l.Hide();
					showIt = false;
					$('div#'+subcat).removeClass('on');
					$('div#'+subcat).addClass('off');
				} else {
					l.Show();
					if (subcat == 'road') {
						 var latLongRoad = new VELatLong(39.0842, -77.0216);
               			map.PanToLatLong(latLongRoad);	
					}
					showIt = true;
					$('div#'+subcat).removeClass('off');
					$('div#'+subcat).addClass('on');	
				}
				return false;
			} 			
		}
		return true;
	}
	
	function createPoints(layer, shapes, j, subcat, cstm) {	
		var info = '';
		if ( items[j]['type'] == 'point' )
			addPoint(shapes, items[j]['georss:point'], j, subcat); 
		else if ( items[j]['type'] == 'line' )
			addPolyline(shapes, items[j]['georss:polyline'], j, subcat); 
		else if ( items[j]['type'] == 'polygon' )
			addPolygon(shapes, items[j]['georss:polygon'], j, subcat); 
	}

	function addPoint(shapes, point, j, subcat, cstm) {
		var obj = this;
		var info = '';
		var point = obj.getLatLong(items[j]['georss:point']); 
		var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(parseFloat(point[0]), parseFloat(point[1])));
			
		var shapeCount = cstm?cstmCount:shapes.length;
		map.ClearInfoBoxStyles();
		
		//Set the icon
		var shapeCount = cstm?cstmCount:shapes.length;
		 	 
		//Set the info box
		map.ClearInfoBoxStyles();
		//shape.SetTitle(items[j]['title']);	

		info = '';
		info = '<a href="#" onclick="map.HideInfoBox();return false;" class="btn-x"><img src="/images/map/x.gif" /></a>';
		
		if (subcat == 'custom') {
			shape.SetCustomIcon('<div id='+items[j].id+'" class="pushpin"><img src="'+items[j].pushpin+'" alt="" /></div>');
			if(items[j].title!==''&&items[j].image!==undefined) {
				info += '<div class="title">'+items[j].title+'</div>';
			}
			if(items[j].address!==''&&items[j].address!==undefined) {
				info += '<div class="address">' + items[j].address + '</div>';
			}
			if(items[j].description!==''&&items[j].description!==undefined) {
				info += items[j].description;
			}			
		} else {
			if(items[j].subcategory!==''&&items[j].subcategory!==undefined) {
				if ( items[j].subcategory.length > 1 ) {
					shape.SetCustomIcon('<div class="pushpin"><img src="/images/map/pins/star.gif" alt="" /></div>');
					var subcatarraytemp = String(items[j].subcategory);
					subcatarraytemp = subcatarraytemp.split(",");
					info += '<ul class="itemsub">';
					for (x in subcatarraytemp){								
						info += '<li id="iconcat-'+ subcatarraytemp[x] + '"><em class="alt">' + subcatarraytemp[x] + '</em></li>';
					}
					info += '</ul>';
					if(items[j].title!==''&&items[j].image!==undefined) {
						info += '<div class="title">'+items[j].title+'</div>';
					}
				} else {
					shape.SetCustomIcon('<div class="pushpin"><img src="/images/map/pins/'+items[j].subcategory+'.gif" alt="" /></div>');
					if(items[j].title!==''&&items[j].image!==undefined) {
						info += '<div class="title" style="padding-top:17px;">'+items[j].title+'</div>';
					}
				}
			}
			if(items[j].image!==''&&items[j].image!==undefined) {
				info += '<div id="pin-image"><img src="'+items[j].image+'" alt="" /></div>';
			}
			if(items[j].address!==''&&items[j].address!==undefined) {
				info += '<div class="address">' + items[j].address + '</div>';
			}
			if(items[j].description!==''&&items[j].description!==undefined) {
				info += '<div class="description">' +  items[j].description + '</div>';
			}
			info += '<p class="box-links">';
			if(items[j].moreinfo!==''&&items[j].moreinfo!==undefined) {
				info += '<a onclick="map.HideInfoBox();return false;" href="'+items[j].moreinfo+'" target="_blank">More Info</a>';
			}		
			if (items[j].moreinfo!==''&& items[j].moreinfo!==undefined && items[j].schedule!=='' && items[j].schedule!==undefined) {
				info += '<span class="box-pipe">|</span>';	
			}		
			if(items[j].schedule!==''&&items[j].schedule!==undefined) {
				info += '<a onclick="map.HideInfoBox();return false;" href="'+items[j].schedule+'" target="_blank">Schedule</a>';
			}
			
			info += '</p>';		
		}
		//info += '</div>';
	
		
		shape.SetDescription(info);

		layer.id = subcat;
		layer.AddShape(shape);
		if (editmode == true){
			updateFields(j,'point');		
		}
	}
	
	function addPolyline(shapes, points, j, subcat, cstm) {
		var obj = this;
		var ll = map.GetCenter();
		var lat = ll.Latitude;
		var lon = ll.Longitude;
		var pts = points.split(" ");
		var ptsArray = new Array( );
		var b = 0;
		for ( var a = 0; a < pts.length; a=a+2) {
			ptsArray.push(new VELatLong(parseFloat(pts[a]),parseFloat(pts[a+1])))
			b++;
		} 

		var shape = new VEShape(VEShapeType.Polyline, ptsArray);
		//Set the line color	 
		switch(items[j].id){
			case '78':
				shape.SetLineColor(new VEColor(240,181,68,1));
				break;
			case '79':
				shape.SetLineColor(new VEColor(176,167,204,1));
				break;
			case '80':
				shape.SetLineColor(new VEColor(246,222,80,1));
				break;
			case '81':
				shape.SetLineColor(new VEColor(175,208,227,1));
				break;
			case '82':
				shape.SetLineColor(new VEColor(229,124,85,1));
				break;
			default:
				var linecolors = items[j]['linecolor'].split(',');	
				var lineColor = new VEColor(linecolors[0],linecolors[1],linecolors[2],1);	
				shape.SetLineColor(lineColor);
		}				
		 
		//Set the line width
		switch(items[j].id){
			case '78':
			case '79':
			case '80':
			case '81':
			case '82':
				shape.SetLineWidth('6');
				break;
			default:
				shape.SetLineWidth(items[j]['linewidth']);
		}
			
		 
		//Set the icon
		var shapeCount = cstm?cstmCount:shapes.length;
		 	 
		//Set the info box
		map.ClearInfoBoxStyles();
		//shape.SetTitle(items[j]['title']);	
		
		info = '';
		info = '<a href="#" onclick="map.HideInfoBox();return false;" class="btn-x"><img src="/images/map/x.gif" /></a>';
		
		if (subcat == 'custom') {
			shape.SetCustomIcon('<div id='+items[j].id+'" class="pushpin"><img src="'+items[j].pushpin+'" alt="" /></div>');
			if(items[j].title!==''&&items[j].image!==undefined) {
				info += '<div class="title">'+items[j].title+'</div>';
			}
			if(items[j].address!==''&&items[j].address!==undefined) {
				info += '<div class="address">' + items[j].address + '</div>';
			}
			if(items[j].description!==''&&items[j].description!==undefined) {
				info += items[j].description;
			}			
		} else {
			if(items[j].subcategory!==''&&items[j].subcategory!==undefined) {
				if ( items[j].subcategory.length > 1 ) {
					shape.SetCustomIcon('<div class="pushpin"><img src="/images/map/pins/star.gif" alt="" /></div>');
					var subcatarraytemp = String(items[j].subcategory);
					subcatarraytemp = subcatarraytemp.split(",");
					info += '<ul class="itemsub">';
					for (x in subcatarraytemp){								
						info += '<li id="iconcat-'+ subcatarraytemp[x] + '"><em class="alt">' + subcatarraytemp[x] + '</em></li>';
					}
					info += '</ul>';
					if(items[j].title!==''&&items[j].image!==undefined) {
						info += '<div class="title">'+items[j].title+'</div>';
					}
				} else {
					switch(items[j].id){
						case '78':
							shape.SetCustomIcon('<div id='+items[j].id+'" class="pushpin" style="padding-top:50px;"><img src="/images/map/pins/contract-a.gif" alt="" /></div>');
							break;
						case '79':
							shape.SetCustomIcon('<div id='+items[j].id+'" class="pushpin"><img src="/images/map/pins/contract-b.gif" alt="" /></div>');
							break;
						case '80':
							shape.SetCustomIcon('<div id='+items[j].id+'" class="pushpin" style="margin-left:-90px;padding-top:10px;"><img src="/images/map/pins/contract-c.gif" alt="" /></div>');
							break;
						case '81':
							shape.SetCustomIcon('<div id='+items[j].id+'" class="pushpin" style="padding-top:20px;"><img src="/images/map/pins/contract-d.gif" alt="" /></div>');
							break;
						case '82':
							shape.SetCustomIcon('<div id='+items[j].id+'" class="pushpin" style="padding-top:23px;margin-left:-20px;"><img src="/images/map/pins/contract-e.gif" alt="" /></div>');
							break;
						default:
							shape.SetCustomIcon('<div id='+items[j].id+'" class="pushpin"><img src="/images/map/pins/'+items[j].subcategory+'.gif" alt="" /></div>');		
							
							
					}


					if(items[j].title!==''&&items[j].image!==undefined) {
						info += '<div class="title" style="padding-top:17px;">'+items[j].title+'</div>';
					}
				}
			}
			if(items[j].image!==''&&items[j].image!==undefined) {
				info += '<div id="pin-image"><img src="'+items[j].image+'" alt="" /></div>';
			}
			if(items[j].address!==''&&items[j].address!==undefined) {
				info += '<div class="address">' + items[j].address + '</div>';
			}
			if(items[j].description!==''&&items[j].description!==undefined) {
				info += '<div class="description">' +  items[j].description + '</div>';
			}
			info += '<p class="box-links">';
			if(items[j].moreinfo!==''&&items[j].moreinfo!==undefined) {
				info += '<a onclick="map.HideInfoBox();return false;" href="'+items[j].moreinfo+'" target="_blank">More Info</a>';
			}		
			if (items[j].moreinfo!==''&& items[j].moreinfo!==undefined && items[j].schedule!=='' && items[j].schedule!==undefined) {
				info += '<span class="box-pipe">|</span>';	
			}		
			if(items[j].schedule!==''&&items[j].schedule!==undefined) {
				info += '<a onclick="map.HideInfoBox();return false;" href="'+items[j].schedule+'" target="_blank">Schedule</a>';
			}
			
			info += '</p>';		
		}
		
		shape.SetDescription(info);
		 
		layer.id = subcat;
		layer.AddShape(shape);
		if (editmode == true){
			updateFields(j,'line');		
		}
	} 

	function addPolygon(shapes, points, j, subcat, cstm){
		var obj = this;
		var ll = map.GetCenter();
		var lat = ll.Latitude;
		var lon = ll.Longitude;
		
		var pts = points.split(" ");
		var ptsArray = new Array( );
		var b = 0;
		for ( var a = 0; a < pts.length; a=a+2) {
			ptsArray.push(new VELatLong(parseFloat(pts[a]),parseFloat(pts[a+1])))
			b++;
		}
	
		var shape = new VEShape(VEShapeType.Polygon, ptsArray);
		 
		//Set the line color
		var linecolors = items[j]['linecolor'].split(',');		 
		var lineColor = new VEColor(linecolors[0],linecolors[1],linecolors[2],1);
		shape.SetLineColor(lineColor);
		 
		//Set the line width
		shape.SetLineWidth(items[j]['linewidth']);
		 
		//Set the fill color
		var fillcolors = items[j]['fillcolor'].split(',');		 
		var fillColor = new VEColor(fillcolors[0],fillcolors[1],fillcolors[2],0.6);
		shape.SetFillColor(fillColor);

		//Set the icon
		var shapeCount = cstm?cstmCount:shapes.length;
		 	 
		//Set the info box
		map.ClearInfoBoxStyles();
		//shape.SetTitle(items[j]['title']);	
		info = '';
		info = '<a href="#" onclick="map.HideInfoBox();return false;" class="btn-x"><img src="/images/map/x.gif" /></a>';
		
		if (subcat == 'custom') {
			shape.SetCustomIcon('<div id='+items[j].id+'" class="pushpin"><img src="'+items[j].pushpin+'" alt="" /></div>');
			if(items[j].title!==''&&items[j].image!==undefined) {
				info += '<div class="title">'+items[j].title+'</div>';
			}
			if(items[j].address!==''&&items[j].address!==undefined) {
				info += '<div class="address">' + items[j].address + '</div>';
			}
			if(items[j].description!==''&&items[j].description!==undefined) {
				info += items[j].description;
			}			
		} else {
			if(items[j].subcategory!==''&&items[j].subcategory!==undefined) {
				if ( items[j].subcategory.length > 1 ) {
					shape.SetCustomIcon('<div class="pushpin"><img src="/images/map/pins/star.gif" alt="" /></div>');
					var subcatarraytemp = String(items[j].subcategory);
					subcatarraytemp = subcatarraytemp.split(",");
					info += '<ul class="itemsub">';
					for (x in subcatarraytemp){								
						info += '<li id="iconcat-'+ subcatarraytemp[x] + '"><em class="alt">' + subcatarraytemp[x] + '</em></li>';
					}
					info += '</ul>';
					if(items[j].title!==''&&items[j].image!==undefined) {
						info += '<div class="title">'+items[j].title+'</div>';
					}
				} else {
					shape.SetCustomIcon('<div class="pushpin"><img src="/images/map/pins/'+items[j].subcategory+'.gif" alt="" /></div>');
					if(items[j].title!==''&&items[j].image!==undefined) {
						info += '<div class="title" style="padding-top:17px;">'+items[j].title+'</div>';
					}
				}
			}
			if(items[j].image!==''&&items[j].image!==undefined) {
				info += '<div id="pin-image"><img src="'+items[j].image+'" alt="" /></div>';
			}
			if(items[j].address!==''&&items[j].address!==undefined) {
				info += '<div class="address">' + items[j].address + '</div>';
			}
			if(items[j].description!==''&&items[j].description!==undefined) {
				info += '<div class="description">' +  items[j].description + '</div>';
			}
			info += '<p class="box-links">';
			if(items[j].moreinfo!==''&&items[j].moreinfo!==undefined) {
				info += '<a onclick="map.HideInfoBox();return false;" href="'+items[j].moreinfo+'" target="_blank">More Info</a>';
			}		
			if (items[j].moreinfo!==''&& items[j].moreinfo!==undefined && items[j].schedule!=='' && items[j].schedule!==undefined) {
				info += '<span class="box-pipe">|</span>';	
			}		
			if(items[j].schedule!==''&&items[j].schedule!==undefined) {
				info += '<a onclick="map.HideInfoBox();return false;" href="'+items[j].schedule+'" target="_blank">Schedule</a>';
			}
			
			info += '</p>';		
		}
		
		shape.SetDescription(info);

		layer.id = subcat;
		layer.AddShape(shape);
		if (editmode == true){
			updateFields(j,'poly');		
		}
  }
  
	function parseQuery(str){
		var vars = str.split("&");
		var pairs = {};
		for (var i=0;i<vars.length;i++) {
			var p = vars[i].split("=");
			pairs[p[0]] = this.urlDecode(p[1]);
		}
		return pairs;
	}
		
	function urlDecode(encodedString) {
		var output = encodedString;
		var binVal, thisString;
		var myregexp = /(%[^%]{2})/;
		while ((match = myregexp.exec(output)) !== null && match.length > 1 && match[1] !== '') {
			binVal = parseInt(match[1].substr(1),16);
			thisString = String.fromCharCode(binVal);
			output = output.replace(match[1], thisString);
		}
		return output;
	}
	

function findCustom(cstmAddress, prepend) {
	var obj = this;
	var address = cstmAddress?cstmAddress:$.trim($('#cstm-address').val());
	//what, where, findType, shapeLayer, startIndex, numberOfResults, showResults, createResults, useDefaultDisambiguation, setBestMapView, callback
	map.Find(null, address, null, null, null, null, null, null, null, false, function(l, resultsArray, places, hasMore, veErrorMessage) {
		if(places) {
			loadCustom(name, places, prepend);
		}
	});
}

function loadCustom(cat, point, prepend) {
	var num = items.length;
	var shapes = []; 
	var notMade = this.checkLayers(cat);
	var block = null;
	if(notMade) {
		layer = new VEShapeLayer();
		layer.id = cat;
		//block = this.createBlock(layer, cat);
		if(loadIds===null) {
			var obj = this;
			$('<div />').addClass('category').html(cat)
				.click(function() {
					obj.loadPoints(cat);
				}).appendTo('#categories');
		}
	} else {
		var mapLayers = this.map.GetShapeLayerCount();
		for(var h=0; h<mapLayers; h++) {
			var l = map.GetShapeLayerByIndex(h);
			if(l.id==regional[''].custom) {
				layer = l;
				block = $('#cat-custom');
				break;
			}
		}
	}
	
	items[num] = {};
	items[num].id = num;
	items[num].subcategory = 'custom';
	items[num].title = point[0].Name;
	items[num].address = $('#cstm-address').val();
	items[num].description = '';
	items[num].image = '';
	items[num].pushpin = '/images/map/pins/pin-violet.gif';
	items[num].moreinfo = '';
	items[num]['georss:point'] = point[0].LatLong.Latitude+" "+point[0].LatLong.Longitude;
	items[num].type = 'point';

	cstmCount++;
	var d = createPoints(layer, shapes, num, 'custom',cstmCount);
	//createPoints(layer, shapes, j, subcat, cstm)
	//$('> div:last', block).append(d);

	if(notMade) { 
		this.map.AddShapeLayer(layer); 
		layer.AddShape(shapes);
		//if (!this._defaults.simpleMap) {
			//this.getBestView();
		//}
	}
}

