jQuery(document).ready(function() {
  var speed = 'slow'; //The speed at which the screensaver changes
  var max = jQuery('#screensaver_img').children().size();
  
  if (max > 1) {  
    jQuery('#screensaver_next').css('display', 'block');
    jQuery('#screensaver_prev').css('display', 'block');
    
    prevScreenSaver = function() {
      var curr = jQuery('#screensaver_img a img.active'), curr_file = curr.attr('src'), next_file = curr_file.replace(/(\d+)\.(jpg|png|gif)$/, function($0, $1, $2) { return (parseInt($1) - 1 == 0) ? max + '.' + $2 : (parseInt($1) - 1) % max + '.' + $2;}), next = jQuery('#screensaver_img img[src=' + next_file + ']');
        curr.fadeOut(speed, function () {
        curr.removeClass('active');
        next.addClass('active');
        next.fadeIn(speed);        
      });
      return false;
    }

    nextScreenSaver = function() {
      var curr = jQuery('#screensaver_img a img.active'), curr_file = curr.attr('src'), next_file = curr_file.replace(/(\d+)\.(jpg|png|gif)$/, function($0, $1, $2) { return (parseInt($1) + 1 == max) ? max  + '.' + $2 : (parseInt($1) + 1) % max + '.' + $2;}), next = jQuery('#screensaver_img img[src=' + next_file + ']');
        curr.fadeOut(speed, function () {
        curr.removeClass('active');
        next.addClass('active');
        next.fadeIn(speed);
      });
      return false;
    }

    loopScreenSaver = function() {
      nextScreenSaver();
      setTimeout('loopScreenSaver()', 4000);
    }
    
    jQuery('#screensaver_prev').click(function() {
      return prevScreenSaver();
    });
    
    jQuery('#screensaver_next').click(function() {
      return nextScreenSaver();
    });
    
    loopScreenSaver();
    
  } else {    
    prevScreenSaver = function() {
      return false;
    }
    
    nextScreenSaver = function() {
      return false;
    }
  }
});

function screenSaverShowModal(i) {
  var html = '<h2>Choose your destination</h2><ul>', link_count = 0;
  jQuery.ajax({
    url: 'img/screensaver/links.xml',
    async: false,
    success: function(data) {
      jQuery(data).find('linkgroup:nth(' + i + ')').find('linkitem').each(function() {
        html += '<li><strong>' + jQuery(this).find('label').text() + '</strong> ';
        link_count = jQuery(this).children('link').size();
        jQuery(this).find('link').each(function(i) {
          html += '<a href="' + jQuery(this).find('url').text() + '" target="_blank">' + jQuery(this).find('text').text() + '</a>';
          if (i < link_count - 1) html += ', ';
        });
        html += '</li>';
      });
    }
  });
  html += '</ul>';
  
  jQuery.modal(html, {
    opacity: 80,
    overlayClose: true,
    
    overlayCss: {
      background: '#000'
    },
    
    onOpen: function(dialog) {
      dialog.overlay.fadeIn('fast', function() {
        dialog.container.slideDown('slow', function() {
          dialog.data.fadeIn('slow');
        });
      });
    },
    
    onClose: function(dialog) {
      dialog.data.fadeOut('slow', function() {
        dialog.container.slideUp('slow', function() {
          dialog.overlay.fadeOut('fast', function() {
            jQuery.modal.close();
          });
        });
      });
    }
  });
  
}