/*******************************************************************************
 *  -- Comment Anything facebook Style --                                      *
 *                                                                             *
 *      Author: Kulikov Alexey <a.kulikov@gmail.com>                           *
 *      Web: http://clops.at                                                   *
 *      Since: 28.03.2010                                                      *
 *                                                                             *
 *******************************************************************************/


/***
 *  When a user is typing a comment the size of the textarea is extended
 ***/
function adjustHeight(textarea){
    var dif = textarea.scrollHeight - textarea.clientHeight;
    if (dif){
        if (isNaN(parseInt(textarea.style.height))){
            textarea.style.height = textarea.scrollHeight + "px"
        }else{
            textarea.style.height = parseInt(textarea.style.height) + dif + "px"
        }
    }
}


/***
 *  Creates placeholders for text in the field
 ***/
function inputPlaceholder (input, color) {

    if (!input) return null;

    /**
    * Webkit browsers already implemented placeholder attribute.
    * This function useless for them.
    */
    if (input.placeholder && 'placeholder' in document.createElement(input.tagName)) return input;

    var placeholder_color = color || '#AAA';
    var default_color = input.style.color;
    var placeholder = input.getAttribute('placeholder');

    if (input.value === '' || input.value == placeholder) {
        input.value = placeholder;
        input.style.color = placeholder_color;
    }

    var add_event = /*@cc_on'attachEvent'||@*/'addEventListener';

    input[add_event](/*@cc_on'on'+@*/'focus', function(){
        input.style.color = default_color;
        if (input.value == placeholder) {
            input.value = '';
        }
    }, false);

    input[add_event](/*@cc_on'on'+@*/'blur', function(){
        if (input.value === '') {
            input.value = placeholder;
            input.style.color = placeholder_color;
        } else {
            input.style.color = default_color;
        }
    }, false);

    input.form && input.form[add_event](/*@cc_on'on'+@*/'submit', function(){
        if (input.value == placeholder) {
            input.value = '';
        }
    }, false);

    return input;
}


/***
 *  Heart and soul of the application -- it ADDS the comment to the database
 ***/
function addEMComment(oid){
    if(j$('#addEmComment_'+oid).val() && j$('#addEmComment_'+oid).val() != j$('#addEmComment_'+oid).attr('placeholder')){
    
        //mark comment box as inactive
        j$('#addEmComment_'+oid).attr('disabled','false');
        j$('#addEmMail_'+oid).attr('disabled','false');
        j$('#addEmName_'+oid).attr('disabled','false');
        j$('#emAddButton_'+oid).attr('disabled','false');

        if(j$('#addEmName_'+oid).val() == j$('#addEmName_'+oid).attr('placeholder')){
            document.getElementById('addEmName_'+oid).value = '';
        }

        if(j$('#addEmMail_'+oid).val() == j$('#addEmMail_'+oid).attr('placeholder')){
            document.getElementById('addEmMail_'+oid).value = '';
        }
        
        j$.post(
            'commentanything2/ajax/addComment.php', { 
                comment:      encodeURIComponent(j$('#addEmComment_'+oid).val()),
                object_id:    oid,
                sender_name:  encodeURIComponent(j$('#addEmName_'+oid).val()),
                sender_mail:  encodeURIComponent(j$('#addEmMail_'+oid).val())
            },
            
            function(data){
                j$('#emContent_'+oid).append('<div class="emComment" id="comment_'+data.id+'" style="display: none;"><div class="emCommentImage">'+data.image+'</div><div class="emCommentText">'+data.text+'</div><div class="emCommentInto">'+data.date+'<div class="emCommentLike"><span id="iLikeThis_'+data.id+'"><em>'+data.like+'</em></span></div></div></div>');
                j$('#comment_'+data.id).slideDown();
                
                if(j$('#total_em_comments_'+oid)){
                    j$('#total_em_comments_'+oid).replaceWith(data.total);
                }
                resetFields(oid);
            }, "json");            
            
    }else{
        j$('#addEmComment_'+oid).focus();
    }

    return false;
}



/***
 *  This loads all the comments to the current object id from the database
 ***/
function loadComments(){
    return;
    if(j$('emComments') && j$('emComments').getAttribute('object')){
        if(!j$('emComments').hasClassName('ignorejsloader')){

            new Ajax.Request('commentanything2/ajax/loadComments.php', {
                  method: 'post',
                  parameters: {
                      object_id: j$('emComments').getAttribute('object')
                  },

                onSuccess: function(reply) {
                    var data = reply.responseText.evalJSON(true);

                    if(data.dberror){
                        alert("DATABASE ERROR:\n"+data.dberror);
                        return;
                    }

                    j$('emComments').innerHTML = data.html;
                    Event.observe('addEmComment', 'keyup', function(){
                        adjustHeight(j$('addEmComment'));
                    });
                    resetFields();
                }
            });

        }else{
            Event.observe('addEmComment', 'keyup', function(){
                adjustHeight(j$('addEmComment'));
            });
            resetFields();
        }
    }
}

function _JQloadComments(){
    return;
    if(j$('#emComments') && j$('#emComments').attr('object')){
        if(!j$('#emComments').hasClass('ignorejsloader')){
        
            j$.post(
                'commentanything2/ajax/loadComments.php', { 
                    object_id: j$('#emComments').attr('object')
                },
                
                function(data){
                
                    if(data.dberror){
                        alert("DATABASE ERROR:\n"+data.dberror);
                        return;
                    }

                    document.getElementById('emComments').innerHTML = data.html;

                    j$('#addEmComment').bind('keyup', function(){                        
                        adjustHeight(document.getElementById('addEmComment'));                    
                    });
                    
                    resetFields();
                }, "json");  
            
        }else{
            j$('#addEmComment').bind('keyup', function(){                        
                adjustHeight(document.getElementById('addEmComment'));                    
            });            
            resetFields();
        }
    }
}

/***
 *  Clear Add Comment Fields
 ***/
function resetFields(oid){
    var obj = document.getElementById('addEmComment_'+oid);
    if(obj){
        obj.value = '';
        obj.style.color = '';
        obj.disabled = false;
        obj.style.height = '29px';
        inputPlaceholder(document.getElementById('addEmComment_'+oid));
    }

    obj = document.getElementById('addEmName_'+oid);
    if(obj){
        obj.value = '';
        obj.style.color = '';
        obj.disabled = false;
        inputPlaceholder(document.getElementById('addEmName_'+oid));
    }

    obj = document.getElementById('addEmMail_'+oid);
    if(obj){
        obj.value = '';
        obj.style.color = '';
        obj.disabled = false;
        inputPlaceholder(document.getElementById('addEmMail_'+oid));
    }

    obj = document.getElementById('emAddButton_'+oid);    
    if(obj){
        obj.disabled = false;
    }
}


/***
 *
 ***/
function iLikeThisComment(cid){
    j$.post(
            'commentanything2/ajax/likeComment.php', { 
                comment_id:   cid
            },
            
            function(data){
                j$('#iLikeThis_'+cid).html('<em>'+data.text+'</em>');
            }, "json"); 
}


/***
 *  When there are more than 2 comments they are hidden and can be opened by this function
 ***/
function viewAllComments(obj){
    j$('.emComment_'+obj).fadeIn();
    j$('#emShowAllComments_'+obj).slideUp();
    j$('#emHideAllComments_'+obj).fadeIn();
}

/***
 *  When there are more than 2 comments they are hidden and can be opened by this function
 ***/
function hideAllComments(obj){
    j$('.emHiddenComment_'+obj).fadeOut();
    j$('#emShowAllComments_'+obj).fadeIn();
    j$('#emHideAllComments_'+obj).slideUp();
}




j$(document).ready(function(){
    //resetFields();
});

