$(document).ready(function() {

    var tagsval = "";
    var tagsID = ""; // "userTag_" + tagsval;
    var tagArticleUniqueID = ""; // $("#UniqueId").val();

    // Hook the tag button
    $("#btntag").click(function() {
        CountVote("Add", $("#UniqueId").val(), $("#tags").val(), ShowTagMessage, true);

        tagsval = $("#tags").val();
        tagsID = "userTag_" + tagsval;
        tagArticleUniqueID = $("#UniqueId").val();
    });

    // Add the ToolTips
    $(".upvote").addClass("tooltip").attr("title", "Like this keyword? Click to add your vote.");
    $(".downvote").addClass("tooltip").attr("title", "Dislike this keyword?  Click to add your vote.");
    $(".user_tag").addClass("tooltip").each(function() {
        $(this).attr("title", "<h3>Tagged with the keyword <strong>\"" + $(this).html() + "\"</strong></h3>");
    });
    $("#tags").addClass("tooltip").attr("title", "Adding keywords to articles makes them easier to find!");
    $("#usertags").fadeIn();

    Intuit.SubscribeTo("CountVoteComplete",
    function(sender, eventName, args) {
        $("#tags").val('');
        Intuit.ReloadPartial("usertags",
        function(data) {
            Intuit.RaiseEvent("TagReloadComplete", null, args);



            var TagsHTMLForLastAdded = "<li id=\"" + tagsID + "_temp\">";
            TagsHTMLForLastAdded += "<a class=\"downvote tooltip\" onclick=\"CountVote('Remove','" + tagArticleUniqueID + "','" + tagsval + "',ShowTagMessage);\" href=\"javascript:void(0);\" title=\"Dislike this keyword? Click to add your vote.\">-</a>";
            TagsHTMLForLastAdded += "<a class=\"user_tag tooltip\" href=\"/Support/Search.aspx?fortag=" + tagsval + "\" title=\"undefined\">" + tagsval + "</a>";
            TagsHTMLForLastAdded += "<a class=\"upvote tooltip\" vote=\"\" onclick=\"CountVote('Add','" + tagArticleUniqueID + "','" + tagsval + "',ShowTagMessage);\" href=\"javascript:void(0);\" title=\"Like this keyword? Click to add your vote.\">+</a></li>";

            $("#usertags").append(TagsHTMLForLastAdded);
        });
    });

    Intuit.SubscribeTo("CountVoteStarted", function() { Intuit.RenderDisabled("article_tagging_interface"); });
    Intuit.SubscribeTo("TagReloadComplete", function(sender, eventName, args) {
        $("#usertags").fadeIn("fast");
        Intuit.RenderEnabled("article_tagging_interface");
        ShowTagMessage(args);
        Intuit.ToolTip();
    });


    $(".user_tag").click(function() {
        sc_capture($(this).html(), "none");
    });
});

function sc_capture(Tag, Additional) {
    Tag = new String(Tag);
    Additional = new String(Additional);
    SC_MakeRequest("hier4=" + Tag.toLowerCase() + "|" + Additional.toLowerCase());
}


function CountVote(AddOrRemove, ArticleId, Tags, callback, isAddButton) {

    Tags = new String(Tags);
    var splitTags = Tags.split(",");

    var reportTag = splitTags.length > 0 ? splitTags[0] : Tags;
    var action = (typeof isAddButton != "undefined" && isAddButton) ? "new" : AddOrRemove;

    sc_capture(reportTag, action);

    Intuit.RaiseEvent("CountVoteStarted", null);
    $.post(__controllerUrl,
    {
        tags: Tags,
        Action: AddOrRemove,
        article_id: ArticleId
    },
    function(d) {
        Intuit.RaiseEvent("CountVoteComplete", null, d);
    }, "json");
}

var __controllerUrl = "/Support/ajaxReceivers/tagsController.aspx";
var __standingMessage = "Add a keyword";
var __timeout = null;

function ShowTagMessage(d) {
    if (d.Error) {
        $("#tagsmessage").html(d.ErrorMessage);
    }
    else {
        $("#tagsmessage").html(d.Message);
    }
    __timeout = setTimeout('$("#tagsmessage").fadeOut("fast", function() { $("#tagsmessage").html(__standingMessage).fadeIn(); });', 2000);
}

/** KB SCRIPTS  FOR LC WIDGET */



function SwitchContextOnLoad() //for community widget
{
    if (!lc_isLoaded) {
        setTimeout(SwitchContextOnLoad, 100);
    }
    else {
        var str = new String($("#categoryList > ul > li:first").text());
        if (str == "")
            str = "Install_Setup";
        str = sanify(str);
        switchContext(str);
    }
}


function sanify(str) {
    str = str.toLowerCase().replace(/[^0-9,a-z,A-Z]/gi, '_');
    return str;
}

$(document).ready(function() {
    //SwitchContextOnLoad();
});	