var geocoder = new GClientGeocoder();

var points = new Array();
var mainOffices = new Array();
var markers = new Array();
var isLast = false;

//All addresses
var addresses = new Array();

//array for icons
var icons = new Array();

//marker images definitions
var iconBlue = new GIcon();
iconBlue.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";
iconBlue.iconSize = new GSize(12, 20);
iconBlue.shadowSize = new GSize(22, 20);
iconBlue.iconAnchor = new GPoint(6, 20);
iconBlue.infoWindowAnchor = new GPoint(5, 1);

//Main Offices
var iconKC = new GIcon();
iconKC.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png";
iconKC.iconSize = new GSize(25, 25);
iconKC.shadowSize = new GSize(25, 25);
iconKC.iconAnchor = new GPoint(15, 15);
iconKC.infoWindowAnchor = new GPoint(5, 1);

var iconYellow = new GIcon();
iconYellow.image = "http://labs.google.com/ridefinder/images/mm_20_yellow.png";
iconYellow.iconSize = new GSize(12, 20);
iconYellow.shadowSize = new GSize(22, 20);
iconYellow.iconAnchor = new GPoint(6, 20);
iconYellow.infoWindowAnchor = new GPoint(5, 1);

var iconGreen = new GIcon();
iconYellow.image = "http://labs.google.com/ridefinder/images/mm_20_green.png";
iconYellow.iconSize = new GSize(12, 20);
iconYellow.shadowSize = new GSize(22, 20);
iconYellow.iconAnchor = new GPoint(6, 20);
iconYellow.infoWindowAnchor = new GPoint(5, 1);

//for setting zoom level
var mapObj;

function init()
{
    if (GBrowserIsCompatible()) 
    {
    	var map = new GMap2(document.getElementById("map"));	
    	mapObj = map;
		icons["MainOffice"] = iconKC;
		icons["blueIcon"] = iconBlue;
		
    	addControls(map);
    	map.setCenter(new GLatLng(centerLat,centerLng));
    	
		for(var i = 0; i<adr.length; i++)
		{
			if(i == adr.length-1)
			{
				isLast = true;
			}
			addMarker(adr[i][0],adr[i][1],adr[i][2],adr[i][3],adr[i][4],adr[i][5],adr[i][6],adr[i][7],adr[i][8],"",adr[i][10],adr[i][11],adr[i][12],adr[i][13],map,isLast);
		}
    }
}

function addMarker(company, name, address, postcode, city, state, country, phone, fax, email, www, type, lat, lng, map, isLast)
{
	var point = new GLatLng(lat,lng);
	if(type == "MainOffice")
	{
		var marker = new GMarker(point, icons["MainOffice"]);	
		mainOffices.push(marker);
	}
	else
	{
		var marker = new GMarker(point, icons["blueIcon"]);
		markers.push(marker);
	}
	
	GEvent.addListener(marker, "click", function(){
		map.setCenter(point);
		var infoBoxText = "<p>" + company + ",<br>" + address + ",<br>" + city + ", " + country  + ",<br>" + phone + ",<br>" + fax;
		if(www.length > 3)
		{
			infoBoxText += "<br><a href='http://" + www + "' target='_blank'>" + www + "</a>";
		}
		infoBoxText += "</p>";
		
		marker.openInfoWindowHtml(infoBoxText);
	
		});
	
	if(isLast==true)
	{
		var mgr = new MarkerManager(map);
		mgr.addMarkers(mainOffices,0);
		mgr.addMarkers(markers,2);
		mgr.refresh();
	}	
	map.setCenter(new GLatLng(centerLat,centerLng), zoomLevel);
}

function addControls(map) 
{
	map.addControl(new GLargeMapControl());
	map.addControl(new GScaleControl());
}

function focusMapByCountry(countryName)
{
	if(countryName == "United Kingdom")
	 {
	 countryName = "Great Britain";
	 }
	else if(countryName.indexOf(" ") > -1 && countryName.indexOf("Arab") == -1 && countryName.indexOf("Africa") == -1)
	 {
	  countryName = countryName.substring(0,countryName.indexOf(" "));
	 }
	
	geocoder.getLatLng(countryName,
		function(point)
		{
			if (point) 
			{
		    	mapObj.setCenter(point);
		    	mapObj.setZoom(4);
		    } 
		}
	);
}


