/* Global variables */
var selected; // Currently selected element

/* Shows or hides the Edit view for an article */
function toggleEditBlock()
{
    var article = document.getElementById("article");
    var source = document.getElementById("edit-hidden");
    var help = document.getElementById("edit-help");

    // Initialize values
    if (article.style.display == "") {
        article.style.display = "block";
        source.style.display = "none";
        help.style.display = "none";
    }

    if (article.style.display == "block") {
        article.style.display = "none";
        source.style.display = "block";
        help.style.display = "block";
    } else {
        article.style.display = "block";
        source.style.display = "none";
        help.style.display = "none";
    }
    return false;
}

/* Shows both article text and Edit view.  Used when previewing edits. */
function showAll()
{
    var article = document.getElementById("article");
    var source = document.getElementById("edit-hidden");
    var help = document.getElementById("edit-help");

    article.style.display = "block";
    help.style.display = "block";
    source.style.display = "block";
}

/* Pops up a help window */
function popupHelp(url)
{
    var props = "height=450,width=400,resizable=yes,status=yes,scrollbars=yes";
    var w = window.open(url, "ickypopup", props);
    w.focus();
}

/* Redirect to given url */
function redirect(url)
{
    document.location = url;
}

/* Search form event handlers */
function resetSearchForm(field)
{
    if (field.value == '') {
        field.style.color = '';
        field.form.reset();
    }
}

function clearSearchForm(field)
{
    field.value = '';
    field.style.color = "#000";
}

/* Grabs incoming article links via Ajax and displays them */
function getIncomingLinks(articleName)
{
    var progress = document.getElementById('progress');
    var loadingMsg = document.createTextNode(" loading...");

    var loadingFunc = function(t) {
        progress.appendChild(loadingMsg);
        progress.style.display = "inline";
    }

    var loadedFunc = function(t) {
        progress.removeChild(loadingMsg);
        progress.style.display = "none";
    }

    var errFunc = function(t) {
        alert('Error ' + t.status + ' -- ' + t.statusText);
    }

    var url = 'http://labs.spaceshipnofuture.org/icky/ajax.php';
    var params = 'mode=getIncomingLinks&q=' + articleName;
    var options = {
        method: 'get',
        parameters: params,
        onLoading: loadingFunc,
        onLoaded: loadedFunc,
        onFailure: errFunc
    }
    new Ajax.Updater('incoming-links', url, options);
}

/* Event handler called when version is selected from article history.
   Allows version element to be selected without clicking directly on 
   radio button, and also sets various background colors. */
function versionSelect(event)
{
    var selectedColor = "#FFAA11";
    var mouseOverColor = "#FFCC66";
    var mouseOutColor = "#FFFFFF";

    // Which HTML element was clicked?
    var target;
    if (!event) var event = window.event;
    if (event.target) {
        target = event.target;
    } else if (event.srcElement) {
        target = event.srcElement;
    }
    if (target.nodeType == 3) {
        // Safari bug, apparently
        target = target.parentNode;
    }
    var targetTag = target.tagName ? target.tagName.toLowerCase() : null;
    if (targetTag == 'a') {
        // Don't do anything if a link was clicked
        return;
    } else {
        // Reset colors for previously selected element
        try {
            selected.style.backgroundColor = "#FFFFFF";
            selected.onmouseover = function() { this.style.backgroundColor = mouseOverColor; };
            selected.onmouseout = function() { this.style.backgroundColor = mouseOutColor; };
        } catch (e) { }
        // Get version ID
        var versionId = this.id.slice(2);
        // Select the radio button
        selected = document.getElementById("v_" + versionId);
        document.getElementById("radio_" + versionId).checked = "checked";
        // Set colors for selected element
        this.style.backgroundColor = selectedColor;
        this.onmouseover = function() { this.style.backgroundColor = mouseOverColor; };
        this.onmouseout = function() { this.style.backgroundColor = selectedColor; };
        // Finally, enable the submit button
        document.getElementById("submitBtn").removeAttribute("disabled");
    }
}

/* Attaches the versionSelect() event handler to every version in the article history */
function initVersionSelect()
{
    var list = document.getElementById("versions");
    var versions = list.getElementsByTagName("li");
    for (var i = 0; i < versions.length; i++) {
        // Attach the versionSelect() event handler
        versions[i].onclick = versionSelect;
        // Attempt to enable the submit button if one of the radio buttons 
        // is already checked upon page load
        if (versions[i].checked == "checked") {
            document.getElementById("submitBtn").removeAttribute("disabled");
        }
    }
}

