﻿jQuery(function() {
    jQuery("input[id*=post_comment]").click(function() {
        //gather info for post
        var comment = encodeURIComponent(jQuery("textarea[id*=txtComment]").val());
        var parentID = $("input[id*=hdnParentID]").val();
        var mediaTypeID = jQuery("input[id*=hdnMediaTypeID]").val();
        var author = jQuery("input[id*=txtAuthor]").val();
        var email = jQuery("input[id*=txtAuthorEmail]").val();
        jQuery("textarea[id*=txtComment]").val('');
        
        //path to the small videoSlider
        var imgLoadingSmall = "<img src='/images/loadingSmall.gif' border='0' class='loading_img' />";
                jQuery(this).after(imgLoadingSmall);
                if (comment != '') {
                    jQuery.ajax({
                        type: "POST",
                        url: "/ajax/commentHandler.ashx?type=post",
                        dataType: "json",
                        data: "typeID=" + mediaTypeID + "&parentID=" + parentID + "&comment=" + comment + "&author=" +author + "&authorEmail=" +email,
                        success: function(msg) {
                            //pop li into ol at the top
                            jQuery("#commentOL").html(msg.formattedComment.comments + jQuery("#commentOL").html());
                        },
                        error: function(xhr, status, error) {
                            // Boil the ASP.NET AJAX error down to JSON.
                            var err = eval("(" + xhr.responseText + ")");
                        }
                    });
                    $(this).parent().after('<br clear="all"/><div class="success-hori"></div><div class="success-wide"><table><tr><td><img src="/content/images/success-trans.png" class="left" /><p> Baie dankie vir jou kommentaar. Ons kyk net of dit kosher is voordat ons dit plaas.</p></td></tr></table></div><div class="success-hori"></div><br clear="all"/>');
                    
					//$(this).after('<div class="success">Message has been posted successfully and is awaiting moderation.</div>');
					setTimeout('$(".success-hori,.success-wide").fadeOut("slow");', 3000);
                    return false;
                }
    });
});
function filterProfileComments(mediaTypeID) 
{
        $("input[id*=hdnCurrentPage]").val("1");
        jQuery("input[id*=hdnMediaTypeID]").val(mediaTypeID);
        getPagedResults("up");
}
function getPagedResults(resultType) {
    
    //slide comments section closed
    window.location.href = "#topComment";
    //$("#commentList").slideUp(600);
    
    //gather info for post
    //var activityDiv = $("#commentList");
    var urlToSend = '/ajax/commentHandler.ashx?type=paged';
    var parentID = $("input[id*=hdnParentID]").val();
    var currentPage = $("input[id*=hdnCurrentPage]").val();
    var mediaTypeID = jQuery("input[id*=hdnMediaTypeID]").val();
    var resp = '';
    
    //any extra parameters will be stored in this variable
    var extraParams = "";
    if (jQuery("#isProfileComments").length > 0)
    {
        extraParams = "&profileComments=1";
    }
    
    //if previous,do manual javascript calculations
    if (resultType == "down") {
        var amountToSubtract = 2;
        currentPage = $("input[id*=hdnCurrentPage]").val() - amountToSubtract;
    }
    else {
        currentPage = $("input[id*=hdnCurrentPage]").val();
    }
    
    //set the loading gif
    $("#commentList").html("<div align='center' style='margin-top:100px;'><img align='center' src='/images/loading/loadingBig.gif' /></div>");
    $.ajax({
        type: "POST",
        url: urlToSend,
        dataType: "json",
        data: "pagedComments=1&parentID=" + parentID + "&typeID=" + mediaTypeID + "&currentPage=" + currentPage + extraParams,
        success: function(msg) {
            //insert the comments
        $("#commentList").html(msg.formattedComment.comments);
        $("#commentList").slideDown(600);

        },
        error: function(msg, other) {
            resp = msg.responseText;
        }
    });

    $("#commentList").html(resp);
    
    return false;
}