function initBAGalleryJQuery()
{
	var $ = jQuery;
	var form = $('#contentForm');
	var obj = $('#filtersecondaryprocedures');
	var limitobj = $('#limit');

	if (form) {
		form.bind('submit', function(e){
			$('input[name=limitstart]:first').val(0); // Reset the limit start to zero
			// Redirect the form to the sub procedure if chosen
			this.action = filtersecondaryprocedureslinks[$('#filtersecondaryprocedures').val()];
			return true;
		});

		var firefunc = function(e){
			form.trigger('submit', e);
		};

		// Make the pager limit submit use fireEvent so we can trap it above
		limitobj.removeAttr('onchange');
		limitobj.bind('change', firefunc);

		// Set the filter select to do the same
		obj.bind('change', firefunc);
	}

	// The filter toggle
	if ($('#filterwrapper')) {
		$('#filterwrapperinner').css('height','auto');

		var widths = { // Don't recalculate the widths due to display:none losing them
			outer: $('#filterwrapperouter').width(),
			inner: $('#filterwrapperinner').width()
		};
		$('#filterwrapperinner').css('display', 'none'); // hide before setting visibility
		$('#filterwrapperinner').css('visibility', 'visible'); // starts as visibility:invisible

		$('#filtertogglewrapper a').bind('click', function(e){
			$('#filterwrapperinner').width(widths.inner); // avoid bounce from no width

			var coords = $('#filterwrapperouter').position();
			var outerheight = $('#filterwrapperouter').outerHeight();

			$("#filterwrapper").css({
				left: (coords.left + widths.outer - widths.inner) + "px",
				top: (coords.top + outerheight - 1) + "px"
			});
			$('#filterwrapperinner').slideToggle();
			e.stopPropagation();
			return false;
		});

		// Don't tap into the body
		$(document.body).bind('click', function(e) {
			var target = $(e.target);
			// If the toggle wrapper is visible (height > 0) on the element clicked on is not a child of it
			if($('#filterwrapperinner').css('display') == 'block' && target.parents().index($('#filterwrapperinner')) == -1) {
				$('#filterwrapperinner').slideToggle();
				e.stopPropagation();
			}
		}); 
	}
};

function initBAGalleryMootools()
{
	var form = $('contentForm');
	var obj = $('filtersecondaryprocedures');
	var limitobj = $('limit');

	if (form) {
		form.addEvent('submit', function(e){
			$E('input[name=limitstart]').value = 0;
			form.action = filtersecondaryprocedureslinks[$('filtersecondaryprocedures').value];
			form.submit();
			return true;
		});

		var firefunc = function(e){
			form.fireEvent('submit', e);
		};
		limitobj.removeProperty('onchange');
		limitobj.addEvent('change', firefunc);
		obj.addEvent('change', firefunc);
	}
	if ($('filterwrapper')) {
		$('filterwrapperinner').setStyle('height','auto');

		var widths = {
			outer: $('filterwrapperouter').getCoordinates().width,
			inner: $('filterwrapperinner').getCoordinates().width
		};
		
		var mySlide = new Fx.Slide('filterwrapperinner').hide();

		$('filterwrapperinner').setStyle('visibility', 'visible');

		$E('a', 'filtertogglewrapper').addEvent('click', function(e){
			var coords = $('filterwrapperouter').getCoordinates();
			$('filterwrapper').setStyle('top', coords.bottom - 1);
			$('filterwrapper').setStyle('left', coords.left + widths.outer - widths.inner);
			mySlide.toggle();
			return false;
		});

		$(document.body).addEvent('click',function(e) {
			// If the toggle wrapper is visible (height > 0) on the element clicked on
			// is not a child of it
			if(this.wrapper.offsetHeight > 0 && !this.wrapper.hasChild(e.target)) {
				$E('a', 'filtertogglewrapper').fireEvent('click', e);
			}
		}.bindWithEvent(mySlide)); 
	}
};
