﻿function SetValue(inputName) {
    if (inputName != '')
        inputName += '.';
    inputName += 'SetValue';
    $('input[name="' + inputName + '"]').attr('checked', true);
    $('input[name="' + inputName + '"]').trigger('change');
}

function AddValue(inputName) {
    if (inputName != '')
        inputName += '.';
    inputName += 'AddValue';
    $('input[name="' + inputName + '"]').attr('checked', true);
    $('input[name="' + inputName + '"]').trigger('change');
}

function SelectBooleanRadio(inputName) {
    $(inputName).attr('checked', true);
    $(inputName).trigger('change');
}

function SwitchBoolean(inputName) {
    $(inputName).attr('checked', !($(inputName).attr('checked')));
    $(inputName).trigger('change');
}

function copyInputValues(inputFrom, inputTo) {
    $('input[name="' + inputTo + '"]').val($('input[name="' + inputFrom + '"]').val());
    $('input[name="' + inputTo + '"]').trigger('change');
}

function InsertAtCaret(textBoxId, value) {
    var textBox = document.getElementById(textBoxId);
    InsertAtCaretInternal(textBox, value);
}

function InsertAtCaretInParent(textBoxId, value) {
    var textBox = window.parent.document.getElementById(textBoxId);
    InsertAtCaretInternal(textBox, value);
}

function InsertAtCaretInternal(textBox, value) {
    textBox.focus();
    //IE support
    if (document.selection) {
        sel = document.selection.createRange();
        sel.text = value;
    }
    //MOZILLA/NETSCAPE support
    else if (textBox.selectionStart || textBox.selectionStart == '0') {
        var startPos = textBox.selectionStart;
        var endPos = textBox.selectionEnd;
        var scrollTop = textBox.scrollTop;
        textBox.value = textBox.value.substring(0, startPos)
                                  + value
                          + textBox.value.substring(endPos, textBox.value.length);
        textBox.focus();
        textBox.selectionStart = startPos + value.length;
        textBox.selectionEnd = startPos + value.length;
        textBox.scrollTop = scrollTop;
    } else {
        textBox.value += value;
    }
    textBox.focus();
};

function WikiAddTag(editor, addTag) {
    TextareaReplaceSelection(
                    editor,
                    addTag + TextareaGetSelection(editor).text + addTag);
    return false;
}

function TextareaGetSelection(the_id) {
    var e = document.getElementById(the_id);

    //Mozilla and DOM 3.0
    if ('selectionStart' in e) {
        var l = e.selectionEnd - e.selectionStart;
        return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
    }
    //IE
    else if (document.selection) {
        e.focus();
        var r = document.selection.createRange();
        var tr = e.createTextRange();
        var tr2 = tr.duplicate();
        tr2.moveToBookmark(r.getBookmark());
        tr.setEndPoint('EndToStart', tr2);
        if (r == null || tr == null) return { start: e.value.length, end: e.value.length, length: 0, text: '' };
        var text_part = r.text.replace(/[\r\n]/g, '.'); //for some reason IE doesn't always count the \n and \r in the length
        var text_whole = e.value.replace(/[\r\n]/g, '.');
        var the_start = text_whole.indexOf(text_part, tr.text.length);
        return { start: the_start, end: the_start + text_part.length, length: text_part.length, text: r.text };
    }
    //Browser not supported
    else return { start: e.value.length, end: e.value.length, length: 0, text: '' };
}

function TextareaReplaceSelection(the_id, replace_str) {
    var e = document.getElementById(the_id);
    selection = TextareaGetSelection(the_id);
    var start_pos = selection.start;
    var end_pos = start_pos + replace_str.length;
    e.value = e.value.substr(0, start_pos) + replace_str + e.value.substr(selection.end, e.value.length);
    TextareaSetSelection(the_id, start_pos, end_pos);
    return { start: start_pos, end: end_pos, length: replace_str.length, text: replace_str };
}


function TextareaSetSelection(the_id, start_pos, end_pos) {
    var e = document.getElementById(the_id);

    //Mozilla and DOM 3.0
    if ('selectionStart' in e) {
        e.focus();
        e.selectionStart = start_pos;
        e.selectionEnd = end_pos;
    }
    //IE
    else if (document.selection) {
        e.focus();
        var tr = e.createTextRange();

        //Fix IE from counting the newline characters as two seperate characters
        var stop_it = start_pos;
        for (i = 0; i < stop_it; i++) if (e.value[i].search(/[\r\n]/) != -1) start_pos = start_pos - .5;
        stop_it = end_pos;
        for (i = 0; i < stop_it; i++) if (e.value[i].search(/[\r\n]/) != -1) end_pos = end_pos - .5;

        tr.moveEnd('textedit', -1);
        tr.moveStart('character', start_pos);
        tr.moveEnd('character', end_pos - start_pos);
        tr.select();
    }
    return TextareaGetSelection(the_id);
}


/*
MENU
original code from http://javascript-array.com/scripts/jquery_simple_drop_down_menu/
*/
var menu_timeout = 500;
var menu_closetimer = 0;
var menu_ddmenuitem = 0;

function menu_open() {
    menu_canceltimer();
    menu_close();
    menu_ddmenuitem = $(this).find('ul').css('visibility', 'visible');
}

function menu_close()
{ if (menu_ddmenuitem) menu_ddmenuitem.css('visibility', 'hidden'); }

function menu_timer()
{ menu_closetimer = window.setTimeout(menu_close, menu_timeout); }

function menu_canceltimer() {
    if (menu_closetimer) {
        window.clearTimeout(menu_closetimer);
        menu_closetimer = null;
    } 
}

$(document).ready(function() {
    $('#menu > li').bind('mouseover', menu_open)
    $('#menu > li').bind('mouseout', menu_timer)
});

document.onclick = menu_close;

