
var carousel_buttons_start = function(el) {
	var imgs = $(el).find("li");
	if (carousel_measure(imgs) > 300) {
		show_carousel_next(el);
	}
}

var carousel_measure = function(imgs) {
	var h = 0;
	$(imgs).each(function() {
		h += $(this).outerHeight();
	});
	return h/2;
}

var carousel_change = function(imgs) {
	var c = $(imgs).first().parents(".carousel"),
	u = c.children("ul").first(),
	offset = $(u).position().top,
	h = carousel_measure($(c).find("li"));

	if (offset < 0) {
		show_carousel_prev(c);
	}
	else {
		hide_carousel_prev(c);
	}
	if (h + offset < 300) {
		hide_carousel_next(c);
	}
	else {
		show_carousel_next(c);
	}
}

var show_carousel_prev = function(el) {
	$(el).next().children(".carousel-button.prev").removeAttr("disabled");
}

var hide_carousel_prev = function(el) {
	$(el).next().children(".carousel-button.prev").attr("disabled","disabled");
}

var show_carousel_next = function(el) {
	$(el).next().children(".carousel-button.next").removeAttr("disabled");
}

var hide_carousel_next = function(el) {
	$(el).next().children(".carousel-button.next").attr("disabled","disabled");
}

var change_splash_image = function(url) {
	var img = new Image(),
		navImg = $("#nav-image-" + window.location.hash.substr(1)),
		url = url || $(navImg).attr("rel");

	$("#splash-image").addClass("loading");
	$("#splash-image").children("img").fadeOut();
	$(img).load(function() {
		$(this).hide();
		$("#splash-image").removeClass("loading").children("img:first").remove();
		$("#splash-image").append(this);
		$(this).fadeIn();
	});
	$(img).attr("src", url);
	$("#splash-image").attr("href", $(navImg).attr("fancybox"));
	$("#image-caption").html(decodeURIComponent($(navImg).attr("data-caption")));
}

$(document).ready(function() {

	if ($(".carousel").length > 0) {
		$(".carousel").jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev",
			circular: false,
			vertical: true,
			afterEnd: carousel_change
		});

		var c = $(".carousel");
		c.each(function() { carousel_buttons_start(this); });
	}

	if ($("#splash-image").length > 0) {
		
		var defaultImgUrl = $("#splash-image").children("img:first").attr("src");

		if (window.location.hash != '') {
			change_splash_image();
		}

		$(window).bind("hashchange", function() {
			var url = (window.location.hash == '') ? defaultImgUrl : null;
			change_splash_image(url);
		});

		$("a.fancybox").fancybox({
			padding: 0,
			margin: 0,
			overlayOpacity: 0.7,
			overlayShow: true,
			overlayColor: "#000",
			"speedIn": 150,
			"speedOut": 150,
			"changeSpeed": 150
		});
	}

	if ($("#jquery_player").length > 0) {

		var player = $("#jquery_player"),
			playListLinks = $("#playlist-files").find("a");

		playListLinks.bind("click", function(e) {
			e.preventDefault();
			playListLinks.removeClass("selected");
			$(this).addClass("selected");
			player.jPlayer("setFile", $(this).attr("href"));
			return false;
		});

		$("#jquery_player").jPlayer({
			swfPath: "/javascript",
			ready: function() {
				playListLinks.first().trigger("click");
			}
		});
	}

	if ($("#video-embed").length > 0) {
		var change_video = function() {
			$.get('/videos/' + window.location.hash.substr(1) + '/embed', function(html) {
				$("#video-embed").html(html);
				$("#video-caption").html($("#nav-image-" + window.location.hash.substr(1)).attr("title"));
			});
		}

		if (window.location.hash != '') {
			change_video();
		}

		$(window).bind("hashchange", function() {
			change_video();
		});
	}

});