
var Generic = {

  load: function()
  {
    if($('#ClientSwitcher'))
    {
      this.clientSwitcher();
    }
    
    //Check all
    $('.selectAll').live('change',function(){
      Generic.checkAll(this);
    });
    
    //Initialise spell checker
    this.spellchecker();
  },
  
  
  spellchecker: function()
  {
    //Spell checker textareas
    $('textarea.spellchecker').each(function(){
      $(this).before('<div class="spellchecker-check" onclick="Generic.spellcheck(this);" rel="#'+$(this).attr('id')+'"><span>Check Spelling</span></div>');
      
      $(this).closest('div.textarea').css({ position:'relative' });
      
      $('.spellchecker-check').css({
        right:'0',
        bottom:'-20px'
      });
    });
  },
  
  spellcheck: function(link)
  {
    $(link).addClass('loading');
  
    var obj = $(link).attr('rel');
  
    $(obj).spellchecker({
      engine: 'google',
      innerDocument: true,
      addToDictionary: false,
      url: '/files/spellchecker/spellchecker.php'
    }).spellchecker("check", function(result){
    
      
      $(link).removeClass('loading');
    
    });
  },
  
  
  clientSwitcher: function()
  {
    var form = $('#ClientSwitcher');
    
    $('#ClientSwitcher select').bind("change", function(e){
      form.submit();
    });
    
    $('#clientAccountsInfo').hover(
      function() {
        var offset = $('#clientAccountsInfo').offset();
        $('#clientAccountsInfoBox').css('top',(offset.top+18)+'px');
        $('#clientAccountsInfoBox').css('left',(offset.left-120)+'px');
        $('#clientAccountsInfoBox').show();
      },
      function() {
        $('#clientAccountsInfoBox').hide();
      }
    );
  },
  
  
  checkAll: function(obj)
  {
    if(obj.checked)
    {
      $(obj).parents('table').find('tbody input:checkbox').each(function(no,obj) { obj.checked = true; });
    }
    else
    {
      $(obj).parents('table').find('tbody input:checkbox').each(function(no,obj) { obj.checked = false; });
    }
  }
  
}


$(document).ready(function() {
  Generic.load();
});

