/* Comments */
function load_everything(extra_id)
{
    /* HIDE STUFF */
    hide_if_js_enabled();
    /* MAKE LINKS EXTERNAL */
    externalLinks();
    
    if (extra_id.length > 0 && extra_id.charAt(extra_id.length-1) != ' ')
        extra_id = extra_id + " ";
    
    $(extra_id + 'div.comments textarea')
        .click(
            function(event)
            {
                id = $(this).attr('id').substr(13);
                val = $(this).val();
                if (val == gettext("Write something..."))
                {
                    $(this).val("");
                    $(this).removeClass('italic lighter');
                    $("#comments_" + id + " .form_comment_toggle div").show();
                    $(this).removeClass("non_active_comment_form");
                }
            }
        )
        .blur(
            function(event)
            {
                textarea = $(this);
                blur_textarea(textarea);
            }
        );

    function blur_textarea (textarea)
    {
        id = textarea.attr('id').substr(13);
        val = textarea.val();
        has_class = textarea.hasClass("non_active_comment_form");
        if (val == "" && !has_class)
        {
            textarea.val(gettext("Write something..."));
            textarea.addClass('italic lighter non_active_comment_form');
            $("#comments_" + id + " .form_comment_toggle div").hide();
        }
    }

    $(extra_id + 'a.comment_button').click(
        function(event)
        {
            id = $(this).attr('id');
            id = id.substr(13, id.length - 13);
            var display = $('#comments_' + id).css('display');
            
            if (display == 'none')
            {
                $('#comments_' + id).slideDown('fast');
            }
            
            var itemOffsetTop = $('#comments_' + id).offset().top;
            var windowBottom = $(window).scrollTop() + $(window).height() - 35;
            
            if (itemOffsetTop > windowBottom)
            {
                $.scrollTo($('#comments_' + id), {duration:500});
            }
            else if (display != 'none')
            {
                $('#comments_' + id).slideUp('fast');
            }
            return false;
        }
    );

    $(extra_id + 'a.comment_delete').click(comment_delete);
    //$(extra_id + 'a.message_delete').click(message_delete);
    $(extra_id + 'a.message_delete').modybox({
        'show_title': false,
        'auto_height': true,
        'media_url': 'media/',
        'dialog_content': gettext("Are you sure you want to delete this message?"),
        'callback_yes': message_delete,
        'type': DIALOG_YES_NO,
        'btn_caption_yes': gettext("Yes"),
        'btn_caption_no': gettext("No")
    });

    $(extra_id + 'a.more_comments').click(
        function(event)
        {
            id = $(this).attr('id');
            ids = id.substr(14, id.length - 14);
    
            ids = ids.split('_');
            
            if (ids.length > 1)
            {
                post_id = ids[0];
                last_comment_id = ids[1];
            
                var url = '/en/comments/ajax/?post_id=' + post_id + '&last_comment_id=' + last_comment_id;
                $('#more_comments_'+post_id).load
                (
                    url, null, function()
                    {
                        //$(this).removeAttr('style');
                        $('#more_comments_'+post_id+' .comment').fadeIn(500);
                        $('#more_comments_'+post_id+' .comment a.comment_delete').click(comment_delete);
                    }
                );
            }
            return false;
        }
    );

    $(extra_id + 'form.form_comment').submit(
        function(event)
        {
            var id = $(this).attr('id');
            id = id.substr(13, id.length - 13);
            $('#comments_'+id+' .commentsContainer').append('<div class="extraComment"></div>');
            var url = $(this).attr('action');
            
            params = {'ajax': 'true'};
            var elems = this.elements;
            for (var i = 0; i < elems.length; i++)
            {
                params[elems[i].name] = elems[i].value;
            }
            
            $('#comments_'+id+' .extraComment').load
            (
                //url, {'ajax': 'true',comment: $('#form_comment_'+id+' textarea').val(), post_id:id, content_type:$('#id_content_type').val(), object_pk:$('#id_object_pk').val(), timestamp:$('#id_timestamp').val(), security_hash:$('#id_security_hash').val()}, function()
                
                url, params, function()
                {
                    $('#comments_'+id+' .extraComment .comment').fadeIn(500);
                    $('#comments_'+id+' .extraComment .comment a.comment_delete').click(comment_delete);
                    $(this).removeClass('extraComment');
                    $('#form_comment_'+id+' textarea').val('');
                    
                    update_comment_count(id);
                    
                    blur_textarea($('#form_comment_'+id+' textarea'));
                }
            );

            return false;
        }
    );


    function trim(value)
    {
        value = value.replace(/^\s+/,'');
        value = value.replace(/\s+$/,'');
        return value;
    }

    function comment_delete(event)
    {
        var id = $(this).attr('id');
        id = id.substr(15, id.length - 15);
        
        var ids = id.split('_');
        
        if (ids.length > 1)
        {
            var post_id = ids[0];
            var comment_id = ids[1];
            
            if (confirm(gettext('Are you sure you want to delete this comment?')))
            {
                // delete comment
                var url = $(this).attr('href').replace(/\?.*/i, '') + "?ajax";
                $.ajax({
                    type: "POST",
                    url: url,
                    error: function(msg) {
                        alert(gettext("The comment could not be deleted."));
                    },
                    success: function(msg){
                        $('#comment_'+comment_id).fadeOut('fast');
                        update_comment_count(post_id, false);
                    }
                });
            }
        }
        return false;
    }

    function message_delete(elem)
    {
        id = elem.attr('id');
        id = id.substr(15,id.length-15);
        
        // Make URL
        url = elem.attr('href');
        url = url + (url.indexOf('?') != -1 ? '&ajax' : '?ajax');

        // Delete message, ajax post request
        $.ajax({
            type: "POST",
            url: url,
            error: function(msg) {
                alert(gettext("The message could not be deleted."));
            },
            success: function(msg){
                $('#message_'+id).fadeOut('fast');
                
                // calculate page
                id = $('a.more_button');
                if (id)
                {
                    id = id.attr('id');
                    if (id != undefined && id != "")
                    {
                        id = id.substr(14, id.length - 14);                        
                        load_extra_message(id-1);
                    }
                }
            }
        });
        
        return false;
    }

    function update_comment_count(id, add)
    {
        if (typeof add == "undefined")
            add = true;
        
        // update comment count
        value = parseInt($('#comment_count_'+id).html());
        
        if (add)
        {
            value = value + 1;
            $('#comment_count_'+id).html(value);
        }
        else
        {
            value = value - 1;
            $('#comment_count_'+id).html(value);
        }
            
        if (value == 1 && $('#comment_count_text_'+id).html() == gettext("comments"))
            $('#comment_count_text_'+id).html(gettext("comment"));
        else if (value != 1 && $('#comment_count_text_'+id).html() == gettext("comment"))
            $('#comment_count_text_'+id).html(gettext("comments"));
    }

    function load_extra_message(page_id)
    {
        var url = '/en/tupperware-bekkers--unit-eerdekens-orchidee-tjes/?ajax=1&last_msg_only=1&group=1&p=' + page_id;

        $('#messagecontainer').append('<div class="extraMessage"></div>');
        
        $('#loader').html('<img src="/media1325589929/img/loader.gif" alt="[loading]" />');

        var name = $('#id_name').val();
        var type = $("input[@name='id_type']:checked").val();
        var sort = $('#id_sort').val();

        $('#messagecontainer .extraMessage').load
            (
                url, {name:name, page:id, type:type, sort:sort}, function()
                {
                    //if ($('#messagediv_'+page_id+' .msg').length > 0)
                    //{
                        $('#more_messages_'+page_id+' .extraMessage .msg').fadeIn(500);
                        load_everything('.extraMessage');
                        $(this).removeClass('extraMessage');
                        $('#loader').html('');
                    //}
                }
            );
        return false;
    }
}

$(document).ready(function(){load_everything('');});

