$(document).ready(function(){
	// var portfolioPopup = new PopupMenu($("counter"), $("menu"));
	
	setupZoom();
	
	/*
	for (i = 0; i < $('menu').childNodes.length; i++) {
		var thisMenu = $('menu').childNodes[i];
		if (thisMenu.className == 'off') {
			thisMenu.popupObject = new PopupMenu(thisMenu, thisMenu.childNodes[1]);
		}
	}
	*/
	
	// Set up tabs on home page
	if ($('#tab-appuyer')) {
		$('#tab-appuyer').click(function() {
			$(this).attr('class','choix');
			$('#tab-participer').attr('class', 'off');
			$('#liste-appuyer').attr('class', 'actif');
			$('#liste-participer').attr('class', 'cache');
		});
		$('#tab-participer').click(function() {
			$(this).attr('class', 'choix');
			$('#tab-appuyer').attr('class', 'off');
			$('#liste-participer').attr('class', 'actif');
			$('#liste-appuyer').attr('class', 'cache');
		});
	}
	
	// Handle activity form; code copied from admin.js out of laziness
	function updateFieldVisibility() {
		$('.hidden').hide();
		$('select, :checked').each(function() {
			targetDiv = $('.' + $(this).attr('name') + $(this).val())
			if (targetDiv.length > 0) {
				targetDiv.show();
			}
		})
	}

	$('select, :checkbox').change(function() {
		updateFieldVisibility();
	})
	
	updateFieldVisibility();
	
	// Handle participants in activity form
	$('div.activityParticipants').eq(0).append('<span id="addParticipant">Ajouter un·e participant·e</span>');
	var participantID = 1;
	$('#addParticipant').click(function() {
		participantID++;
		while ($('#activityParticipant' + participantID).length) {
			// This ID is already used
			participantID++;
		}
		var newParticipant = $('#activityParticipant').clone();
		// Remove note about contact requirements
		newParticipant.children('div.note').remove();
		// Remove "missing" class
		newParticipant.children('div.missing').removeClass();
		// Remove content of fields
		newParticipant.find('input').val('');
		newParticipant.find('textarea').val('');
		// Change field names
		newParticipant.children('div').each(function() {
			var label = $(this).children('label');
			var field = $(this).children('div').children();
			label.attr('for', label.attr('for') + participantID);
			field.attr('id', field.attr('id') + participantID);
			field.attr('name', field.attr('name') + participantID);
		});
		newParticipant.insertBefore(this).hide().slideDown().attr('id', 'activityParticipant' + participantID);
		newParticipant.children("h2").html(
			'Participant·e <span>Enlever</span>'
		).children("span").click(function() {
			// Get rid of this participant
			$(this).parent().parent().slideUp(function() {
				$(this).remove();
			});
		});
	})
 	
});
