$(document).ready(function() {

    // comment lengthcheck
    $('.checkcommentlength').click(function()
        {
            if ($('form textarea#id_comment').val().length < 3) {
                alert('Dein Kommentar ist zu kurz!');
                return false;
            } else {
                return true;
            }
        }
    );

    // hightlight pm's
    var lines = $('div.messaging pre.highlighter');
        if(lines.length != 0){
            lines = lines.html().split('\n');

            var color = new Array();
            color[0] = '#762169';
            color[1] = '#281255';
            color[2] = '#b4a433';
            color[3] = '#604220';
            color[4] = '#c74141';
            color[5] = '#061836';
            color[6] = '#6c3a5c';
            color[7] = '#968922';
            color[8] = '#374a34';
            color[9] = '#d2507d';

            var result = ''

            var oldcount = undefined;

            for (var i = 0; i < lines.length; ++i) {
                var count = lines[i].split('&gt;').length-1;
                var place = count.toString().length-1;

                for (var y = 0; y < count; ++y) {
                    if (y != count-1) {
                        lines[i] = lines[i].replace('&gt;', '<span style="color:'+color[y+1]+'">|</span>');
                    }
                }

                if (oldcount == count) {
                    replace_dings = '&nbsp;';
                } else {
                    replace_dings = '↪';
                }

                oldcount = count;
                result += '<span style="color:'+color[count.toString().charAt(place)]+'">'+lines[i].replace('&gt;',replace_dings)+'</span>\n';
            }

            $('pre.highlighter').html(result);
        }

    // flash messageBox
    $('div#flash-messages a.smallbutton').live('click', function(e) {
        $('div#flash-messages').fadeOut('fast');
        return false;
    });

    $(document).keypress(function(e) {
            if(e.keyCode == 13 && $('div#flash-messages').length != 0) {
                $('div#flash-messages').fadeOut('fast');
                return false;
            }
    });

    jQuery.fn.extend({
        thankyou: function(api_url, contenttype_id, object_id) {
            return this.each(function() {
                var $this = $(this);
                var options = {
                    'api_url': api_url,
                    contenttype_id: contenttype_id,
                    object_id: object_id
                };

                if(options.api_url == '' || options.contenttype_id < 1 || options.object_id < 1) return;

                $this.find('.upvote').click(function(e) {
                    e.preventDefault();

                    $.post(options.api_url, {
                        'ct': options.contenttype_id,
                        'id': options.object_id,
                        'vote': '1'
                    }, function(response) {
                        $this.find('.singular').hide();
                        $this.find('.plural').hide();
                        $this.find('.score').text(response.score[1]);
                        if (response.score[1] > 0) {
                            if (response.score[1] > 1) {
                                $this.find('.plural').show();
                            } else {
                                $this.find('.singular').show();
                            }
                        }
                    });
                });
            });
        }
    });
});

function QueryData(get_params) {
    this.params = {};

    this.addParam = function(key, value) {
        if (!(key in this.params)) {
            this.params[key] = [];
        }
        this.params[key].push(value);
    }

    this.deleteParam = function(key, value) {
        if (!(key in this.params)) { return; }
        console.log(this);
        if (value == undefined) {
            delete this.params[key];
        } else {
            var idx = this.params[key].indexOf(value);
            if (idx != -1) {
                this.params[key].splice(idx, 1);
            }
        }
    }

    this.toQueryString = function() {
        var qs = '';
        for (var key in this.params) {
            for (var i=0; i<this.params[key].length; i++) {
                if (qs.length > 0) { qs += '&'; };
                qs += key + '=' + encodeURIComponent(this.params[key][i]);
            }
        }
        return qs;
    }

    if (get_params == undefined) {
        get_params = location.search ? location.search : '';
    }

    if (get_params.charAt(0) == '?') {
        get_params = get_params.substring(1);
    }

    if (get_params.length > 0) {
        get_params = get_params.replace(/\+/g, ' ');
        var query_parts = get_params.split(/[&;]/g);

        for (var i=0; i<query_parts.length; i++) {
            var pair = query_parts[i].split('=');
            var key = decodeURIComponent(pair[0]);
            var value = pair.length > 1 ? decodeURIComponent(pair[1]) : '';

            this.addParam(key, value);
        }
    }
}

jQuery.fn.exists = function()
{
    return jQuery(this).length>0;
}

