$(document).ready(
  function() {
    var aryRSS = new Array(5);

    $.get(
      'slider_info.xml',
      function($xml) {
        var html = "";
        var i = 1;
        $($xml).find('item').each(
          function() {
            var item_link  = $(this).find('item_link').text();  // The three pieces of information requested within the XML file
            var item_title = $(this).find('item_title').text().replace(/'/g, '&#39;'); 
            var item_image = $(this).find('item_image').text();
            // Build list items for slider
            html += "<li id='hp_images_nav" + i + "' title='" + item_title + "'";
            if (i == 1) { html += " class='hp_images_navSelected'"; }
            html += ">" + i + "</li>";
            // Build title, link, and image
            aryRSS[i] = "<h2><a href='" + item_link + "'>"
            aryRSS[i] += item_title + "</a></h2>"; // Add link and title for display outside rotation
            aryRSS[i] += "<a href='" + item_link + "'><img src='" + item_image + "' alt='" + item_title + "' title='Click to view article' /></a>"; // Extract the image from the description
            i++;
          }
        )
        $('#remove_this').remove(); // Remove empty list item, which exists for purpose of validation
        $('#hp_images_nav_items').append(html); // 
        $('#hp_images_nav').append('<div id="stop_start">||</div>');
        $('#hp_images').append(aryRSS[1]); // Create initial item
      },
      'xml'
    );

    var TransitionSeparation = 8000;

    // Begin rotation
    i = 1;
    var Rotation = setInterval(Transition, TransitionSeparation); // Time between transition starts

    // Rotate the information
    function Transition() {
      i++;
      if (i >= aryRSS.length) { i = 1; } // Wrap to the beginning
      $('#hp_images').fadeOut(1000,
        function() {
          // Update navigation highlighting
          $('#hp_images_nav li').removeClass('hp_images_navSelected');
          if (i >= aryRSS.length) { i = 0; }
          $('#hp_images_nav' + i).addClass('hp_images_navSelected');
          // Replace code and display
          $('#hp_images').html(aryRSS[i]);
          $('#hp_images').fadeIn(1000);
        }
      );
    }

    // Navigation
    $('#hp_images_nav ul li').live('click', function() {
      // Stop rotation
      clearInterval(Rotation);
      $('#stop_start').text(">");
      // Go to selected item
      i = $(this).text();
      $('#hp_images').fadeOut(600,
        function() {
          // Update navigation highlighting
          $('#hp_images_nav li').removeClass('hp_images_navSelected');
          $('#hp_images_nav' + i).addClass('hp_images_navSelected');
          // Replace code and display
          $('#hp_images').html(aryRSS[eval(i)]);
          $('#hp_images').fadeIn(600);
        }
      );
    });

    // Start or stop rotation
    $('#stop_start').live('click', function() {
      if ($('#stop_start').text() == "||") { 
        // Stop rotation
        $('#stop_start').text(">");
        clearInterval(Rotation);
      } else { 
        // Start rotation
        $('#stop_start').text("||");
        Transition();
        Rotation = setInterval(Transition, TransitionSeparation);
      }
    });

  }
)
