var map = null;
var geocoder = null;
var bounds = null;
var gdir;
var zoomLevel = 14;
var iconImage = "http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/blank.png";
var shopSize = 0;
var plottedShopNumber = 0;
//var showMap = false;

function initializeGmapsMultiplePoints(numOfshops) {
	try {
		if (GBrowserIsCompatible()) {
			geocoder = new GClientGeocoder();
			map = new GMap2($('map_canvas'));
			map.disableDragging(); 
			map.disableDoubleClickZoom(); 
			map.disableContinuousZoom(); 
			map.disableScrollWheelZoom(); 
			map.setCenter(new GLatLng(0,0),0);
		    bounds = new GLatLngBounds();
		    shopSize = numOfshops;
//		    $('map_canvas').hide();
		}
	} catch (err) {
		txt = "There was an error on this page.\n\n"
		txt += "Error description: " + err.description + "\n\n"
		txt += "Please check your proxy/firewall settings.\n\n"
		txt += "Click OK to continue.\n\n"
		alert(txt)
	}
}

function add2Map(address, link) {
	if (geocoder) {
		geocoder.getLatLng(address, function(point) {
			plottedShopNumber = plottedShopNumber + 1;
			if (point) {
			     map.addOverlay(createLinkMarker(point, link));
				 bounds.extend(point);
//				 showMap = true;
			}
			if(plottedShopNumber >= shopSize){
				 map.setZoom(map.getBoundsZoomLevel(bounds));
				 map.setCenter(bounds.getCenter());
//				 if(showMap){
//					 $('map_canvas').show();
//				 }
			 }
		});
	}
}

function createLinkMarker(latlng, link) {
	// Create our "tiny" marker icon
	var blueIcon = new GIcon(G_DEFAULT_ICON);
	blueIcon.image = iconImage;
	// Set up our GMarkerOptions object
	markerOptions = {
		icon : blueIcon
	};

	var marker = new GMarker(latlng, markerOptions);
	marker.value = 1;
	GEvent.addListener(marker, "click", function() {
		document.location = link;
	});
	return marker;
}

function initializeGmaps(address, phone, thumbnailImg, addressText, phoneText) {
	try {
		if (GBrowserIsCompatible()) {
			geocoder = new GClientGeocoder();
			map = new GMap2($('map_canvas'));
			map.addControl(new GLargeMapControl());
			showAddress(address, phone, thumbnailImg, addressText, phoneText);

		}
	} catch (err) {
		txt = "There was an error on this page.\n\n"
		txt += "Error description: " + err.description + "\n\n"
		txt += "Please check your proxy/firewall settings.\n\n"
		txt += "Click OK to continue.\n\n"
		alert(txt)
	}
}

function createMarker(latlng, address, phone, thumbnailImg, addressText,
		phoneText) {
	// Create our "tiny" marker icon
	var blueIcon = new GIcon(G_DEFAULT_ICON);
	blueIcon.image = iconImage;
	// Set up our GMarkerOptions object
	markerOptions = {
		icon : blueIcon
	};

	var marker = new GMarker(latlng, markerOptions);
	marker.value = 1;
	GEvent.addListener(marker, "click", function() {
		map.openInfoWindowHtml(latlng, createHtmlContent(address, phone,
				thumbnailImg, addressText, phoneText));
	});
	return marker;
}

function createHtmlContent(address, phone, thumbnailImg, addressText, phoneText) {
	var myHtml = "<table ><tbody ><tr ><td width='122px'><div><b>"
			+ addressText + ":</b><br/>" + address + "<br/><br/><b>"
			+ phoneText + ":</b><br/>" + phone + "</div></td>";
	if (thumbnailImg != null && thumbnailImg != '')
		myHtml += "<td><table ><tbody ><tr align='center' ><td><img border='0' style='width: 154px; height: 101px;'src='"
				+ thumbnailImg
				+ "'/></td></tr></tbody></table></td></tr></tbody></table>"
	return myHtml;
}

function showAddress(address, phone, thumbnailImg, addressText, phoneText) {
	if (geocoder) {
		geocoder.getLatLng(address, function(point) {
			if (!point) {
				alert(address + " not found");
			} else {
				map.setCenter(point, zoomLevel);
				map.addOverlay(createMarker(point, address, phone,
						thumbnailImg, addressText, phoneText));
			}
		});
	}
}
