var init_pre_settings = function(){
  $.tablesorter.defaults.textExtraction = function(node){
    var this_node = $(node);
    if($('.sort', this_node).size() > 0){ return $('.sort', this_node).text(); }
    else{ return this_node.text(); }
  };
};

var init_post_settings = function(){
  // DATEPICKER
    $.datepicker.setDefaults({showWeekNumbers: true, changeMonth: true, changeYear: true, showOtherMonths: true, showWeek: true, showButtonPanel: true, duration: 'fast', altFormat: 'yy-mm-dd'});
    //$.datepicker.setDefaults($.datepicker.regional['et']); //TODO: localize
  
  // AJAX 
    //VERY IMPORANT 'AUTHENTICATION TOKEN', DO NOT REMOVE!!1
    $(document).ajaxSend(function(event, request, settings){
      // data
      if (typeof(AUTH_TOKEN) != "undefined"){
        // settings.data is a serialized string like "foo=bar&baz=boink" (or null)
        settings.data = ((settings.data) ? settings.data + "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
      }
    });  
    $(document).ajaxStart(function(){$('#ajaxstatus').show();});
    $(document).ajaxStop(function(){$('#ajaxstatus').hide();});      
    //$().ajaxComplete(onComplete);
    //$().ajaxSuccess(onSuccess);
    $(document).ajaxError(function(event, xhr, ajaxOptions, thrownError){
      pushAjaxErrorMsg(xhr.responseText);
      $('#status-top > .messages > div').oneTime(5000, function(){ 
        $_t = $(this);
        $_t.fadeOut( 1000, function(){ $_t.remove(); });
      });
    }); 
    
    
  // TIPSY
    $("[data-title-n]").tipsy({gravity: 'n', html: true, offset: 5});
    $("[data-title-s]").tipsy({gravity: 's', html: true, offset: 5});
    $("[data-title-w]").tipsy({gravity: 'w', html: true, offset: 5});
    $("[data-title-e]").tipsy({gravity: 'e', html: true, offset: 5});
    
  //  
    $('.focused').focus(); //неужели работает
    
  // OVERLAY
    $('#overlay > #status-top > div, #overlay > #status-bottom > div').oneTime(5000, function(){ $(this).fadeOut(); });
  
  //
    // $('.controllable').hover(
    //   function(e){
    //     $('.controls-noprop', this).show();
    //     //$(this).hide();
    //     //$(e.target).css('background', '#cde');
    //     //$(e.currentTarget).css('background', '#d00');
    //     //if (e.target == e.currentTarget) {
    //     //  alert(1);
    //     
    //     //}
    //   },
    //   function(e){
    //     $('.controls-noprop', this).hide();
    //     //$(e.target).css('background', '#fff');
    //     //$(e.currentTarget).css('background', '#fff');
    //     //if (e.target == e.currentTarget) {
    //     //$('.controls-noprop', $(this)).hide();
    //     //}        
    //   }  
    // );
    
  //  
    // $("table").delegate("td", "hover", function(){
    //   $(this).toggleClass("hover");
    // });
    // Is equivalent to the following code written using .live():
    // 
    // $("table").each(function(){
    //   $("td", this).live("hover", function(){
    //     $(this).toggleClass("hover");
    //   });
    // });

    $('ul#roles').delegate('.controllable', 'mouseover', function(){    
      $('.content', $(this)).addClass('lift');
      $('.controls-noprop', $(this)).show();
    });
    
    $('ul#roles').delegate('.controllable', 'mouseout', function(){    
      $('.content', $(this)).removeClass('lift');
      $('.controls-noprop', $(this)).hide();
    });
    
    
    
    // $('.controllable').live('mouseover',
    //   function(){
    //     $('.content', $(this)).addClass('lift');
    //     $('.controls-noprop', $(this)).show();
    //   }
    // );
    // $('.controllable').live('mouseout',
    //   function(){
    //     $('.content', $(this)).removeClass('lift');
    //     $('.controls-noprop', $(this)).hide();
    //   }
    // );
    
};