// Javascript Code for Locator Club

ClubMapUtil = {
	map   : null,
	clubs : new Array(),
	mapId : '',
	siteName: null,
	label : {},
	isSelectedProvince : false,
	zoomLevel : 5,
	isInWorldWide : false,
	zoomLevels : {
		zoomLevel_canada : 4,
		zoomLevel_france : 5,
		zoomLevel_germany : 5,
		zoomLevel_italy : 5,
		zoomLevel_japan : 5,
		zoomLevel_northeurope : 5,
		zoomLevel_spain : 5,
		zoomLevel_uk : 5,
		zoomLevel_usa : 4		
	}
};


ClubMapUtil.createClubMap = function(){
	var divMapEl = $(ClubMapUtil.mapId);
	
	if (divMapEl){
		
		// Creazione Mappa
		ClubMapUtil.map = createMap(divMapEl.id);
				
		// Insert in Map dei club
		if (ClubMapUtil.clubs){

			for (var i = 0; i < ClubMapUtil.clubs.length; i++) {
				ClubMapUtil._addClub(ClubMapUtil.clubs[i], i );
			}
			
			if (gmarkers.length > 0){
				var center = getLatLngBounds().getCenter();
				// Recupero lo zoomLevel in funzione del sito in cui mi trovo 
				var key = 'zoomLevel_'+ClubMapUtil.siteName;			
				
				var zoomLevel = ClubMapUtil.zoomLevels[key];				
				
				if (ClubMapUtil.isSelectedProvince){
					zoomLevel = 10;
				}
				
				if (zoomLevel == null){
					// Level di default
					zoomLevel = ClubMapUtil.zoomLevel;
				}
				
				if (ClubMapUtil.isInWorldWide){
					// se sono in worldwide -> Aggiungo tutti i maker
					// e fisso il centro della mappa sul centro di default
					ClubMapUtil.map.setCenter(new GLatLng(0, 0), 1);
				} else {
					ClubMapUtil.map.setCenter(center, zoomLevel);
				}

				if (gmarkers.length == 1) {				
					GEvent.trigger(gmarkers[0], 'click') ;
				}
			}
			
		}
		
		// Per il world wide
		if (gmarkers.length == 0){
			// centro di default, ovvero il mondo intero
			ClubMapUtil.map.setCenter(new GLatLng(0, 0), 1);
		}	
	}

};

/**
 * @param club clubDTO JSON
 * @param j indice
 */
ClubMapUtil._addClub = function (club , j){
	
	var point = new GLatLng(club.latitudine, club.longitude); 
	
	var htmlClub = '<div style="font-family: Arial; font-size: 12px; font-weight: bold; color: rgb(255, 0, 0); display: inline;"><a href="' + club.detailUrl + '"> ' + club.name + ' </a></div><br/>';
	htmlClub += '<div style="font-family: Arial; font-size: 11px; color: rgb(0, 0, 0);">' + club.address + '<br/>';
	htmlClub += club.cap;
	if (club.city) {
		htmlClub += ' ' + club.city;
	}
	if (club.provinceName) {
		htmlClub += ' (' + club.provinceName+')';
	}
	if (club.countryName) {
		htmlClub += ' ' + club.countryName;
	}
	htmlClub += '<br/><br/>'; 
	
	if (club.email){
		htmlClub += '<a href="mailto:' + club.email + '"><img src="/fe-web/img/temp/ico_mail_infowindow.gif" alt="" title="" border="0"/></a>';
	}
		
	htmlClub += ' </div>';		
	
	// The info window version with the "to here" form open (The Directions Form part.)
	to_htmls[j] = '<div id=\"fumetto\" style=\overflow: hidden; line-height :15px;\" >'+ htmlClub + '<div align=\"left\" ></br>' + ClubMapUtil.label.routeLabel + ': <b>' + ClubMapUtil.label.arrivalLabel + '</b> - <a href="javascript:fromhere(' + j + ')">' + ClubMapUtil.label.departureLabel + '</a>' +
	     '<br><form action="http://maps.google.com/maps" method="get" target="_blank">' +
	     '<input type="text" size=40 maxlength=40 name="saddr" id="saddr" value="' + ClubMapUtil.label.addressLabel + '" onclick="if (this.value == \'' + ClubMapUtil.label.addressLabel + '\') this.value = \'\';"/>' +
	     '<input value="' + ClubMapUtil.label.searchButtonText + '" type="submit" class="cercaMap">' +
	     '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() +
	     '"/></div></div>';
	     
	// The info window version with the "to here" form open
	from_htmls[j] = '<div id=\"fumetto\" style=\"overflow: hidden; line-height :15px;\" >'+ htmlClub + '<div align=\"left\" ></br>' + ClubMapUtil.label.routeLabel + ': <a href="javascript:tohere(' + j + ')">' + ClubMapUtil.label.arrivalLabel + '</a> - <b>' + ClubMapUtil.label.departureLabel + '</b>' +
	     '<br><form action="http://maps.google.com/maps" method="get"" target="_blank">' +
	     '<input type="text" size=40 maxlength=40 name="daddr" id="daddr" value="' + ClubMapUtil.label.addressLabel + '" onclick="if (this.value == \'' + ClubMapUtil.label.addressLabel + '\') this.value = \'\';"/>' +
	     '<input value="' + ClubMapUtil.label.searchButtonText + '" type="submit" class="cercaMap">' +
	     '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +					           
	     '"/></div></div>';
	
	// The inactive version of the direction info
	 var html = '<div id=\"fumetto\" >' + htmlClub + '<div align=\"left\" style=\"margin-top:10px;\"></br>' 
			  + ClubMapUtil.label.routeLabel+ ': <a href="javascript:tohere(' + j + ')">'+ ClubMapUtil.label.arrivalLabel +'</a>' 
			  + ' - <a href="javascript:fromhere(' + j + ')">' + ClubMapUtil.label.departureLabel + '</a></div></div>';
	
	ClubMapUtil._addClubMaker(club, html);
};

ClubMapUtil._addClubMaker = function(club, html){
	
	
	var markerIcon = new GIcon(G_DEFAULT_ICON); 
	var iconWidth = 20;
	var iconHeight = 20;
	
	markerIcon.image = '/fe-web/img/gmap_marker.png';
	markerIcon.iconSize = new GSize(iconWidth, iconHeight); 
	markerIcon.shadowSize = new GSize(Math.floor(iconWidth*1.6), iconHeight);
	markerIcon.iconAnchor = new GPoint(iconWidth/2, iconHeight);
	markerIcon.infoWindowAnchor = new GPoint(iconWidth/2, Math.floor(iconHeight/12)+1); 

	if (ClubMapUtil.map) {
		
		if (club.latitudine && club.longitude) {					
			var point = new GLatLng(club.latitudine,club.longitude);
			var marker = createMarker(point, html, markerIcon);
			ClubMapUtil.map.addOverlay(marker);				
		}
	}
	
}


