$(
	function()
	{
		$('a.delete-link').bind(
			'click',
			function(e)
			{
				return confirm('Are you sure you want to delete this?');
			}
		);
		
		handleVideoUrls();
		handleExternalUrls();
		// add check/ uncheck all links on fieldsets with a class of checkbox-group
		$('fieldset.checkbox-group').each(
			function()
			{
				var $this = $(this);
				var $checkboxes = $this.find(':checkbox');
				$this.prepend(
					$('<ul/>').append(
						$('<li/>').append(
							$('<a href="#">Check all</a>').bind(
								'click',
								function(e)
								{
									$checkboxes.attr('checked', 'checked');
									return false;
								}
							)
						),
						$('<li/>').append(
							$('<a href="#">Uncheck all</a>').bind(
								'click',
								function(e)
								{
									$checkboxes.attr('checked', null);
									return false;	
								}
							)
						)
					)
				);
			}
		);
		// Only allow projects or organisations to be enabled at any one time...
		$('#select-entity-box :radio, .select-entity-box :radio').bind('change',
			function(e)
			{
				if (this.checked) {
					var selectedValue = this.value;
					$('#select-entity-box select, .select-entity-box select').each(
						function()
						{
							this.disabled = this.id != selectedValue + 's-input';
						}
					)
				}
			}
		).trigger('change');
	}
);

		// translate links with a class of video and a well formed youtube url into an inline video
	function handleVideoUrls(){
		$('a.video').each(
			function(i){
				var link = this.href.match(/youtube\.com\/watch\?v=(.*)/);
				
				if (link && link.length > 1) {
					var l = link[1];
					var hashLocation = l.indexOf('#');
					if (hashLocation > -1) {
						l = l.substring(0, hashLocation);
					}
					$(this).after($('<object width="228" height="188"><param name="movie" value="http://www.youtube.com/v/' + l + '&hl=en&fs=1&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + l + '&hl=en&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="228" height="188"></embed></object>'));
					$(this).remove();
				}
			}
		);
	}
	
	function handleExternalUrls(){
		$('a').each(
			function(i){
				var link = this.href.match(/http:\/\/(.+)/);		
				if (link && link.length > 1) {
					if( this.host != document.domain ) {
						$(this).click(function(){
					        window.open(this.href);
					        return false;
					    });
					}
				}
			}
		);
	}