// www.cam.ac.uk 'Rotating Feature' script
// (c) University of Cambridge Computing Service 2008 

// Assorted DOM utility functions

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function hasClass(node,theClass) {
    return node.className.match(new RegExp('(\\s|^)'+theClass+'(\\s|$)'));
}
 
function addClass(node,theClass) {
    if (!this.hasClass(node,theClass)) node.className += " "+theClass;
}
 
function removeClass(node,theClass) {
    if (hasClass(node,theClass)) {
        var reg = new RegExp('(\\s|^)'+theClass+'(\\s|$)');
        node.className=node.className.replace(reg,' ');
    }
}

// Support functions

var features;
var nFeatures;

// Given the index of the current feature and a direction to move 
// (negative=previous, positive=next), hide of show the requested 
// feature 
function change_feature(current,direction) {
    var wanted = current+direction;
    if (wanted < 0) wanted = nFeatures-1;
    if (wanted > nFeatures-1) wanted = 0;
    for (var i = 0; i < nFeatures; ++i) {
        if (i == wanted) {
          removeClass(features[i],"hidden");
        } else {
          addClass(features[i],"hidden");
        }
    }
    // suppress default href="..." action
    return false;
}

// Dynamically add next/previous links to all features 
function addLinks(node,seq) {

    // Try to find the .feature-bg child 
    var target;    
    var children = node.childNodes
    for (var i = 0; i < children.length; ++i) {
       if (children[i].nodeType == 1 && hasClass(children[i],"feature-bg")) { 
           target = children[i];
           break;
       }
    }
    if (!target) return;

    // Build the next/previous links:
    var p = document.createElement("p");
    if (!p) return; // In case createElement doesn't work
    p.className = "feature-links";
    
    var prevLink = document.createElement("a");
    prevLink.setAttribute("href","#");
    prevLink.onclick = function() {
       return change_feature(seq,-1);  
    } 
    prevLink.appendChild(document.createTextNode("Previous"));
    p.appendChild(prevLink);

    p.appendChild(document.createTextNode(" | "));

    var nextLink = document.createElement("a");
    nextLink.setAttribute("href","#");
    nextLink.onclick = function() {
       return change_feature(seq,1);  
    } 
    nextLink.appendChild(document.createTextNode("Next"));
    p.appendChild(nextLink);

    // Append the links to the target
    target.appendChild(p);
    
}

// Initialise JavaScript banner processing
function setupFeatures() {

    // Sanity check for DOM compatibility
    if (!document.getElementById) return false;

    // Find all features. They are the <div> children of the element 
    // with ID "features"
    features = new Array();
    var counter = 0;
    var container = document.getElementById("features");
    if (container) {
        var elements = container.childNodes;
        for (var i = 0; i < elements.length; ++i) {
            if (elements[i].nodeType == 1 && elements[i].nodeName == "DIV") {
                features[counter++] = elements[i];
            }
        }
        nFeatures = features.length;

        // Choose one feature at random for display
        var ranFeature = Math.floor(Math.random()*nFeatures);

        // Hide all but the selected feature and add next/previous links
        for (var i = 0; i < nFeatures; ++i) {
            if (i == ranFeature) {
              removeClass(features[i],"hidden");
            } else {
              addClass(features[i],"hidden");
            }
            addLinks(features[i],i);
        }
    }

}

// Run setupFeatures at window.load time
addLoadEvent(setupFeatures);

// End
