var map;
var localSearch = new GlocalSearch();
var gdir;
var icon = new GIcon();
icon.image = "http://www.google.com/mapfiles/marker.png";
icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
icon.iconSize = new GSize(20, 34);
icon.shadowSize = new GSize(37, 34);
icon.iconAnchor = new GPoint(10, 34);
var reasons=[];
      reasons[G_GEO_SUCCESS]            = "Success";
      reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
      reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
      reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
      reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
      reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
      reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
      reasons[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
      reasons[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";
      reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";
function pointData(lat, lng, type, html, frmHTML, marker, tmpMarker, beds, minPrice, nearTown, phoneNumber, county) {
	this.lat = lat;
	this.lng = lng;
	this.type = type;
	this.html = html;
	this.frmHTML = frmHTML;
	this.marker = marker;
	this.tmpMarker = tmpMarker;
	this.beds = beds;
	this.minPrice = minPrice;
	this.nearTown = nearTown;
	this.phoneNumber = phoneNumber;
	this.county = county;
}
var myPointDataCount = 0;
var myPointData = new Array();
var sTmpAdd = '';
function addPointToMap(lng, lat, url, desc, type, devName, beds, minPrice, nearTown, phoneNumber, county){	
	switch (type){
		case 'dev':
		var WINDOW_HTML = '<h1 class="googleMapHeader">' + devName + '</h1>' + '<b>' + nearTown + ''
			if (county.length>1){
 				WINDOW_HTML = WINDOW_HTML + ', '+ county
			}
			WINDOW_HTML = WINDOW_HTML + '</b>'
			WINDOW_HTML = WINDOW_HTML + '<br>For information call ' + phoneNumber
			//WINDOW_HTML = WINDOW_HTML + '<br><a href="javascript:tohere('+myPointDataCount+')">Get directions to ' + devName + '</a>';
			//WINDOW_HTML = WINDOW_HTML + '<form action="javascript:getDirections()"><br>Get directions from (place or postcode):<br><input type="text" SIZE=30 MAXLENGTH=40 name="saddr" id="saddr" value="" style="font-size:12px;" />'		
			WINDOW_HTML = WINDOW_HTML + '<form style="margin:0;" action="http://maps.google.com/maps" method="get" target="_blank"><br>Get directions from (place or postcode):<br><input type="text" SIZE=35 MAXLENGTH=40 name="saddr" id="saddr" value="" style="font-size:12px;" />'		
			//WINDOW_HTML = WINDOW_HTML + '<br><br>To: <B>' + devName + ', ' + nearTown + '</B>'        	 
			WINDOW_HTML = WINDOW_HTML + '<br><br><INPUT value="Get directions" TYPE="SUBMIT" style="font-size:12px;">' 
			//WINDOW_HTML = WINDOW_HTML + '<input type="hidden" id="daddr" value="'+name+"@"+ lng + ',' + lat 
			//WINDOW_HTML = WINDOW_HTML + '<input type="hidden" id="daddr" value="' + lng + ',' + lat + '"/></Form>';
			WINDOW_HTML = WINDOW_HTML + '<input type="hidden" id="daddr" name="daddr" value="' + lng + ',' + lat + '"/></Form>';
			break;
		case '':
			break;
		default:
			var tmpIcon = icon;
	}
	var WINDOW_HTML_DIR = '<form action="javascript:getDirections()"><br>From (place or postcode):<br><input type="text" SIZE=30 MAXLENGTH=40 name="saddr" id="saddr" value="" style="font-size:12px;" />'		
		WINDOW_HTML_DIR = WINDOW_HTML_DIR + '<br><br>To: <B>' + devName + ', ' + nearTown + '</B>'        	 
		WINDOW_HTML_DIR = WINDOW_HTML_DIR + '<br><br><INPUT value="Get directions" TYPE="SUBMIT" style="font-size:12px;">' 
		WINDOW_HTML_DIR = WINDOW_HTML_DIR + '<input type="hidden" id="daddr" value="'+name+"@"+ lng + ',' + lat 
		WINDOW_HTML_DIR = WINDOW_HTML_DIR + '"/></Form>';
	var tmpIcon = getIcon(type)
    var marker = new GMarker(new GLatLng(lng, lat), {icon:tmpIcon});	
	myPointData[myPointDataCount] = new pointData(lat, lng, type, WINDOW_HTML, WINDOW_HTML_DIR, marker, tmpIcon, null, beds, minPrice, phoneNumber, county);
	map.addOverlay(marker);
	GEvent.addListener(marker, "click", function() {
		try {
			addMessage(WINDOW_HTML, type, lng, lat, getID(WINDOW_HTML, type, lng, lat));
		}
		catch (e)
	  	{
			alert(e.constructor + '-constructor');
			alert(e.description + '-description ');
			alert(e.message + '-message ');
			alert(e.filename + '-filename ');
			alert(e.lineNumber + '-lineNumber ');
			alert(e.name + '-name ');
			alert(e.number + '-number ');
			alert(e + '-');
	  	}
      });
	myPointDataCount = myPointDataCount + 1;
}
function getID(html, type, lng, lat){
	var i=0
	for (i=0;i<myPointDataCount;i++)
	{
		if ((myPointData[i].html==html)&&(myPointData[i].type==type)&&(myPointData[i].lng==lng)&&(myPointData[i].lat==lat)){
		
			return i;
		}
	}
}
function addMessage(sVal, type, lng, lat, id){
	try{
		
		var marker = new GMarker(new GLatLng(lng, lat));
		map.addOverlay(marker);
		marker.hide();
		marker.openInfoWindowHtml(sVal);
		myPointData[id].tmpMarker = marker;
	}
	catch (e){
		alert(e.constructor + '-constructor');
		alert(e.description + '-description ');
		alert(e.message + '-message ');
		alert(e.filename + '-filename ');
		alert(e.lineNumber + '-lineNumber ');
		alert(e.name + '-name ');
		alert(e.number + '-number ');
		alert(e + '-');
	}
}
function getIcon(type){
	switch (type){
		case 'millSmall':
			var tmpIcon = iconMillSmall;
			break;
		case 'millLarge':
			var tmpIcon = iconMillLarge;
			break;
		case 'dev':
			var tmpIcon = icon;
			break;
		case 'devSmall':
			var tmpIcon = iconDevSmall; 
			break;
		case 'devLarge':
			var tmpIcon = iconDevLarge;
			break;
		case '':
			break;
		default:
			var tmpIcon = icon;
	}
	return tmpIcon;
}
function tohere(i) {
	var tmp = myPointData[i].tmpMarker;
	tmp.openInfoWindowHtml(myPointData[i].frmHTML);
}
function getDirections() {
	var saddr = document.getElementById("saddr").value	
	var daddr = document.getElementById("daddr").value
	sTmpAdd = daddr;
	getLongAndLat(saddr, testinggg);
}
function getLongAndLat(sAdd, callbackFunction){
	localSearch.setSearchCompleteCallback(null, 
		function() {
			if (localSearch.results[0])
			{	
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var sTmp = '@' + resultLat+', '+resultLng
				gdir=new GDirections(map, document.getElementById("message"));
				addDirListner();
				gdir.load("from: "+sTmp+" to: "+sTmpAdd);
				map.closeInfoWindow();
			}else{
				alert("Sorry, unable to get driving directions!");
			}
		});	
	localSearch.execute(sAdd+', UK');
}
function addDirListner(){
	GEvent.addListener(gdir, "error", function() {
        var code = gdir.getStatus().code;
        var reason="Code "+code;
        if (reasons[code]) {
          reason = reasons[code]
        } 
        	alert("Failed to obtain directions, "+reason);
      });
}
function testinggg(sTmp){
	alert('testingg');
}
function fromhere(i) {
	gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
function addUnLoadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
	  window.onunload = func;
	} else {
	  window.onunload = function() {
	    oldonunload();
	    func();
	  }
	}
}
addUnLoadEvent(GUnload);