if (!Array.prototype.indexOf)  // This bit of code is to make "indexOf" work in all browsers (start's here)
{  
  Array.prototype.indexOf = function(elt /*, from*/)  
  {  
    var len = this.length >>> 0;  
  
    var from = Number(arguments[1]) || 0;  
    from = (from < 0)  
         ? Math.ceil(from)  
         : Math.floor(from);  
    if (from < 0)  
      from += len;  
  
    for (; from < len; from++)  
    {  
      if (from in this &&  
          this[from] === elt)  
        return from;  
    }  
    return -1;  
  };  
}  // (End's here)

	var map;
    var directionsPanel;
    var directions;
	var flag_array = new Array('AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AN', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AX', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BM', 'BN', 'BO', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 'CV', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER', 'ES', 'ET', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GG', 'GH', 'GI', 'GL', 'GM', 'GN', 'GQ', 'GR', 'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JE', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MO', 'MP', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RO', 'RS', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'ST', 'SV', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'YE', 'ZA', 'ZM', 'ZW');
	
    function LoadMap(map_location) {
      map = new GMap2(document.getElementById("map"));
		map.setUIToDefault();
		map.disableScrollWheelZoom();
        map.setMapType(G_HYBRID_MAP);
		//map.addMapType(G_SATELLITE_3D_MAP);
		map.setCenter(new GLatLng(34, 0), 2);
		
		
		
      geocoder = new GClientGeocoder();
      var address = map_location;
	  geocoder.getLocations(address, addAddressToMap);
	 
		  
    function addAddressToMap(response) {
      map.clearOverlays();
      if (!response || response.Status.code != 200) {
		//document.getElementById("map").className = "hidden";
      } else {
		document.getElementById("map").className = "visible";
        place = response.Placemark[0];
		if(map_location.match(/(\-?[0-9]{1,3}[.][0-9]{1,}),\s(\-?[0-9]{1,3}[.][0-9]{1,})/)){
			lat_long = map_location.split(', ');
			 point = new GLatLng(lat_long[0], lat_long[1]);
		}else{
			point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		}
		map.setCenter(point, 2);
        marker = new GMarker(point);
        map.addOverlay(marker);
		var html = '<div style="width:250px;">'+map_location+'</div><br /><span style="color:blue;  text-decoration:underline; cursor:pointer;" onclick="Loaddirections1(\''+map_location+'\')">Get directions</span><br />';
        marker.openInfoWindowHtml(html);
		
		var curent_flag = place.AddressDetails.Country.CountryNameCode;
		if(flag_array.indexOf(curent_flag) !== -1){
		document.getElementById("contry_flag").src = 'images/flags/' + curent_flag + '.jpg';
		document.getElementById("contry_flag").className = 'image_center';
		}
		else{ document.getElementById("test").innerHTML = curent_flag; }
		
		GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
		});
      }
    }
	}
	
    function Loaddirections1(map_location) {
	document.getElementById("directions").innerHTML = "";
	marker.openInfoWindowHtml('<strong>From:</strong><input id="from" type="text" size="10" /><input type="submit" value="ok" onclick="Loaddirections2()"/><div style="width:250px;"><strong>To:</strong> '+map_location+'</div>');
	}
	
    function Loaddirections2() {
	  document.getElementById("directions").className = "visible";
	  directionsPanel = document.getElementById("directions");
      directions = new GDirections(map, directionsPanel);
      directions.load("from: "+document.getElementById("from").value+" to: "+point, {local:"en_US"});
	  map.closeInfoWindow();
    }
