// Controls the subnavigation elements on the page.
var JobWidget = Class.create({
   initialize: function(element) {
      this.element = element;
      this.jobs = this.element.select('#job_list li');
      
      // The length of time in seconds before a new job is automatically displayed.
      this.interval = 5;
      
      // The length of time in seconds that it takes for a transition to be performed.
      this.transition = 0.5;
      
      this.jobs.each( function(job) {
      if(job.id != 'job_1') {
         job.hide();
         new Effect.Move(job, {
            y: 50, 
            transition: Effect.Transitions.full
         });
      } 
      });
      this.currentJob = 1;
      
      // Setup a request for the autoupdate.
      this.request = this.updateJob.delay(this.interval,this);
      
      // The buttons that allow you to cycle through jobs manually.
      this.nextButton = $('next_job');
      this.previousButton = $('previous_job');
      Event.observe(this.nextButton, 'click', this.nextJob.bindAsEventListener(this));
      Event.observe(this.previousButton, 'click', this.previousJob.bindAsEventListener(this));
      
      // A flag used to determine whether or not a button click to cycle jobs should occur. 
      // --> A false value prevents a job switch from occuring.
      this.active = true;
   },

   changeToJob: function(id) {
      this.active = false;
      new Effect.Fade(this.jobs[this.currentJob-1], {
         duration: this.transition
      });
      new Effect.Move(this.jobs[this.currentJob-1], {
         y: 50, 
         transition: Effect.Transitions.sinoidal,
         duration: this.transition
      });
      this.currentJob = id;
      new Effect.Appear(this.jobs[this.currentJob-1], {
         duration: this.transition,
         delay: this.transition/2
      });
      new Effect.Move(this.jobs[this.currentJob-1], {
         y: -50, 
         transition: Effect.Transitions.sinoidal,
         duration: this.transition,
         delay: this.transition/2
      });
      this.activationRequest = this.reactivateActions.delay(this.transition,this);
      this.request = this.updateJob.delay(this.interval,this);
   },
   
   updateJob: function(source) {
      source.nextJob(null);
   },
   
   previousJob: function(e) {
      this.resetAutoUpdate();
      if(this.active==true) {
         if(this.currentJob-1 == 0) {
            this.changeToJob(this.jobs.length);
         } else {
            this.changeToJob(this.currentJob-1);
         }
      }
      if(e) { e.stop(); }
   },
   
   nextJob: function(e) {
      this.resetAutoUpdate();
      if(this.active==true) {
         if(this.currentJob+1 > this.jobs.length) {
            this.changeToJob(1);
         } else {
            this.changeToJob(this.currentJob+1);
         }
      }
      if(e) { e.stop(); }
   },
   
   resetAutoUpdate: function() {
      if(this.request) { window.clearTimeout(this.request); }
   },
   
   reactivateActions: function(source) {
      source.active = true;
   }
});


// Extending all elements to support special interactive functionality.
function locateJobWidget(e){ 
   if($('job_opportunities')) {
      var jobs = new JobWidget($('job_opportunities'));
   }
}

Event.observe(window, 'load', locateJobWidget);