$(function(){
    var $textcontainer = $('.voicemail_text');
    var $textdiv = $('.jotttextdiv', $textcontainer);
    var $textarea = $('textarea', $textcontainer);
    var $buttons = $('.buttons', $textcontainer);
    var defaultText = null;
    $textcontainer.click(function(){
        $textarea.focus(); 
    });
    
    $textarea.blur(function(){
        if (this.value == defaultText){
            $(this).removeClass('focused');
            $buttons.hide();
            if ($textdiv.html()){
                $(this).hide();
                $textdiv.show();
            }
        }
    }).focus(function(){
        $(this).addClass('focused');
        if ($textdiv.html()){
            $textdiv.hide();
            $(this).show();
        }
        $buttons.show();
        if (null === defaultText){
            defaultText = this.value;
        }
    });
    
    if (!$textdiv.html()){
        $textdiv.hide();
    }
    
    $('#cancel').click(function(){
        $('.voicemail_text textarea')
            .val(defaultText)
            .blur();
        return false;
    });
    
    $('.options a').click(function(){
        $('#more_options').show();
        return false;
    });
    
    $(document).click(function(evt){
        if (evt.target.id != 'more_options'){
            $('#more_options').hide();
        }
    });
    
    $('[id$=edit]').click(function(){
        $textarea.focus();
    });
});
