var TiktracApi = Class.create()
TiktracApi.prototype = {
  initialize: function(url, email, password)
  {
    this.url = url
    this.email = email
    this.password = password
    
    this.login_credentials = this.email + ':' + this.password
    this.login_credentials = this.encode64(this.email + ':' + this.password)
  },
  
  // This code was written by Tyler Akins and has been placed in the
  // public domain.  It would be nice if you left this header intact.
  // Base64 code from Tyler Akins -- http://rumkin.com
  encode64: function(input)
  {
    var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;

    do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
        enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
        enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
      keyStr.charAt(enc3) + keyStr.charAt(enc4);
    } while (i < input.length);

    return output;
  },
  
  request: function(url, on_success)
  {
    var options = Object.extend({
      method:    'get',
      parameters: null,
      format:     'application/xml'
    }, arguments[2] || {})

    var ajax_options = {
      method: options['method'],
      parameters: options['parameters'],
      requestHeaders: [
        'Cache-Control', 'no-cache',
        'Pragma', 'no-cache',
        'Accept', options['format'],
        'Content-Type', options['format'],
        'Authorization', "Basic " + this.login_credentials,
      ],
      onComplete: on_success.bind(this)
    };

    try
    {
      new Ajax.Request(url, ajax_options)
    }
    catch (exception)
    {
      console.log(exception)
    }
  },
  
  get_sheets: function()
  {
    this.request(this.url + '/sheets', function(r)
    {
      $A(r.responseXML.firstChild.getElementsByTagName('sheet')).each(function(sheet)
      {
        var sheet_id = sheet.getElementsByTagName('id')[0].firstChild.data
        var sheet_name = sheet.getElementsByTagName('name')[0].firstChild.data
        
        new Insertion.Bottom($('test_results'), '<li>Sheet: ' + sheet_id + ', ' + sheet_name + '</li>')
      })
    })
  },
  
  get_tasks: function(sheet_id)
  {
    this.request(this.url + '/sheets/' + sheet_id + '/tasks', function(r)
    {
      $A(r.responseXML.firstChild.getElementsByTagName('task')).each(function(task)
      {
        var task_id = task.getElementsByTagName('id')[0].firstChild.data
        var task_duration = task.getElementsByTagName('duration')[0].firstChild.data
        
        new Insertion.Bottom($('test_results'), '<li>Task: ' + task_id + ', ' + task_duration + '</li>')
      })
    })
  },
  
  update_task: function(task_id, options)
  {
    this.request(this.url + '/tasks/' + task_id + '?_method=put', function(r)
    {
      // console.log(r.responseXML)
    }, options)
  },
  
  get_json_tasks: function(sheet_id)
  {
    this.request(this.url + '/sheets/' + sheet_id + '/tasks.json', function(r)
    {
      console.log(r.responseText)
    }, { format: 'application/x-json'})
  }
}

api = new TiktracApi('http://helicoid.tiktrac.com', 'alex@example.com', 'password')
api.get_sheets()
api.get_tasks(2)
api.get_json_tasks(2)
api.update_task(6831, { parameters: '<task><duration>9911</duration></task>', method: 'post' })


