
/* ----------------------------- */
/*	Commenting related functions */
/* ----------------------------- */

addEvent(window, 'load', initCommentWatch);

function initCommentWatch() {
    if (jQuery('#design').length > 0) initNotes();
    jQuery('#addCommentMessage').val('');
    jQuery('.MBbuttonAddNote').click(function() { AddNote(); });
    jQuery('#addCommentForm').submit(function() {
        jQuery('#commentLoader').show();
        var did = document.addCommentForm.designid.value;
        var ctxt = document.addCommentForm.commenttext.value;
        //Fix for IE6 not updating the textarea, and having a ctxt length of 1.
        if (ctxt.length <= 1) ctxt = tinyMCE.getInstanceById('addCommentMessage').getBody().innerHTML;
        var params = 'action=addEditComment&request=partial&commentid=-1&designid=' + did + '&commenttext=' + escape(ctxt) + '';
        if (jQuery('#commentprivate:checked').length > 0) params += '&commentprivate=yes';
        jQuery.ajax({
            type: "POST",
            url: getAccUrl() + 'partial/comments/' + did,
            data: params,
            success: function(data) {
                updateCommentList(data);
                jQuery('#commentLoader').hide();
            },
            error: function(data, txtStatus) {
                if (data.status == 401) {
                    jQuery('#dBoxSml').hide();
                    jQuery('.moxBoxMed').hide();
                    jQuery('#dbox-content').html(data.responseText).show();
                    jQuery('#dBoxMed').show();
                    dialogBoxesWatch(jQuery('#dbox-content'));
                }
                else {
                    jQuery('#dBoxSml').show();
                    dboxSmlError(jQuery('#mbsContent'));
                }
                jQuery('#commentLoader').hide();
            }
        });
        document.addCommentForm.commenttext.value = '';
        tinyMCE.execCommand('mceSetContent', false, '');
        return false;
    });
    if (jQuery('.addComment').length > 0) {
        addCommentAjax();
    }
    jQuery('.viewNote a').click(function() {
        jQuery('#PhotoContainer > div').show();
        id = jQuery(this).attr('class').substring(jQuery(this).attr('class').indexOf('-') + 1);
        for (i = 0; i < notes.notes.length; i++)
            if (notes.notes[i].id == id) notes.notes[i].ShowNoteText();
    });
    jQuery('.designImage').bind('mouseenter', function() {
        jQuery('.fn-area').fadeIn(); //css('visibility', 'visible');
    }).bind('mouseleave', function() {
        if (jQuery(this).parents('#PhotoContainer').length == 0)
            jQuery('.fn-area').fadeOut(); //.css('visibility', 'hidden');
    });
    jQuery('.fn-area').fadeOut();
}

function addCommentAjax() {
    jQuery('.noteLink').click(function() {
        var id = jQuery(this).attr('class').substring(jQuery(this).attr('class').indexOf('-') + 1);
        for (i = 0; i < notes.notes.length; i++) {
            if (notes.notes[i].id == id) {
                notes.notes[i].ShowNoteText();
                break;
            }
        }
    });
    jQuery('.deleteComment').click(function() {
        var id = jQuery(this).attr('class').substring(jQuery(this).attr('class').indexOf('-') + 1);
        if (id.indexOf(' ') > -1) id = id.substring(0, id.indexOf(' '));
        jQuery('.moxBoxMed').hide();
        jQuery(".moxBoxSml").hide();
        jQuery('#updateDetails').val('Delete');
        jQuery('#mbsDeleteForm > .deleteCancel > a').text('Cancel and don\'t delete');
        showDeleteDialog('Are you sure you want to delete this comment?', 'deleteComment', 'commentid', id);
        return false;
    });
}

function addEditCommentNoteAjax(note, cid, did, text, posX, posY, posW, posH) {
    if (cid == 0) return -1
    var params = 'action=addEditComment&request=partial&commentid=' + cid + '&designid=' + did + '&commenttext=' + escape(text) + '&hasposition=1&positionx=' + posX + '&positiony=' + posY + '&positionw=' + posW + '&positionh=' + posH;

    jQuery.ajax({
        type: "POST",
        url: getAccUrl() + 'partial/comments/' + did,
        data: params,
        success: function(data) {
            newId = data.substring(data.indexOf(':') + 1);
            if (data.indexOf('success') >= 0 && newId > 0)
                note.id = newId;
            else
                alert('There was a probelm saving the note.');
            //Now update the comments below
            jQuery.ajax({
                type: "GET",
                url: getAccUrl() + 'partial/comments/' + did,
                success: function(data) {
                    updateCommentList(data);
                }
            });
        }
    });
    return 0;
}

function deleteCommentNoteAjax(note, cid, did) {
    if (cid == 0) return -1
    var params = 'action=deleteComment&request=partial&hasPosition=1&commentid=' + cid;

    jQuery.ajax({
        type: "POST",
        url: getAccUrl() + 'partial/comments/' + did,
        data: params,
        success: function(data) {
            newId = data.substring(data.indexOf(':') + 1);
            if (data.indexOf('success') >= 0 && newId > 0)
                note.id = newId;
            else
                alert('There was a probelm deleting the note.');
            //Now update the comments below
            jQuery.ajax({
                type: "GET",
                url: getAccUrl() + 'partial/comments/' + did,
                success: function(data) {
                    updateCommentList(data);
                }
            });
        }
    });
    return 0;
}

function updateCommentList(data) {
    jQuery('#commentList').replaceWith(data).parent().removeClass('hidden');
    //jQuery('#commentList').siblings('h2').html(jQuery('#commentList').siblings('h2').children('span')).prepend('Comments on');
    jQuery('#commentListContainer').show();
    jQuery('#firstToComment').slideUp();
    addCommentAjax();
    fadeWatch();
    dialogBoxesWatch(jQuery('#commentList'))
}

