jQuery().ajaxError(function(_, xhr) {
    jQuery(document.body).replaceWith(xhr.responseText); 
});

jQuery(function($) {
    $('[data-default-value]')
        .blur(function(event) {
            var self = $(this);
            if(self.blank()) self.val($.query_params()[self.attr('name')] || self.attr('data-default-value'));
        })
        .focus(function(event) {
            var self = $(this);
            if(self.val() == self.attr('data-default-value')) self.val('');
        })
        .blur();
        
    $('form input[data-default-value]').parents('form').submit(function(event) {
        $(this).find('input[data-default-value]').each(function() {
            var self = $(this); 
            if(self.val() == self.attr('data-default-value')) self.val('');
        });
    });
           /*
           var numExtends = function(obj){
                   //like regular $.extend, but if both keys are
                   // present, instead of replacing the initial value,
                   //the values are added
               for (var i = 0; i < arguments.length; i++){
               }
               
           }*/
        
    var tooltip_html = '\
        <div class="perpetually-ui perpetually-tooltip">\
            <h1>#{tip}</h1>\
        </div>\
    ';

           /* TODO note tooltip is also defined with the exact same code in
            diffviewer.js, this is a hack but I cna't figure out hwo to make
            diffviewer load this version, be careful when editting tooltip
            behaviour to make sure you are editting the right one*/



    $.fn.extend({
        tooltip: function(tip, options){
            var css = {
                top: this.offset().top + 
                    this.outerHeight() + 
                    9 + 
                    options.topFudge,
                left: this.offset().left + 3,
                'z-index': 99999999999999};
            if(!options) options = {};
            if(options.left) css.left = options.left - 16;
            if(options.top) css.top = options.top;

            $$(this).tooltip && $$(this).tooltip.remove();
            var tooltip = $$(this).tooltip = $(
                (options.tooltip_html || tooltip_html)
                    .interpolate({tip: tip})).css(css);
            $(document.body).append(tooltip);
            return tooltip;
        },
        
        clear_tooltip: function(){
            $$(this).tooltip && $$(this).tooltip.remove();
            return this;
        }
    });
});