﻿// IMPORTANT!!
// Compile this code useing Google Closure Comiler prior to release
// HERE: http://closure-compiler.appspot.com/home

/* Related files
----------------
/_/images/sprites/concertina.png
/_/css/adhoc/concertina.css
/_/css/hacks/ie6.css
*/

// ==ClosureCompiler==
// @compilation_level SIMPLE_OPTIMIZATIONS
// @output_file_name concertina.js
// ==/ClosureCompiler==

if (($("dt").size() > 0) && ($("div.tabs>div, div.tabbedUI>div").size() > 0)) { pageHasTabsAndConcertinas = true; }
var parentTabId;
$("dt").each(function eachDT(i) {
	var $dt = $(this);

	// Create
	var dtText = $dt.text().toLowerCase().replace(/why|will|where|how|when|what|do|you|be|is|if|get| |[\?'’"£$\-_=\+]/gi, "").substring(0, 10);
	var concertinaID = $dt.attr("id") || dtText;
	if ($("dt[id='" + concertinaID + "']").size() > 0) {
		concertinaID = dtText + i;
	}

	$dt
	.data("previewText", "Click to read in full:" + $dt.next().text().substring(0, 300) + "...")
	.attr("id", concertinaID)
	.wrapInner("<a href=\"#" + concertinaID + "\" id=\"jumpAnchor_" + concertinaID + "\"></a>");
	//has someone got to this section by following URL? If so, need to expand it	
	var locConcertina = location.href;
	if (locConcertina.indexOf(concertinaID) != -1) {
		//yep followed URL - flag it
		locConcertina = locConcertina.split("#")[1];
		if (locConcertina == concertinaID) {
			$dt.find("a").addClass("autoExpand");
			// MAKE THE PARENT DIV ALSO VISIBLE (LATER ONLOAD).
			parentTabId = $dt.closest("div").attr("id");
		}
	}
});
$("dd").each(function () {
	$(this).hide();
});
$("dt>a").each(function () {
	$(this)
		.attr("title", $(this).parent().data("previewText"))
		.prepend("<b></b>")
		.toggle(function () {
			$(this)
			.addClass("expanded")
			.removeAttr("title");
			$(this).parent().next().addClass("visible").slideDown("medium");
			return false;
		}, function () {
			$(this)
			.removeClass("expanded")
			.attr("title", $(this).parent().data("previewText"));
			$(this).parent().next().removeClass("visible").slideUp("medium");
			return false;
		})
	});
// If there's a link inside a concertina to another concertina, then enable switching, and tab switching.
// Hopefully if there are not tabs, then the null result will not have negative impact
	$("dd a[href*='#'],a.concertinaLink[href*='#']").click(function () {
		var concertinaID = $(this).attr("href").split("#")[1];
		//alert(concertinaID)
		var $concertina = $("#" + concertinaID);
		//alert($concertina)
		var $parentTab = $concertina.closest("div");
		var parentTabId = $parentTab.attr("id");
		$(".tabbedListLI a[href='#" + parentTabId + "']").trigger("click");
		$concertina.find("a:first:not(.expanded)").click();

	});
$("dl").each(function () {
	if ($(this).find("dt").size() > 5) {
		$(this).prepend("<dt class=\"showHideAll\"><a href=\"#\"><b></b>Show all</a></dt>");
	}
});
$(".showHideAll a").each(function () {
	$(this).toggle(function () {
		$(this).parent().parent().addClass("showAll");
		$(this).parent().parent().removeClass("hideAll");
		$(this).html("<b></b>Hide all");
		$(this).parent().parent().find("dd").addClass("visible").show();
		$(this).parent().parent().find("dt a").not("dt.showHideAll a").each(function () {
			if (!($(this).hasClass("expanded"))) {
				$(this).click();
			}
		});
		$(this).parent().parent().find("dt a").addClass("expanded");
		$(this).parent().addClass("expanded");
		return false;
	}, function () {
		$(this).parent().parent().addClass("hideAll");
		$(this).parent().parent().removeClass("showAll");
		$(this).html("<b></b>Show all");
		$(this).parent().parent().find("dd").removeClass("visible").hide();
		$(this).parent().parent().find("dt a").not("dt.showHideAll a").each(function () {
			if ($(this).hasClass("expanded")) {
				$(this).click();
			}
		});
		$(this).parent().parent().find("dt a").removeClass("expanded");
		return false;
	});
});
if ($("a.autoExpand").size() > 0) {
	$("a.autoExpand").each(function () {
		if ($(this).html() != "") {
			$(this).click().focus();
			concertinaOpened = true;
		}
	});
}
// Tabs is loaded after concertina, so delay the auto switch until it's all finished.
$(window).load(function () {
	if (parentTabId != "") {
		$("a[href='#" + parentTabId + "']").click();
	}
});
