var bounds;

$(function() {
				   
	/* Menu */
	$("#un").lavaLamp({
		fx: "backout",
		speed: 500,
		click: function(event, menuItem) {
			return false;
		}
	});
	
	/* Lightbox */
	$('.img_residence').lightBox();
	
	/* Slider */
	$('#coda-slider-1').codaSlider({autoHeight:false});
	
	
	/* Maps */
	bounds = new GLatLngBounds();
	
	if($('#bandeau_contenu').length != 0) {
		initializeGmapUnique();
	} else {
		//initializeGmapAll();
	}
			
});

function initializeGmapUnique() {

	
	if (GBrowserIsCompatible()) {
	
		map = new GMap2(document.getElementById("bandeau_contenu"));
		
		var scaleControl = new GLargeMapControl3D();
		var typeControl = new GMenuMapTypeControl();
		$("#bandeau_contenu").mouseenter(function() {
			map.addControl(scaleControl);
			map.addControl(typeControl);
		}).mouseleave(function() {
			map.removeControl(scaleControl);
			map.removeControl(typeControl);
		});
		map.disableScrollWheelZoom();
		
		latitude = $("#latitude").text();
		longitude = $("#longitude").text();
		
		var markerLatLng = new GLatLng(latitude, longitude);
		map.setCenter(markerLatLng, 13);
		
		var marker = new GMarker(markerLatLng);
		map.addOverlay(marker);
	
	}
}

function initializeGmapAll() {
	if (GBrowserIsCompatible()) {
		
		map = new GMap2(document.getElementById("all_programmes"));
		
		var scaleControl = new GLargeMapControl3D();
		var typeControl = new GMenuMapTypeControl();
		$("#all_programmes").mouseenter(function() {
			map.addControl(scaleControl);
			map.addControl(typeControl);
		}).mouseleave(function() {
			map.removeControl(scaleControl);
			map.removeControl(typeControl);
		});
		map.setCenter(new GLatLng(45.194276,5.731634), 9);
		map.disableScrollWheelZoom();
		
		// Create a base icon for all of our markers that specifies the
	    // shadow, icon dimensions, etc.
	    var baseIcon = new GIcon(G_DEFAULT_ICON);
	    baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	    baseIcon.iconSize = new GSize(20, 34);
	    baseIcon.shadowSize = new GSize(37, 34);
	    baseIcon.iconAnchor = new GPoint(9, 34);
	    baseIcon.infoWindowAnchor = new GPoint(9, 2);
		//fin
		
		$('.prog').each(function() {
			var latitude = $(this).find(".latitude").text();
			var longitude = $(this).find(".longitude").text();
			var title = $(this).find(".title").text();
			var url = $(this).find(".url").text();
			var photo = $(this).find(".photo").text();
			var type = $(this).find(".type").text();
			
			if(photo)
			{
				photo = "<img src=\""+photo+"\" width=\"140px\" /><br/>";
			}
			else
			{
				photo = ""
			}
			
			
			// Create a lettered icon for this point using our icon class
			var letter = String.fromCharCode(type.charCodeAt(0));
			var letteredIcon = new GIcon(baseIcon);
			letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
			// Set up our GMarkerOptions object
			markerOptions = { icon:letteredIcon };
			//fin
			
			var markerLatLng = new GLatLng(latitude,longitude);
			var marker = new GMarker(markerLatLng, markerOptions);
			
			GEvent.addListener(marker, "click", function() {
			
				//location.href = "http://hcresidences.lianis.fr/programme/"+id+"/"+url;
			
				marker.openInfoWindowHtml("<p><strong>"+title+"</strong><br/>"+photo+"<a href=\""+url+"\">Voir l'offre</a></p>");
			});
			
			bounds.extend(marker.getPoint());
			map.addOverlay(marker);
		});
		
		map.setCenter( bounds.getCenter(), map.getBoundsZoomLevel( bounds ));
	
	}
}
