

jQuery(document).ready(function($) {
    
    if ($("#follow_project").length > 0 && !LAUNCHSET.params.IS_PROJECT_OWNER) {
        LAUNCHSET.projects.check_if_follower(LAUNCHSET.params.PROJECT_ID);
    }

    if ($("#txt_project_update").length > 0) {
        $("#txt_project_update").charCounter(500, {container: "#text_counter"});
    }

    if ($("#txt_comment").length > 0) {
        $("#txt_comment").charCounter(500, {container: "#text_counter"});
    }
    
    if ($("#project_tabs").length > 0) {
        $("#project_tabs").tabs();
        if (LAUNCHSET.params.IS_PROJECT_OWNER) {
            $("#update_form").show();
        }
    }
    
    if ($("#updates_box").length > 0) {
        $(".update_item_text").each(function(i){
            var text = $(this).html();
            $(this).html(LAUNCHSET.utils.linkify(text));
        });
    }

    if ($("#comments_box").length > 0) {
        $(".comment_text").each(function(i){
            var text = $(this).html();
            $(this).html(LAUNCHSET.utils.linkify(text));
        });
    }
    
});

(function(LAUNCHSET) {
    
    //private
    function test() {
        alert("colorbox complete");
    }
    
    // public
    LAUNCHSET.projects = {

        save_update: function() {
            var project_id = 0;
            var update_type = 0;
            if ($("#ddl_project_to_update").length > 0) {
                project_id = $("#ddl_project_to_update").val();
            }
            else {
                project_id = LAUNCHSET.params.PROJECT_ID;
                update_type = "user";
            }
            txt_project_update = $("#txt_project_update");
            project_update = txt_project_update.val();
            if (project_id == "" || project_id == 0 || project_update.length == 0) {
                return;
            }

            var update_button = $("#update_button");
            var update_button_orig_html = update_button.html();
            update_button.html(LAUNCHSET.utils.get_loading_html());
            
            $.post(LAUNCHSET.urls.project_save_update, {id: project_id, update: project_update, type : update_type}, function(response) {
                if (response.status == "ok") {
                    $.gritter.add({
                        title: 'Success!'
                        ,text: response.message
                    });
                    txt_project_update.val("");
                    new_update_html = response.html;
                    $("#updates_box").html(new_update_html + $("#updates_box").html());
                    new_update = $("#update_" + response.id).find(".update_item_text");
                    new_update.html(LAUNCHSET.utils.linkify(new_update.html()));
                }
                else {
                    alert(response.message);
                }
                update_button.html(update_button_orig_html);
            }, "json");
        },

        delete_update: function(update_id) {
            var confirm_delete = confirm("Are you sure you weant to delete this update? There is no Undo!");
            if (confirm_delete) {
                $.post(LAUNCHSET.urls.project_delete_update, {id: update_id}, function(response) {
                    if (response.status == "ok") {
                        $.gritter.add({
                            title: 'Deleted!'
                            ,text: response.message
                        });
                        $("#update_"+update_id).effect("blind",{},500);
                    }
                }, "json");
            }
        },

        save_comment: function() {
            txt_comment = $("#txt_comment");
            comment_text = txt_comment.val();
            if (comment_text.length == 0) {
                return;
            }

            var comment_button = $("#comment_button");
            var comment_button_orig_html = comment_button.html();
            comment_button.html(LAUNCHSET.utils.get_loading_html());
            
            $.post(LAUNCHSET.urls.project_save_comment, {id: LAUNCHSET.params.PROJECT_UPDATE_ID, text: comment_text}, function(response) {
                if (response.status == "ok") {
                    $.gritter.add({
                        title: 'Success!'
                        ,text: response.message
                    });
                    txt_comment.val("");
                    $("#comments_box").html($("#comments_box").html() + response.html);
                    new_comment = $("#comment_" + response.id).find(".comment_text");
                    new_comment.html(LAUNCHSET.utils.linkify(new_comment.html()));
                }
                else {
                    alert(response.message);
                }
                comment_button.html(comment_button_orig_html);
            }, "json");
        },
        
        check_if_follower: function(project_id) {
            $.post(LAUNCHSET.urls.project_check_if_follower, {id: project_id}, function(response) {
                if (response.status == "ok") {
                    if (response.result) {
                        $("#unfollow_project").show();
                        $("#update_form").show();
                    }
                    else {
                        $("#follow_project").show();
                    }
                }
            }, "json");
        },
        
        follow: function(project_id) {
            var follow_button = $("#follow_project");
            var follow_button_orig_html = follow_button.html();
            follow_button.html(LAUNCHSET.utils.get_loading_html());
            
            $.post(LAUNCHSET.urls.project_follow, {id: project_id}, function(response) {
                if (response.status == "ok") {
                    $("#follow_project").hide();
                    $("#update_form").show();
                    $("#unfollow_project").show();
                    $.gritter.add({
                        title: 'Success!',
                        text: response.message
                    });
                }
                follow_button.html(follow_button_orig_html);
            }, "json");
        },

        unfollow: function(project_id) {
            var unfollow_button = $("#unfollow_project");
            var unfollow_button_orig_html = unfollow_button.html();
            unfollow_button.html(LAUNCHSET.utils.get_loading_html());
            $.post(LAUNCHSET.urls.project_unfollow, {id: project_id}, function(response) {
                if (response.status == "ok") {
                    $("#unfollow_project").hide();
                    $("#update_form").hide();
                    $("#follow_project").show();
                    $.gritter.add({
                        title: 'Success!',
                        text: response.message
                    });
                    unfollow_button.html(unfollow_button_orig_html);
                }
            }, "json");
        }	
    };

})(LAUNCHSET);