﻿var _canPost = false;

$(document).ready(function() {
    $("#btnPost").buttonify();
    $("#btnSearch").buttonify();

    $("#q").keypress(function(e) {
        if (e.keyCode == 13) {
            e.preventDefault();
            goSearch();
        }
    });

    $("#btnSearch").click(function(e) {
        e.preventDefault();
        goSearch();
    });

    $("#btnPost").click(function(e) {
        e.preventDefault();
        if (_canPost) post();
    });

    $("[remove]").live("click", function() {
        remove($(this).attr("remove"));
    });

    $("[reply]").click(function() {
        replyToPost($(this).attr("reply"));
    });

    $("#switch-search, #switch-post").click(function() {
        switchInputs($(this).attr("id").substr(7));
    });
});

function init(allowPost, query) {
    _canPost = allowPost;
    
    if(allowPost) {
        //$("#btnPost").click(function(e) {
        //    e.preventDefault();
        //    post();
        //});
    } else {
        //$("#txtPost").attr("disabled", true);
        //$("#btnPost").buttonify("disable").next().after("<span style='margin-left: 10px;'>Please <a href='/login?redirect=/support/community'>login</a> to post</span>");
        $("#divPost").html("<div style='text-align: center; font-size: 14px;'>Please <a href='/login?redirect=/support/community'>login</a> to post</div>");
    }

    $("#q").val(query);
    switchInputs("search");
}

function switchInputs(to) {
    if (to == "post") {
        $("#divSearch").hide();
        $("#divPost").show();
        $("#switch-post").addClass("community-tab-selected");
        $("#switch-search").removeClass("community-tab-selected");
    } else {
        $("#divSearch").show();
        $("#divPost").hide();
        $("#switch-post").removeClass("community-tab-selected");
        $("#switch-search").addClass("community-tab-selected");
    }
}

function goSearch() {
    window.location.href = "?q=" + $("#q").val();
}

function post() {
    var $box = $("#txtPost");
    
    if($box.val() != "") {
        $box.attr("disabled", true);            
        $("#btnPost").buttonify("wait", {caption: "Posting"});
        
        $.ajax({
            type: "POST",
            url: "/handlers/community.ashx",
            data: {"action": "post", "post": encodeURIComponent($box.val())},
            dataType: "json",
            success: function(result) {
                if(result.successful) {
                    addDiv(result);
                    $("#txtPost").val("");
                } else {
                    var msg = "We're unable to post this. Please try again later";
                    if(result.error) msg = result.error;
                    alert(msg);
                }
                
                if(result.errorcode == 1) {
                    $("#btnPost").buttonify("disable", {caption: "Post"});
                    $("#txtPost").val("");
                } else {
                    $("#btnPost").buttonify("enable");
                    $box.attr("disabled", false);
                }
            },
            error: function() {
                $box.attr("disabled", false);
                $("#btnPost").buttonify("enable");
                
                alert("An error has occured. Please try again");
            }
        });
        
    } else {
        // empty post
    }
}

function remove(postid) {
    if(confirm("Are you sure you want to remove this post?")) {
        $.ajax({
            type: "POST",
            url: "/handlers/community.ashx",
            data: {"action": "remove", "id": postid},
            dataType: "json",
            success: function(result) {
                if(result.successful) {
                    $("#post_" + postid).slideUp(300);
                } else {
                    // error (user may not be logged in
                }
            },
            error: function() {
                // oops
            }
        });
    }
}

function replyToPost(name) {
    $("#txtPost").val($("#txtPost").val() + "@" + name + " ").focus();
    switchInputs("post");
}

function addDiv(post) {
    $("#posts").prepend("<div id='post_" + post.id + "' class='community_ownpost community_main' style='display: none;'><div class='community_post'><img src='http://www.gravatar.com/avatar/" + post.gravatar + "?d=http://www.appfigures.com/images/pixel.gif&s=35' align='right' style='margin-left: 10px;'><b>" + post.name + "</b> " + $("#txtPost").val() + "</div><div class='community_date'><span class='remove-post' remove='" + post.id + "'>Remove</span> | " + post.timestamp + "</div><div class='community_div'></div></div>");
    $("#post_" + post.id).slideDown(350);
}