var map;

var markerIndex = 1;

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);


// Create a base icon for all of our markers that specifies the
// shadow, icon dimensions, etc.
var baseIcon = new GIcon(G_DEFAULT_ICON);
baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
baseIcon.iconSize = new GSize(20, 34);
baseIcon.shadowSize = new GSize(37, 34);
baseIcon.iconAnchor = new GPoint(9, 34);
baseIcon.infoWindowAnchor = new GPoint(9, 2);


// Creates a marker whose info window displays the letter corresponding
// to the given index.
function createMarker(point, index) {
  // Create a lettered icon for this point using our icon class
  //var letter = String.fromCharCode("A".charCodeAt(0) + index);
  var letteredIcon = new GIcon(baseIcon);
  //letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
  letteredIcon.image = "http://www.animaloracle.com/pet/images/markers/marker" + index + ".png";

  // Set up our GMarkerOptions object
  markerOptions = { icon:letteredIcon };
  var marker = new GMarker(point, markerOptions);

  //GEvent.addListener(marker, "click", function() {
  //  marker.openInfoWindowHtml("Marker <b>" + letter + "</b>");
  //});
  return marker;
}




function JinitMyGMap(latitude, longitude, mapZoom) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		//map.addControl(new GLargeMapControl());
		map.addControl(new GSmallMapControl());
		//map.setUIToDefault();


		if(isNaN(latitude) || isNaN(longitude) || isNaN(mapZoom)) {
			map.setCenter(new GLatLng(54.622978,-2.592773), 5, G_NORMAL_MAP);
		}	else {
			map.setCenter(new GLatLng(latitude,longitude), mapZoom, G_NORMAL_MAP);
		}
  }
}


function jrwPlaceMarkerAtPoint(point) {
	if (GBrowserIsCompatible()) {
		//var marker = new GMarker(point,icon);
		var marker = createMarker(point,index++);
		map.addOverlay(marker);
  }
}

function JMarker(p_lat, p_long, p_markerIndex) {
	if (GBrowserIsCompatible()) {
		//var marker = new GMarker(point,icon);
    var point = new GLatLng(p_lat,p_long);
		var marker = createMarker(point,jamesD(p_markerIndex));
		map.addOverlay(marker);
  }
}


function jamesD(inx) {
		inx = inx-27;
		var newinx;
		if      (inx >=  0 && inx <= 25) {newinx= inx/2;}
		else if (inx >= 46 && inx <=88) {newinx= inx/3;}
		else if (inx >= 89 && inx <=174) {newinx= inx/9;}
		else if (inx >= 178 && inx <=235) {newinx= inx/6;}
		else if (inx >= 242 && inx <=310) {newinx= inx/5;}
		else if (inx >= 311 && inx <=402) {newinx= inx/8;}
		else if (inx >= 418 && inx <=474) {newinx= inx/6;}
		else if (inx >= 478 && inx <=555) {newinx= inx/8;}
		else if (inx >= 560 && inx <=700) {newinx= inx/7;}
		else if (inx >= 702 && inx <=870) {newinx= inx/9;}
		else if (inx >= 871 && inx <=989) {newinx= inx/8;}
		else if (inx >= 990 && inx <=1100) {newinx= inx/10;}
		else if (inx >= 1101 && inx <=1310) {newinx= inx/9;}
		else if (inx >= 1311 && inx <=1487) {newinx= inx/11;}
		else if (inx >= 1492 && inx <=1666) {newinx= inx/10;}
		else if (inx >= 1667 && inx <=1799) {newinx= inx/12;}
		else if (inx >= 1800 && inx <=2002) {newinx= inx/11;}
		else if (inx >= 2041 && inx <=2234) {newinx= inx/13;}
		else if (inx >= 2250 && inx <=2499) {newinx= inx/12;}
		else if (inx >= 2500) {newinx= inx/14;}
		else {newinx= 0;}
		return newinx;
		
}


function HDjzar(p_postcode, p_index) {
	if (GBrowserIsCompatible()) {
		//var marker = new GMarker(point,icon);
		var sTmp1 = myArray[p_postcode];
		if(typeof sTmp1 == 'string') {
			var sTmp2 = sTmp1.split(",");
			var m_lat = sTmp2[0];
			var m_long = sTmp2[1];
			
			var point = new GLatLng(m_lat,m_long);
			var marker = createMarker(point,p_index);
			map.addOverlay(marker);
		}
  }
}




function jrwCenterMapDoIt(point) {
	if (GBrowserIsCompatible()) {
		map.setCenter(point, 11, G_NORMAL_MAP);
	}
}


function usePointFromPostcode(postcode, callbackFunction) {
	var localSearch = new GlocalSearch();	
	localSearch.setSearchCompleteCallback(null, 
		function() {
			
			if (localSearch.results[0])
			{		
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				callbackFunction(point);
			}else{
				alert("Postcode not found!");
			}
		});	
		
	localSearch.execute(postcode + ", UK");
}

function jrwSetHiddenFields(point) {
	if (GBrowserIsCompatible()) {
		document.Form1.latitude.value = point.lat();
		document.Form1.longitude.value = point.lng();
  }
}



function usePointFromPostcode2(postcode, callbackFunction) {
	var localSearch = new GlocalSearch();	
	localSearch.setSearchCompleteCallback(null, 
		function() {
			
			if (localSearch.results[0])
			{		
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				document.Form1.latitude.value = point.lat();
				document.Form1.longitude.value = point.lng();				
				document.Form1.imgPostcodeValid.style.visibility='visible';
				
				callbackFunction(point);
			}else{
				document.Form1.txtPostcode.value = "";
				document.Form1.latitude.value = "";
				document.Form1.longitude.value = "";
				document.Form1.imgPostcodeValid.style.visibility='hidden';
				alert("Postcode not found!");
			}
		});	
		
	localSearch.execute(postcode + ", UK");
}

function jrwLoadMapAndMarker(point) {
	if (GBrowserIsCompatible()) {
		
		JinitMyGMap(point.lat(), point.lng(), 11)

		var marker = createMarker(point,1);
		map.addOverlay(marker);
  }
}





function jrwCenterMapByPlacename(address) {
  var geocoder = new GClientGeocoder();

  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " not found");
        } else {
          map.setCenter(point, 11);
        }
      }
    );
  }
}

function jrwCenterMapByCounty(address) {
  var geocoder = new GClientGeocoder();

  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " not found");
        } else {
          map.setCenter(point, 8);
        }
      }
    );
  }
}




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();
	  }
	}
}


