function initFurspots(){
    initCharacterChangeForm();
    $j("textarea").bind("keyup", function(e){
        if ($(this).value.length > 65536)
            this.value = this.value.substring(0, 65536)
    });
}

// For the header form when a user has multiple characters.
function initCharacterChangeForm(){
    var charForm = $('charChangeForm');
    if (charForm){
        charForm.onchange = function(){
            charForm.submit();
            return false;
        }
    }
}

function changeShortnameURL(){
    // At some point, add in a regexp to filter out non-appropriate characters
    var shortnameInput = $('shortname');
    // console.log(shortnameInput.value);
    shortnameInput.onkeyup = function(){
        $('shortnameURL').update(shortnameInput.value);
    }        
}

$j(document).ready(function(){
    $j('#edit-mode-link').click(function(){
        $j.post(char_url_for("/index/edit_mode"), function(){
            window.location.reload()
        })
    });
    
    $j('#view-mode-link').click(function(){
        $j.post(char_url_for("/index/view_mode"), function(){
            window.location.reload()
        })
    });
    
    $j('.date').mask('99/99/9999')
    $j('.time').mask('99:99')
    
    $j('.date').blur(function(){
        if (!$j(this).val())
            $j(this).val("mm/dd/yyyy")
    })
    
    $j('.error-field:first').focus()
    
    $j('#top_char_search').val(' Find a character').focus(function(){$j(this).val('')})
    
    $j('span.char_tip').tooltip({
        bodyHandler: function() {
            return $j(this).children("span.tooltip").html()
        }, 
        showURL: false 
    })
    
})

function event_field_setup(){
    $j(document).ready(function(){
        $j('#end_date').focus(function(){
            if ($j(this).val() == '__/__/____')
            {
                $j(this).val($j("#start_date").val())   
                $j(this).focus()
            }
        })
        
        if ($j('#all_day')[0].checked)
            $j('span.time').hide()
        
        $j('#all_day').click(function(){
            $j('span.time').toggle()
        })
        
        function setupPrivate(){
            if ($j('.cbox_private').attr('checked'))
            {
                $j('#is_open').attr('disabled', true).removeAttr("checked")
                $j('#is_listed').attr('disabled', true).removeAttr("checked")
            }
            else
            {
                $j('#is_open').removeAttr('disabled')
                $j('#is_listed').removeAttr('disabled')
            }
        }

        setupPrivate()
        
        $j('.cbox_private').click(setupPrivate)
        $j('#personal').click(function(){
            if ($j(this).attr('checked'))
            {
                $j('#attending').attr('disabled', true).attr('checked', true)
            }
            else
            {
                $j('#attending').removeAttr('disabled')
            }
        })
        
        $j('form').submit(function(){
            if ($j('#start_time').val())
                $j('#start_time').val($j('#start_time').val() + $j('#start_m').val())
            if ($j('#end_time').val())
                $j('#end_time').val($j('#end_time').val() + $j('#end_m').val())
        })
        
    });
}
