﻿/// <reference path="../../../../../References/jquery-vsdoc.js" />

(function($)
{
	// get the data path from src attibute of this file's scipt element ( ie <script src="thisfile.js?[dataPath] ) 
	// needs to be executed straight away for it to work
	var dataPath = function()
	{
		try
		{
			// get data path from querystring in this script's src (everything after "?data=")
			var path = getLastChild(document.lastChild).getAttribute('src').replace(/.*\.js\?data=/, '');
			// unescape path (Url Decode it)
			path = unescape(path);
			// covert any &amp; back to &
			path = path.replace(/&amp;/g, '&');
			// if path is less than 4 characters long then assume it isn't correct
			if (path.length < 4) throw { message: "Couldn't find data path." };
			return path;
		}
		catch (e)
		{
			// if there are any errors then fall back to the hard coded path below
			return "/_assets/data/json_IALF-quicksearch.js";
		}
		function getLastChild(el) { return (el.lastChild && el.lastChild.nodeName != '#text') ? getLastChild(el.lastChild) : el; }
	} ();

	// show module (hidden by default in case JS is disabled)
	var module = $("div.module-IALF").eq(0);
	module.show();


	// the rest of this script is to be triggered when dom is ready...

	// when dom ready...
	$(document).ready
	(
		function()
		{
			// check to see if target div module exists - if not then DIE
			if (module.length == 0) return false;

			// load in data
			$.getJSON(dataPath, function(data)
			{
				// drop last element of item array & sort
				data.items.length -= 1; data.items.sort(customSort);

				// loop through and do the same for the child items
				$.each(data.items, function(i, item) { item.items.length -= 1; item.items.sort(customSort); });

				// data all good to go - pass it on to main method...
				RenderModule(module, data);

			});

			/* ## PRIVATES ## */
			function customSort(a, b)
			{
				a = normalise(a.hdl); b = normalise(b.hdl);
				if (a < b) return -1;
				if (a > b) return 1;
				return 0;
				function normalise(str)
				{
					return str.toLowerCase()
						.replace(/á|â|ä|æ/g, 'a')
						.replace(/ç/g, 'c')
						.replace(/é|è|ê|ë/g, 'e')
						.replace(/î|ï/g, 'i')
						.replace(/ô|œ/g, 'o')
						.replace(/ù|û|ü/g, 'u')
						;
				}
			}
		}
	);

	function RenderModule(module, data)
	{
		// hide preloader and display content
		$(".loader", module).hide();
		$(".action", module).css("visibility", "visible");

		// build accordion
		BuildAccordionMarkup();

		// initialize toggle variable
		var isHidden = true;

		// add event to button
		$(".btn", module).disableTextSelect();
		$(".btn", module).click(function(e) { ToggleAccordion(); e.stopPropagation(); });

		// add event to document to hide accordian
		$(document).click(HideAccordion);

		// add function to child links in Accordion
		$("#iIALFAccordian ul a").click(function()
		{
			$(".loader", module).show();
			$(".action", module).css("visibility", "hidden");
		});


		/**** FUNCTIONS *****/

		function ToggleAccordion()
		{
			(isHidden == true) ? ShowAccordion() : HideAccordion();
		}
		function ShowAccordion()
		{
			isHidden = false;
			$("#iIALFAccordian").show();
			$("p.btn", module).addClass("down");
		}
		function HideAccordion()
		{
			isHidden = true;
			$("#iIALFAccordian").hide();
			$("p.btn", module).removeClass("down");
		}

		function BuildAccordionMarkup()
		{
			// main containing UL
			module.append('<ul id="iIALFAccordian"/>');

			// loop through and add children...
			$.each(data.items, function(i, item)
			{
				$("#iIALFAccordian").append('<li class="header"><a href="#">' + item.hdl + '</a></li>');

				// check to see if item has children
				if ($(item.items).length > 1)
				{
					$("#iIALFAccordian > li:last").append('<ul/>');
					$.each(item.items, function(p, child)
					{
						$("#iIALFAccordian ul:last").append('<li><a href="' + data.link.replace("{id}", child.id) + '">' + child.hdl + '</a></li>');
					});
				}

			});

			// position
			var left = $("p.btn", module).offset().left - module.offset().left;
			var top = $("p.btn", module).offset().top - module.offset().top + $("p.btn", module).outerHeight() - 1;
			$("#iIALFAccordian").css({ "top": top, "left": left });

			// initialise accordion
			$("#iIALFAccordian").accordion({ alwaysOpen: false, autoHeight: false, active: false, header: "li.header > a" });

			// hide accordion
			$("#iIALFAccordian").hide();

		}

	} // end - RenderModule()

})(jQuery);

(function($) { if ($.browser.mozilla) { $.fn.disableTextSelect = function() { return this.each(function() { $(this).css({ "MozUserSelect": "none" }) }) }; $.fn.enableTextSelect = function() { return this.each(function() { $(this).css({ "MozUserSelect": "" }) }) } } else { if ($.browser.msie) { $.fn.disableTextSelect = function() { return this.each(function() { $(this).bind("selectstart.disableTextSelect", function() { return false }) }) }; $.fn.enableTextSelect = function() { return this.each(function() { $(this).unbind("selectstart.disableTextSelect") }) } } else { $.fn.disableTextSelect = function() { return this.each(function() { $(this).bind("mousedown.disableTextSelect", function() { return false }) }) }; $.fn.enableTextSelect = function() { return this.each(function() { $(this).unbind("mousedown.disableTextSelect") }) } } } })(jQuery)