﻿
myBbcodeSettings = {
    nameSpace: "bbcode", // Useful to prevent multi-instances CSS conflict
    markupSet: [
 { name: 'Bold', key: 'B', openWith: '[b]', closeWith: '[/b]' },
 { name: 'Italic', key: 'I', openWith: '[i]', closeWith: '[/i]' },
 { name: 'Underline', key: 'U', openWith: '[u]', closeWith: '[/u]' },
 { separator: '---------------' },
 { name: 'Picture', key: 'P', replaceWith: '[img][![Url]!][/img]' },
 { name: 'Link', key: 'L', openWith: '[url=[![Url]!]]', closeWith: '[/url]', placeHolder: 'Your text to link here...' },
 { separator: '---------------' },
 { name: 'Colors', openWith: '[color=[![Color]!]]', closeWith: '[/color]', dropMenu: [
 { name: 'Yellow', openWith: '[color=yellow]', closeWith: '[/color]', className: "col1-1" },
 { name: 'Orange', openWith: '[color=orange]', closeWith: '[/color]', className: "col1-2" },
 { name: 'Red', openWith: '[color=red]', closeWith: '[/color]', className: "col1-3" },
 { name: 'Blue', openWith: '[color=blue]', closeWith: '[/color]', className: "col2-1" },
 { name: 'Purple', openWith: '[color=purple]', closeWith: '[/color]', className: "col2-2" },
 { name: 'Green', openWith: '[color=green]', closeWith: '[/color]', className: "col2-3" },
 { name: 'White', openWith: '[color=white]', closeWith: '[/color]', className: "col3-1" },
 { name: 'Gray', openWith: '[color=gray]', closeWith: '[/color]', className: "col3-2" },
 { name: 'Black', openWith: '[color=black]', closeWith: '[/color]', className: "col3-3" }
 ]
 },
 { separator: '---------------' },
 { name: 'Quotes', openWith: '[quote]', closeWith: '[/quote]' }
 ]
};

function HideCommentsData() {
    $('#comments-data').hide();
}

function ShowCommentsEditor() {
    setTimeout(function () {
        $('#comment-text').markItUpRemove();
        $('#comment-text').markItUp(myBbcodeSettings);
        $('.userdata.selected').text($('#comments-metadata').text());
    }, 500);
}

function MessageSent() {
    $('#send-dialog').dialog('close');
}

var editedComment=null;
var oldHtml;
var saveUrl;
function EditComment(id, saveName, cancelName, url) {
    if (editedComment != null)
        CancelCommentEdit();
    saveUrl = url;
    editedComment = id;
    var elem = $("#message-" + id + "-text");
    oldHtml = elem.html();
    elem.html('<div><textarea id="tmpeditor"></textarea></div><a href="#" onclick="SaveComment();return false;" class="imagelink"><span class="icon ok"></span> ' + saveName + '</a><a href="#" onclick="CancelCommentEdit();return false;" class="imagelink"><span class="icon delete"></span>' + cancelName + '</a>');
    $('#tmpeditor').val($("#message-" + id + '-bb').text());
    $('#tmpeditor').markItUp(myBbcodeSettings);
}
function CancelCommentEdit() {
    $('#tmpeditor').markItUpRemove();
    $('#tmpeditor').remove();
    var elem = $("#message-" + editedComment + "-text");
    elem.html(oldHtml);
    editedComment = null;
}
function SaveComment() {
    var newText = $('#tmpeditor').val();
    $("#message-" + editedComment + '-bb').text(newText);
    $('#tmpeditor').markItUpRemove();
    $('#tmpeditor').remove();
    $("#message-" + editedComment + "-text").load(saveUrl,{text:newText,id:editedComment});
    editedComment = null;

}

function ddl(anchor, list) {
    var a = $(anchor);
    var l = $(list);
    $.extend(l, { 'anchor': a });
    a.addClass('combo');
    a.html('<span class="left">&nbsp;</span><span class="mid">' + a.html() + '</span><span class="right">&nbsp;</span>');
    l.addClass('combo-list');
    a.bind('click', function () {
        l.fadeIn();
        a.addClass('pressed');
        l.position({ of: a, my: 'left top', at: 'left bottom', collision: 'flip flip' });
        return false;
    });
    var fnHide = function () {
        l.fadeOut();
        a.removeClass('pressed');
    }
    var fnCancelHide = function () {
        if (l.data('timer')) {
            clearTimeout(l.data('timer'));
            l.data('timer', null);
        }
    }
    a.bind('mouseleave', function () {
        l.data('timer', setTimeout(fnHide, 1000));
    });
    l.bind('mouseleave', function () {
        l.data('timer', setTimeout(fnHide, 1000));
    });
    l.bind('mousemove', function () {
        fnCancelHide();
    });
    a.bind('mousemove', function () {
        fnCancelHide();
    });
}



function btn(anchor) {
    var a = $(anchor);
    a.each(function () {
        $(this).addClass('combo');
        $(this).html('<span class="left">&nbsp;</span><span class="mid">' + $(this).html() + '</span><span class="right noarrow">&nbsp;</span>');
    });
}

function pluralForm(rule, forms, number) {
    var nplurals;
    var n = number;
    var plural;
    eval(rule);

    return String(forms.split('|')[plural % nplurals]).replace('~', number);
}


function RGBColor(color_string) {
    this.ok = false;

    if (color_string.charAt(0) == '#') { // remove # if any
        color_string = color_string.substr(1, 6);
    }

    color_string = color_string.replace(/ /g, '');
    color_string = color_string.toLowerCase();
    // array of color definition objects
    var color_defs = [
        {
            re: /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/,
            process: function (bits) {
                return [
                    parseInt(bits[1]),
                    parseInt(bits[2]),
                    parseInt(bits[3])
                ];
            }
        },
        {
            re: /^(\w{2})(\w{2})(\w{2})$/,
            process: function (bits) {
                return [
                    parseInt(bits[1], 16),
                    parseInt(bits[2], 16),
                    parseInt(bits[3], 16)
                ];
            }
        },
        {
            re: /^(\w{1})(\w{1})(\w{1})$/,
            process: function (bits) {
                return [
                    parseInt(bits[1] + bits[1], 16),
                    parseInt(bits[2] + bits[2], 16),
                    parseInt(bits[3] + bits[3], 16)
                ];
            }
        }
    ];

    // search through the definitions to find a match
    for (var i = 0; i < color_defs.length; i++) {
        var re = color_defs[i].re;
        var processor = color_defs[i].process;
        var bits = re.exec(color_string);
        if (bits) {
            channels = processor(bits);
            this.r = channels[0];
            this.g = channels[1];
            this.b = channels[2];
            this.ok = true;
        }

    }

    this.r = (this.r < 0 || isNaN(this.r)) ? 0 : ((this.r > 255) ? 255 : this.r);
    this.g = (this.g < 0 || isNaN(this.g)) ? 0 : ((this.g > 255) ? 255 : this.g);
    this.b = (this.b < 0 || isNaN(this.b)) ? 0 : ((this.b > 255) ? 255 : this.b);

    this.toHex = function () {
        var r = this.r.toString(16);
        var g = this.g.toString(16);
        var b = this.b.toString(16);
        if (r.length == 1) r = '0' + r;
        if (g.length == 1) g = '0' + g;
        if (b.length == 1) b = '0' + b;
        return '#' + r + g + b;
    }
}


