// file: /js/ea/default.js
//
// This file contains all E-Anthology-specific Javascript code


// Simple function to highlight a particular nav item
function highlight_nav(keyword) {
    $("#" + keyword + "_nav").addClass("cur");
}

// Expand the post iFrame to the right height on pageload
function autoIframe(frameId) {
    try {
        frame = document.getElementById(frameId);
        innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
        objToResize = (frame.style) ? frame.style : frame;
        targetH = 0;
        scrollH = innerDoc.body.scrollHeight;
        clientH = innerDoc.body.clientHeight;
        offsetH = innerDoc.body.offsetHeight;

        // DEBUG
        // alert("scrollH: " + scrollH + ", clientH: " + clientH + ", offsetH: " + offsetH);

        // In this case (mostly safari) we're not seeing everything
        if ( offsetH < clientH || offsetH < scrollH ) {
            targetH = offsetH;
        }
        else {
            targetH = scrollH;
        }

        objToResize.height = targetH + 30 + 'px';
    }
    catch(err){
        window.status = err.message;
    }
}

// This provides nicer classnames for the WYSIWYG web editor
function webeditor_custom_formatclass_option(name) {
    switch (name) {
        case "font1":
            return "11 point (normal)";
            break;
        case "font2":
            return "12 point";
            break;
        case "font3":
            return "14 point";
            break;
        case "font4":
            return "16 point";
            break;
        case "font5":
            return "20 point";
            break;
        case "font6":
            return "24 point";
            break;
        case "font7":
            return "32 point";
            break;
        default:
            return name;
            break;
    }
}

// This JS function handles all the link tags within a post:
// (d is the document object from within the IFRAME)
function cleanPost(d) {
    var linkTags = d.getElementsByTagName('a');
    for (var i = 0; i < linkTags.length; i++) {
        var link = linkTags[i];

        // Make sure links open in new windows
        link.setAttribute("target", "_blank");

        // Add the "we're going away from the site" image.
        if (     link.getAttribute("href")                                            // ignore <a name=".."></a>
            && ! link.getAttribute("href").match(/\/(www.)?(nwp|writingproject).org/) // ignore internal links
            &&   link.innerHTML                                                       // ignore <a> tags with nothing inside
            &&   link.innerHTML.match(/\S+/)                                          // ignore <a> tags with just space inside
            ) {
            text = link.innerHTML;
            text += ' <img src="/img/icons_a/dkgrey_white_10px.png" />';
            link.innerHTML = text;
        }
    }
}

//This JS handle the search function on ea header
function searchFocus(searchbox) {
    if (searchbox.value == 'Search E-Anthology') { searchbox.value = ''; }
    searchbox.style.color = '#000';
}

function searchBlur(searchbox) {
    if (searchbox.value == '') {
        searchbox.style.color = '#777';
        searchbox.value = 'Search E-Anthology';
    }
}


