$(document).ready(function() {
	
	$("body").addClass("js");
	
	Cufon.replace("#header p, h1, h2, h3, #content-right h3, #green h4 a:not(.rss), #menu li span.inner, #green li a span.date", { hover: true });
	
	$("#gallery-slideshow").cycle({
		speed: 2000,
		slideExpr: ".images a"
	});
	
	// remove extra row from calendar if empty
	$(".calendar tr").each(function() {
		if($(this).find("td.noday").length == 7) {
			$(this).remove();
		}
	});
	
	// calendar select
	$("#calendar-select select").change(function() {
		window.location.href = $(this).val();
	});
	
	// filter
	$("#filter-field").change(function() {
		window.location.href = $(this).val();
	});
	
	// map
	$("#map").css("background", "url(/media/web/img/map.png) top left no-repeat").find("img").attr("src", "/media/web/img/map-blank.gif");
	if($.browser.msie && $.browser.version.substring(0,1)) { DD_belatedPNG.fix("#map"); }
	$("#jetaa-map area").each(function() {
		$(this).attr("title", "");
		$(this).attr("alt", "");
	}).hover(function() {
		offset = parseInt($(this).attr("rel")) * 340;
		$("#map").css("background-position", "0 -" + offset + "px");
	}, function() {
		$("#map").css("background-position", "0 0");
	});
	
	// map big
	$("#map-big").css("background", "url(/media/web/img/map-big.png) top left no-repeat").find("img").attr("src", "/media/web/img/map-blank.gif");
	if($.browser.msie && $.browser.version.substring(0,1)) { DD_belatedPNG.fix("#map-big"); }
	$("#jetaa-map-big area").each(function() {
		$(this).attr("title", "");
		$(this).attr("alt", "");
	}).hover(function() {
		offset = parseInt($(this).attr("rel")) * 485;
		$("#map-big").css("background-position", "0 -" + offset + "px");
	}, function() {
		$("#map-big").css("background-position", "0 0");
	});
	
	// archive accordion
	$("#archive ul").hide();
	$("#archive h3 a").click(function() {
		$(this).toggleClass("open").parent().next("ul").slideToggle("medium");
		Cufon.refresh();
		return false;
	});
	
	// add selected file
	$("label.file input").change(function() {
		$(this).parent().parent().parent().addClass("selected-file").after("<tr><td class=\"selected-file\" colspan=\"2\">" + $(this).val() + "</td></tr>");
	});
	
	// search
	$("#search-field").focus(function() {
		if($(this).val() == "Search") {
			$(this).val("");
		}
	});
	
	// gallery
	$("ul.gallery li a.image").fancybox({
		"overlayColor": "#000000",
		"overlayOpacity": 0.6
	}); 
	$("ul.gallery").each(function() {
		max = 0;
		$(this).find("li").each(function() {
			max = Math.max(max, $(this).height());
		}).height(max);
	})
	
	// open images, uploaded as attachments, in fanbybox
	$("ul.attachments li").each(function() {
		ext = $(this).attr("class");
		if(ext == "jpg" || ext == "jpeg" || ext == "gif" || ext == "png") {
			$(this).find("a").fancybox( {
				"overlayColor": "#000000",
				"overlayOpacity": 0.6
			});
		}
	});

	
	// register form dynamic fields
	//console.log(		);
	registerForm($(".register-form #id_exjet").val()==1);
	$(".register-form tr").each(function() {
		id = $(this).attr("id");
		if(id == "row_chapter" || id == "row_prefecture" || id == "row_location_on_programme" || id == "row_start_year" || id == "row_end_year") {
			$(this).find("td.label").append("<span class=\"required\">*</span>");
		}
	});
	$(".register-form #id_exjet").change(function() {
		registerForm($(".register-form #id_exjet").val()==1);
	});
	
});

function registerForm(show) {
	$(".register-form tr").each(function() {
		id = $(this).attr("id");
		if(id == "row_chapter" || id == "row_prefecture" || id == "row_location_on_programme" || id == "row_start_year" || id == "row_end_year") {
			if(show) {
				$(this).show();
			} else {
				$(this).hide();
			}
		}
	});
}