/*
* Copyright (c) 2010 Alexander Wiederhold
*/

var images = {};

/* Kommentare */
function Comment()
{
    var oc = this;
    oc.current = null;
    //oc.mediaLimit = 5;

    /* Kommentarbild anzeigen / verbergen */
    oc.image = function(e, v, i) {
        if (typeof images[e][v][i] == 'undefined')
            return false;

        var image = images[e][v][i];

        if (jQuery('#c_images_' + v).is(':hidden')) {
            jQuery('#c_images_' + v).show();
        }

        if (oc.current != i) {
            oc.current = i;

            var src = '<img border="0" src="/archive/images/comments/inside/' + image.file + '" />';
            src = '<div class="imgth_ctr2"><a class="prev_imgth2" href=\"/archive/images/comments/original/' + image.file + '" target="_blank">' + src + '</a></div>';

            jQuery('#c_images_' + v).find('.c_image_title').text(image.title);
            jQuery('#c_images_' + v).find('.c_image > div.imgth_ctr2').remove();
            jQuery('#c_images_' + v).find('.c_image').append(src);
        }
        else if (oc.current == i) {
            oc.current = null;
            jQuery('#c_images_' + v).hide();
        }

        return false;
    }

    /* Kommentar löschen */
    oc.remove = function(e, v) {
        jQuery.ajax({
            url: '/archive/ajax/votedel.php',
            data: {vid: v},
            dataType: 'json',
            type: 'post',
            success: function(r) {
                mc.load(e);
            }
        });
    }

    /* Löschbestätigung anzeigen */
    oc.removeConfirm = function(v) {
        jQuery('#votedel_' + v).show();
    }

    /* Löschbestätigung ausblenden */
    oc.removeCancel = function(v) {
        jQuery('#votedel_' + v).hide();
    }

    /* Kommentar speichern */
    oc.add = function(e) {
        v = jQuery('#comment_id').length ? jQuery('#comment_id').val() : 0;

        if (typeof tinyMCE.editors['vote_msg' + e] != 'undefined')
            var c = tinyMCE.editors['vote_msg' + e].getContent();
        else
            var c = jQuery('#vote_msg' + e).val();

        jQuery('#comment-submit').attr('disabled', 'disabled');

        var s = jQuery('#send_notice_' + e).is(':checked') ? 1 : 0;
        var a = jQuery('#voteform_' + e).serialize();

        var script = 0 == v ? '/archive/ajax/voteadd.php' : '/archive/ajax/voteedit2.php';

        jQuery.ajax({
            url: script,
            data: 'id=' + e +'&v=' + v + '&msg=' + encodeURIComponent(c) + '&vote=2&sn=' + s + '&' + a,
            dataType: 'json',
            type: 'post',
            success: function(r) {
                oc.enableSubmit(3000);

                // Fehlermeldung, wenn gegeben
                if (r && typeof r.error != 'undefined' && r.error)
                    jQuery('#voteform_' + e).prepend(r.content);
                else
                    mc.load(e);
            }
        });
    }

    /* Laden zum Bearbeiten */
    oc.loadEdit = function(e, v) {
        jQuery.ajax({
            url: '/archive/ajax/voteedit2.php',
            data: { 'vid': v, 'id': e, 'type': 'comment' },
            dataType: 'json',
            type: 'post',
            success: function(r) {
                jQuery('#vote_msg' + e).val(r.content);

                if (typeof tinyMCE.editors['vote_msg' + e] != 'undefined')
                    var c = tinyMCE.editors['vote_msg' + e].setContent(r.content);

                jQuery('#vote_title').text('Kommentar Editieren');
                jQuery('#voteform_' + e).append('<input type="hidden" name="comment_id" id="comment_id" value="' + v + '" />');

                if (r.files.length > 0) {
                    m.files = r.files;
                    m.manager();
                }
            }
        });
    }

    /* Submit aktivieren */
    oc.enableSubmit = function(t)
    {
        if (typeof t != 'undefined') {
            setTimeout('oc.enableSubmit()', t);
            return;
        }

        jQuery('#comment-submit').attr('disabled', '');
    }
}

/* Kommentare */
function Comments()
{
    var mc = this;

    /* Kommentare anzeigen / verbergen */
    mc.toggle = function(e) {
        if (jQuery('#co_' + e).is(':hidden')) {
            jQuery('[id^=co_].add_bereiche').hide();
            mc.load(e);
        }
        else {
            hide_commentslink(e);
            none('co_' + e);
        }
    }

    /* Kommentare laden */
    mc.load = function(e) {
        jQuery('#cs-' + e).prepend(loading);

        if (typeof d == 'undefined') d = 0;
        else if(d) d = 1;

        jQuery.ajax({
            url: '/archive/ajax/votelist.php',
            data: 'id=' + e,
            dataType: 'json',
            type: 'post',
            success: function(r) {
                var v = jQuery('#images_' + e).is(':visible');
                hideshow_events(e, 'co');
                if (v) jQuery('#images_' + e).show();

                jQuery('#cs-' + e).find('img').remove();
                jQuery('#co_' + e).html(r.content);

                show_commentslink(e);
                images[e] = r.images;

                jQuery('#cs-' + e).html(r.amount);
            }
        });
    }
}

var oc = new Comment();
var mc = new Comments();
