/* Object for dealing with the start timeline page
*/

function StartTimeline() {
  Event.observe(window, 'load', this.initialize.bind(this));
}

/* Initialize once the page has loaded
*/
StartTimeline.prototype.initialize = function () {

  // Fill in current date
  var now = new Date();
  var year;
  var month;
  var day;
    
  if ( ! $F('date') ) {
    year  = '' + now.getFullYear();
    month = now.getMonth()+1;
    if ( month < 10 ) {
      month = '0' + month;
    }
    day = now.getDate();
    if ( day < 10 ) {
      day = '0' + day;
    }
    month = '' + month;
    day   = '' + day;
  
    $('date').value = year + month + day;
  }
  // init event handlers for form inputs
  $('section').observe('change', this.checkKeys.bindAsEventListener(this));
  $('date').observe('change', this.checkKeys.bindAsEventListener(this));
  $('slug').observe('change', this.checkKeys.bindAsEventListener(this));
  $('section').observe('blur', this.checkKeys.bindAsEventListener(this));
  $('date').observe('blur', this.checkKeys.bindAsEventListener(this));
  $('slug').observe('blur', this.checkKeys.bindAsEventListener(this));
  $('date').observe('keyup', this.checkKeys.bindAsEventListener(this));
  $('slug').observe('keyup', this.checkKeys.bindAsEventListener(this));
  this.checkKeys();  
  this.pollerId = setInterval(  function () {
    this.checkKeys();
  }.bind(this), 2000);
}

/* checkKeys
   Check to see if all of the timeline info has been filled out
   before requesting a button to create, edit, or overwrite 
*/
StartTimeline.prototype.checkKeys = function (e) {

  if ( this.keyPressed == true ) {
    return null;
  } 

  if ( $F('slug') != '' && $F('date') != '' && $F('section') != ''
       && $F('slug').search(/^\s+$/) < 0
       && $F('date').search(/^\s+$/) < 0
//     && $F('startbuttonexists') != 'true' ) // dont check everytime onChange
     )
  {
    this.keyPressed = true;
    
    var slug = $F('slug').replace(/\s/g,'');
    var date = $F('date').replace(/\s/g,''); 
    var button_url = Conf.Path.appRoot + '/' + Conf.Path.controller 
                   + '/startbutton/' + $F('section')
                   + '/' + date + '/' + slug + '/';
   
    new Ajax.Updater(
      'startbuttonbox',
      button_url,
      {
        method : "get",
        onComplete : function () {
          $('startbuttonexists').value = 'true';
          this.setupButtonHandlers();      
          $('content').innerHTML='Checking for timeline : '+ button_url;
          this.keyPressed = false;
        }.bind(this)
      }  
    );
  }
  else { 
    $('startbuttonexists').value  = 'false';
    $('startbuttonbox').innerHTML = '';
    $('content').innerHTML='';
  }

}

/* Handlers for potential create, edit, or replace buttons.
   If replace is selected, an extra server call is done
   to clear out the old timeline.
*/
StartTimeline.prototype.setupButtonHandlers = function () {

  var main_url = Conf.Path.appRoot + '/' + Conf.Path.controller 
                 + '/main/' + $F('section')
                 + '/' + $F('date') + '/' + $F('slug') + '/';  

  if ( $F('timelineexists') == 'true' ) {
    $('create_new_button').observe('click', function (e) {
        this.deleteTimeline();
        window.location.replace(main_url);        
        Event.stop(e);
      }.bind(this)
    );
    $('edit_existing_button').observe('click', function (e) {
        window.location.replace(main_url);
        Event.stop(e);
      }
    );
  }
  else {
    $('create_new_button').observe('click', function (e) {
        window.location.replace(main_url);
        Event.stop(e);
      }
    );
  }
}

/* deleteTimeline
   Make a server request to delete the timeline... this is
   not async -- the page waits until completed
*/
StartTimeline.prototype.deleteTimeline = function () {

  var delete_url = Conf.Path.appRoot + '/' + Conf.Path.controller 
                 + '/deletetimeline/' + $F('section')
                 + '/' + $F('date') + '/' + $F('slug') + '/';

  new Ajax.Request( 
    delete_url, 
    { 
      method : "get",
      asynchronous : false
    } 
  );
}

// Call the object constructor
new StartTimeline();


