﻿jQuery(document).ready(function () {

    $(".ann-edit").hide();
    $(".announcement-form").hide();

    // When we hover, show the menu.
    $("div.announcement").hover(
        function () {
            if (!$(".announcement-form").is(":visible")) {
                $(this).find(".ann-edit").show();
            }
        }, function () {
            $(this).find(".ann-edit").hide();
        }); //hover

    $(".ann-edit").click(function () {
        // $(".announcement-form").show();
        $(".announcement-form").animate({ height: 'toggle' }, 500);
        $(this).hide();
    });

    $(".announcement #cancelButton").click(function () {
        // $(".announcement-form").hide();
        $(".announcement-form").animate({ height: 'toggle' }, 500);
    });

    $(".announcement #saveButton").click(function () {
        $.ajax({
            url: "/Service/ChangeAnnouncement?uId=" + $("#uid").val() +
                        "&t=" + $("#announcementEdit").val() +
                        "&tzo=" + $("input#tzo").val(),
            type: "POST",
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            success:
                function (result) {
                    var text = $("#announcementEdit").val();
                    text = replaceURLWithHTMLLinks(text);
                    $("div.plain p").fadeTo('slow', 0, function () {
                        $(this).html(text).fadeTo('slow', 1.0)
                    });

                    $(".announcement-form").animate({ height: 'toggle' }, 500);
                }
        });
    });

    function replaceURLWithHTMLLinks(text) {
        var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
        return text.replace(exp, "<a href='$1'>$1</a>");
    }
});
