// we create a 'namespace' (instance scope) to avoid messing up 'window'
// too much
this.atcode = new function atcode() {
    // note that this is all private - to expose something, attach it to
    // 'this'

    // external link functionality
    function is_extlink(a) {
        if (!a.href) {
            return false;
        };
        if (a.href.indexOf('/') == 0) {
            return false;
        };
        var proto = location.protocol;
        if (proto.indexOf('http') != 0) {
            return false;
        };
        var hostname = location.hostname;
        if (a.href.indexOf(proto + '//' + hostname) != 0) {
            return true;
        };
        return false;
    };

    function add_class(node, className) {
        var current = node.className ? node.className.split(' ') : [];
        for (var i=0; i < current.length; i++) {
            if (current[i] == className) {
                // class already available
                return;
            };
        };
        current.push(className);
        node.className = current.join(' ');
    };

    // this is called on the body's onload
    function init_extlinks() {
        var anchors = document.getElementsByTagName('a');
        for (var i=0; i < anchors.length; i++) {
            var anchor = anchors[i];
            if (is_extlink(anchor)) {
                add_class(anchor, 'external-link');
            };
        };
    };

    // show/hide forms and other elements on clicking elements (links) before
    // them
    function showhide_next(el) {
        var next = el.nextSibling;
        while (next.nodeType != 1) {
            next = next.nextSibling;
        };
        next.style.display = next.style.display == 'none' ? 'block' : 'none';
    };

    function init_showhide_next() {
        // XXX grmbl, pretty sure this can be done a nicer in jq
        var links = jq('.show-hide-next');
        for (var i=0; i < links.length; i++) {
            var link = links[i];
            link.onclick = function(evt) {
                showhide_next(link);
                evt.cancelBubble;
                if (evt.preventDefault) {
                    evt.preventDefault();
                };
                evt.returnValue = false;
            };
        };
    };

    // Initialize using jquery (imported by plone)
    jq(document).ready(init_extlinks);
    jq(document).ready(init_showhide_next);

    // Make collapser for search refinements
    jq(document).ready(function(){
        jq.each(jq('ul.collapsible'), function() {
            var lis = jq('li', this);
            var full_length = lis.length;
            lis = lis.slice(5, lis.length);
            lis.hide();
            if (full_length > 5) {
                jq(this).after(
                    '<span class="more-link">more &raquo;</span><br />');
            }
            jq(this).next().click(function(){
               lis.show();
               jq(this).hide();
               return false;
             });
         });
    });
}();
