// Javascript functions for ProjectPantry


// Insert page link from drop-down list
function insertPageLink(ctl)
{
    // Ignore if no options, or first (message) selected
    if ( ctl.selectedIndex == 0 || ctl.length < 2 )
        return;

    // Add text to textarea
    var editor = document.getElementById("editor");
    var link = ctl.options[ctl.selectedIndex].text;
    insertAtCursor(editor, "[[[" + link + "]]]");
}


// Insert page link from drop-down list
function insertImage(ctl)
{
    // Ignore if no options, or first (message) selected
    if ( ctl.selectedIndex == 0 || ctl.length < 2 )
        return;

    // Add text to textarea
    var editor = document.getElementById("editor");
    var img = ctl.options[ctl.selectedIndex].value;
    insertAtCursor(editor, "<img src=\"/"  + img + "\" />");
}


// Insert text at cursor position in a textarea
// http://alexking.org/blog/2003/06/02/inserting-at-the-cursor-using-javascript
function insertAtCursor(myField, myValue) 
{
    // IE
    if ( document.selection ) {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }

    // Firefox
    else if ( myField.selectionStart || myField.selectionStart == '0' ) {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)
            + myValue
            + myField.value.substring(endPos, myField.value.length);
    } 

    // Anything else
    else 
        myField.value += myValue;
}


// Show/hide banner when tiny arrow is clicked
function toggleBanner()
{
    var btn = document.getElementById("bannerButton");
    var b = document.getElementById("banner"),
        m = document.getElementById("menuList");
    
    if ( btn.src.indexOf("_up") > 0 ) { // banner now showing, hide it
        b.style.display = "none";
        m.style.display = "none";
        btn.src = btn.src.substr(0, btn.src.indexOf("_up")) + "_down.gif";
    }
    else {
        b.style.display = "block";
        m.style.display = "block";
        btn.src = btn.src.substr(0, btn.src.indexOf("_down")) + "_up.gif";
    }

}


