// [+] comentarii content video ------------------------------------------------
function openReplyBox(id_content, reply) {
    orig = $('reply_comment_form').remove();

    holder = $('adaugareply_' + reply);
    holder.appendChild(orig);

    form = $('add_comment_f');
    form['reply'].value = reply;
    form.show();
    $('errors_comment').hide();

    orig.show();
    if (reply < 0) {
        $('add_comment').show();
    } else {
        $('add_comment').hide();
    }
}

function closeReplyBoxDelayed(refresh) {
    closeReplyBox(1);
    setTimeout("Effect.Fade('errors_comment', { duration: 1.0 });", 2000);
    setTimeout("$('reply_comment_form').hide();", 3000);
    if (refresh) {
        setTimeout("window.ajaxPagination.gotoPage(1);", 3000);
    }
}

function closeReplyBox(delayed, refresh) {
    newForm = $('reply_comment_form');
    $('add_comment').hide();
    form = $('add_comment_f');
    form['reply'].value = '';
    form.hide();
    if (!delayed)
    newForm.hide();
}
// [-] comentarii content video ------------------------------------------------


// [+] Zona ajax comentarii ----------------------------------------------------
function fetchComments(limit, offset) {

	if (typeof(userId) != 'undefined') {
        div = $('ajax_comments');
        params = {limit: limit, offset: offset, user_id: userId};
        sn_getAjax('fetch_comments', params, 'ajax_comments');
    }


	if (typeof(contentId) != 'undefined') {
        div = $('comments');

        while (div.childNodes.length > 0) {
            div.removeChild(div.childNodes[0]);
        }
        params = {limit: limit, offset: offset, content_id: contentId, options: window.fetched3 ? 'v3' : ''};
        orig = $('reply_comment_form');
        if (orig) {
            orig.parentNode.removeChild(orig);
        }
        sn_getAjax('fetch_content_comments', params, 'comments');
    }
}

function sn_fetchContentCommentsHandler(result, resultElementName, userData) {
    if (result.code == 0) {
        result = result.text.evalJSON();
        element = $(resultElementName);

        if (typeof element != 'undefined') {
            Element.update(element, result.comments);
        }

        window.ajaxPagination.returnCall(result.num_comments);
        if($('nrComments')) $('nrComments').update(result.num_comments);
    }
}
// [-] Zona ajax comentarii ----------------------------------------------------


// Reply la comentariu
function reply_content_comment(form) {
    var f = (typeof (form) != 'undefined' ? $(form.id) : $('add_comment_f'));

    if(typeof(window.canSendComment) == 'undefined') window.canSendComment = 1;
    if (window.canSendComment != 1) return;
    else {
        if (f.message.value == 'login pentru a posta comentariu la acest film') {
            alert('Nu sunteti autentificat sau nu ati completat mesajul!');
            return;
        }
        if ($('posteaza_comentariu')) $('posteaza_comentariu').disabled = true;
        window.canSendComment = 0;
    }
    sn_getAjaxByQuery('post_reply_content_comment', f.serialize());
}

function assign_comment_reply(cid, user){
    var reply = $('id_post_reply');
    if (!reply) return;
    var reply_user = $$('#id_post_reply_user span')[0];
    reply.value = cid;
    reply_user.update(user);
    if (cid) $('id_post_reply_user').show();
    else $('id_post_reply_user').hide();
}

function sn_postReplyContentCommentHandler(result, resultElementName, userData) {
    if (result.code == 0) {
        if($('add_comment_f_content_tarea_main')) {
            $('add_comment_f_content_tarea_main').value = '';
            window.ajaxPagination.gotoPage(1);
        }
        else {
            $('add_comment_f').reset();
            $('errors_comment').update(result.text.text);
            $('errors_comment').show();
            closeReplyBoxDelayed(true);
        }
    } else {
        if($('add_comment_f_content_tarea_main')) {
            alert(result.text.text);
        }
        else {

            $('errors_comment').update(result.text.text);
            $('errors_comment').show();
        }
    }
    window.canSendComment = 1;
    if ($('posteaza_comentariu')) $('posteaza_comentariu').disabled = false;
}
//

// [+] Votare comentarii -------------------------------------------------------
function voteaza_comentariu(comment_id, nota) {
    customParams = {comment_id: comment_id, nota: nota};
    sn_getAjax('voteaza_comentariu', customParams, 'commentRating_' + comment_id);
}

function sn_voteazaComentariuHandler(result, resultElementName, userData) {
    if (result.code == 0) {
        //alert('Vot inregistrat');
        result = result.text.evalJSON();
        $(resultElementName).update(result['nota']);
    }
    else  {
        alert(result.text);
    }
}
function sn_voteazaComentariuErrorHandler(result, resultElementName, userData) {
    if (result.code == 1) {
        alert('Eroare la salvare');
    }
    else {
        alert('A aparut o eroare');
    }
}
// [-] Votare comentarii -------------------------------------------------------


function toggle_comment_form(elementId,targetId) {
    try {
        if($(elementId).visible()){
        	blind($(elementId));
        	if($(targetId)) $(targetId).update('Comenteaza');
        }
        else{
        	blindDown($(elementId));
        	$(targetId).update('Inchide');
        }
    }
    catch(e) {;}
}
