$(function() {
	if ($("#SectionMethod") != null && $("#SectionMethod").size() > 0) {
		$("#SectionMethod").change(function() {
			updateForm($(this));
		});
		updateForm($("#SectionMethod"));
	}
	
	if ($("#thumbs") != null && $("#thumbs img").size() > 0) {
		$("#thumbs a.show").click(function() {
			var href = $(this).attr('href');
			var alt = $(this).children("img").attr('alt');
			var title = $(this).children("img").attr('title');
			if (alt == undefined) {
				alt = title;
			}
			var img = document.createElement("img");
			$(img).attr('src', href).attr('alt', alt).attr('title', title);
			$("#image .img").html($(img).get(0));
			$("#image .img").append("<h4>"+title+"</h4>");
			if (alt != title) {
				$("#image .img").append("<p>"+alt+"</p>");
			}
			var next = ($(this).next("a.show").size() > 0) ? $(this).next("a.show"): $("#thumbs a.show:first");
			$("#image a.next").attr('href', next.attr('href'));
			$("#image a.next").attr('title', next.children("img").attr('title'));
			$("#image a.next").attr('alt', next.children("img").attr('alt'));
			$("#thumbs").fadeOut("fast", function() {
				$("#image").fadeIn("slow");
			});
			return false;
		});
		
		$("#image a.back").click(function() {
			$("#image").fadeOut("slow", function() {
				$("#thumbs").fadeIn("slow");
			});
			return false;
		});
		
		$("#image a.next, #image .img").click(function() {
			var href = $("#image a.next").attr('href');
			var alt = $("#image a.next").attr('alt');
			var title = $("#image a.next").attr('title');
			if (alt == undefined) {
				alt = title;
			}
			$("#image .img").fadeOut("fast", function() {
				var img = document.createElement("img");
				$(img).attr('src', href).attr('alt', alt).attr('title', title);
				$("#image .img").html(img);
				$("#image .img").append("<h4>"+title+"</h4>");
				if (alt != title) {
					$("#image .img").append("<p>"+alt+"</p>");
				}
				$("#image .img").fadeIn("fast");
			});
			var next = ($("#thumbs a[href='"+href+"']").next("a.show").size() > 0) ? $("#thumbs a[href='"+href+"']").next("a.show"): $("#thumbs a:first");
			
			$("#image a.next").attr('href', next.attr('href'));
			$("#image a.next").attr('title', $("img", next).attr('title'));
			var newalt = ($("img", next).attr('alt') != undefined) ? $("img", next).attr('alt'): '';
			$("#image a.next").attr('alt', newalt);
			return false;
		});
		$("#image").hide();
	}
	
	if ($("#slide") != null && $("#slide img").size() > 0) {
		$("#slide img").each(function() {
			var img = $(this);
			img.get(0).onload = function() {
				var w = (img.width() == 690) ? 0: ((690 - img.width())/2);
				var h = (img.height() == 690) ? 0: ((690 - img.height())/2);
				img.css({left: w+"px", top: h+"px"});
			}
		}).not(":first").hide();
		setInterval("changeSlide()", 3000);
	}
	
	if ($("form.ajax").size() > 0) {
		$("form.ajax").submit(function() {
			$(this).ajaxSubmit();
			$(this).parent(".addform").hide();
			return false;
		});
	}
});

function updateForm(el) {
	$("#SectionContentExtra").remove();
	if (el.val() == "thumbnails" || el.val() == "slideshow" || el.val() == "media") {
		$("#SectionContent").parent("div").hide("fast");
	} else {
		$("#SectionContent").parent("div").show("fast");
	}
	if (el.val() == "thumbnails") {
		$("#SectionCategorise").parent("div").show("fast");
	} else {
		$("#SectionCategorise").parent("div").hide("fast");
	}
}

function changeSlide() {
	var vis = $("#slide img:visible");
	var next = (vis.next("img").size() != 0) ? vis.next("img") : $("#slide img:first");
	vis.fadeOut("slow");
	next.fadeIn("slow");
}