// Copyright (c) 2009 Helder de Azevedo (http://www.altran.ch)

// methode JSON
var shop;
var map;
var staticMapUrl;
var geocoder;
var marker;

function isNotEmpty(value) {
	return value&&value!='undefined';
}

function addField(content, value1, value2) {
	if (isNotEmpty(value1) && isNotEmpty(value2)) {
		return content + value1+'&nbsp;'+value2+'<br/>';
	} else if (isNotEmpty(value1)) {
		return content + value1+'<br/>';
	} else if (isNotEmpty(value2)) {
		return content + value2+'<br/>';
	}
	
	return content;
}	

function addEmailField(content, value) {
	if (isNotEmpty(value)) {
		return content+'<a href="mailto:'+value+'">'+value+'</a><br/>';
	} 
	return content;
}	

function addUrlField(content, value) {
	if (isNotEmpty(value)) {
		var hasProtocol = value.indexOf('http://');
		if (hasProtocol == -1) {
			var href = 'http://' + value;
		} else {
			var href = value;
		}
		return content+'<a href="' + href + '" target="_blank">' + value + '</a><br/>';
	} 
	return content;
}	

function updateValuesStaticMap(map) {
   var baseUrl = "http://maps.google.com/maps/api/staticmap?";
   var key = "ABQIAAAA8MmJcnZjGUkEYgmG0i5unhS6Mty2rHzbzA0VE--QdqCFwOzVMxT5L1rRfBDl_8i1RePhyZptZbPwtw";
   // http://maps.google.com/maps/api/staticmap?zoom=8&size=500x500&maptype=roadmap&markers=size:mid|color:red|34.42059,-119.644103&mobile=true&sensor=false&key=
  
   var params = [];
   params.push("center=" + marker.getPosition().lat() + "," + marker.getPosition().lng());
   params.push("zoom=" + map.getZoom());
   params.push("markers=color:red|"+ marker.getPosition().lat() + "," + marker.getPosition().lng());
   params.push("size=500x500");
   params.push("sensor=true");
   
   if (map.getMapTypeId() == google.maps.MapTypeId.SATELLITE) {
     params.push("maptype=satellite");
   }
   if (map.getMapTypeId() == google.maps.MapTypeId.HYBRID) {
     params.push("maptype=hybrid");
   }
   if (map.getMapTypeId() == google.maps.MapTypeId.TERRAIN) {
     params.push("maptype=terrain");
   }
   if (map.getMapTypeId() == google.maps.MapTypeId.ROADMAP) {
	     params.push("maptype=roadmap");
   }
   params.push("key=" + key);

   staticMapURL = baseUrl + params.join("@");
}

function initializeMap() {
	geocoder = new google.maps.Geocoder();
	// position lat & lng	
	var latlng;
	
	if (shop.lng != "" && shop.lat != "") {
		latlng = new google.maps.LatLng(shop.lat, shop.lng);
		renderMap(latlng);
	} else {
		var address = shop.address1 + "," + shop.city;
		
	    if (geocoder) {
	      geocoder.geocode( { 'address': address}, function(results, status) {
	        if (status == google.maps.GeocoderStatus.OK) {
	        	latlng = results[0].geometry.location;
	        	renderMap(latlng)
	        } else {
	        	document.getElementById("contentMap").style.display='none';
	        	document.getElementById("buttonszone").style.display='none';
	        	alert("Geocode was not successful for the following reason: " + status);
	        }
	      });
	    }
	}
}

function renderMap(latlng) {	
    var myOptions = {
		zoom: 13,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};	
	
	map = new google.maps.Map(document.getElementById("map"), myOptions);
	
	// anchor
	marker = new google.maps.Marker({
  		position: latlng, 
  	    map: map, 
  	    title: "Shop Jaeger-LeCoultre"
  	});

	// content of infowindow
	var html = '<div style="font-size:12px; color:#000000;">';
	html = "<strong>" + addField(html, shop.name) + "</strong>";
	html = addField(html, shop.address1);
	html = addField(html, shop.postcode, shop.city);
	html += '<br /><strong>' + markerGetDirection + '</strong><br />' + markerStardAddress + '<br />' +
	'<form action="http://maps.google.com/maps" method="get" target="_blank">' +
	'<input type="text" size="40" maxlength="40" name="saddr" value="" />' +
    '<input id="formInputGetDirection" value="GO" type="submit" />' +
    '<input id="formButtonGetDirection" type="hidden" name="daddr" value="' + latlng.lat() + ',' + latlng.lng() + '" /></form>';	    	
	html += "</div>";
	
	var infowindow = new google.maps.InfoWindow({ 
		content: html,
        //size: new google.maps.Size(50,50),
        position: latlng
    });
	infowindow.open(map, marker);
	document.getElementById("printMap").style.display='inline';
	document.getElementById("displayMap").style.display='inline';
	
	setEvents(map, marker);
	updateValuesStaticMap(map);
	
}

function setEvents(map, marker) {
	google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map, marker);
    });
	google.maps.event.addListener(map, 'dragend', function() {
		updateValuesStaticMap(map);
	});
	google.maps.event.addListener(map, 'zoom_changed', function() {
		updateValuesStaticMap(map);
	});
	google.maps.event.addListener(map, 'mapTypeId_changed', function() {
		updateValuesStaticMap(map);
	});	
}
