// MediaWiki JavaScript support functions

var clientPC = navigator.userAgent.toLowerCase(); // Get client info
var is_gecko = ((clientPC.indexOf('gecko')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('khtml') == -1) && (clientPC.indexOf('netscape/7.0')==-1));
var is_safari = ((clientPC.indexOf('applewebkit')!=-1) && (clientPC.indexOf('spoofer')==-1));
var is_khtml = (navigator.vendor == 'KDE' || ( document.childNodes && !document.all && !navigator.taintEnabled ));
// For accesskeys
var is_ff2_win = (clientPC.indexOf('firefox/2')!=-1 || clientPC.indexOf('minefield/3')!=-1) && clientPC.indexOf('windows')!=-1;
var is_ff2_x11 = (clientPC.indexOf('firefox/2')!=-1 || clientPC.indexOf('minefield/3')!=-1) && clientPC.indexOf('x11')!=-1;
if (clientPC.indexOf('opera') != -1) {
    var is_opera = true;
    var is_opera_preseven = (window.opera && !document.childNodes);
    var is_opera_seven = (window.opera && document.childNodes);
    var is_opera_95 = (clientPC.search(/opera\/(9.[5-9]|[1-9][0-9])/)!=-1);
}

// add any onload functions in this hook (please don't hard-code any events in the xhtml source)
var doneOnloadHook;

if (!window.onloadFuncts) {
    var onloadFuncts = [];
}

function addOnloadHook(hookFunct) {
    // Allows add-on scripts to add onload functions
    onloadFuncts[onloadFuncts.length] = hookFunct;
}

function hookEvent(hookName, hookFunct) {
    if (window.addEventListener) {
        window.addEventListener(hookName, hookFunct, false);
    } else if (window.attachEvent) {
        window.attachEvent("on" + hookName, hookFunct);
    }
}

var mwEditButtons = [];
var mwCustomEditButtons = [];

// this function generates the actual toolbar buttons with localized text
// we use it to avoid creating the toolbar where javascript is not enabled
function addButton(imageFile, speedTip, tagOpen, tagClose, sampleText, imageId) {
    // Don't generate buttons for browsers which don't fully
    // support it.
    mwEditButtons[mwEditButtons.length] =
        {"imageId": imageId,
         "imageFile": imageFile,
         "speedTip": speedTip,
         "tagOpen": tagOpen,
         "tagClose": tagClose,
         "sampleText": sampleText};
}

// this function generates the actual toolbar buttons with localized text
// we use it to avoid creating the toolbar where javascript is not enabled
function mwInsertEditButton(parent, item) {
    var image = document.createElement("img");
    image.width = 23;
    image.height = 22;
    image.className = "mw-toolbar-editbutton";
    if (item.imageId) image.id = item.imageId;
    image.src = item.imageFile;
    image.border = 0;
    image.alt = item.speedTip;
    image.title = item.speedTip;
    image.style.cursor = "pointer";
    image.onclick = function() {
        insertTags(item.tagOpen, item.tagClose, item.sampleText);
        return false;
    };
    parent.appendChild(image);
    return true;
}

function mwSetupToolbar() {
    var toolbar = document.getElementById('toolbar');
    if (!toolbar) { return false; }

    var textbox = document.getElementById('wiki_text_box');
    if (!textbox) { return false; }

    // Don't generate buttons for browsers which don't fully
    // support it.
    if (!(document.selection && document.selection.createRange)
        && textbox.selectionStart === null) {
        return false;
    }

    for (var i = 0; i < mwEditButtons.length; i++) {
        mwInsertEditButton(toolbar, mwEditButtons[i]);
    }
    for (var i = 0; i < mwCustomEditButtons.length; i++) {
        mwInsertEditButton(toolbar, mwCustomEditButtons[i]);
    }
    return true;
}

function escapeQuotes(text) {
    var re = new RegExp("'","g");
    text = text.replace(re,"\\'");
    re = new RegExp("\\n","g");
    text = text.replace(re,"\\n");
    return escapeQuotesHTML(text);
}

function escapeQuotesHTML(text) {
    var re = new RegExp('&',"g");
    text = text.replace(re,"&amp;");
    re = new RegExp('"',"g");
    text = text.replace(re,"&quot;");
    re = new RegExp('<',"g");
    text = text.replace(re,"&lt;");
    re = new RegExp('>',"g");
    text = text.replace(re,"&gt;");
    return text;
}

// apply tagOpen/tagClose to selection in textarea,
// use sampleText instead of selection if there is none
function insertTags(tagOpen, tagClose, sampleText) {
    var txtarea;
    if (document.getElementById('wiki_text_box')) {
        txtarea = document.getElementById('wiki_text_box');
    } else {
        // some alternate form? take the first one we can find
        var areas = document.getElementsByTagName('textarea');
        txtarea = areas[0];
    }
    var selText, isSample = false;

    if (document.selection  && document.selection.createRange) { // IE/Opera

        //save window scroll position
        if (document.documentElement && document.documentElement.scrollTop)
            var winScroll = document.documentElement.scrollTop
        else if (document.body)
            var winScroll = document.body.scrollTop;
        //get current selection  
        txtarea.focus();
        var range = document.selection.createRange();
        selText = range.text;
        //insert tags
        checkSelectedText();
        range.text = tagOpen + selText + tagClose;
        //mark sample text as selected
        if (isSample && range.moveStart) {
            if (window.opera)
                tagClose = tagClose.replace(/\n/g,'');
            range.moveStart('character', - tagClose.length - selText.length); 
            range.moveEnd('character', - tagClose.length); 
        }
        range.select();   
        //restore window scroll position
        if (document.documentElement && document.documentElement.scrollTop)
            document.documentElement.scrollTop = winScroll
        else if (document.body)
            document.body.scrollTop = winScroll;

    } else if (txtarea.selectionStart || txtarea.selectionStart == '0') { // Mozilla

        //save textarea scroll position
        var textScroll = txtarea.scrollTop;
        //get current selection
        txtarea.focus();
        var startPos = txtarea.selectionStart;
        var endPos = txtarea.selectionEnd;
        selText = txtarea.value.substring(startPos, endPos);
        //insert tags
        checkSelectedText();
        txtarea.value = txtarea.value.substring(0, startPos)
            + tagOpen + selText + tagClose
            + txtarea.value.substring(endPos, txtarea.value.length);
        //set new selection
        if (isSample) {
            txtarea.selectionStart = startPos + tagOpen.length;
            txtarea.selectionEnd = startPos + tagOpen.length + selText.length;
        } else {
            txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length;
            txtarea.selectionEnd = txtarea.selectionStart;
        }
        //restore textarea scroll position
        txtarea.scrollTop = textScroll;
    } 

    function checkSelectedText(){
        if (!selText) {
            selText = sampleText;
            isSample = true;
        } else if (selText.charAt(selText.length - 1) == ' ') { //exclude ending space char
            selText = selText.substring(0, selText.length - 1);
            tagClose += ' '
        } 
    }

}

hookEvent("load", mwSetupToolbar);



