// JavaScript Document

//var shoppingLgString = '<tmpl_var name=lg>';
//var urlScriptGooglemapProductLocalisation = '<tmpl_var name=URL_SCRIPT_googlemap_localisation_layer>';
//var currentCategWfcode = '<tmpl_var name=wfec_category_wfcode>';
//var currentGooglemapDivId = 'googlemap_search_localisation_products';
//var currentGooglemapOptions = null;

var update_combo_departements_on_region_change = function(googlemapSearchProducts, categWfcode){
	categWfcode = (undefined) ? null : categWfcode;
	
	var url_update_localisation_departement = '/pl/update_localisation_departement.pl?lg='+shoppingLgString;
	
	/* réinit des contenus */
	$('contener_dept').innerHTML = '';
	
	if ($('form_search_product_by_localisation_region').value){
		url_update_localisation_departement += '&r='+$('form_search_product_by_localisation_region').value;
	}
	if (categWfcode){
		url_update_localisation_departement += '&c='+categWfcode;
	}
	
	/* Chargement de la requete */
	ChargeContenu(url_update_localisation_departement,'idDest:contener_dept,loading:true');
	updateGooglemapMarkersByLocalisation(googlemapSearchProducts, categWfcode);
}

var updateGooglemapMarkersByLocalisation = function (googlemapObject, categWfcode, regionWfcode, departementWfcode, fullcontent){
	categWfcode = (undefined) ? null : categWfcode;
	regionWfcode = (undefined) ? null : regionWfcode;
	departementWfcode = (undefined) ? null : departementWfcode;
	
	googlemapObject.deleteMarkers();
	
	var url_get_googlemap_product_markers = '/pl/product_localisation_googlemap_json.pl';
	var params = {'lg': shoppingLgString};
	
	if (categWfcode){
		params.c = categWfcode;
	}
	
	if (regionWfcode){
		params.r = regionWfcode;
	} else {
		if ($('form_search_product_by_localisation_region')){
			if ($('form_search_product_by_localisation_region').value){
				params.r = $('form_search_product_by_localisation_region').value;
			}
		}
	}
	
	if (regionWfcode){
		params.d = departementWfcode;
	} else {
		if ($('form_search_product_by_localisation_departement')){
			if ($('form_search_product_by_localisation_departement').value){
				params.d = $('form_search_product_by_localisation_departement').value;
			}
		}
	}
	
	var jsonRequest = new Request.JSON({
		url: url_get_googlemap_product_markers, 
		onSuccess: function(responseJSON, responseText){
			var markersArray = new Array();
			
			for(i=0;i<responseJSON.length;i++){
				currentMarker = new GooglemapMarker(responseJSON[i]['wfec_product_infos_latitude'], responseJSON[i]['wfec_product_infos_longitude']);
				currentMarker.setId(responseJSON[i]['wfec_product_wfcode']);
				currentMarker.setTitle(responseJSON[i]['wfec_product_infos_lg_name-js']);
				currentMarker.setImage(markerImage);
				currentMarker.setShadow(markerShadow);
				currentMarker.setShape(markerShape);
				
				if (fullcontent){
					var markerContent = '';
					markerContent += '<div style="width:310px;padding-right:7px;">';
					if (responseJSON[i]['photo']){
						markerContent += '<img src="/images/upload/generated/products/002/mini/'+responseJSON[i]['photo']+'" style="float:left;padding-right:10px;"/>';
					}
					markerContent += '<div style="float:left;width:270px;">'
					markerContent += '<span style="font-family:tahoma;font-size:14px;">'+responseJSON[i]['wfec_product_infos_lg_name-js']+'</span>';
					markerContent += '<br><span style="font-family:tahoma;font-size:12px;color:#333;">';
					markerContent += 'Famille : '+responseJSON[i]['wfec_category_name_fr-js'];
					markerContent += '<br>Collection : '+responseJSON[i]['wfec_collection_name_fr-js'];
					if (responseJSON[i]['wfec_product_infos_auteur']){
						markerContent += '<br>Auteur : '+responseJSON[i]['wfec_product_infos_auteur-js'];
					}
					markerContent += '<br><div onclick="parent.location.href=\'/pl/shopping/fiche_produit.pl?itm='+responseJSON[i]['wfec_product_wfcode']+'\'" style="text-decoration:none;color:#666;cursor:pointer;">&rsaquo; Voir le produit</div>';
					markerContent += '</span>';
					markerContent += '</div>';
					markerContent += '</div>';
					currentMarker.setContent(markerContent);	
				}
				
				markersArray.push(currentMarker);
			}
			
			googlemapObject.setMarkers(markersArray);
			googlemapObject.addMarkers(true);
		}
	}).get(params);
}
				
var openGoogleMapSearchLocalisationLayer = function (categWfcode){
	categWfcode = (undefined) ? null : categWfcode;
	
	if (pageLoaded){
		var url_to_open = urlScriptGooglemapProductLocalisation;
		if ($('form_search_product_by_localisation_region').value){
			url_to_open += '&r='+$('form_search_product_by_localisation_region').value;
		}
		if ($('form_search_product_by_localisation_departement').value){
			url_to_open += '&d='+$('form_search_product_by_localisation_departement').value;
		}
		if (categWfcode){
			url_to_open += '&c='+categWfcode;
		}
		
		Shadowbox.open({
			content:    url_to_open,
			player:     "iframe",
			title:      "Recherche d'un guide sur une région ou un département",
			width:      900,
			height:     550
		});
	}
}


var openGoogleMapProductMarkers = function (){
	if (pageLoaded){
		var url_to_open = urlScriptGooglemapProductMarkers;
		if (current_product_wfcode){
			url_to_open += '&itm='+current_product_wfcode;
		}
		Shadowbox.open({
			content:    url_to_open,
			player:     "iframe",
			title:      "Points remarquables",
			width:      900,
			height:     550
		});
	}
}

var updateGooglemapMarkersProductFiche = function (googlemapObject, productWfcode, fullcontent){
	productWfcode = (undefined) ? null : productWfcode;
	
	if (! productWfcode){
		return null;
	}
	
	googlemapObject.deleteMarkers();
	
	var url_get_googlemap_product_markers = '/pl/product_markers_googlemap_json.pl';
	var params = {'lg': shoppingLgString};
	
	if (productWfcode){
		params.itm = productWfcode;
	}
		
	var jsonRequest = new Request.JSON({
		url: url_get_googlemap_product_markers, 
		onSuccess: function(responseJSON, responseText){
			var markersArray = new Array();
			
			for(i=0;i<responseJSON.length;i++){				
				currentMarker = new GooglemapMarker(responseJSON[i]['wfec_product_googlemap_marker_latitude'], responseJSON[i]['wfec_product_googlemap_marker_longitude']);
				currentMarker.setId(responseJSON[i]['wfec_product_googlemap_marker_wfcode']);
				currentMarker.setTitle(responseJSON[i]['wfec_product_googlemap_marker_title_fr-js']);
				currentMarker.setImage(markerImage);
				currentMarker.setShadow(markerShadow);
				currentMarker.setShape(markerShape);
				
				if (fullcontent){
					var markerContent = '';
					markerContent += '<div style="width:310px;padding-right:7px;">';
					if (responseJSON[i]['wfec_product_googlemap_marker_photo']){
						markerContent += '<img src="'+responseJSON[i]['wfec_product_googlemap_marker_photo']+'" style="float:left;padding-right:10px;"/>';
					}
					markerContent += '<div style="float:left;width:270px;">'
					markerContent += '<span style="font-family:tahoma;font-size:14px;">'+responseJSON[i]['wfec_product_googlemap_marker_title_fr-js']+'</span>';
					markerContent += '<br><span style="font-family:tahoma;font-size:12px;color:#333;">';
					markerContent += responseJSON[i]['wfec_product_googlemap_marker_text_fr-js'];
					markerContent += '</span>';
					markerContent += '</div>';
					markerContent += '</div>';
					currentMarker.setContent(markerContent);	
				}
				
				markersArray.push(currentMarker);
			}
			
			googlemapObject.setMarkers(markersArray);
			googlemapObject.addMarkers(true);
		}
	}).get(params);
}
	
