﻿window.$ = $telerik.$;

var LastAddedClass="";

function pageLoad() {
    if (Sys.Services.AuthenticationService.get_isLoggedIn()) {
        Sys.Services.ProfileService.load();
    }
}

function LaunchComments() {
    $("#CommentTab").click();
}
    

function doAddComment() {
    if (Page_ClientValidate("CommentsForm")) {
        $("#CommentsWrapper .SendButton").attr("disabled", "true");
        $("#loadingGif").toggle();
        var comment = new UserComment();
        comment.Fullname = Sys.Services.ProfileService.properties.Firstname + " " + Sys.Services.ProfileService.properties.Lastname;
        comment.Website = Sys.Services.ProfileService.properties.Website;
        comment.Comment = $get("ctl00_ContentPlaceHolder1_txtComment").value.replace(/[\s\r\n]+$/, '');
        comment.CommentType = $get("hdnType").value;
        comment.ItemID = $get("hdnID").value;
        
        //Add the comment via an ajax enabled web service!
        CommentsService.AddComment(comment, onCommentAdded, onFailure);
        $("#CommentsWrapper .SendButton").attr("disabled", "");
    }
    return false;
}


function onCommentAdded(comment) {
    $get("rFirstName").innerHTML = comment.FirstName;
    var UserCommentsDiv = $get("UserComments");

    ToggleCommentsForm();
    if (comment.Valid) {
        var divTag = document.createElement("div");
        divTag.id = "comment-" & comment.CommentID;
        var lastComment = $("#UserComments .IsComment:last div:first");
        if ($(lastComment).hasClass("odd") && LastAddedClass == "") {
            divTag.className = "even";
            LastAddedClass = "even"
        } else if ($(lastComment).hasClass("even") && LastAddedClass == "") {
            divTag.className = "odd";
            LastAddedClass = "odd"
        } else if (LastAddedClass == "even") {
            divTag.className = "odd";
            LastAddedClass = "odd"
        } else if (LastAddedClass == "odd") {
            divTag.className = "even";
            LastAddedClass = "even"
        } else {
            divTag.className = "odd";
            LastAddedClass = "odd"
        }

        var aFullname = document.createElement("a");
        aFullname.href = comment.Website;
        aFullname.setAttribute("rel", "nofollow");
        aFullname.setAttribute("target", "_blank");
        aFullname.innerHTML = comment.Fullname;

        var divSaid = document.createElement("div");
        divSaid.className = "said";
        divSaid.appendChild(aFullname);
        divSaid.innerHTML += " said,";

        divTag.appendChild(divSaid);

        var divWhen = document.createElement("div");
        divWhen.className = "when";
        divWhen.innerHTML = comment.CreatedDate + " @ ";

        var aTime = document.createElement("a");
        aTime.href = "#comment-" + comment.CommentID;
        aTime.innerHTML = comment.CreatedTime;
        divWhen.appendChild(aTime);
        divTag.appendChild(divWhen);

        var divComment = document.createElement("div")
        divComment.className = "comment"
        divComment.innerHTML = comment.Comment
        divTag.appendChild(divComment)

        UserCommentsDiv.appendChild(divTag);
    }
    $("#loadingGif").toggle();
}

function onFailure(ret) {
    alert("Ajax Web Service did not respond.  Comment has not been received.");
}

function scrollTo(selector, speed) {
    var targetOffset = $(selector).offset().top;
    $('html,body').animate({ scrollTop: targetOffset }, speed);
}

function ToggleCommentsForm(){
    $("#CommentsWrapper").slideUp(1000, function() {
        $("#ResponseWrapper").slideDown(750);
        scrollTo('#ResponseWrapper', 1000); 
        $("#ctl00_ContentPlaceHolder1_txtComment").attr('value', ''); 
    });
}

function doDelete(e, id) {
    if (confirm("Are you sure you want to permanently delete this comment?")) {
        $(e).closest("div.IsComment").hide('slow');
        CommentsService.DeleteComment(id);
    }
    return false;
}

function doApprove(e, id) {
    $(e).closest("div.CommentAdmin").hide('slow');
    CommentsService.ApproveComment(id);
    return false;
}


$(document).ready(function() {
    $("#CommentTab").click(function() {
        if ($("#CommentsWrapper").is(":hidden") && $("#ResponseWrapper").is(":hidden")) {
            $("#CommentTabImage").attr('src', '/images/Create-Comment-Tab-Open.gif');
            $("#CommentsWrapper").slideDown(1000);
            scrollTo('#CommentsWrapper', 1000);
        } else if ($("#CommentsWrapper").is(":hidden") && $("#ResponseWrapper").is(":nothidden")) {
            $("#ResponseWrapper").slideUp(750, function() { $("#CommentsWrapper").slideDown(1000); scrollTo('#CommentsWrapper', 1000); }); 
        } else if ($("#CommentsWrapper").is(":nothidden") && $("#ResponseWrapper").is(":hidden")) {
            $("#CommentTabImage").attr('src', '/images/Create-Comment-Tab.gif');
            $("#CommentsWrapper").slideUp(1000);
        }
    });
});


if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
