var layer;
var shape;
var loctxt;
var pt;

//var map = null;
//var findPlaceResults = null;



function GetMap(){
	
	map = new VEMap('listingmap');
	map.LoadMap();
	
	map.AttachEvent("onmousedown",OnClickHandler);
	
	TryFindLoc();
	
} 



function GetMap2(address){
	map = new VEMap('listingmap');
	map.LoadMap();
	
	loctxt = "";
	loctxt = address;
	
	map.Find(null,loctxt, null, null, null, null, true, true, false, true, GetCoordinates); 
 
}

function GetCoordinates(layer, resultsArray, places, hasMore, veErrorMessage){

	try{
		findPlaceResults = places[0].LatLong;
		var myShape = new VEShape(VEShapeType.Pushpin, findPlaceResults);
		myShape.SetDescription(loctxt);
		map.AddShape(myShape);
	}
	catch(e) {}
} 



function OnClickHandler(e){
    
	if (e.rightMouseButton) {
		
		var x = e.mapX;
		var y = e.mapY;
		pixel = new VEPixel(x, y);
		LL = map.PixelToLatLong(pixel);
		
		map.FindLocations(LL, GetResults);
	}
}

function GetResults(locations){
	
	var s = "";
	var ptitle = "";
	
	if(locations != null){
		
		s += '<span class="addressline">' + locations[0].Name + '</span>';
		ptitle = locations[0].Name;
		setFormAddress(locations[0].Name, LL);
	}
	else{
		s += "No Result found.";
	} 
	
	if (LL.Latitude != null && LL.Longitude != null) {
		
		s += "<br />Latitude: " + LL.Latitude + "<br />Longitude: " + LL.Longitude;
		addPin( ptitle, LL );
	}
	else 
		s += "<br />You have to choose road view to see location information."
	
	
	document.getElementById('map-address-div').innerHTML = s;
}


 
 
function setFormAddress(locName, longlat){
	
	var ilatitude = document.getElementById("latitude");
	var ilongitude = document.getElementById("longitude");
	
	var iaddress = document.getElementById("address");
	var icity = document.getElementById("city");
	var izip = document.getElementById("zip");
	
	var sselect = document.getElementById("listing_state");
	
	
	if (longlat.Latitude != null && longlat.Longitude != null) {
		
		ilatitude.value = longlat.Latitude;
		ilongitude.value = longlat.Longitude;
	}
	
	var locarray = new Array();
	locarray = locName.split(",");
	
	if (locarray.length == 3) {
		
		iaddress.value = locarray[0];
		icity.value = locarray[1];
		
		var statezip = new Array();
		var tstatezip = locarray[2].replace(/^\s+/, '');
		statezip = tstatezip.split(" ");
		if (statezip.length == 2) {
			
			izip.value = statezip[1];
			
			scode = statezip[0];
			soption = sselect.options[sselect.selectedIndex];
			
			if (soption.value.toUpperCase() != scode.toUpperCase()) {
				
				var aoptions = new Array();			
				aoptions = sselect.options;
				for (var i = 0; i < aoptions.length; i++) {
					
					otocompare = sselect.options[i].value;
					if( otocompare.toUpperCase() == scode.toUpperCase() ){
						
						sselect.selectedIndex = i;
						stateSelected2();
					}
					
				}
			}
		}
	}
	
} 


         
function FindLoc(loctxt){
	
	try {
		map.Find(null, loctxt);
	}
	catch(e) {}
}


function TryFindLoc(){
	
	var iaddress = document.getElementById("address");
	var icity = document.getElementById("city");
	var izip = document.getElementById("zip");
	
	var sselect = document.getElementById('listing_state');
	var soption = null;
	if( sselect.selectedIndex != 0 )	soption = sselect.options[sselect.selectedIndex];
	
	var ilong = document.getElementById("longitude");
	var ilat = document.getElementById("latitude");
	
	try {
		
		loctxt = "";
		
		if (ilong.value != null && ilong.value.length > 0 && ilong.value != "0" && ilat.value != null && ilat.value.length > 0 && ilat.value != "0" ) {
			
			loctxt = "Longitude: " + ilong.value + ", Latitude: " + ilat.value;
			
			pt = new VELatLong( ilat.value, ilong.value );
			map.SetZoomLevel(7);
			map.SetCenter(pt);
			addPin( loctxt, pt );
			
		}
		
		else {
		
			if (iaddress.value != null && iaddress.value.length > 0) 
				loctxt += iaddress.value + ", ";
			if (icity.value != null && icity.value.length > 0) 
				loctxt += icity.value;
			if (soption != null && soption.value.length > 0) 
				loctxt += ", " + soption.value;
			if (izip.value != null && izip.value.length > 0) 
				loctxt += " " + izip.value;
			
			map.Find("", loctxt, VEFindType.Businesses, null, 0, 10, true, true, false, true, TryFindCallback);
		}
		
	} 
	catch (e) {}
}

function TryFindCallback( shape, fresarray, fplacearray, moreres, ferror ){
	
	try {
		//var td = document.getElementById("tdiv");
		if (fplacearray != null) {
			
			//alert('tfcallback');
			addPin( loctxt, fplacearray[0].LatLong );
			
			//td.innerHTML = fplacearray[0].LatLong;
		}	
		
	}
	catch(e) {}
	
}

function addPin( pintitle, pinlatlong ){
	
	if( shape != null ){
		layer.DeleteShape(shape);
		shape = null; 
	}
	
	if( layer != null ){
		map.DeleteShapeLayer(layer);
        layer = null;
	}

	
	layer = new VEShapeLayer();
    map.AddShapeLayer(layer);
	
	//shape = new VEShape(VEShapeType.Pushpin, map.GetCenter());
	shape = new VEShape(VEShapeType.Pushpin, pinlatlong);
	shape.SetTitle( pintitle );
    //shape.SetDescription('This is a pushpin.');
		
	layer.AddShape(shape);
}


