/* Sitewide functions Requires: JQuery 1.2.2 (jquery_1-2-2.js) */ /* ======================== Create sorted, unique array http://www.martienus.com/code/javascript-remove-duplicates-from-array.html ========================= */ Array.prototype.unique = function () { var r = new Array(); o:for(var i = 0, n = this.length; i < n; i++) { for(var x = 0, y = r.length; x < y; x++) { if(r[x]==this[i]) { continue o; } } r[r.length] = this[i]; } return r; } /* ======================= Add rounded corners ======================= */ // Add the rounded corners to all classes specified function setCorners() { var ne = " "; var nw = " "; var se = " "; var sw = " "; // Gather elements var targetElems = $(".rounded"); // Add extra divs loop $(targetElems).each(function(i){ var targetElement = $(targetElems[i]); if (targetElement.children("span.ne, span.sw, span.se, span.sw").length == 0) { targetElement.prepend(nw).prepend(ne).append(se).append(sw); } }); } /* ======================= Add links to certain elements ======================= */ // Add extra 'links' (mouse only) to certain elements function setLinks() { // Gather elements var targetContainers = $(".linkified-box, .linkified-heading, .linkified-image"); // Stores linked elements var linkedContainers = new Array(); // Add links loop $(targetContainers).each(function(i){ var targetContainer = $(targetContainers[i]); linkedContainers.push(new linkContainer(targetContainer)) }); function linkContainer(targetContainer) { // Get the first link (although there should only really be one) var sourceLink = $(targetContainer.find("a")[0]); if (targetContainer.is(".linkified-box")) { // adding an onclick to the container element var targetElement = targetContainer; linkify(targetElement, sourceLink); } if (targetContainer.is(".linkified-heading")) { // adding a "linkified span" to the first and highest heading (prioritising highest over first) var heading = $(targetContainer.find("h1, h2, h3, h4, h5")[0]); // Add a span with the heading content var targetElement = $("").html(heading.html()); // Clear the heading content and replace it with the span heading.empty(); heading.append(targetElement); linkify(targetElement, sourceLink); } if (targetContainer.is(".linkified-image")) { // adding an onclick to the first image var targetElement = $(targetContainer.find("img")[0]); linkify(targetElement, sourceLink); } // Linkify the element with the source link target function linkify(targetElement, sourceLink) { // Ensure there are no child links that would be rendered inaccessible (ignoring the source link) var noChildren = true; var allLinks = targetElement.find("a"); if (allLinks.length > 0) { // There are child links if (allLinks.length > 1 || allLinks[0] != sourceLink[0]) { // There is more than one child link, or the one link doesn't equal the source link noChildren = false; } } // OK to proceed if (noChildren == true) { // Add class for styling targetElement.addClass("linkified"); // Add and remove hover styles when hovering, also set window status to url targetElement.mouseover(function(){ // Apply to all linked elements targetContainer.addClass("hover"); window.status = sourceLink[0]; }); targetElement.mouseout(function(){ // Apply to all linked elements targetContainer.removeClass("hover"); window.status = window.defaultStatus; }); // Map onClick Event to sourceLink Click handler targetElement.click(function(){ self.location.href = sourceLink[0]; }); } } } } /* ======================= Layout switcher, switches classes at different sizes ======================= */ function setLayoutSwitcher(switchArray) { var switchers = new Array(); var Switcher = function(switchParams) { var jqTarget = $(switchParams.selector); var ranges = switchParams.ranges; var lastClass = ""; var checkThreshold = function(point) { var rangeToApply = null; for (var i = 0; i < ranges.length; i++) { var thBegin = 0; var thEnd = 9999999999; if (i == 0 && ranges[i].th != 0) { thBegin = ranges[i].th; } else if (i+1 < ranges.length) { thBegin = ranges[i].th; thEnd = ranges[i+1].th; } else { thBegin = ranges[i].th; } if (point >= thBegin && point < thEnd) { rangeToApply = i; } } if (rangeToApply == null) { applyClass(""); } else { if (ranges[rangeToApply].className) { applyClass(ranges[rangeToApply].className) } else { applyClass("") } } } var applyClass = function(className) { if (lastClass != className) { if (lastClass != "") { jqTarget.removeClass(lastClass); } if (className != "") { jqTarget.addClass(className); } lastClass = className; } } return({ checkThreshold:checkThreshold }); } for (var i = 0; i < switchArray.length; i++) { switchers.push(new Switcher(switchArray[i])); } $(window).bind('resize', function() { checkWidth() } ); checkWidth() function checkWidth(){ var bodyWidth = $("body").width(); for (var i = 0; i < switchers.length; i++) { //console.log(bodyWidth) switchers[i].checkThreshold(bodyWidth); } } } /* ======================= Table rows Show/Hide ======================= */ function addAtoZ() { var tableRows = $("#docsTable tbody tr"); var htmlHeading = $(".atozControls #docsTitle"); // Check for #docsTable and rows if ($(tableRows).length > 0) { if ($(htmlHeading).length == 0) { // Create div & ul above it. $("#docsTable").before('
');
$(toggleButton).addClass(icon);
// Give it an action
$(toggleButton).click(function(){
Sitetree.toggle($(this).parent("li"));
return false;
});
// Add it into the begining of the LI
$(liElements[i]).prepend(toggleButton);
// Set the alt text
Sitetree.setAlt(liElements[i]);
});
// Open first level
var firstLevel = $(root).children("."+hidden);
$(firstLevel).each(function(i){
Sitetree.toggle(firstLevel[i]);
});
}
};
Sitetree.toggle = function(liElement) {
$(liElement).toggleClass(hidden);
Sitetree.setAlt(liElement)
}
Sitetree.setAlt = function(liElement) {
// Get the last link for the alt text
var links = $(liElement).children("a");
if (links.length == 1) {
// It only matched the icon, see if it's inside a H2
var links = $(liElement).children("h2");
}
var linkName = $(links[links.length-1]).text();
linkName = $.trim(linkName);
// Expand/Collapse based on class
if ($(liElement).is("."+hidden)) {
var preText = "Expand ";
} else {
var preText = "Collapse ";
}
// Set alt text on icon image
$($($(liElement).children("."+icon)).children("img")).attr("alt", preText+linkName);
}
/* =======================
INITIALISE Scripts
======================= */
$(function(){
// Add corner spans to elements with class .rounded
setCorners();
// Linkify boxes with classes .linkified-box, .linkified-heading, .linkified-image"
setLinks();
// Add 'section' class to open navigation lists.
addNavClass();
// Add 'cl' class to every other (odd) li in the central content block.
addClearClass()
// Add filter functionality to the publications tables.
addAtoZ()
// Empty the search box on focus
emptySearch()
// Open PDFs and .external links in a new window.
newWindows()
// Hide duplicate table headings for docs.
hideTableHeadings()
// Sitetree: opening closing nested lists.
Sitetree();
// Set style switcher
setLayoutSwitcher([{
selector:".mainHeading", ranges:new Array(
// The default will be no class at 0-500
{th:900, className:"mainHeadingSwitch"}
)},{
selector:".twoColumnLayout", ranges:new Array(
{th:0}, // Default, although not required (0-300)
{th:900, className:"twoColumnLayoutSwitch"}
)},{
selector:"#switch-three", ranges:new Array(
{th:0, className:"layout1"},
{th:300}, // Default
{th:500, className:"layout3"}
)},{
selector:"#switch-four", ranges:new Array(
{th:0, className:"layout1"},
{th:300, className:"layout2"},
{th:500, className:"layout3"},
{th:700, className:"layout4"},
{th:900} // Default
)}]);
});