/* 
 These script are for the links at the bottom of the record.
 toggleByClass toggles the detail view. showc
*/

// ############### Supporting Function ####################

toggle = function(el) {
    if (MochiKit.Style.getStyle(el, 'display') == 'block') {
         el.style.display = 'none';
    } else {
         el.style.display = 'block';
    }
 };

toggleDisplay = function (theLink) {
    if ( theLink.innerHTML == "Show" ) { 
       theLink.innerHTML = "Hide";
    } else {
       theLink.innerHTML = "Show";
    }
}


// MochiKit Simple XMLHttpRequest Call 
function loadContainerInfo(url, el) {
    var doReplace = function (req) {
        el.innerHTML = req.responseText;
    };
                                                                                                                                                         
    var doReplaceError = function () {
        el.innerHTML = "<span class='warning3'>Sorry, an error occured.  Consult the finding aid directly but cliking on the title.</span>";
    };
 
    el.innerHTML = '<img src="/images/indicator.gif" style="width:24px;height:24px;" alt="Busy retrieving relevant containers"/>'
    var req = MochiKit.Async.doSimpleXMLHttpRequest(url);

    req.addCallbacks(doReplace, doReplaceError);


}


// ################# Called Functions #############3

 // Toggle visibility for divs with a specific class.  
toggleByClass = function (cls, theLink) {
    var theDiv = theLink.parentNode
    var theRecord = theDiv.parentNode
    var elems = MochiKit.DOM.getElementsByTagAndClassName('div', cls, theRecord);
    MochiKit.Base.map(toggle, elems);
    if (theLink.innerHTML == "Show Details") { 
        theLink.innerHTML = "Hide Details";
    } else {
        theLink.innerHTML = "Show Details";
    }
}

// Do search on xml src doc and show relevant containers
showContainers = function (theLink) {
    var qs = window.location.search.substring(1);
    var theURL = theLink.rel.replace("cao","staff/ead");
    var src_uri = theURL + "/sns/container?" + qs;
    var theDiv = theLink.parentNode
    var theRecord = theDiv.parentNode
    var elems = MochiKit.DOM.getElementsByTagAndClassName('div', 'relevant', theRecord);
    MochiKit.Base.map(toggle, elems);
    if (theLink.innerHTML.indexOf("Show") > 1)  {
        loadContainerInfo(src_uri, elems[0]);
        theLink.innerHTML = "|  Hide Relevant Containers";
    } else {
        theLink.innerHTML = "|  Show Relevant Containers";
    }
}


focusSearchBox = function () {
    document.searchitems.query1.focus();
}

MochiKit.DOM.addLoadEvent(focusSearchBox);


