$(document).ready(function (){
	// set JS class for CSS
	$('HTML').addClass('JS');
	
	// Add login link and set it to open and close the login panel
	$("div#login a[href='/register/']").before("<a href='#' class='login-link'>Log in</a> <span>|</span> ");
	$("a.login-link").click(function () {
		if ($("div#login-form").css("display")=="none") {
			$("div#login-form").slideDown();
			$("a.login-link").addClass("open");
		} else {
			$("div#login-form").slideUp();			
			$("a.login-link").removeClass("open");
		}
	});
	
	// If we're on home page, set up cycling of featured programmes
	$(window).load(
	    function() {
			var displayedFeature = 1;
			function nextFeature() {
				$("div.featured-programme").eq(displayedFeature-1).fadeOut(800, function() {
					$("div.featured-programme").eq(displayedFeature-1).fadeIn(800); // displayedFeature will have been incremented by the time this is called
				});
				displayedFeature++;
				if (displayedFeature > $("div.featured-programme").size()) displayedFeature=1;
				setTimeout(nextFeature, 6000);
			}
			if ($("body.home").length) {
				setTimeout(nextFeature, 5000);
			}
		}
	);
	
	// if we're on search results page, see if we need to display the "no results" text
	if ($("body.catalogue.search").length) {
		if (!$("ul.programme-list li").length) {
			$("ul.programme-list").before("<div class='no-results'><h3>No results found!</h3><p>Sorry, no matches were found. You could try:</p><ul><li>Using less specific keywords</li><li>Including all genres</li><li>Not specifying a format</li></ul><p>Or try browsing our <a href='/catalogue/'>latest releases</a></div>");
		}
	}
});