jQuery("div.newsitem a, div.result a").livequery("click", function() {
	var href = $(this).attr("href");
	if (href.indexOf("://") && href.indexOf("://www.ntda.co.uk/") == -1)
		return confirm('You are about to transferred to an external internet site – NTDA is not responsible for the content of external internet sites – would you like to continue?');
	return true;
});
ddaccordion.init({
	headerclass: "expandable", //Shared CSS class name of headers group that are expandable
	contentclass: "categoryitems", //Shared CSS class name of contents group
	revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
	mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
	collapseprev: true, //Collapse previous content (so only one open at any time)? true/false 
	defaultexpanded: [0], //index of content(s) open by default [index1, index2, etc]. [] denotes no content
	onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
	animatedefault: false, //Should contents open by default be animated into view?
	persiststate: true, //persist state of opened contents within browser session?
	toggleclass: ["", "openheader"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
	togglehtml: ["prefix", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
	animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
	oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
		//do nothing
	},
	onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
		//do nothing
	}
})

var makeDialog = makeDialog || function() {};
var bgAction = bgAction || function() {};

if (jQuery.fn.fullCalendar) $(document).ready(function() {

	var edit_event = makeDialog({
		width: 500,
		on_form_save: function(data) {
			$('#calendar').fullCalendar("refetchEvents");
		}
	});

	var delete_event = bgAction("delete", {
		confirm: true
	});

	var open_event = function(event) {
		$("<div></div").load("/event.html", {id: event.id}, function() {
			$(this).dialog({
				autoOpen: true,
				bgiframe: true,
				modal: true,
				title: "View event",
				width: 400,
				position:'center',
				buttons: {
					Close: function() { $(this).dialog("close") }
				}
			}).each(function() {
				if (admin) {
					$this = $(this);
					$this.dialog("option", "buttons", $.extend($this.dialog("option", "buttons"), {
						Delete: function() {
							delete_event(event.id, {
								onsuccess: function() {
									$this.dialog("close");
									$('#calendar').fullCalendar("refetchEvents");
								}
							});
						}
					}));
				}
			});
		});
	};

	if (document.location.hash.length > 1)
		open_event({id: document.location.hash.substring(1)});

    $('#calendar').fullCalendar({
		dayClick: function(date) {
			if (admin) {
				edit_event({
					date: date.getFullYear() + "/" + String(Number(date.getMonth()+1)) + "/" + date.getDate()
				})
			}
		},
		eventClick: open_event,
		eventSources: [
			"/fetch-events"
		]
    });

});

if (typeof makeDialog != "undefined") {
	var edit_cms = makeDialog({
		url: "/cms",
		on_form_save: function(data) {
			if (data.success) {
				$("#cms-content").html(data.content);
			}
		}
	});

	var edit_member = makeDialog({
		url: "/edit-member",
		width: 700,
		on_form_save: function(data) {
			if (data.success) {
				$("#cms-content").html(data.content);
			}
		}
	});
	var delete_member = bgAction("delete", { confirm: true, url: "/edit-member" });

	var edit_news = makeDialog({
		on_form_save: function() {
			$("#object-search").submit();
		}
	});

	var delete_news = bgAction("delete", { confirm: true });

	var edit_publication = makeDialog({
		on_form_save: function() {
			$("#object-search").submit();
		}
	});
	var delete_publication = bgAction("delete", { confirm: true });
}

