var Emoticon = Class.create({
    
    initialize: function (image, sign, textarea) {
        this.obj        = new Element('img', {src: '/images/emoticons/' + image});
        this.textarea   = textarea;
        this.sign       = sign;

        this.obj.observe('click', this.insertEmoticon.bind(this));
        this.obj.observe('mouseover', this.hover);
    },

    hover: function() {
        this.style.cursor = "pointer";
    },

    insertEmoticon: function() {
        if (document.selection) {
            this.textarea.focus();
            sel = document.selection.createRange();
            sel.text = this.sign;
        } else if (this.textarea.selectionStart || this.textarea.selectionStart == '0') {
             var startPos = this.textarea.selectionStart;
             var endPos = this.textarea.selectionEnd;
             this.textarea.value = this.textarea.value.substring(0, startPos) + this.sign + this.textarea.value.substring(endPos, this.textarea.value.length);
        } else {
            this.textarea.value += this.sign;
        }
    }

});

var Emoticons = Class.create({

    initialize: function(emoticons, textarea, textBox) {
        this.emoticons = emoticons;
        this.textarea = textarea;
        textBox.innerHTML = this.replaceTekst(textBox.innerHTML);

        this.createEmoticonsDialog();
    },

    createEmoticonsDialog: function() {
        var emoticonsHolder = new Element('div').addClassName('emoticons');
        Element.insert( this.textarea, { after: emoticonsHolder } );
        this.emoticons.each( function(pair) {
            emoticonsHolder.insert(new Emoticon(pair.key, pair.value.first(), this.textarea).obj);
        }.bind(this));
    },

    replaceTekst: function (tekst) {
        this.emoticons.each( function (pair) {
            pair.value.each( function (item) {
                var reg = new RegExp(RegExp.escape(item), "g");
                tekst = tekst.replace(reg, "<img src='/images/emoticons/" + pair.key + "' alt='emoticon' />");
            });
        });
        return tekst;
    }

});

RegExp.escape = function(text) {
  
    if (!arguments.callee.sRE) {
        var specials = [
            '/', '.', '*', '+', '?', '|',
            '(', ')', '[', ']', '{', '}', '\\', '$', '='
        ];
        arguments.callee.sRE = new RegExp(
            '(\\' + specials.join('|\\') + ')', 'g'
        );
    }
    return text.replace(arguments.callee.sRE, '\\$1');

}

function initialize() {
    var emoticons = new Hash();
    emoticons.set("Smile.png", new Array(":)", ":-)", "=)"));
    emoticons.set("Happy.png", new Array(":D", ":-D"));
    emoticons.set("Joking.png", new Array(":P", ":-P"));
    emoticons.set("Wink.png", new Array(";)", ";-)"));
    emoticons.set("Disappointed.png", new Array(":(", ":-("));
    emoticons.set("Angry.png", new Array(":@"));
    emoticons.set("Cry.png", new Array(":'-(", ":'("));
    emoticons.set("Surprised.png", new Array(":O", ":-O", ":-o", ":o"));
    emoticons.set("Confused.png", new Array(":S", ":s", "^o)"));
    emoticons.set("Dont Tell.png", new Array(":-$"));
    emoticons.set("Thumbs Up.png", new Array("(Y)"));
    
    new Emoticons(emoticons, $$('.guestbook-form textarea')[0], $$('.guestbook-list')[0]);
}

function setCaptcha() {
    var BASE64_DATA = /^data:.*;base64/i;
    var base64Path = "/captcha.php";
    var img = $$('img[alt=code]')[0];
    
    if (BASE64_DATA.test(img.src)) {
        // pass the data to the PHP routine
        img.src = base64Path + "?" + img.src.slice(5);
    }
}

document.observe("dom:loaded", setCaptcha)
document.observe("dom:loaded", initialize);
