/**
 * This method updates the values for the ELA Teaser CRM 2.0.
 * 
 * The component ELA Teaser 2.0 (elateaser2) will render multiple containers
 * which include the data for the default news area (if CRM 2.0 is not available
 * or the user is not logged in) and the data for the different configuration
 * states of the user (logged in / soft-logged in, hasFavorite / hasOwnedVehicle).
 * These containers and their content have special naming conventions.
 * 
 * The following method will determine the users loggin state and his configurations,
 * replaces links accordingly to the user and dynamically set the correct container
 * to visible state. It will (if necessary) hide the news area content.
 */
function updateElaTeaser2() {
	// Determine user state
	var pm = embGetProfileManager();
	var isLoggedIn = pm.isLoggedIn();
	var isSoftLoggedIn = pm.isSoftLoggedIn();
	var hasFavorite = pm.getFavoriteBmCode() != null && pm.getFavoriteBmCode().length > 0;
	var hasOwnedVehicles = pm.getOwnedVehiclesCount() > 0;

	// Update links for favorite
	if (hasFavorite) {
		var favoriteName = pm.getFavoriteName();
		if (favoriteName != undefined && favoriteName != "") {
			$("*[name=ms-tdl-ela2-favorite-name]").each(function() {
				$(this).html(favoriteName);
			});
		}
		var favoriteHref = "embGetRedirectManager().redirectToCmsPage(EmbRedirectManager.MYFAVORITE);";
		$("*[name=ms-tdl-ela2-favorite-link]").each(function() {
			$(this).attr("onclick", favoriteHref);
		});
	}
	
	// Update links for owned vehicle
	if (hasOwnedVehicles) {
		if (isLoggedIn) {
			var ownedVehName = pm.getOwnedMainVehicleName();
			if (ownedVehName != undefined && ownedVehName != "") {
				$("*[name=ms-tdl-ela2-ownedvehicle-name]").each(function() {
					$(this).html(ownedVehName);
				});
			}
		}
		
		var ownedVehHref = "embGetRedirectManager().redirectToCmsPage(EmbRedirectManager.OWNED_VEHICLES);";
		$("*[name=ms-tdl-ela2-ownedvehicle-link]").each(function() {
			$(this).attr("onclick", ownedVehHref);
		});
	}
	
	// Update service page links
	var servicePageHref = "embGetRedirectManager().redirectToCmsPage(EmbRedirectManager.VEHICLE_SERVICE);";
	$("*[name=ms-tdl-ela2-servicepage-link]").each(function() {
		$(this).attr("onclick", servicePageHref);
	});
	
	// Update vehicle page links
	var vehiclePageHref = "embGetRedirectManager().redirectToCmsPage(EmbRedirectManager.OWNED_VEHICLES);";
	$("*[name=ms-tdl-ela2-vehiclepage-link]").each(function() {
		$(this).attr("onclick", vehiclePageHref);
	});
	
	// Show the matching teaser
	if (hasFavorite && hasOwnedVehicles) {
		$("#ms-tdl-ela2-all").show();
		$("#crmteaser-wrapper").hide();	
	} else if (hasFavorite) {
		$("#ms-tdl-ela2-favorite").show();
		$("#crmteaser-wrapper").hide();
	} else if (hasOwnedVehicles) {
		$("#ms-tdl-ela2-owner").show();
		$("#crmteaser-wrapper").hide();
	}
}
