$j(document).ready(function()
{
	/* A fancy rotator for the entries in "highlights". */
	$controls = $j(".highlights_controls");
	$container = $j(".highlights_container");
	$highlights = $container.children(".highlight");
	
	var place = 0;
	
	var length = $highlights.length;
	var width = $highlights.outerWidth() + 2;
	
	$h_controls = $controls.children(".control");
	$left = $controls.children(".control.left");
	$right = $controls.children(".control.right");
	if (length > 3) {
		activate_control($left, "off");
		activate_control($right, "on");
	} else {
		activate_control($left, "off");
		activate_control($right, "off");
	}
	$h_controls.click(function(event)
	{
		var anim_dur = 500;
		
		$this = $j(this);
		if ($this.hasClass("left")) {
			var add = -1;
		} else
		if ($this.hasClass("right")) {
			var add = 1;
		}
		var success = update_place(add);
		if (success) {
			$container.animate({marginLeft: -(place * width)}, {duration: anim_dur, queue: false});
		}
		return false;
	});
	
	function update_place(add)
	{
		/* Three checks per side: whether the user is able to scroll to either
		   side (if not, ignore request) and whether there's at least
		   one more space to scroll to (toggles transparent/opaque)
		   on both sites. */
		
		if (place <= 1 && add < 0) activate_control($left, "off");
		else activate_control($left, "on");
		if (place >= length - 4 && add > 0) activate_control($right, "off");
		else activate_control($right, "on");
		if (place <= 0 && add < 0) return false;
		if (place >= length - 3 && add > 0) return false;
		place += add;
		return true;
	}
	
	function activate_control(ctrl, on_or_off)
	{
		/* Makes the control button transparent (no more items) or opaque. */
		
		var anim_dur = 500;
		
		if (ctrl == null) return false;
		if (on_or_off == null) return false;
		$ctrl = $j(ctrl);
		if ($ctrl.hasClass(on_or_off)) return false;
		$ctrl.removeClass("on");
		$ctrl.removeClass("off");
		switch (on_or_off) {
			case "on":
				var opacity = 1;
				break;
			case "off":
				var opacity = 0.4;
				break;
		}
		$ctrl.addClass(on_or_off);
		$ctrl.fadeTo(anim_dur, opacity);
	}
	
	$error = $j("#loginerror");
	err_h = $error.outerHeight();
	$error.css({marginTop: -err_h+"px"});
	$error.animate({marginTop: 0}, {duration: 1000});
	$error.click(function(ev)
	{
		$this = $j(this);
		$this.animate({marginTop: -err_h+"px"}, {duration: 1000})
		    .fadeOut(1);
		return false;
	});
	if (jQuery.browser.mozilla) {
		$error.css("-moz-border-radius-bottomright", "2px");
	}
});