
// define an array of inputs that will automatically
// clear themselves upon gaining focus.
// Example: ['s', 'email']
var auto_clearing_inputs = ['s'];

function validateSearch() {

   var field = $('s');
   var term = $F('s');

   if (term && term != field.defaultValue) {
      $('go').disable();
      return true;
   } else {
      alert('Please enter a word or phrase to search for.');
      field.focus();
      return false;
   }

}

Event.observe(window, 'load', function() {

   $A(auto_clearing_inputs).each( function(input) { autoClearInput(input); } );
   if ($('site-search')) $('site-search').onsubmit = function() { return validateSearch(); };

   if ($('rotator')) {

      var spotlight = {
         wait_seconds: 5,
         features: 0,
         feature_count: 0,
         current_feature: 0,
         autoplay: false,
         timer: 0,
         load_feature: function(index) {
            // load the current_feature and rotate thumbs
            if (index != undefined) spotlight.current_feature = index;
            var feature = spotlight.features[spotlight.current_feature];

            $('spotlight-title').innerHTML = feature.headline;
            $('spotlight-body').innerHTML = feature.teaser;
            $('spotlight').down('div.image').setStyle({'background': ' url(/uploadedimages/HomePage/'+feature.image+') center'});
            $('spotlight').down('img').src = '/uploadedimages/HomePage/'+feature.image;

            $('spotlight-image').href = feature.link;
            $('spotlight-title').href = feature.link;
            $('spotlight-more').down('a').href = feature.link;

            spotlight.rotate_thumbs();

         },
         rotate_thumbs: function() {
            var loaded_feature = spotlight.current_feature;
            var total_features = spotlight.feature_count;
            var spot1, spot2, spot3;  // to hold the index for each thumb

            spot1 = loaded_feature + 1;
            if (spot1 >= total_features) spot1 = (spot1 - total_features);

            spot2 = loaded_feature + 2;
            if (spot2 >= total_features) spot2 = (spot2 - total_features);

            spot3 = loaded_feature + 3;
            if (spot3 >= total_features) spot3 = (spot3 - total_features);
            
            spotlight.load_thumb(1, spot1);
            spotlight.load_thumb(2, spot2);
            spotlight.load_thumb(3, spot3);
         },
         load_thumb: function(thumb_number, feature_index) {
            $('thumb'+thumb_number).down('img').src = '/uploadedimages/HomePage/'+spotlight.features[feature_index].thumb;
            $('thumb'+thumb_number).down('a').href = spotlight.features[feature_index].link;
            $('thumb'+thumb_number).down('a').onclick = function() {
               spotlight.pause();
               spotlight.load_feature(feature_index);
               return false;
            };
         },
         pause: function() {
            spotlight.autoplay = false;
            clearTimeout(spotlight.timer);
            $('play-pause-button').down('img').src = '/ui/images/btn-play.jpg';
            $('play-pause-button').href = '#play';
         },
         play: function() {
            spotlight.autoplay = true;
            spotlight.load_feature();
            $('play-pause-button').href = '#pause';
            $('play-pause-button').down('img').src = '/ui/images/btn-pause.jpg';
            clearTimeout(spotlight.timer);
            spotlight.timer = window.setTimeout(spotlight.forward, (spotlight.wait_seconds*1000));
         },
         forward: function() {
            spotlight.current_feature++;
            if (spotlight.current_feature == spotlight.feature_count) spotlight.current_feature = 0;
            spotlight.load_feature();
            if (spotlight.autoplay) {
               clearTimeout(spotlight.timer);
               spotlight.timer = window.setTimeout(spotlight.forward, (spotlight.wait_seconds*1000));
            }
         }, 
         rewind: function() {
            if (spotlight.current_feature == 0) spotlight.current_feature = spotlight.feature_count;
            spotlight.current_feature--;
            spotlight.load_feature();
         }
      };

      new Ajax.Request('/ui/scripts/spotlight.js', {
         method: 'get',
         onCreate: function() {
            $('spotlight').down('div.image').setStyle({'background-image': 'url(/ui/images/spinner.gif)'});
         },
         onSuccess: function(transport) {
            var json = transport.responseText.evalJSON();
            spotlight.features = json.spotlight;
            spotlight.feature_count = spotlight.features.length;
      
            var play_pause = new Element('a', { id: 'play-pause-button', href: '#pause' }).update(new Element('img', { src: '/ui/images/btn-pause.jpg', alt:'Pause' }));
            play_pause.onclick = function() {
               if (spotlight.autoplay) spotlight.pause();
               else spotlight.play();
               return false;
            };
            
            var previous = new Element('a', { id: 'previous-button', href: '#previous' }).update(new Element('img', { src: '/ui/images/btn-previous.gif', alt:'Previous' }));
            previous.onclick = function() {
               spotlight.pause();
               spotlight.rewind();
               return false;
            };
            
            var forward = new Element('a', { id: 'next-button', href: '#next' }).update(new Element('img', { src: '/ui/images/btn-next.gif', alt:'Next' }));
            forward.onclick = function() {
               spotlight.pause();
               spotlight.forward();
               return false;
            };
            
            if (!$('previous-button')) $('spotlight-control').insert(previous);
            if (!$('play-pause-button')) $('spotlight-control').insert(play_pause);
            if (!$('next-button')) $('spotlight-control').insert(forward);

            spotlight.play();

         }
      });

   }

});
