// script to generate tag filtering interface and behaviour
// Note: dependent on jQuery scripts.

// This script will only function correctly if the page has:
//  * a list with id "aspect-links" containing li elements.
//  * within #aspect-links, descendants with class "item-tags",
//    each of which contains "li" elements, each of which contains a tag name.
//    These specify the tags that apply to each child of #aspect-links.
//    An li child of .item-tags is not regarded as a tagname if it has class
//    "tag-title".
//  * the elements with class "item-tags" are 5 levels below #aspect-links
//    in the DOM hierarchy.  

//     Example markup:
//     <ul id="aspect-links">
//      <li>
//       <p>visible content here</p>
//       <ul class="item-tags">
//        <li class="tag-title">Tags:</li>
//        <li>apprentice</li>
//        <li>employer</li>
//       </ul>
//      </li>
//     </ul>

// use this to turn on or off the message displayed when the disability filter is chosen
var showDisabilityMessage = false;

function contains(a, obj) {
  var i = a.length;
  while (i--) {
    if (a[i] === obj) {
      return true;
    }
  }
  return false;
}

$().ready(function() {
	// --- create the filtering interface ---
	// check all tags on the page, build list of unique tags
	var tagArray = new Array();
	$("#aspect-links .item-tags li:not(.tag-title)").each(function (i) {
		// if the tag is not yet in the list, add it
		if (!(contains(tagArray,$(this).text()))) tagArray.push($(this).text());
	});
	// sort the list alphabetically
	tagArray.sort();
	// create #filter div
	$("#aspect-links").before("<div id=\"filter\"><span class=\"tag-title\">Filter by tag:</span> <ul id=\"tag-list\"><li id=\"all-tags\" class=\"tag-chosen\">show all</li></ul></div>");
	
	// create other li elements based on what tags are on the page
	for (x in tagArray)
	{
		$("#filter ul").append("<li>" + tagArray[x] + "</li>");
	}
	
	// create, but don't show, Disability Employment Services message
	if (showDisabilityMessage)
	{
		$("#aspect-links").before("<div id=\"DES-message\">From 1 March 2010, new employment services for people with a disability, injury or health condition will be introduced to better help people prepare for, find and keep, a job. They will be known as Disability Employment Services (DES).</div>");
		$("#DES-message").hide();
	}
	
	// bind click/keyup function to each li
	$("#filter li").bind("click keyup", function(e){		
		$("#filter li").removeClass("tag-chosen");
		
		// don't show Disability Employment Services message, by default (check below may show it)
		if (showDisabilityMessage) $("#DES-message").hide();
		$("#DES-item-top").hide();
		$("#DES-item").show();
		
		if ($(this).hasClass("tag-chosen") == false) $(this).addClass("tag-chosen");		
		$("#aspect-links > *").hide();
		if ($(this).attr("id")=="all-tags")
		{
			// click/keyup was on "show all"
			$("#aspect-links > *").show();
			$("#DES-item-top").hide();
		}
		else
		{
			// click/keyup was on a specific tag
			// tagChosen stores the selected tag
			var tagChosen = $(this).text();
			tagChosen = jQuery.trim(tagChosen);

			// find matching tags, and show accordingly
			// (note that the 4 levels of 'parent' include the generated elements
			// from the "cb" rounded-box script)
			$("#aspect-links > *").hide().find(".item-tags:contains("+tagChosen+")").parent().parent().parent().parent().show();
			
			// show message about Disability Employment Services only if "disability" tag was chosen
			if (tagChosen == "disability") 
			{
				if ( !$("#DES-item-top").length )
				{
					// if it doesn't already exist, create a copy of DES list item to place at top of list 
					// (when disability tag is chosen, it appears at the top)
					$("#DES-item").clone().prependTo("#aspect-links").attr("id","DES-item-top");
				}
				
				if (showDisabilityMessage) $("#DES-message").show();
				$("#DES-item").hide();
			}
		}
    });

});