﻿
function loadOverlay() {
    $("#notLoggedInDiv").overlay({
        // custom top position 
        top: 272,
        // disable this for modal dialog-type of overlays 
        closeOnClick: false,

        // we want to use the programming API 
        api: true
        // load it immediately after the construction 
    }).load();


}
function checkEmail(email) {
    
    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email)) {
        return false;
    }
    else {
        return true;
    }
}
jQuery(function() {
    jQuery("input[id*=post_comment]").click(function() {
        //gather info for post
        $("#errorBlock").hide();
        var errorString = "";
        var comment = encodeURIComponent(jQuery("textarea[id*=txtComment]").val());
        var parentID = $("input[id*=hdnParentID]").val();
        var mediaTypeID = jQuery("input[id*=hdnMediaTypeID]").val();

        var dataToSend = "typeID=" + mediaTypeID + "&parentID=" + parentID + "&comment=" + comment;

        if ($("#txtAuthor").length) {
            if (!checkEmail($("#txtAuthorEmail").val())) {
                errorString += "<li>" + noEmailMsg + "</li>";
            }
            if ($("#txtAuthor").val() == "") {
                errorString += "<li>" + noNameMsg + "</li>";
            }
            if (errorString == "") {
                dataToSend += "&author=" + $("#txtAuthor").val() + "&authEmail=" + $("#txtAuthorEmail").val();
                $("#txtAuthor").val('');
                $("#txtAuthorEmail").val('');
            }
        }
        if (comment == "") {
            errorString += "<li>" + noCommentMsg + "</li>";
        }
        if (errorString == '') {
            //path to the small videoSlider
            jQuery("textarea[id*=txtComment]").val('');
            var imgLoadingSmall = "<img src='/images/loading/loadingSmall.gif' border='0' class='loading_img' />";
            jQuery(this).after(imgLoadingSmall);
            jQuery.ajax({
                type: "POST",
                url: "/ajax/commentHandler.ashx?type=post",
                dataType: "json",
                data: dataToSend,
                success: function(msg) {
                    //pop li into ol at the top
                    //jQuery("#commentOL").html(msg.formattedComment.comments + jQuery("#commentOL").html());
                    $(".loading_img").remove();
                    loadOverlay();

                },
                error: function(xhr, status, error) {
                    // Boil the ASP.NET AJAX error down to JSON.
                    var err = eval("(" + xhr.responseText + ")");
                }
            });
            return false;
        }
        else {
            $("#errorBlock").fadeIn('slow');
            $("#commentErrors").html(errorString);
        }
        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 = document.getElementById("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();
    
    //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 class='red_comments_huis_box_content' align='center'><img align='center' src='/images/loading/loadingsmall.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           
            activityDiv.innerHTML = msg.formattedComment.comments;
            if ($("span[id*=commentCount]").html() == "0") {
                //$("#comments_wrap").hide();
                $("#comments_wrap").append("<div class='red_comments_huis_box_content' style='padding-bottom:80px' align='center'><p>No Results Found</div>");
            }
            //$("#commentList").slideDown(600);
        }
    });

    return false;
}